You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by azadbolour <az...@bolour.com> on 2013/03/19 02:52:27 UTC

embedding karaf

Hi, 

I am a newbie to karaf, and I am trying to figure out how to embed it in
another program. My understanding is that something like the following code
should work (taken from
http://njbartlett.name/2011/03/07/embedding-osgi.html):

FrameworkFactory frameworkFactory =
ServiceLoader.load(FrameworkFactory.class).iterator().next(); 
Map<String, String> config = new HashMap<String, String>(); 
// TODO: add some config properties 
Framework framework = frameworkFactory.newFramework(config); 
framework.start(); 

Should this work for karaf? And if so what jar files need to be included to
obtain the framework factory as in above? 

On the other hand, the karaf Main (2.3.1) does something different: 

        String factoryClass =
configProps.getProperty(KARAF_FRAMEWORK_FACTORY); 
        if (factoryClass == null) { 
            InputStream is =
classLoader.getResourceAsStream("META-INF/services/" +
FrameworkFactory.class.getName()); 
            BufferedReader br = new BufferedReader(new InputStreamReader(is,
"UTF-8")); 
            factoryClass = br.readLine(); 
            br.close(); 
        } 
        FrameworkFactory factory = (FrameworkFactory)
classLoader.loadClass(factoryClass).newInstance(); 
        framework = factory.newFramework(new StringMap(configProps, false)); 

Would this be the correct way of embedding karaf? If so, what jar file
should include the FrameworkFactory services resource, and what jar file
should include the actual factoryClass implementation? 

Thanks in advance for any help in clarifying the right way to embed karaf. 

Azad




--
View this message in context: http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: embedding karaf

Posted by azadbolour <az...@bolour.com>.
As it turned out, the problem with "factory already defined" was a red
herring. It had to do with the fact that I was b=not stopping my framework
when my test completed. And in my setup (I am using scala sbt to run tests),
the factory hung around from one test to another. Stopping the framework in
the test solved that problem. Sorry for the false alarm.

Azad




jbonofre wrote
> Hi Azad,
> 
> Did you take a look on karaf-pax-exam ?
> 
> As I mentioned earlier, we tried pax exam and had issues that we could not
> solve in reasonable time. The trial was done by a colleague of mine. So I
> don't know the details. Is karaf-pax-exam different from just simple pax
> exam? I am not really versed in pax. So please forgive the naive question.
> 
> Thanks.
> 
> Azad
> 
> Regards
> JB
> 
> On 03/22/2013 05:12 PM, azadbolour wrote:
>>
>> Hi Achim,
>>
>> No worries. I am only looking into using Felix for testing: to embed an
>> OSGI
>> container in a test so the test does not have to rely on starting an
>> external Karaf. We have tried pax exam for testing but had issues with
>> dependencies. So I thought of trying a more direct approach of embedding
>> Felix in a test. Unfortunately, I am also running into difficulties
>> starting
>> bundles in the embedded Felix. I get the error below which I have yet to
>> figure out. Just FYI. Maybe someone has an idea how to fix this.
>>
>> Thanks again.
>>
>> Azad
>>
>> ERROR: Unable to start system bundle. (java.lang.Error: factory already
>> defined)
>> java.lang.Error: factory already defined
>> 	at java.net.URL.setURLStreamHandlerFactory(URL.java:1077)
>> 	at org.apache.felix.framework.URLHandlers.
> <init>
> (URLHandlers.java:178)





--
View this message in context: http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257p4028311.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: embedding karaf

Posted by azadbolour <az...@bolour.com>.
Hi Azad,

Did you take a look on karaf-pax-exam ?

As I mentioned earlier, we tried pax exam and had issues that we could not
solve in reasonable time. The trial was done by a colleague of mine. So I
don't know the details. Is karaf-pax-exam different from just simple pax
exam? I am not really versed in pax. So please forgive the naive question.

Thanks.

Azad

Regards
JB

On 03/22/2013 05:12 PM, azadbolour wrote:
>
> Hi Achim,
>
> No worries. I am only looking into using Felix for testing: to embed an
> OSGI
> container in a test so the test does not have to rely on starting an
> external Karaf. We have tried pax exam for testing but had issues with
> dependencies. So I thought of trying a more direct approach of embedding
> Felix in a test. Unfortunately, I am also running into difficulties
> starting
> bundles in the embedded Felix. I get the error below which I have yet to
> figure out. Just FYI. Maybe someone has an idea how to fix this.
>
> Thanks again.
>
> Azad
>
> ERROR: Unable to start system bundle. (java.lang.Error: factory already
> defined)
> java.lang.Error: factory already defined
> 	at java.net.URL.setURLStreamHandlerFactory(URL.java:1077)
> 	at org.apache.felix.framework.URLHandlers.<init>(URLHandlers.java:178)




--
View this message in context: http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257p4028310.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: embedding karaf

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Azad,

Did you take a look on karaf-pax-exam ?

Regards
JB

On 03/22/2013 05:12 PM, azadbolour wrote:
>
> Hi Achim,
>
> No worries. I am only looking into using Felix for testing: to embed an OSGI
> container in a test so the test does not have to rely on starting an
> external Karaf. We have tried pax exam for testing but had issues with
> dependencies. So I thought of trying a more direct approach of embedding
> Felix in a test. Unfortunately, I am also running into difficulties starting
> bundles in the embedded Felix. I get the error below which I have yet to
> figure out. Just FYI. Maybe someone has an idea how to fix this.
>
> Thanks again.
>
> Azad
>
> ERROR: Unable to start system bundle. (java.lang.Error: factory already
> defined)
> java.lang.Error: factory already defined
> 	at java.net.URL.setURLStreamHandlerFactory(URL.java:1077)
> 	at org.apache.felix.framework.URLHandlers.<init>(URLHandlers.java:178)
> 	at
> org.apache.felix.framework.URLHandlers.registerFrameworkInstance(URLHandlers.java:559)
> 	at
> org.apache.felix.framework.URLHandlersActivator.start(URLHandlersActivator.java:71)
> 	at
> org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
> 	at
> org.apache.felix.framework.Felix$SystemBundleActivator.start(Felix.java:4475)
> 	at
> org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
> 	at org.apache.felix.framework.Felix.init(Felix.java:767)
> 	at org.apache.felix.framework.Felix.start(Felix.java:816)
>
>
>
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257p4028289.html
> Sent from the Karaf - User mailing list archive at Nabble.com.
>

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: embedding karaf

Posted by azadbolour <az...@bolour.com>.
Hi Achim,

No worries. I am only looking into using Felix for testing: to embed an OSGI
container in a test so the test does not have to rely on starting an
external Karaf. We have tried pax exam for testing but had issues with
dependencies. So I thought of trying a more direct approach of embedding
Felix in a test. Unfortunately, I am also running into difficulties starting
bundles in the embedded Felix. I get the error below which I have yet to
figure out. Just FYI. Maybe someone has an idea how to fix this.

Thanks again.

Azad

ERROR: Unable to start system bundle. (java.lang.Error: factory already
defined)
java.lang.Error: factory already defined
	at java.net.URL.setURLStreamHandlerFactory(URL.java:1077)
	at org.apache.felix.framework.URLHandlers.<init>(URLHandlers.java:178)
	at
org.apache.felix.framework.URLHandlers.registerFrameworkInstance(URLHandlers.java:559)
	at
org.apache.felix.framework.URLHandlersActivator.start(URLHandlersActivator.java:71)
	at
org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
	at
org.apache.felix.framework.Felix$SystemBundleActivator.start(Felix.java:4475)
	at
org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
	at org.apache.felix.framework.Felix.init(Felix.java:767)
	at org.apache.felix.framework.Felix.start(Felix.java:816)




--
View this message in context: http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257p4028289.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: embedding karaf

Posted by Achim Nierbeck <bc...@googlemail.com>.
No Problem,

just a little reminder, do not try to re-invent the wheel ;)
Karaf almost got all you need to develop and run OSGi bundles.
Just a little example, if you need logging Karaf does use Pax Logging and
comes with a easy to use configuration (thanks to the felix file-installer)
and much more. I encourage you in reading the manual (the only one
available right now
is at [1], due to some changes in documentation deployment :) )
I somehow have the feeling you are probably better off with using Karaf as
a standalone Container :)

