You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2007/05/03 16:24:42 UTC

svn commit: r534887 - in /incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo: ./ composite/service/importer/ composite/service/instantiator/ composite/service/provides/ parser/ util/

Author: rickhall
Date: Thu May  3 07:24:40 2007
New Revision: 534887

URL: http://svn.apache.org/viewvc?view=rev&rev=534887
Log:
Applied patch (FELIX-278) to provide support for unnamed component instances.

Modified:
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/importer/ImportExportHandler.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java
    incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/Activator.java Thu May  3 07:24:40 2007
@@ -170,12 +170,13 @@
                 } else {
                     outsiders = new Dictionary[] { conf };
                 }
-
             }
         }
 
-        // Create the instance creator
-        m_creator = new InstanceCreator(m_bundleContext, outsiders);
+        // Create the instance creator if needed.
+        if (outsiders.length > 0) {
+            m_creator = new InstanceCreator(m_bundleContext, outsiders);
+        }
     }
 
 }

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/ComponentFactory.java Thu May  3 07:24:40 2007
@@ -114,6 +114,11 @@
      * True when the factory is active (non stopping and non starting).
      */
     private boolean m_active = false;
+    
+    /**
+     * Index used to generate instance name if not set.
+     */
+    private long m_index = 0;
 
     /**
      * FactoryClassloader.
@@ -293,11 +298,15 @@
                 ci.dispose();
             }
         }
+
+        m_logger.stop();
+
         m_componentInstances.clear();
         if (m_sr != null) {
             m_sr.unregister();
         }
         m_sr = null;
+
     }
 
     /**
@@ -362,6 +371,7 @@
 
     /**
      * Get the component type description attached to this factory.
+     * 
      * @return : the component type description
      * @see org.apache.felix.ipojo.Factory#getComponentDescription()
      */
