You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by Benedikt Ritter <be...@googlemail.com> on 2012/03/07 11:34:15 UTC

Using org.osgi.framework.launch in SimpleShape example

Hi,

as Richard has suggested in
https://issues.apache.org/jira/browse/FELIX-3375 I'm
about to change the SimpleShape examples to use the OSGi way of launching
an embedded framework. I'd like to discuss, what would be the best way to
accomplish this.

First of all, I think we should not mix up bundle activation with the
application start. So I'd move the static void main from the Activator to
its own class (let's call it Application.java). Looking at what has to be
done to start an embedded framework I can identify three main task, with
some subtasks:

1. create a FrameworkFactory
  a. parse file META-INF/services/org.osgi.framework.launch.FrameworkFactory
  b. find the first valid line
  c. try to get a class with the name of that line
  d. try to instantiate that class
2. create a framework configuration
  a. create a temporary cache dir
  b. add a shutdown hook to delete that dir after system termination
  c create a config map, containing the chache dir as well as some other
config params
3. create the framework
  a. call newFramework() on the framework factory and pass the
configuration to that method
  b. call init()
  c. call start()
  d. wait till the application gets terminated

I've applied this to the extenderbased example, and it works. However I'm
having trouble to get rid of the felix-specific config parameters for auto
bundle activation. What is the non-felix-specific way of starting a
predefined set of bundles?

Another problem I'm seeing is how the bundles are being added to the
configuration. Have a look at
org.apache.felix.example.extenderbased.host.Activator lines 155-163:
        Map<String, Object> configMap = new HashMap<String, Object>();
        configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
            "org.apache.felix.example.extenderbased.host.extension;
version=1.0.0");
        configMap.put(AutoActivator.AUTO_START_PROP + ".1",

"file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar " +

"file:../extenderbased.square/target/extenderbased.square-1.0.0.jar " +

"file:../extenderbased.triangle/target/extenderbased.triangle-1.0.0.jar");
        configMap.put(FelixConstants.LOG_LEVEL_PROP, "4");
        configMap.put(Constants.FRAMEWORK_STORAGE,
cachedir.getAbsolutePath());

As you can see, this will only work if the example is launched from withing
your IDE, because it assumes that the bundles reside in the target
directory of the other projects. I think this should be made somehow more
flexible, so that the example can be used outside your IDE by just putting
the jars together in one directory. But at the moment I don't know how we
can achieve that, while also keeping the possibility to start from your
workspace.

Another problem is that the embedded framework start of the serverbased
example does not work add all. When I try to run the Activator.java I get
the following:

ERROR: Error starting
file:../servicebased.circle/target/servicebased.circle-1.0.0.jar
(org.osgi.framework.BundleException: Unresolved constraint in bundle 1:
package; (package=javax.swing))
ERROR: Error starting
file:../servicebased.square/target/servicebased.square-1.0.0.jar
(org.osgi.framework.BundleException: Unresolved constraint in bundle 2:
package; (package=javax.swing))
ERROR: Error starting
file:../servicebased.triangle/target/servicebased.triangle-1.0.0.jar
(org.osgi.framework.BundleException: Unresolved constraint in bundle 3:
package; (package=javax.swing))
org.osgi.framework.BundleException: Unresolved constraint in bundle 1:
package; (package=javax.swing)
at org.apache.felix.framework.Felix.resolveBundle(Felix.java:3097)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1439)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:984)
at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:263)
at java.lang.Thread.run(Thread.java:722)
org.osgi.framework.BundleException: Unresolved constraint in bundle 2:
package; (package=javax.swing)
at org.apache.felix.framework.Felix.resolveBundle(Felix.java:3097)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1439)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:984)
at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:263)
at java.lang.Thread.run(Thread.java:722)
org.osgi.framework.BundleException: Unresolved constraint in bundle 3:
package; (package=javax.swing)
at org.apache.felix.framework.Felix.resolveBundle(Felix.java:3097)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1439)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:984)
at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:263)
at java.lang.Thread.run(Thread.java:722)

As you can see, javax.swing can not be resolved. ATM I don't know how to
fix that. Any idea how to fix that?

Regards,
Benedikt

PS: my ICLA has been filed and I've created an account in confluence, so
that you can grant me some karma ;)