regards, Achim

[1] - http://karaf.apache.org/manual/latest-2.3.x/


2013/3/19 azadbolour <az...@bolour.com>

>
> Thank you so much for your clarification. In comparison to a servlet
> container, I was thinking that maybe Karaf is like Jetty and can be used
> either as a a separate process or embedded. But from your response I see
> that I was confused and have to look to Felix for embedding.
>
> Thanks again.
>
> Azad
>
>
>
> --
> View this message in context:
> http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257p4028262.html
> Sent from the Karaf - User mailing list archive at Nabble.com.
>



-- 

Apache Karaf <http://karaf.apache.org/> Committer & PMC
OPS4J Pax Web <http://wiki.ops4j.org/display/paxweb/Pax+Web/> Committer &
Project Lead
OPS4J Pax for Vaadin <http://team.ops4j.org/wiki/display/PAXVAADIN/Home>
Commiter & Project Lead
blog <http://notizblog.nierbeck.de/>

Re: embedding karaf

Posted by azadbolour <az...@bolour.com>.
Thank you so much for your clarification. In comparison to a servlet
container, I was thinking that maybe Karaf is like Jetty and can be used
either as a a separate process or embedded. But from your response I see
that I was confused and have to look to Felix for embedding.

Thanks again.

Azad 



--
View this message in context: http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257p4028262.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: embedding karaf

