Create an OSGi-based for Web Application - Set to initialize bundle of StartLevel

A few days ago an article described how the Web Application start OSGi, see the

Create an OSGi-based for Web Application - start OSGi in WebApplication
It was later found loaded in the initialization bundle approach, and some fly in the ointment. Bundle is loaded in this way have the same boot sequence, that is, the default bundle initialization start level, have not done in the previous special settings, so the default values are 1, this will cause all of the bundle of the startup sequence can not control, in some hope that the occasion special bundle load priority (such as logging functionality, you need first to load), we hope to be able to specify when to initialize bundle a special start level, so that all of the bundle will be able to According to our pre-boot sequence to load the. Here is my optimized initialization code, the boot sequence to solve the problem.

Working principle is that, first of all, initialize the bundle in the original store directory, which is OSGi-Web project / WEB-INF/osgi/plugins catalog below to add a directory named start, in the start directory, and then In accordance with expectations set start level to create a subdirectory, for example, expect to set start level to 1 bundle, put plugins/start/1 directory; expectations set start level of 2 bundle, put plugins/start/2 directory and so on.

The code, the set bundle's start level, need to use StartLevel Service, can be obtained through the following code:
1 / / StartLevel Service, used to set the bundle of startlevel
2 ServiceReference slRef = bundleContext.getServiceReference (StartLevel.class.getName ());
3 StartLevel sl = slRef == null? Null: (StartLevel) bundleContext.getService (slRef); and then set the initial bundle start level:
1 / / set up a new bundle initial startlevel for the system configuration items: org.osgi.framework.startlevel.beginning the value of the
2 String bsl = bundleContext.getProperty ( "org.osgi.framework.startlevel.beginning");
3 if (bsl! = Null & & isInteger (bsl)) sl.setInitialBundleStartLevel (Integer.parseInt (bsl)); so that all newly installed bundle initialization start level will be set and system configuration items: org.osgi . framework.startlevel.beginning the same value to ensure that all of the bundle can be installed by default to start.
Modify osgi.properties on org.osgi.framework.startlevel.beginning configuration items, I changed it to 5:
1 # Specifies the beginning start level of the framework. See Start
2 # Level Service Specification on page 235 for more information.
3 #
4 org.osgi.framework.startlevel.beginning = 5

To add a method for installing a directory of all the immediate bundle, and set the start level:
1 private static void installBundles (BundleContext bundleContext, File bundleRoot, StartLevel sl, int bsl) (
2 File bundleFiles [] = bundleRoot.listFiles (new FilenameFilter () (
3 public boolean accept (File dir, String name) (
4 return name.endsWith ( ". Jar");
5)
6));
7
8 if (bundleFiles! = Null & & bundleFiles.length> 0) (
9 for (File bundleFile: bundleFiles) (
10 try (
11 Bundle bundle = bundleContext.installBundle (bundleFile.toURL (). ToExternalForm ());
12 if (sl! = Null & & bsl> 0) sl.setBundleStartLevel (bundle, bsl);
13 if (logger.isInfoEnabled ()) logger.info ( "Install bundle success:" + bundleFile.getName ());
14) catch (Throwable e) (
15 if (logger.isWarnEnabled ()) logger.warn ( "Install bundle error:" + bundleFile, e);
16)
17)
18)
19)
Finally, the subdirectories of the directory traversal start to install all of the bundle:
1 / / install the bundle and set the appropriate start level
2 File slRoot = new File (bundleRoot, "start");
3 if (slRoot.isDirectory ()) (
4 File slDirs [] = slRoot.listFiles (new FileFilter () (
5 public boolean accept (File file) (
6 return file.isDirectory () & & isInteger (file.getName ());
7)
8));
9
10 for (File slDir: slDirs) (
11 installBundles (bundleContext, slDir, sl, Integer.parseInt (slDir.getName ()));
12)
13)
14
15 / / installed directly under the directory below bundle
16 installBundles (bundleContext, bundleRoot, sl, 0);
1 private static boolean isInteger (String value) (
2 try (
3 Integer.parseInt (value);
4 return true;
5) catch (NumberFormatException e) (
6 return false;
7)
8)
Finally, because the presence of Declarative Services, slightly adjusted his launch strategy, all of the header contains the Service-Component definition bundle, also calls the start method to start:
1 for (Bundle bundle: bundleContext.getBundles ()) (
2 if (bundle.getState () == Bundle.INSTALLED | | bundle.getState () == Bundle.RESOLVED) (
3 if (bundle.getHeaders (). Get (Constants.BUNDLE_ACTIVATOR)! = Null | | bundle.getHeaders (). Get ( "Service-Component")! = Null) (
4 try (
5 bundle.start (Bundle.START_ACTIVATION_POLICY);
6 if (logger.isInfoEnabled ()) logger.info ( "Start bundle:" + bundle);
7) catch (Throwable e) (
8 if (logger.isWarnEnabled ()) logger.warn ( "Start bundle error:" + bundle, e);
9)
10)
11)
12)
clean Server and then start the Server, we can see the initialization of the bundle has been given the designated Start Level.