Re: Using org.osgi.framework.launch in SimpleShape example

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 3/10/12 6:21, Benedikt Ritter wrote:
> Am 8. März 2012 16:04 schrieb Marcel Offermans<ma...@luminis.nl>:
>> On Mar 7, 2012, at 20:12 , Richard S. Hall wrote:
>>> On 3/7/12 13:26 , Benedikt Ritter wrote:
>>>> Am 7. März 2012 16:13 schrieb Richard S. Hall<he...@ungoverned.org>:
>>>>> What's your JIRA user name?
>>>>>
>>>> My JIRA user name is britter.
>>>> My Confluence user name is benediktritter.
>>> Sorry, I meant your Confluence user name... :-o
>>>
>>> I'm sure Marcel can figure it out... :-)
>>>
>>> ->  richard
>>>
>>>>> Marcel, can you give Benedikt wiki karma?
>>>>>
>>>>> Thanks.
>> Done!
>>
> Thanks, Marcel!
>
> What is the process for contributions in confluence? Is there some
> sort of review involved or do I just go ahead and start changing the
> example page?

Just go ahead and start changing stuff. Generally, adding/editing 
content isn't much of a big deal. On the other hand, if you want to do 
structural changes to the web site, you'd probably be better off opening 
a discussion first. If in doubt, just ask.

-> richard

>
>> Thanks for helping out Benedikt!
>>
>> Greetings, Marcel
>>

Re: Using org.osgi.framework.launch in SimpleShape example

Posted by Benedikt Ritter <be...@googlemail.com>.
Am 8. März 2012 16:04 schrieb Marcel Offermans <ma...@luminis.nl>:
> On Mar 7, 2012, at 20:12 , Richard S. Hall wrote:
>> On 3/7/12 13:26 , Benedikt Ritter wrote:
>>> Am 7. März 2012 16:13 schrieb Richard S. Hall<he...@ungoverned.org>:
>>
>>>> What's your JIRA user name?
>>>>
>>> My JIRA user name is britter.
>>> My Confluence user name is benediktritter.
>>
>> Sorry, I meant your Confluence user name... :-o
>>
>> I'm sure Marcel can figure it out... :-)
>>
>> -> richard
>>
>>>
>>>> Marcel, can you give Benedikt wiki karma?
>>>>
>>>> Thanks.
>
> Done!
>

Thanks, Marcel!

What is the process for contributions in confluence? Is there some
sort of review involved or do I just go ahead and start changing the
example page?

> Thanks for helping out Benedikt!
>
> Greetings, Marcel
>

Re: Using org.osgi.framework.launch in SimpleShape example

Posted by Marcel Offermans <ma...@luminis.nl>.
On Mar 7, 2012, at 20:12 , Richard S. Hall wrote:
> On 3/7/12 13:26 , Benedikt Ritter wrote:
>> Am 7. März 2012 16:13 schrieb Richard S. Hall<he...@ungoverned.org>:
> 
>>> What's your JIRA user name?
>>> 
>> My JIRA user name is britter.
>> My Confluence user name is benediktritter.
> 
> Sorry, I meant your Confluence user name... :-o
> 
> I'm sure Marcel can figure it out... :-)
> 
> -> richard
> 
>> 
>>> Marcel, can you give Benedikt wiki karma?
>>> 
>>> Thanks.

Done!

Thanks for helping out Benedikt!

Greetings, Marcel


Re: Using org.osgi.framework.launch in SimpleShape example

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 3/7/12 13:26 , Benedikt Ritter wrote:
> Am 7. März 2012 16:13 schrieb Richard S. Hall<he...@ungoverned.org>:
>> Yeah, I don't know. The current approach was just intended to be
>> simple/simplistic.
>>
>> You'd likely have to create a new project that built all of the existing
>> projects and then copied the JARs or something...similar to what we do for
>> the framework in the main/main.distribution projects. Not sure what is best
>> here, since I'm not an expert maven user either.
>>
> What if we pass the locations of all bundles that should be started as
> cmd line params?
> If users want to try the project from within their IDE, they can
> configure a Run configuration that passes, for example
> "file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar"
> to static void main
> If the application is deployed as a jar outside the workspace, one can
> simply pass "extenderbased.circle-1.0.0.jar" (if that jar is in the
> same directory) in the command line.

That makes starting it from the command line somewhat of a pain, but in 
the end I'm fine with it, since you could also say it is more flexible.