Posted by Achim Nierbeck <bc...@googlemail.com>.
Hi,

I'm not sure to follow what you are trying to achieve here?
To give a simple comparison:
- Karaf is a Container for OSGi applications as Tomcat is a Container for
web applications.

Now why would you want to embed a container, if you want to embed the OSGi
framework in your application you need to look
for Apache Felix, Eclipse Equinox, Knopflerfish .....
What kind of use-cases do you have?
Do you want to have a Web Application using OSGi services?
Well use Karaf as standalone Container cause it also supports Web
Application Bundles (WAB, aka std. War with OSGi Manifest).

regards, Achim



2013/3/19 azadbolour <az...@bolour.com>

>
> Hi,
>
> I am a newbie to karaf, and I am trying to figure out how to embed it in
> another program. My understanding is that something like the following code
> should work (taken from
> http://njbartlett.name/2011/03/07/embedding-osgi.html):
>
> FrameworkFactory frameworkFactory =
> ServiceLoader.load(FrameworkFactory.class).iterator().next();
> Map<String, String> config = new HashMap<String, String>();
> // TODO: add some config properties
> Framework framework = frameworkFactory.newFramework(config);
> framework.start();
>
> Should this work for karaf? And if so what jar files need to be included to
> obtain the framework factory as in above?
>
> On the other hand, the karaf Main (2.3.1) does something different:
>
>         String factoryClass =
> configProps.getProperty(KARAF_FRAMEWORK_FACTORY);
>         if (factoryClass == null) {
>             InputStream is =
> classLoader.getResourceAsStream("META-INF/services/" +
> FrameworkFactory.class.getName());
>             BufferedReader br = new BufferedReader(new
> InputStreamReader(is,
> "UTF-8"));
>             factoryClass = br.readLine();
>             br.close();
>         }
>         FrameworkFactory factory = (FrameworkFactory)
> classLoader.loadClass(factoryClass).newInstance();
>         framework = factory.newFramework(new StringMap(configProps,
> false));
>
> Would this be the correct way of embedding karaf? If so, what jar file
> should include the FrameworkFactory services resource, and what jar file
> should include the actual factoryClass implementation?
>
> Thanks in advance for any help in clarifying the right way to embed karaf.
>
> Azad
>
>
>
>
> --
> View this message in context:
> http://karaf.922171.n3.nabble.com/embedding-karaf-tp4028257.html
> Sent from the Karaf - User mailing list archive at Nabble.com.
>



-- 

Apache Karaf <http://karaf.apache.org/> Committer & PMC
OPS4J Pax Web <http://wiki.ops4j.org/display/paxweb/Pax+Web/> Committer &
Project Lead
OPS4J Pax for Vaadin <http://team.ops4j.org/wiki/display/PAXVAADIN/Home>
Commiter & Project Lead
blog <http://notizblog.nierbeck.de/>