Hi,
Here is an example:
package org.apache.felix.ipojo.test.api;
import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.MissingHandlerException;
import org.apache.felix.ipojo.UnacceptableConfiguration;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Validate;
import org.apache.felix.ipojo.api.ComponentType;
import org.apache.felix.ipojo.api.PrimitiveComponentType;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@Component
@Instantiate
public class Creator {
ComponentType type;
ComponentInstance instance;
private BundleContext context;
public Creator(BundleContext bc) {
context = bc;
}
@Validate
public void start() throws MissingHandlerException, ConfigurationException,
UnacceptableConfiguration {
Bundle bundle = getBundle();
if (bundle == null) {
System.err.println("Cannot find the bundle");
return;
}
type = new
PrimitiveComponentType().setBundleContext(bundle.getBundleContext())
.setComponentTypeName("raw.type")
.setClassName("org.apache.felix.ipojo.test.asm.Raw")
.setImmediate(true);
type.start();
instance = type.createInstance();
}
@Invalidate
public void stop() {
if (instance != null) {
instance.dispose();
instance = null;
}
if (type != null) {
type.stop();
type = null;
}
}
private Bundle getBundle() {
for (Bundle b : context.getBundles()) {
if ("bundle-using-asm".equals(b.getSymbolicName())) {
return b;
}
}
return null;
}
}
It just creates a component type and create an instance. The classname of the
create type is "org.apache.felix.ipojo.test.asm.Raw". This class is not in the
bundle containing the "Raw" class (the Raw class is in another bundle named
"bundle-using-asm".
The bundle-using-asm is a regular bundle, containing my class. However it
imports org.apache.felix.ipojo:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Private-Package>org.apache.felix.ipojo.test.asm</Private-Package>
<Import-Package>org.apache.felix.ipojo,
*</Import-Package>
</instructions>
</configuration>
</plugin>
This import is important as the class will be manipulated and will (After
manipulation) rely on iPOJO classes.
To avoid this manual import you can:
1) made this bundle an iPOJO bundle (it won't change the semantic of any
classes) - the import is added during the manipulation
2) use the iPOJO url handler and so install the bundle with:
ipojo://file:/the.path.to.my.bundle.jar
Regards,
Clement
PS: just for completion here is the raw class:
package org.apache.felix.ipojo.test.asm;
public class Raw {
public Raw() {
System.out.println("Raw...");
}
}
On 17.04.2012, at 04:35, fabiolf wrote:
> Hi Clement,
>
> What do you mean by "import"? Do you mean the Import-Package in bundle
> metadata? Or the "import" keyword in java code? I am doing both, btw, and
> still having the same error. :-/
>
> Regards,
> Fabio
>
> On Sun, Apr 15, 2012 at 3:18 AM, clement escoffier [via Apache Felix] <
> [email protected]> wrote:
>
>> Hi,
>>
>> Actually the error makes a lot more sense. Because iPOJO manipulation rely
>> on some classes of org.apache.felix.ipojo, you need to import this package.
>> All bundles intended to have iPOJO component, need to import:
>> org.apache.felix.ipojo and org.apache.felix.ipojo.architecture.
>>
>> Regards,
>>
>> Clement
>>
>>
>> On 15.04.2012, at 02:41, fabiolf wrote:
>>
>>> Hi Clement,
>>>
>>> I did as you said and I was surely doing something wrong because now the
>>> error changed. My code now is:
>>>
>>>
>>> Bundle bundle = m_context.getBundle(21);
>>> BundleContext localContext = bundle.getBundleContext();
>>> try {
>>> System.out.println("myType");
>>>
>>> new PrimitiveComponentType()
>>> .setBundleContext(localContext)
>>> .setClassName(ProviderA.class.getName())
>>> .setValidateMethod("start")
>>> .setInvalidateMethod("stop")
>>> .addService(new Service())
>>> .createInstance("TestProviderA");
>>> [...]
>>>
>>> The bundle 21 is the one that has the TestProviderA class. I'm getting
>> the
>>> following error:
>>>
>>> [ERROR] org.test.iPOJO.providerA.ProviderA : org/apache/felix/ipojo/Pojo
>>> java.lang.NoClassDefFoundError: org/apache/felix/ipojo/Pojo
>>> at java.lang.ClassLoader.defineClass1(Native Method)
>>> at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
>>> at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
>>> at
>>>
>> org.apache.felix.ipojo.ComponentFactory$FactoryClassloader.defineClass(ComponentFactory.java:501)
>>
>>> at
>>>
>> org.apache.felix.ipojo.ComponentFactory.defineClass(ComponentFactory.java:215)
>>
>>> at
>>>
>> org.apache.felix.ipojo.ComponentFactory.loadClass(ComponentFactory.java:244)
>>
>>> at org.apache.felix.ipojo.InstanceManager.load(InstanceManager.java:575)
>>> at
>>>
>> org.apache.felix.ipojo.InstanceManager.getClazz(InstanceManager.java:887)
>>> at org.apache.felix.ipojo.util.Callback.searchMethod(Callback.java:164)
>>> at org.apache.felix.ipojo.util.Callback.call(Callback.java:223)
>>> at org.apache.felix.ipojo.util.Callback.call(Callback.java:193)
>>> at
>>>
>> org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallback.call(LifecycleCallback.java:86)
>>
>>> at
>>>
>> org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallbackHandler.__stateChanged(LifecycleCallbackHandler.java:162)
>>
>>> at
>>>
>> org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallbackHandler.stateChanged(LifecycleCallbackHandler.java)
>>
>>> at
>>>
>> org.apache.felix.ipojo.InstanceManager.setState(InstanceManager.java:472)
>>> at
>> org.apache.felix.ipojo.InstanceManager.start(InstanceManager.java:354)
>>> at
>>>
>> org.apache.felix.ipojo.ComponentFactory.createInstance(ComponentFactory.java:178)
>>
>>> at
>>>
>> org.apache.felix.ipojo.IPojoFactory.createComponentInstance(IPojoFactory.java:301)
>>
>>> at
>>>
>> org.apache.felix.ipojo.IPojoFactory.createComponentInstance(IPojoFactory.java:238)
>>
>>> at
>>>
>> org.apache.felix.ipojo.api.ComponentType.createInstance(ComponentType.java:99)
>>
>>> at
>>>
>> org.test.iPOJO.fabioManipulator.FabioManipulatorCmdImpl.create(FabioManipulatorCmdImpl.java:80)
>>
>>> at
>>>
>> org.test.iPOJO.fabioManipulator.FabioManipulatorCmdImpl.execute(FabioManipulatorCmdImpl.java:61)
>>
>>> at
>>>
>> org.apache.felix.shell.impl.Activator$ShellServiceImpl.executeCommand(Activator.java:286)
>>
>>> at
>>>
>> org.apache.felix.shell.tui.Activator$ShellTuiRunnable.run(Activator.java:184)
>>
>>> at java.lang.Thread.run(Thread.java:680)
>>> Caused by: java.lang.ClassNotFoundException: org.apache.felix.ipojo.Pojo
>> not
>>> found by org.test.iPOJO.providerA [21]
>>> at
>>>
>> org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1460)
>>
>>> at
>>>
>> org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
>>
>>> at
>>>
>> org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
>>
>>> at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
>>> at org.apache.felix.framework.Felix.loadBundleClass(Felix.java:1723)
>>> at org.apache.felix.framework.BundleImpl.loadClass(BundleImpl.java:926)
>>> at
>>>
>> org.apache.felix.ipojo.ComponentFactory$FactoryClassloader.loadClass(ComponentFactory.java:528)
>>
>>> at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
>>> ... 25 more
>>> [ERROR] org.test.iPOJO.providerA.ProviderA : org/apache/felix/ipojo/Pojo
>>> org.apache.felix.ipojo.ConfigurationException: The configuration is not
>>> correct for the type org.test.iPOJO.providerA.ProviderA :
>>> org/apache/felix/ipojo/Pojo
>>> at
>>>
>> org.apache.felix.ipojo.IPojoFactory.createComponentInstance(IPojoFactory.java:308)
>>
>>> at
>>>
>> org.apache.felix.ipojo.IPojoFactory.createComponentInstance(IPojoFactory.java:238)
>>
>>> at
>>>
>> org.apache.felix.ipojo.api.ComponentType.createInstance(ComponentType.java:99)
>>
>>> at
>>>
>> org.test.iPOJO.fabioManipulator.FabioManipulatorCmdImpl.create(FabioManipulatorCmdImpl.java:80)
>>
>>> at
>>>
>> org.test.iPOJO.fabioManipulator.FabioManipulatorCmdImpl.execute(FabioManipulatorCmdImpl.java:61)
>>
>>> at
>>>
>> org.apache.felix.shell.impl.Activator$ShellServiceImpl.executeCommand(Activator.java:286)
>>
>>> at
>>>
>> org.apache.felix.shell.tui.Activator$ShellTuiRunnable.run(Activator.java:184)
>>
>>> at java.lang.Thread.run(Thread.java:680)
>>>
>>> I saw that the class org.apache.felix.ipojo.Pojo can be found in the
>> iPOJO
>>> bundle and I'm with it started as you can see below:
>>>
>>> -> ps
>>> START LEVEL 1
>>> ID State Level Name
>>> [ 0] [Active ] [ 0] System Bundle (4.0.2)
>>> [ 1] [Active ] [ 1] Apache Felix Bundle Repository (1.6.6)
>>> [ 2] [Active ] [ 1] Apache Felix iPOJO (1.9.0.SNAPSHOT)
>>> [ 3] [Active ] [ 1] Apache Felix iPOJO API (1.7.0.SNAPSHOT)
>>> [ 4] [Active ] [ 1] Apache Felix iPOJO Arch Command
>>> (1.7.0.SNAPSHOT)
>>> [ 5] [Active ] [ 1] Apache Felix iPOJO Composite
>> (1.9.0.SNAPSHOT)
>>> [ 6] [Active ] [ 1] Apache Felix Shell Service (1.4.2)
>>> [ 7] [Active ] [ 1] Apache Felix Shell TUI (1.4.1)
>>> [ 8] [Active ] [ 1] Java Utils (1.0.0)
>>> [ 19] [Active ] [ 1] Fabio Manipulator (1.0.0)
>>> [ 20] [Active ] [ 1] Testing iPOJO API: Services (1.0.0)
>>> [ 21] [Active ] [ 1] Testing iPOJO API: Provider A (1.0.0)
>>> [ 22] [Active ] [ 1] Testing iPOJO API: Provider B (1.0.0)
>>> [ 23] [Active ] [ 1] Testing iPOJO API: Consumer (1.0.0)
>>>
>>> I'm probably doing something wrong again...
>>>
>>> Thanks a lot!
>>> Fabio
>>>
>>> --
>>> View this message in context:
>> http://apache-felix.18485.n6.nabble.com/Using-iPOJO-API-to-create-components-and-instances-tp4881802p4882641.html
>>
>>> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden
>>> email]<http://user/SendEmail.jtp?type=node&node=4883007&i=0>
>>> For additional commands, e-mail: [hidden
>>> email]<http://user/SendEmail.jtp?type=node&node=4883007&i=1>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden
>> email]<http://user/SendEmail.jtp?type=node&node=4883007&i=2>
>> For additional commands, e-mail: [hidden
>> email]<http://user/SendEmail.jtp?type=node&node=4883007&i=3>
>>
>>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://apache-felix.18485.n6.nabble.com/Using-iPOJO-API-to-create-components-and-instances-tp4881802p4883007.html
>> To unsubscribe from Using iPOJO API to create components and instances, click
>> here<http://apache-felix.18485.n6.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4881802&code=ZmFiaW8ubC5mb25zZWNhQGdtYWlsLmNvbXw0ODgxODAyfDE5NjM1NDUwODE=>
>> .
>> NAML<http://apache-felix.18485.n6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
> --
> View this message in context:
> http://apache-felix.18485.n6.nabble.com/Using-iPOJO-API-to-create-components-and-instances-tp4881802p4888756.html
> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]