>
> [CUT]
>
>>> As you can see, javax.swing can not be resolved. ATM I don't know how to
>>> fix that. Any idea how to fix that?
>>
>> Odd. It work for me:
>>
> Then it has to be some maven/eclipse problem. I'll try to solve that
> and let you know.

I build and run from the command line.

>> What's your JIRA user name?
>>
> My JIRA user name is britter.
> My Confluence user name is benediktritter.

Sorry, I meant your Confluence user name... :-o

I'm sure Marcel can figure it out... :-)

-> richard

>
>> Marcel, can you give Benedikt wiki karma?
>>
>> Thanks.
>>
>> ->  richard
>>

Re: Using org.osgi.framework.launch in SimpleShape example

Posted by Benedikt Ritter <be...@googlemail.com>.
Am 7. März 2012 16:13 schrieb Richard S. Hall <he...@ungoverned.org>:
> On 3/7/12 05:34 , Benedikt Ritter wrote:

[CUT]

> You need to register a framework listener with the framework started event,
> then auto-start your bundles once you receive this event.
>

I'll try to have a look at that tonight or tomorrow. Since the
serverbased example is not running at all in my setup, I've decided to
get things working in the extenderbased example and fix the
serverbased afterwards.

[CUT]

>
> Yeah, I don't know. The current approach was just intended to be
> simple/simplistic.
>
> You'd likely have to create a new project that built all of the existing
> projects and then copied the JARs or something...similar to what we do for
> the framework in the main/main.distribution projects. Not sure what is best
> here, since I'm not an expert maven user either.
>

What if we pass the locations of all bundles that should be started as
cmd line params?
If users want to try the project from within their IDE, they can
configure a Run configuration that passes, for example
"file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar"
to static void main
If the application is deployed as a jar outside the workspace, one can
simply pass "extenderbased.circle-1.0.0.jar" (if that jar is in the
same directory) in the command line.

[CUT]

>>
>> As you can see, javax.swing can not be resolved. ATM I don't know how to
>> fix that. Any idea how to fix that?
>
>
> Odd. It work for me:
>

Then it has to be some maven/eclipse problem. I'll try to solve that
and let you know.

[CUT]

>
> What's your JIRA user name?
>

My JIRA user name is britter.
My Confluence user name is benediktritter.

> Marcel, can you give Benedikt wiki karma?
>
> Thanks.
>
> -> richard
>
>>
>

Re: Using org.osgi.framework.launch in SimpleShape example

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 3/7/12 05:34 , Benedikt Ritter wrote:
> Hi,
>
> as Richard has suggested in
> https://issues.apache.org/jira/browse/FELIX-3375 I'm
> about to change the SimpleShape examples to use the OSGi way of launching
> an embedded framework. I'd like to discuss, what would be the best way to
> accomplish this.
>
> First of all, I think we should not mix up bundle activation with the
> application start. So I'd move the static void main from the Activator to
> its own class (let's call it Application.java).

That's probably fine. They were in the same file since they were both 
related to application activation: either as a bundle or as an application.

> Looking at what has to be
> done to start an embedded framework I can identify three main task, with
> some subtasks:
>
> 1. create a FrameworkFactory
>    a. parse file META-INF/services/org.osgi.framework.launch.FrameworkFactory
>    b. find the first valid line
>    c. try to get a class with the name of that line
>    d. try to instantiate that class
> 2. create a framework configuration
>    a. create a temporary cache dir
>    b. add a shutdown hook to delete that dir after system termination
>    c create a config map, containing the chache dir as well as some other
> config params
> 3. create the framework
>    a. call newFramework() on the framework factory and pass the
> configuration to that method
>    b. call init()
>    c. call start()
>    d. wait till the application gets terminated
>
> I've applied this to the extenderbased example, and it works. However I'm
> having trouble to get rid of the felix-specific config parameters for auto
> bundle activation. What is the non-felix-specific way of starting a
> predefined set of bundles?

You need to register a framework listener with the framework started 
event, then auto-start your bundles once you receive this event.

>
> Another problem I'm seeing is how the bundles are being added to the
> configuration. Have a look at
> org.apache.felix.example.extenderbased.host.Activator lines 155-163:
>          Map<String, Object>  configMap = new HashMap<String, Object>();
>          configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
>              "org.apache.felix.example.extenderbased.host.extension;
> version=1.0.0");
>          configMap.put(AutoActivator.AUTO_START_PROP + ".1",
>
> "file:../extenderbased.circle/target/extenderbased.circle-1.0.0.jar " +
>
> "file:../extenderbased.square/target/extenderbased.square-1.0.0.jar " +
>
> "file:../extenderbased.triangle/target/extenderbased.triangle-1.0.0.jar");
>          configMap.put(FelixConstants.LOG_LEVEL_PROP, "4");
>          configMap.put(Constants.FRAMEWORK_STORAGE,
> cachedir.getAbsolutePath());
>
> As you can see, this will only work if the example is launched from withing
> your IDE, because it assumes that the bundles reside in the target
> directory of the other projects. I think this should be made somehow more
> flexible, so that the example can be used outside your IDE by just putting
> the jars together in one directory. But at the moment I don't know how we
> can achieve that, while also keeping the possibility to start from your
> workspace.

Yeah, I don't know. The current approach was just intended to be 
simple/simplistic.

You'd likely have to create a new project that built all of the existing 
projects and then copied the JARs or something...similar to what we do 
for the framework in the main/main.distribution projects. Not sure what 
is best here, since I'm not an expert maven user either.

>
> Another problem is that the embedded framework start of the serverbased
> example does not work add all. When I try to run the Activator.java I get
> the following:
>
> ERROR: Error starting
> file:../servicebased.circle/target/servicebased.circle-1.0.0.jar
> (org.osgi.framework.BundleException: Unresolved constraint in bundle 1:
> package; (package=javax.swing))
> ERROR: Error starting
> file:../servicebased.square/target/servicebased.square-1.0.0.jar
> (org.osgi.framework.BundleException: Unresolved constraint in bundle 2:
> package; (package=javax.swing))
> ERROR: Error starting
> file:../servicebased.triangle/target/servicebased.triangle-1.0.0.jar
> (org.osgi.framework.BundleException: Unresolved constraint in bundle 3:
> package; (package=javax.swing))
> org.osgi.framework.BundleException: Unresolved constraint in bundle 1:
> package; (package=javax.swing)
> at org.apache.felix.framework.Felix.resolveBundle(Felix.java:3097)
> at org.apache.felix.framework.Felix.startBundle(Felix.java:1439)
> at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:984)
> at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:263)
> at java.lang.Thread.run(Thread.java:722)
> org.osgi.framework.BundleException: Unresolved constraint in bundle 2:
> package; (package=javax.swing)
> at org.apache.felix.framework.Felix.resolveBundle(Felix.java:3097)
> at org.apache.felix.framework.Felix.startBundle(Felix.java:1439)
> at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:984)
> at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:263)
> at java.lang.Thread.run(Thread.java:722)
> org.osgi.framework.BundleException: Unresolved constraint in bundle 3:
> package; (package=javax.swing)
> at org.apache.felix.framework.Felix.resolveBundle(Felix.java:3097)
> at org.apache.felix.framework.Felix.startBundle(Felix.java:1439)
> at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:984)
> at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:263)
> at java.lang.Thread.run(Thread.java:722)
>
> As you can see, javax.swing can not be resolved. ATM I don't know how to
> fix that. Any idea how to fix that?

Odd. It work for me:

servicebased.host$ java -jar target/servicebased.host-1.0.0.jar
DEBUG: WIRE: 1.0 -> javax.swing -> 0
DEBUG: WIRE: 1.0 -> org.osgi.framework -> 0
DEBUG: WIRE: 1.0 -> org.apache.felix.example.servicebased.host.service -> 0
DEBUG: WIRE: 2.0 -> javax.swing -> 0
DEBUG: WIRE: 2.0 -> org.osgi.framework -> 0
DEBUG: WIRE: 2.0 -> org.apache.felix.example.servicebased.host.service -> 0
DEBUG: WIRE: 3.0 -> javax.swing -> 0
DEBUG: WIRE: 3.0 -> org.osgi.framework -> 0
DEBUG: WIRE: 3.0 -> org.apache.felix.example.servicebased.host.service -> 0

> PS: my ICLA has been filed and I've created an account in confluence, so
> that you can grant me some karma ;)

What's your JIRA user name?

Marcel, can you give Benedikt wiki karma?

Thanks.

-> richard

>