@@ -377,8 +387,9 @@
      * @throws ClassNotFoundException : happen when the class is not found
      */
     public Class loadClass(String className) throws ClassNotFoundException {
-        if (m_clazz != null && className.equals(m_componentClassName)) { 
-            // Used the factory classloader to load the component implementation class
+        if (m_clazz != null && className.equals(m_componentClassName)) {
+            // Used the factory classloader to load the component implementation
+            // class
             if (m_classLoader == null) {
                 m_classLoader = new FactoryClassloader();
             }
@@ -418,26 +429,33 @@
     }
 
     /**
-     * Create an instance.
-     * The given configuration needs to contain the 'name' property.
+     * Create an instance. The given configuration needs to contain the 'name'
+     * property.
+     * 
      * @param configuration : configuration of the created instance.
      * @return the created component instance.
-     * @throws UnacceptableConfiguration : occurs if the given configuration is not consistent with the component type of this factory.
+     * @throws UnacceptableConfiguration : occurs if the given configuration is
+     * not consistent with the component type of this factory.
      * @see org.apache.felix.ipojo.Factory#createComponentInstance(java.util.Dictionary)
      */
     public ComponentInstance createComponentInstance(Dictionary configuration) throws UnacceptableConfiguration {
+        if (configuration == null) {
+            configuration = new Properties();
+        }
+        
         try {
             checkAcceptability(configuration);
         } catch (UnacceptableConfiguration e) {
             m_logger.log(Logger.ERROR, "The configuration is not acceptable : " + e.getMessage());
-            throw new UnacceptableConfiguration("The configuration " + configuration + " is not acceptable for " + m_factoryName + ": " + e.getMessage());
+            throw new UnacceptableConfiguration("The configuration " + configuration + " is not acceptable for " + m_factoryName + ": " + e);
         }
-
+        
         String pid = null;
         if (configuration.get("name") != null) {
             pid = (String) configuration.get("name");
         } else {
-            throw new UnacceptableConfiguration("The name attribute is missing");
+            pid = generateName();
+            configuration.put("name", pid);
         }
 
         if (m_componentInstances.containsKey(pid)) {
@@ -448,12 +466,10 @@
         ComponentInstance instance = null;
         if (!m_isComposite) {
             InstanceManager inst = new InstanceManager(this, context);
-            // context.setComponentInstance(inst);
             inst.configure(m_componentMetadata, configuration);
             instance = inst;
         } else {
             CompositeManager inst = new CompositeManager(this, context);
-            // context.setComponentInstance(inst);
             inst.configure(m_componentMetadata, configuration);
             instance = inst;
         }
@@ -464,15 +480,21 @@
     }
 
     /**
-     * Create an instance.
-     * The given configuration needs to contain the 'name' property.
+     * Create an instance. The given configuration needs to contain the 'name'
+     * property.
+     * 
      * @param configuration : configuration of the created instance.
      * @param serviceContext : the service context to push for this instance.
      * @return the created component instance.
-     * @throws UnacceptableConfiguration : occurs if the given configuration is not consistent with the component type of this factory.
+     * @throws UnacceptableConfiguration : occurs if the given configuration is
+     * not consistent with the component type of this factory.
      * @see org.apache.felix.ipojo.Factory#createComponentInstance(java.util.Dictionary)
      */
     public ComponentInstance createComponentInstance(Dictionary configuration, ServiceContext serviceContext) throws UnacceptableConfiguration {
+        if (configuration == null) {
+            configuration = new Properties();
+        }
+        
         try {
             checkAcceptability(configuration);
         } catch (UnacceptableConfiguration e) {
@@ -480,13 +502,15 @@
             throw new UnacceptableConfiguration("The configuration " + configuration + " is not acceptable for " + m_factoryName + ": " + e.getMessage());
         }
 
+        
         String pid = null;
         if (configuration.get("name") != null) {
             pid = (String) configuration.get("name");
         } else {
-            throw new UnacceptableConfiguration("The name attribute is missing");
+            pid = generateName();
+            configuration.put("name", pid);
         }
-
+        
         if (m_componentInstances.containsKey(pid)) {
             throw new UnacceptableConfiguration("Name already used : " + pid);
         }
@@ -495,12 +519,10 @@
         ComponentInstance instance = null;
         if (!m_isComposite) {
             InstanceManager inst = new InstanceManager(this, context);
-            // context.setComponentInstance(inst);
             inst.configure(m_componentMetadata, configuration);
             instance = inst;
         } else {
             CompositeManager inst = new CompositeManager(this, context);
-            // context.setComponentInstance(inst);
             inst.configure(m_componentMetadata, configuration);
             instance = inst;
         }
@@ -512,6 +534,7 @@
 
     /**
      * Delete an instance.
+     * 
      * @param pid : name of the instance to delete
      * @see org.osgi.service.cm.ManagedServiceFactory#deleted(java.lang.String)
      */
@@ -526,6 +549,7 @@
 
     /**
      * Get the name of this factory.
+     * 
      * @return the name of this factory
      * @see org.apache.felix.ipojo.Factory#getName()
      */
@@ -535,17 +559,19 @@
 
     /**
      * Create of update an instance.
+     * 
      * @param pid : name of the instance
      * @param properties : configuration of the instance
-     * @throws ConfigurationException : if the configuration is not consistent for this component type
-     * @see org.osgi.service.cm.ManagedServiceFactory#updated(java.lang.String, java.util.Dictionary)
+     * @throws ConfigurationException : if the configuration is not consistent
+     * for this component type
+     * @see org.osgi.service.cm.ManagedServiceFactory#updated(java.lang.String,
+     * java.util.Dictionary)
      */
     public void updated(String pid, Dictionary properties) throws ConfigurationException {
         InstanceManager cm = (InstanceManager) m_componentInstances.get(pid);
         if (cm == null) {
             try {
-                properties.put("name", pid); // Add the name in the
-                // configuration
+                properties.put("name", pid); // Add the name in the configuration
                 createComponentInstance(properties);
             } catch (UnacceptableConfiguration e) {
                 m_logger.log(Logger.ERROR, "The configuration is not acceptable : " + e.getMessage());
@@ -553,8 +579,7 @@
             }
         } else {
             try {
-                properties.put("name", pid); // Add the name in the
-                // configuration
+                properties.put("name", pid); // Add the name in the configuration
                 checkAcceptability(properties); // Test if the configuration is
                 // acceptable
             } catch (UnacceptableConfiguration e) {
@@ -567,17 +592,13 @@
 
     /**
      * Check if the given configuration is acceptable as a component instance
-     * configuration. This checks that a name is given in the configuration and
-     * if all the configurable properties have a value.
+     * configuration. This method checks that if all the configurable properties
+     * have a value.
      * 
      * @param conf : the configuration to check
      * @return true when the configuration seems to be acceptable
      */
     public boolean isAcceptable(Dictionary conf) {
-        // First check that the configuration contains a name :
-        if (conf.get("name") == null) {
-            return false;
-        }
         PropertyDescription[] props = m_componentDesc.getProperties();
         for (int i = 0; i < props.length; i++) {
             PropertyDescription pd = props[i];
@@ -597,9 +618,6 @@
      * @throws UnacceptableConfiguration : the configuration is not acceptable.
      */
     private void checkAcceptability(Dictionary conf) throws UnacceptableConfiguration {
-        if (conf == null || conf.get("name") == null) {
-            throw new UnacceptableConfiguration("The configuration does not contains the \"name\" property");
-        }
         PropertyDescription[] props = m_componentDesc.getProperties();
         for (int i = 0; i < props.length; i++) {
             PropertyDescription pd = props[i];
@@ -613,8 +631,10 @@
 
     /**
      * Reconfigure an existing instance.
+     * 
      * @param properties : the new configuration to push.
-     * @throws UnacceptableConfiguration : occurs if the new configuration is not consistent with the component type.
+     * @throws UnacceptableConfiguration : occurs if the new configuration is
+     * not consistent with the component type.
      * @see org.apache.felix.ipojo.Factory#reconfigure(java.util.Dictionary)
      */
     public void reconfigure(Dictionary properties) throws UnacceptableConfiguration {
@@ -631,9 +651,21 @@
         if (cm == null) {
             return; // The instance does not exist.
         } else {
-            checkAcceptability(properties); // Test if the configuration is
-            // acceptable
+            checkAcceptability(properties); // Test if the configuration is acceptable
         }
         cm.reconfigure(properties); // re-configure the component
+    }
+    
+    /**
+     * Generate an instance name.
+     * @return an non already used name
+     */
+    private synchronized String generateName() {
+        String name = getName() + "-" + m_index;
+        while (m_componentInstances.containsKey(name)) {
+            m_index = m_index + 1;
+            name = getName() + "-" + m_index;
+        }
+        return name;
     }
 }

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/InstanceManager.java Thu May  3 07:24:40 2007
@@ -256,6 +256,7 @@
         for (int i = m_handlers.length - 1; i > -1; i--) {
             m_handlers[i].stop();
         }
+        
         m_pojoObjects = new Object[0];
 
         m_state = STOPPED;
@@ -699,7 +700,6 @@
         if (!isValid && m_state == VALID) {
             // Need to update the state to UNRESOLVED
             setState(INVALID);
-            m_pojoObjects = new Object[0];
             return;
         }
         if (isValid && m_state == INVALID) {

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/importer/ImportExportHandler.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/importer/ImportExportHandler.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/importer/ImportExportHandler.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/importer/ImportExportHandler.java Thu May  3 07:24:40 2007
@@ -90,14 +90,11 @@
             boolean aggregate = false;
             String specification = null;
 
-            if (!imp[i].containsAttribute("specification")) { // Malformed
-                                                                // import
+            if (!imp[i].containsAttribute("specification")) { // Malformed import
                 im.getFactory().getLogger().log(Logger.ERROR, "Malformed import : the specification attribute is mandatory");
             } else {
                 specification = imp[i].getAttribute("specification");
-                String filter = "(&(objectClass=" + specification + ")(!(service.pid=" + m_manager.getInstanceName() + ")))"; // Cannot
-                                                                                                                                // import
-                                                                                                                                // yourself
+                String filter = "(&(objectClass=" + specification + ")(!(service.pid=" + m_manager.getInstanceName() + ")))"; // Cannot import yourself
                 if (imp[i].containsAttribute("optional") && imp[i].getAttribute("optional").equalsIgnoreCase("true")) {
                     optional = true;
                 }
@@ -105,12 +102,8 @@
                     aggregate = true;
                 }
                 if (imp[i].containsAttribute("filter")) {
-                    String classnamefilter = "(objectClass=" + specification + ")";
-                    filter = "";
                     if (!imp[i].getAttribute("filter").equals("")) {
-                        filter = "(&" + classnamefilter + imp[i].getAttribute("filter") + ")";
-                    } else {
-                        filter = classnamefilter;
+                        filter = "(&" + filter + imp[i].getAttribute("filter") + ")";
                     }
                 }
                 ServiceImporter si = new ServiceImporter(specification, filter, aggregate, optional, m_context, m_scope, this);

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/instantiator/SvcInstance.java Thu May  3 07:24:40 2007
@@ -90,11 +90,6 @@
     private String m_filterStr;
 
     /**
-     * Name of the last create instance.
-     */
-    private static Long m_index = new Long(0);
-
-    /**
      * Constructor.
      * 
      * @param h : the handler.
@@ -191,13 +186,6 @@
                 p.put(k, m_configuration.get(k));
             }
             
-            if (p.get("name") == null) {
-                synchronized (m_index) {
-                    p.put("name", this.toString() + "-" + m_index.longValue());
-                    m_index = new Long(m_index.longValue() + 1);
-                }
-            }
-            
             ComponentInstance instance = factory.createComponentInstance(p);
             m_usedRef.put(ref, instance);
             m_context.ungetService(ref);
@@ -225,13 +213,6 @@
                 p.put(k, m_configuration.get(k));
             }
             
-            if (p.get("name") == null) {
-                synchronized (m_index) {
-                    p.put("name", this.toString() + "-" + m_index.longValue());
-                    m_index = new Long(m_index.longValue() + 1);
-                }
-            }
-            
             ComponentInstance instance = factory.createComponentInstance(p);
             m_usedRef.put(ref, instance);
             m_context.ungetService(ref);
@@ -313,13 +294,6 @@
                 while (keys.hasMoreElements()) {
                     String k = (String) keys.nextElement();
                     p.put(k, m_configuration.get(k));
-                }
-                
-                if (p.get("name") == null) {
-                    synchronized (m_index) {
-                        p.put("name", this.toString() + "-" + m_index.longValue());
-                        m_index = new Long(m_index.longValue() + 1);
-                    }
                 }
 
                 // Check the acceptability.

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/CompositionMetadata.java Thu May  3 07:24:40 2007
@@ -207,9 +207,10 @@
 
     /**
      * Build service implementation metadata.
+     * @param in : name of the future instance (used to avoid cycle)
      * @return Component Type metadata. 
      */
-    protected Element buildMetadata() {
+    protected Element buildMetadata(String in) {
         Element elem = new Element("component", "");
         Attribute className = new Attribute("className", m_name);
         Attribute factory = new Attribute("factory", "no");
@@ -231,6 +232,7 @@
                 if (field.getSpecification().isOptional()) {
                     dep.addAttribute(new Attribute("optional", "true"));
                 }
+                dep.addAttribute(new Attribute("filter", "(!(service.pid=" + in + "))"));
                 elem.addElement(dep);
             }
         }

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/composite/service/provides/ProvidedService.java Thu May  3 07:24:40 2007
@@ -104,15 +104,15 @@
      * Build service implementation type, factory and instance.
      */
     public void start() {
+        String name = m_composition.getSpecificationMetadata().getName() + "Provider";
         m_clazz = m_composition.buildPOJO();
-        m_metadata = m_composition.buildMetadata();
+        m_metadata = m_composition.buildMetadata(name);
 
         // Create the factory
         m_factory = new ComponentFactory(m_context, m_clazz, m_metadata);
         m_factory.start();
 
         Properties p = new Properties();
-        String name = m_composition.getSpecificationMetadata().getName() + "Provider";
         p.put("name", name);
         try {
             m_instance = m_factory.createComponentInstance(p, m_scope);

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/parser/ManifestMetadataParser.java Thu May  3 07:24:40 2007
@@ -85,13 +85,13 @@
      */
     private Dictionary parseInstance(Element instance) throws ParseException {
         Dictionary dict = new Properties();
-        if (!instance.containsAttribute("name")) {
-            throw new ParseException("An instance does not have the 'name' attribute");
+        if (instance.containsAttribute("name")) {
+            dict.put("name", instance.getAttribute("name"));
         }
         if (!instance.containsAttribute("component")) {
             throw new ParseException("An instance does not have the 'component' attribute");
         }
-        dict.put("name", instance.getAttribute("name"));
+        
         dict.put("component", instance.getAttribute("component"));
 
         for (int i = 0; i < instance.getElements("property").length; i++) {

Modified: incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java?view=diff&rev=534887&r1=534886&r2=534887
==============================================================================
--- incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java (original)
+++ incubator/felix/trunk/ipojo/src/main/java/org/apache/felix/ipojo/util/Logger.java Thu May  3 07:24:40 2007
@@ -198,6 +198,18 @@
                 m_log = (LogService) m_context.getService(m_ref);
             }
         }
-
+    }
+    
+    /**
+     * Stop the logger.
+     * This method unget the used log service, and remove stop the service listenning.
+     */
+    public void stop() {
+        m_context.removeServiceListener(this);
+        if (m_ref != null) {
+            m_log = null;
+            m_context.ungetService(m_ref);
+            m_ref = null;
+        }
     }
 }