InitFramework method attached the complete code, more codes, please participate in the previous post:
1 / / initialize the Framework Environment
2 private static void initFramework (Framework framework, ServletContextEvent event) throws IOException (
3 BundleContext bundleContext = framework.getBundleContext ();
4 ServletContext servletContext = event.getServletContext ();
5
6 / / to be registered as a service ServletContext
7 registerContext (bundleContext, servletContext);
8
9 File file = bundleContext.getDataFile ( ". Init");
10 if (! File.isFile ()) (/ / first time initialization
11 if (logger.isInfoEnabled ()) logger.info ( "Init Framework");
12
13 String pluginLocation = servletContext.getInitParameter (CONTEXT_PARAM_OSGI_PLUGINS_LOCATION);
14 if (pluginLocation == null) pluginLocation = DEFAULT_OSGI_PLUGINS_LOCATION;
15 else if (! PluginLocation.startsWith ("/")) pluginLocation = "/". Concat (pluginLocation);
16
17 / / Install bundle
18 File bundleRoot = new File (servletContext.getRealPath (pluginLocation));
19 if (bundleRoot.isDirectory ()) (
20 if (logger.isInfoEnabled ()) logger.info ( "Load Framework bundles from:" + pluginLocation);
21
22 / / StartLevel Service, used to set the bundle of startlevel
23 ServiceReference slRef = bundleContext.getServiceReference (StartLevel.class.getName ());
24 StartLevel sl = slRef == null? Null: (StartLevel) bundleContext.getService (slRef);
25 / / Set the initial startlevel a new bundle for the system configuration items: org.osgi.framework.startlevel.beginning the value of the
26 String bsl = bundleContext.getProperty ( "org.osgi.framework.startlevel.beginning");
27 if (bsl! = Null & & isInteger (bsl)) sl.setInitialBundleStartLevel (Integer.parseInt (bsl));
28
29 / / install the bundle and set the appropriate start level
30 File slRoot = new File (bundleRoot, "start");
31 if (slRoot.isDirectory ()) (
32 File slDirs [] = slRoot.listFiles (new FileFilter () (
33 public boolean accept (File file) (
34 return file.isDirectory () & & isInteger (file.getName ());
35)
36));
37
38 for (File slDir: slDirs) (
39 installBundles (bundleContext, slDir, sl, Integer.parseInt (slDir.getName ()));
40)
41)
42
43 / / installed directly under the directory below bundle
44 installBundles (bundleContext, bundleRoot, sl, 0);
45
46 for (Bundle bundle: bundleContext.getBundles ()) (
47 if (bundle.getState () == Bundle.INSTALLED | | bundle.getState () == Bundle.RESOLVED) (
48 if (bundle.getHeaders (). Get (Constants.BUNDLE_ACTIVATOR)! = Null | | bundle.getHeaders (). Get ( "Service-Component")! = Null) (
49 try (
50 bundle.start (Bundle.START_ACTIVATION_POLICY);
51 if (logger.isInfoEnabled ()) logger.info ( "Start bundle:" + bundle);
52) catch (Throwable e) (
53 if (logger.isWarnEnabled ()) logger.warn ( "Start bundle error:" + bundle, e);
54)
55)
56)
57)
58
59 if (slRef! = Null) bundleContext.ungetService (slRef);
60)
61
62 new FileWriter (file). Close ();
63 if (logger.isInfoEnabled ()) logger.info ( "Framework inited.");
64)
65)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Create an OSGi-based for Web Application - Set to initialize bundle of StartLevel

  • Ruby will replace Java? I think not so soon

    I have heard a lot of such talk: Ruby On Rails (RoR) cool easy enough to prepare one In Web Application speed 10 times faster than Java, and it has some Java features not superior. By To, Ruby will replace Java to become the next generation of programming

  • Struts Spring Hibernate test Noodles

    Hibernate working principle and why to use? Principle: 1. To read and parse configuration file 2. Read and analytic mapping information, create a SessionFactory 3. Open Sesssion 4. Create Affairs transation 5. Persistence operation 6. Submitted Services 7

  • Pring Beanfactory at the jsp, servlet, web.xml and other configuration

    Keywords: spring beanfactory Spring in the web application called the Beanfactory 1) Configure web.xml Java code <? xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE web-app PUBLIC "- / / Sun Microsystems, Inc. / / ...

  • Wait for prompt js General category

    This is a very easy-to-use widget, the parameters have default values, default values, see the Notes wait = new WaitingTip(); wait.show(document.getElementById('id')); //Parameter 1 is displayed, in reference to the object ... wait.hide(); //Hide The use

  • log4j easy application in java

    JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu

  • RoR explained

    ROR is Ruby on Rails. Ruby is a well-known has been very good dynamic language It's dynamic language. Simple and easy. Dynamic languages are interpreted, but the performance may make a discount, but not absolute, because the application is complex, th

  • Some interview questions java

    The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, althoug

  • JAVA interview questions

    JAVA interview questions 1, object-oriented features of what has 1. Abstract 2. Inheritance 3. Packaging 4. Polymorphisms 2, String data types are the most basic right? Basic data types include byte, int, char, long, float, double, boolean and short. java

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries