Many thanks.

Finally, do I have a full access to my bundle services, references, properties 
using the declaration instance? Can I manipulate those dynamically using that 
instance?



Zaid

-----Original Message-----
From: Clement Escoffier [mailto:[email protected]] 
Sent: Wednesday, January 22, 2014 2:23 PM
To: Apache Felix - Users Mailing List
Subject: Re: Felix iPOJO - The factory associated with the component type is 
invalid (not started or missing handlers)

Hi,

On 22 janv. 2014, at 10:48, Zaid Jamal Saeed Al Mahmoud 
<[email protected]> wrote:

> Thanks. My number 1 goal is to be able to create, start, and stop the 
> components dynamically. I don't think this is possible with annotations, is 
> it?

You can create instances dynamically using either the Factory service, or the 
ManagedServiceFactory configurations (configuration admin), or creating 
instance declarations (the way I proposed in the previous mail).



> 
> Following your advice,  I tried this:
> 
> InstanceDeclaration declaration = new 
> DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");

DefaultInstanceDeclaration declaration = new 
DefaultInstanceDeclaration(b.getBundleContext(), "my-factory"); 
declaration.start();

Regards,

Clement

> 
> It shows no errors, but the author of the link you posted says that you can 
> start and stop the declaration instance. Though, I did not find a start or 
> stop method (declaration.start() showed a syntax error). What's going on?
> 
> 
> 
> Zaid
> 
> -----Original Message-----
> From: Clement Escoffier [mailto:[email protected]]
> Sent: Wednesday, January 22, 2014 12:53 PM
> To: Apache Felix - Users Mailing List
> Subject: Re: Felix iPOJO - The factory associated with the component 
> type is invalid (not started or missing handlers)
> 
> Hi,
> 
> The issue comes form the asynchronous start of iPOJO. When you create your 
> instance, not everything is available.
> 
> I've several question: Why are you using the iPOJO API to declare your type 
> and instance ? Can't you just use the annotations ? In that case it will just 
> create everything smoothly.
> 
> If you really need / want to use the API, don't create the instance like you 
> do, but expose an instance declaration: 
> http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-factory-service.html#deleting-instances.
>  The declaration waits until the factory is valid to create the instance.
> 
> Regards,
> 
> Clement
> 
> 
> On 21 janv. 2014, at 19:47, Zaid Jamal Saeed Al Mahmoud 
> <[email protected]> wrote:
> 
>> I am trying to learn how to use Felix iPOJO API to create components 
>> dynamically.
>> 
>> I have a simple bundle with the following files:
>> 
>> 1- HelloService.java (Contains a Service Interface).
>> 
>> /*
>> 
>> * @author zaid almahmoud
>> 
>> *
>> 
>> */
>> 
>> package helloipojo.service;
>> 
>> 
>> 
>> public interface HelloService
>> 
>> {
>> 
>> 
>> 
>>   public void sayHello();
>> 
>> 
>> 
>> }
>> 
>> 2- Its implementation HelloServiceImpl.java:
>> 
>> package helloipojo;
>> 
>> 
>> 
>> import helloipojo.service.HelloService;
>> 
>> 
>> 
>> 
>> 
>> public class HelloServiceImpl implements HelloService{
>> 
>> 
>> 
>>   @Override
>> 
>>   public void sayHello() {
>> 
>> 
>> 
>>       System.out.println("Hello iPojo!");
>> 
>> 
>> 
>>   }
>> 
>> 
>> 
>> 
>> 
>> }
>> 
>> 3- Activator.java :
>> 
>> package helloipojo;
>> 
>> 
>> 
>> import org.osgi.framework.BundleActivator;
>> 
>> import org.osgi.framework.BundleContext;
>> 
>> 
>> 
>> 
>> 
>> public class Activator implements BundleActivator {
>> 
>> 
>> 
>> 
>> 
>>       public void start(BundleContext context) throws Exception {
>> 
>> 
>> 
>> 
>> 
>>           System.out.println("Bundle Started!");
>> 
>> 
>> 
>> 
>> 
>>       }
>> 
>>       public void stop(BundleContext context) throws Exception {
>> 
>> 
>> 
>>            context = null;
>> 
>>            System.out.println("Bundle Stopped!");
>> 
>> 
>> 
>> 
>> 
>>       }
>> 
>> 
>> 
>> }
>> 
>> MANIFEST.MF:
>> 
>> Manifest-Version: 1.0
>> 
>> Bundle-ManifestVersion: 2
>> 
>> Bundle-Name: HelloIPojo
>> 
>> Bundle-SymbolicName: HelloIPojo
>> 
>> Bundle-Version: 1.0.0.qualifier
>> 
>> Bundle-Activator: helloipojo.Activator
>> 
>> Bundle-RequiredExecutionEnvironment: JavaSE-1.6
>> 
>> Import-Package: org.osgi.framework
>> 
>> In my application, I start Felix framework and deploy the following bundles:
>> 
>> iPOJO (core)
>> 
>> iPOJO Composite
>> 
>> iPOJO API
>> 
>> According to this<http://felix.apache.org/site/apache-felix-ipojo-api.html> 
>> source.
>> 
>> Next, I install my bundle, and instantiate the component. Below is my class:
>> 
>> import java.util.ArrayList;
>> 
>> import java.util.HashMap;
>> 
>> import java.util.List;
>> 
>> import java.util.Map;
>> 
>> 
>> 
>> import org.apache.felix.framework.Felix;
>> 
>> import org.apache.felix.framework.util.FelixConstants;
>> 
>> import org.osgi.framework.Bundle;
>> 
>> import org.osgi.framework.BundleContext;
>> 
>> import org.osgi.framework.BundleException;
>> 
>> import org.osgi.framework.Constants;
>> 
>> import org.osgi.framework.ServiceRegistration;
>> 
>> 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.api.ComponentType;
>> 
>> import org.apache.felix.ipojo.api.PrimitiveComponentType;
>> 
>> import org.apache.felix.ipojo.api.Service;
>> 
>> 
>> 
>> public class HostApplication
>> 
>> {
>> 
>>   private HostActivator m_activator = null;
>> 
>>   private Felix m_felix = null;
>> 
>> 
>> 
>> 
>> 
>>   public HostApplication()
>> 
>>   {
>> 
>>       // Create a configuration property map.
>> 
>>       Map config = new HashMap();
>> 
>>       config.put(Constants.FRAMEWORK_STORAGE_CLEAN, 
>> Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
>> 
>>       // Create host activator;
>> 
>>       m_activator = new HostActivator();
>> 
>>       List list = new ArrayList();
>> 
>>       list.add(m_activator);
>> 
>> 
>> 
>>       config.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
>> 
>> 
>> 
>>       try
>> 
>>       {
>> 
>>           // Now create an instance of the framework with
>> 
>>           // our configuration properties.
>> 
>>           m_felix = new Felix(config);
>> 
>>           // Now start Felix instance.
>> 
>>           m_felix.start();
>> 
>>       }
>> 
>>       catch (Exception ex)
>> 
>>       {
>> 
>>           System.err.println("Could not create framework: " + ex);
>> 
>>           ex.printStackTrace();
>> 
>>       }
>> 
>> 
>> 
>> 
>> 
>>       // Register the application's context as an OSGi service!
>> 
>>       BundleContext bundleContext1 = m_felix.getBundleContext();
>> 
>> 
>> 
>> 
>> 
>>               System.out.println("6");
>> 
>> 
>> 
>>               try {
>> 
>> 
>> 
>>                   //starting ipojo required bundles
>> 
>>                   Bundle coreBundle = 
>> bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox
>> \\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\
>> bundle\\org.apache.felix.ipojo-1.6.2.jar");
>> 
>>                   coreBundle.start();
>> 
>>                   if(coreBundle.getState()== coreBundle.ACTIVE)
>> 
>>                       System.out.println("Core Bundle is Active!");
>> 
>> 
>> 
>>                   Bundle apiBundle = 
>> bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox
>> \\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\
>> bundle\\org.apache.felix.ipojo.api-1.6.0.jar");
>> 
>>                   apiBundle.start();
>> 
>>                   if(apiBundle.getState()== apiBundle.ACTIVE)
>> 
>>                       System.out.println("API Bundle is Active!");
>> 
>> 
>> 
>> 
>> 
>>                   Bundle compositeBundle = 
>> bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox
>> \\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\
>> bundle\\org.apache.felix.ipojo.composite-1.6.0.jar");
>> 
>>                   compositeBundle.start();
>> 
>>                   if(compositeBundle.getState()== 
>> compositeBundle.ACTIVE)
>> 
>>                       System.out.println("Composite Bundle is 
>> Active!");
>> 
>> 
>> 
>> 
>> 
>> //HERE INSTALLING AND STARTING MY BUNDLE!!
>> 
>> Bundle b = 
>> bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Desktop
>> \\plugins\\HelloIPojo_1.0.0.201401211340.jar");
>> 
>>               b.start();
>> 
>> 
>> 
>>                   try {
>> 
>> 
>> 
>>                       ComponentType type = new 
>> PrimitiveComponentType()
>> 
>>                               .setBundleContext(b.getBundleContext())
>> 
>>                               .setComponentTypeName("hello.type")
>> 
>>                               
>> .setClassName("helloipojo.HelloServiceImpl")
>> 
>>                               .setImmediate(true);
>> 
>>                       type.start();
>> 
>> 
>> 
>>                       ComponentInstance instance = 
>> type.createInstance();
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>                   }
>> 
>> 
>> 
>>                   catch (UnacceptableConfiguration
>> 
>>                           | MissingHandlerException | 
>> ConfigurationException e) {
>> 
>>                       // TODO Auto-generated catch block
>> 
>>                       e.printStackTrace();
>> 
>>                   } // Create the instance
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>                   System.out.println("done starting bundles!");
>> 
>> 
>> 
>> 
>> 
>>               } catch (BundleException e) {
>> 
>> 
>> 
>>                   e.printStackTrace();
>> 
>> 
>> 
>>                   System.out.println("Not done!");
>> 
>>               }
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>               //shutdownApplication();
>> 
>> 
>> 
>>   }
>> 
>> 
>> 
>>   public Bundle[] getInstalledBundles()
>> 
>>   {
>> 
>>       // Use the system bundle activator to gain external
>> 
>>       // access to the set of installed bundles.
>> 
>>       return m_activator.getBundles();
>> 
>>   }
>> 
>> 
>> 
>>   public void shutdownApplication()
>> 
>>   {
>> 
>>       // Shut down the felix framework when stopping the
>> 
>>       // host application.
>> 
>>       try {
>> 
>>           m_felix.stop();
>> 
>>       } catch (BundleException e1) {
>> 
>>           // TODO Auto-generated catch block
>> 
>>           e1.printStackTrace();
>> 
>>       }
>> 
>>       try {
>> 
>>           m_felix.waitForStop(0);
>> 
>>       } catch (InterruptedException e) {
>> 
>>           // TODO Auto-generated catch block
>> 
>>           e.printStackTrace();
>> 
>>       }
>> 
>>   }
>> 
>> }
>> 
>> When I run my application, it shows the following output (with the error at 
>> the end):
>> 
>> 6
>> 
>> Core Bundle is Active!
>> 
>> API Bundle is Active!
>> 
>> Composite Bundle is Active!
>> 
>> Bundle Started!
>> 
>> Exception in thread "main" java.lang.IllegalStateException: The 
>> factory associated with the component type is invalid (not started or 
>> missing handlers)
>> 
>>   at 
>> org.apache.felix.ipojo.api.ComponentType.ensureFactory(ComponentType.
>> java:189)
>> 
>>   at 
>> org.apache.felix.ipojo.api.ComponentType.ensureAndGetFactory(Componen
>> tType.java:177)
>> 
>>   at 
>> org.apache.felix.ipojo.api.ComponentType.createInstance(ComponentType
>> .java:79)
>> 
>>   at HostApplication.<init>(HostApplication.java:109)
>> 
>>   at Embedder.main(Embedder.java:11)
>> 
>> Where did I go wrong? Thanks.
>> 
>> Update
>> 
>> I could see that I am missing 2 handlers. I knew that by adding the 
>> following two lines:
>> 
>> System.out.println(type.getFactory().getRequiredHandlers());
>> 
>> System.out.println(type.getFactory().getMissingHandlers());
>> 
>> The output of the above two lines is:
>> 
>> [org.apache.felix.ipojo:architecture, 
>> org.apache.felix.ipojo:callback]
>> 
>> [org.apache.felix.ipojo:architecture, 
>> org.apache.felix.ipojo:callback]
>> 
>> I also tried:
>> 
>> type.getFactory().createComponentInstance(new Properties());
>> 
>> then I got:
>> 
>> org.apache.felix.ipojo.MissingHandlerException: Missing handlers : 
>> org.apache.felix.ipojo:architecture org.apache.felix.ipojo:callback
>> 
>> I don't know why these handlers are missing. I tried to add them, but could 
>> not figure out the right syntax. Any help? Thanks.
>> 
>> 
>> Zaid
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to