users@glassfish.java.net

Re: Embed Apache Felix in a Web Application

From: Sanjeeb Sahoo <Sahoo_at_Sun.COM>
Date: Thu, 24 Jun 2010 19:43:49 +0530

That's good to know, but I would love to see you detecting the already
started Felix runtime and taking advantage of it rather than starting
your own. Many popular appservers are now OSGi enabled...

Sahoo
On Thursday 24 June 2010 03:38 AM, glassfish_at_javadesktop.org wrote:
> Thanks very much Sahoo and Shing Wai Chan. With your help I'm able to get Felix HTTP Bridge running on Glassfish and register a simple Servlet deployed as a bundle.
>
> For the history, the correct framework.properties is as follows:
>
> ######################################
> org.osgi.framework.storage.clean=onFirstInit
> org.osgi.framework.bundle.parent=framework
> org.osgi.framework.system.packages.extra=javax.servlet, javax.servlet.http
> felix.bootdelegation.implicit=true
> felix.log.level=4
> org.apache.felix.http.debug=true
> osgi.shell.telnet.port=7789
> #####################################
>
> and the content of sun-web.xml is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE sun-web-app PUBLIC '-//Sun Microsystems, Inc.//DTD
> Application Server 9.0 Servlet 2.5//EN'
> 'http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd'>
> <sun-web-app>
> <class-loader delegate="false" />
> </sun-web-app>
>
> Greetings,
> Davide Boltri
>
>
>
>> The delegate flag in sun-web.xml information can be
>> found in
>> http://docs.sun.com/app/docs/doc/820-7693/bearq?l=en&a
>> =view
>>
>> Shing Wai Chan
>>
>> On 6/23/2010 7:18 AM, Sanjeeb Sahoo wrote:
>>
>>> I think I know what's going on here. Since
>>>
>> GlassFish is already
>>
>>> running on top of Felix, when you are trying to
>>>
>> load Felix, Felix
>>
>>> actually gets loaded by one of GlassFish class
>>>
>> loader that's parent of
>>
>>> web app class loader. The class loader that loads
>>>
>> Felix classes does
>>
>>> not really have visibility to Java EE APIs like
>>>
>> javax.servlet. So,
>>
>>> even if you set
>>>
>>> org.osgi.framework.bundle.parent=framework
>>>
>>> it's not making any difference. You may like to
>>>
>> launch Felix using a
>>
>>> custom class loader in OsgiManager.java:
>>>
>>> ClassLoader cl = new
>>>
>>>
>> ClassLoader(Thread.currentThread().getContextClassLoad
>> er()) {
>>
>>> protected Class loadClass(String className)
>>>
>> throws
>>
>>> ClassNotFoundException() {
>>>
>>>
>> f(className.startsWith("org.apache.felix")) {
>>
>>> URL url =
>>>
>>>
>> findResource(convertClassNameToResource(className));
>>
>>> if(url != null) {
>>> byte[] bytes =
>>>
>> readData(url);
>>
>>> defineClass(...);
>>> }
>>> }
>>> return super.loadClass(className);
>>> }
>>> }
>>>
>>> Use this class to load org.apache.felix.main.Main
>>>
>> and FrameworkFactory
>>
>>> class. I understand this is really not an elegant
>>>
>> solution.
>>
>>> An easier option is to switch off delegation flag
>>>
>> in web app class
>>
>>> loader. You can do it by having a suitable
>>>
>> sun-web.xml file in your
>>
>>> web app. Just google for it.
>>>
>>> I understand you don't want to depend on specific
>>>
>> container behavior,
>>
>>> but can you detect that your code is running inside
>>>
>> an OSGi enabled
>>
>>> container and use that instead of launching a new
>>>
>> one? e.g., the code
>>
>>> below gets you a BundleContext object in glassfish:
>>>
>>> org.osgi.framework.BundleReference ref =
>>>
>>>
>> (BundleReference)javax.servlet.class.getClassLoader();
>>
>>> Bundle b = ref.getBundle();
>>> BundleContext bctx = b.getBundleContext();
>>> Use it to load your management bundle and pass
>>>
>> control to it.
>>
>>> Thanks,
>>> Sahoo
>>>
>>> p.s: I have not really thought hard about this
>>>
>> problem - so I am not
>>
>>> sure if all these will work or not in reality.
>>>
>>>
> [Message sent by forum member 'dboltri']
>
> http://forums.java.net/jive/thread.jspa?messageID=475596
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>