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)

分类:Web 时间:2010-03-30 人气:261
分享到:
blog comments powered by Disqus

相关文章

  • Web application firewall? Web Security Gateway? 2010-11-14

    Original http://netsecurity.51cto.com/art/201007/210932.htm Whether it is Web-based application system or a Web site, they are faced with a variety of security threats and sources of uncertainty. Some of them have been found, and has an identifiable

  • The OSGi-based to create a Web Application - Setting initialization bundle of StartLevel 2010-03-30

    Article a few days ago in Web Application describes how to start OSGi, see The OSGi-based to create a Web Application - Start OSGi in WebApplication It was later found loaded in the initialization bundle approach, and some fly in the ointment. Bundle

  • Tomcat Web application using web.xml control Xiangjie (2) 2010-07-30

    Tomcat Web application using web.xml control Xiangjie (2) Study: feng_sundy Source: unknown (2006-04-29 13:42:24) 7 specified welcome page If the user provides a http://host/webAppPrefix/directoryName/ such as a directory name that contains the file

  • Struts2 + Spring + iBatis on the web application of best practices Series 2 (Access Control chapter I) 2010-03-29

    Access control for a web applications is almost indispensable. When the web application to access certain resources which, if you are not logged in then, it will be redirected to the login page, and only after the login will only be allowed to visit.

  • tomcat three ways to deploy web application 2010-03-15

    Reprinted: http://hi.baidu.com/xiaoba130/blog/item/493daad003f51e319a5027a4.html 1, directly into the Webapps directory Tomcat's Webapps directory is the default Tomcat application directory, when the server starts, it will load all the directory app

  • FreeMarker in struts2.1.8, JAVA, web application example 2010-04-11

    FreeMarker Overview FreeMarker is a template engine, a template generates text output based on a common tool, written using pure Java Template + data model = output FreeMarker is a very good template engine, template engine that can be used for any s

  • Web application HTTP Session Sharing Scheme 2010-04-18

    In building the level of flexibility for expansion, high availability when Java Web applications, on the http session of the treatment strategy is largely decided by the application scalability, availability. In general on the http session the follow

  • 10 Web Application Interface Technology 2010-05-10

    Today more and more applications migrate to web platform. Since there is no platform restrictions and installation requirements, SAAS model looks very attractive. Web application interface design, the core is the web design, but its focus is mainly i

  • Java web Filter Description and working principle 2010-05-30

    Filter (filters) Introduction Filter the basic function is to process Servlet Servlet container to intercept calls to the Servlet to respond before and after treatment to achieve some special features. In the Servlet API defines three interface class

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.306 (s). 12 q(s)