felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r730247 - in /felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/felix/ src/main/java/org/apache/felix/ipojo/ src/main/java/or...
Date Tue, 30 Dec 2008 21:37:00 GMT
Author: clement
Date: Tue Dec 30 13:37:00 2008
New Revision: 730247

URL: http://svn.apache.org/viewvc?rev=730247&view=rev
Log:
Initial commit of the iPOJO API to generate component types. 
Only primitive component types are (fully) supported. Composites will arrive soon.
This API will be coupled with the iPOJO introspection and reconfiguration API.

Added:
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/   (with props)
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/metadata.xml   (with props)
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/pom.xml
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ComponentType.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Dependency.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Property.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Service.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ServiceProperty.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/APITest.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Bar.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/BarService.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Foo.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/FooService.java
    felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/resources/

Propchange: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Dec 30 13:37:00 2008
@@ -0,0 +1,7 @@
+.project
+
+.classpath
+
+target
+
+.settings

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/metadata.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/metadata.xml?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/metadata.xml (added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/metadata.xml Tue Dec 30 13:37:00
2008
@@ -0,0 +1,10 @@
+<ipojo>
+<component classname="org.apache.felix.ipojo.api.test.APITest">
+	<callback transition="validate" method="start"/>
+	<callback transition="invalidate" method="stop"/>
+	<requires field="fs" optional="true"/>
+	<requires field="bs" optional="true"/>
+	
+</component>
+<instance component="org.apache.felix.ipojo.api.test.APITest"/>
+</ipojo>
\ No newline at end of file

Propchange: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/metadata.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/pom.xml?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/pom.xml (added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/pom.xml Tue Dec 30 13:37:00
2008
@@ -0,0 +1,51 @@
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>bundle</packaging>
+  <groupId>org.apache.felix</groupId>
+  <artifactId>org.apache.felix.ipojo.api</artifactId>
+  <version>1.1.0-SNAPSHOT</version>
+  <name>iPOJO API</name>
+  
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <version>1.4.2</version>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+            <Import-Package>! org.apache.felix.ipojo.xml.*, *</Import-Package>
+            <Export-Package>org.apache.felix.ipojo.api</Export-Package>
+            <Private-Package>org.apache.felix.ipojo.api.test, org.apache.felix.ipojo.manipulation,
org.apache.felix.ipojo.manipulation.annotations, org.objectweb.asm.commons, org.objectweb.asm,
org.objectweb.asm.tree</Private-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+      <plugin>
+	      <groupId>org.apache.felix</groupId>
+	      <artifactId>maven-ipojo-plugin</artifactId>
+	      <version>1.1.0-SNAPSHOT</version>
+		  <executions>
+          	<execution>
+            	<goals>
+	              <goal>ipojo-bundle</goal>
+               </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  <dependencies>
+  	<dependency>
+  		<groupId>org.apache.felix</groupId>
+  		<artifactId>org.apache.felix.ipojo</artifactId>
+  		<version>1.1.0-SNAPSHOT</version>
+  	</dependency>
+  	 <dependency>
+  		<groupId>org.apache.felix</groupId>
+  		<artifactId>org.apache.felix.ipojo.manipulator</artifactId>
+  		<version>1.1.0-SNAPSHOT</version>
+  	</dependency>
+  </dependencies>
+</project>

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ComponentType.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ComponentType.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ComponentType.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ComponentType.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,99 @@
+package org.apache.felix.ipojo.api;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.MissingHandlerException;
+import org.apache.felix.ipojo.UnacceptableConfiguration;
+
+public abstract class ComponentType {
+        
+    private List m_instances = new ArrayList();
+    
+    public abstract Factory getFactory();
+    
+    public abstract void start();
+    public abstract void stop();
+    
+    
+    public ComponentInstance createInstance() throws UnacceptableConfiguration, MissingHandlerException,
ConfigurationException {
+        ComponentInstance ci = _getFactory().createComponentInstance(null); 
+        m_instances.add(ci);
+        return ci;
+    }
+    
+    public ComponentInstance createInstance(String name) throws UnacceptableConfiguration,
MissingHandlerException, ConfigurationException {
+        Dictionary dict = null;
+        if (name != null) {
+            dict = new Properties();
+            dict.put("instance.name", name);
+        } 
+        ComponentInstance ci = _getFactory().createComponentInstance(dict); 
+        m_instances.add(ci);
+        return ci;
+    }
+    
+    public ComponentInstance createInstance(Dictionary conf) throws UnacceptableConfiguration,
MissingHandlerException, ConfigurationException {
+        ComponentInstance ci = _getFactory().createComponentInstance(conf);
+        m_instances.add(ci);
+        return ci;
+    }
+    
+    public boolean disposeInstance(ComponentInstance ci) {
+        if (m_instances.remove(ci)) {
+            ci.dispose();
+            return true;
+        } else {
+            System.err.println("The instance was not created from this component type");
+            return false;
+        }
+    }
+    
+    public ComponentInstance getInstanceByName(String name) {
+        for (int i = 0; i < m_instances.size(); i++) {
+            ComponentInstance ci = (ComponentInstance) m_instances.get(i);
+            if (ci.getInstanceName().equals(name)) {
+                return ci;
+            }
+        }
+        return null;
+    }
+    
+    public boolean disposeInstance(String name) {
+        ComponentInstance ci = getInstanceByName(name);
+        if (ci == null) {
+            System.err.println("The instance was not found in this component type");
+            return false;
+        } else {
+            return disposeInstance(ci);
+        }
+    }
+    
+    private Factory _getFactory() {
+        ensureFactory();
+        return getFactory();
+    }
+    
+    /**
+     * Checks if the factory is already created.
+     */
+    private void ensureFactory() {
+        if (getFactory() == null) {
+            throw new IllegalStateException("The factory associated with the component type
is not created");
+        } else {
+            if (getFactory().getState() == Factory.INVALID) {
+                throw new IllegalStateException("The factory associated with the component
type is invalid (not started or missing handlers)");
+            }
+        }
+    }
+    
+    
+    
+
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Dependency.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Dependency.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Dependency.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Dependency.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,167 @@
+package org.apache.felix.ipojo.api;
+
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+
+public class Dependency {
+    
+    public static final int DYNAMIC = org.apache.felix.ipojo.handlers.dependency.Dependency.DYNAMIC_BINDING_POLICY;
+    public static final int STATIC = org.apache.felix.ipojo.handlers.dependency.Dependency.STATIC_BINDING_POLICY;
+    public static final int DYNAMIC_PRIORITY = org.apache.felix.ipojo.handlers.dependency.Dependency.DYNAMIC_PRIORITY_BINDING_POLICY;
+
+    private String m_specification;
+    private String m_filter;
+    private String m_field;
+    private boolean m_optional;
+    private boolean m_aggregate;
+    private String m_bind;
+    private String m_unbind;
+    private int m_policy;
+    private String m_comparator;
+    private String m_di;
+    private String m_from;
+    private String m_id;
+    private boolean m_nullable;
+
+    public Element getElement() {
+        ensureValidity();
+        
+        Element dep = new Element("requires", "");
+        if (m_specification != null) {
+            dep.addAttribute(new Attribute("specification", m_specification));
+        }
+        if (m_filter != null) {
+            dep.addAttribute(new Attribute("filter", m_filter));
+        }
+        if (m_field != null) {
+            dep.addAttribute(new Attribute("field", m_field));
+        }
+        if (m_bind != null) {
+            Element cb = new Element("callback", "");
+            cb.addAttribute(new Attribute("type", "bind"));
+            cb.addAttribute(new Attribute("method", m_bind));
+            dep.addElement(cb);
+        }
+        if (m_unbind != null) {
+            Element cb = new Element("callback", "");
+            cb.addAttribute(new Attribute("type", "unbind"));
+            cb.addAttribute(new Attribute("method", m_unbind));
+            dep.addElement(cb);
+        }
+        if (m_comparator != null) {
+            dep.addAttribute(new Attribute("comparator", m_comparator));
+        }
+        if (m_di != null) {
+            dep.addAttribute(new Attribute("default-implementation", m_di));
+        }
+        if (m_from != null) {
+            dep.addAttribute(new Attribute("from", m_from));
+        }
+        if (m_id != null) {
+            dep.addAttribute(new Attribute("id", m_id));
+        }
+        if (! m_nullable) {
+            dep.addAttribute(new Attribute("nullable", "false"));
+        }
+        if (m_optional) {
+            dep.addAttribute(new Attribute("optional", "true"));
+        }
+        if (m_aggregate) {
+            dep.addAttribute(new Attribute("aggregate", "true"));
+        }
+        if (m_policy != -1) {
+            if (m_policy == DYNAMIC) {
+                dep.addAttribute(new Attribute("policy", "dynamic"));
+            } else if (m_policy == STATIC) {
+                dep.addAttribute(new Attribute("policy", "static"));
+            } else if (m_policy == DYNAMIC_PRIORITY) {
+                dep.addAttribute(new Attribute("policy", "dynamic-priority"));
+            }
+            // No other possibilities.
+        }
+        return dep;
+    }
+    
+    public Dependency setSpecification(String spec) {
+        m_specification = spec;
+        return this;
+    }
+    
+    public Dependency setFilter(String filter) {
+        m_filter = filter;
+        return this;
+    }
+    
+    public Dependency setField(String field) {
+        m_field = field;
+        return this;
+    }
+    
+    public Dependency setOptional(boolean opt) {
+        m_optional = opt;
+        return this;
+    }
+    
+    public Dependency setAggregate(boolean agg) {
+        m_aggregate = agg;
+        return this;
+    }
+    
+    public Dependency setNullable(boolean nullable) {
+        m_nullable = nullable;
+        return this;
+    }
+    
+    public Dependency setBindMethod(String bind) {
+        m_bind = bind;
+        return this;
+    }
+    
+    public Dependency setUnbindMethod(String unbind) {
+        m_unbind = unbind;
+        return this;
+    }
+    
+    public Dependency setBindingPolicy(int policy) {
+        m_policy = policy;
+        return this;
+    }
+    
+    public Dependency setComparator(String cmp) {
+        m_comparator = cmp;
+        return this;
+    }
+    
+    public Dependency setDefaultImplementation(String di) {
+        m_di = di;
+        return this;
+    }
+    
+    public Dependency setFrom(String from) {
+        m_from = from;
+        return this;
+    }
+    
+    public Dependency setId(String id) {
+        m_id = id;
+        return this;
+    }
+    
+    /**
+     * Checks dependency configuration validity.
+     */
+    private void ensureValidity() {
+        // At least a field or methods.
+        if (m_field == null && m_bind == null && m_unbind == null) {
+            throw new IllegalStateException("A dependency must have a field or bind/unbind
methods");
+        }
+        // Check binding policy.
+        if (m_policy != -1) {
+            if (!(m_policy == DYNAMIC || m_policy == STATIC || m_policy == DYNAMIC_PRIORITY))
{
+                throw new IllegalStateException("Unknow binding policy : " + m_policy);
+            }
+        }
+    }
+    
+    
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/PrimitiveComponentType.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,244 @@
+package org.apache.felix.ipojo.api;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.manipulation.Manipulator;
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+import org.osgi.framework.BundleContext;
+
+public class PrimitiveComponentType extends ComponentType {
+
+    private BundleContext m_context;
+    private String m_classname;
+    private String m_name;
+    private boolean m_immediate;
+    private Element m_manipulation;
+    private ComponentFactory m_factory;
+    private Element m_metadata;
+    private List m_services = new ArrayList(1);
+    private List m_dependencies = new ArrayList();
+    private List m_properties = new ArrayList();
+    private String m_validate;
+    private String m_invalidate;
+    private boolean m_propagation;
+    private String m_factoryMethod;
+    private boolean m_public = true;
+    private String m_msPID;
+    
+    private void ensureNotInitialized() {
+        if (m_factory != null) {
+            throw new IllegalStateException("The component type was already initialized,
cannot modify metadata");
+        }
+    }
+    
+    private void ensureValidity() {
+        if (m_classname == null) {
+            throw new IllegalStateException("The primitive component type as no implementation
class");
+        }
+    }
+
+    public Factory getFactory() {
+        initializeFactory();
+        return m_factory;
+    }
+
+    public void start() {
+        initializeFactory();
+        m_factory.start();
+    }
+
+    public void stop() {
+        initializeFactory();
+        m_factory.stop();
+    }
+    
+    private void initializeFactory() {
+        if (m_factory == null) {
+            createFactory();
+        }
+    }
+    
+    public PrimitiveComponentType setBundleContext(BundleContext bc) {
+        ensureNotInitialized();
+        m_context = bc;
+        return this;
+    }
+    
+    public PrimitiveComponentType setClassName(String classname) {
+        ensureNotInitialized();
+        m_classname = classname;
+        return this;
+    }
+    
+    public PrimitiveComponentType setComponentTypeName(String name) {
+        ensureNotInitialized();
+        m_name = name;
+        return this;
+    }
+    
+    public PrimitiveComponentType setImmediate(boolean immediate) {
+        ensureNotInitialized();
+        m_immediate = immediate;
+        return this;
+    }
+    
+    public PrimitiveComponentType setFactoryMethod(String method) {
+        ensureNotInitialized();
+        m_factoryMethod = method;
+        return this;
+    }
+    
+    public PrimitiveComponentType setPropagation(boolean propagation) {
+        ensureNotInitialized();
+        m_propagation = propagation;
+        return this;
+    }
+    
+    public PrimitiveComponentType setPublic(boolean visible) {
+        ensureNotInitialized();
+        m_public = visible;
+        return this;
+    }
+    
+    public PrimitiveComponentType setManagedServicePID(String pid) {
+        ensureNotInitialized();
+        m_msPID = pid;
+        return this;
+    }
+    
+    public PrimitiveComponentType setValidateMethod(String method) {
+        ensureNotInitialized();
+        m_validate = method;
+        return this;
+    }
+    
+    public PrimitiveComponentType setInvalidateMethod(String method) {
+        ensureNotInitialized();
+        m_invalidate = method;
+        return this;
+    }
+    
+    private Element generateComponentMetadata() {
+        Element element = new Element("component", "");
+        element.addAttribute(new Attribute("classname", m_classname));
+        if (m_name != null) {
+            element.addAttribute(new Attribute("name", m_name));
+        }
+        if (m_factoryMethod != null) {
+            element.addAttribute(new Attribute("factory-method", m_factoryMethod));
+        }
+        if (! m_public) {
+            element.addAttribute(new Attribute("public", "false"));
+        }
+        if (m_immediate) {
+            element.addAttribute(new Attribute("immediate", "true"));
+        }
+        for (int i = 0; i < m_services.size(); i++) {
+            Service svc = (Service) m_services.get(i);
+            element.addElement(svc.getElement());
+        }
+        for (int i = 0; i < m_dependencies.size(); i++) {
+            Dependency dep = (Dependency) m_dependencies.get(i);
+            element.addElement(dep.getElement());
+        }
+        if (m_validate != null) {
+            Element callback = new Element("callback", "");
+            callback.addAttribute(new Attribute("transition", "validate"));
+            callback.addAttribute(new Attribute("method", m_validate));
+            element.addElement(callback);
+        }
+        if (m_invalidate != null) {
+            Element callback = new Element("callback", "");
+            callback.addAttribute(new Attribute("transition", "invalidate"));
+            callback.addAttribute(new Attribute("method", m_invalidate));
+            element.addElement(callback);
+        }
+        
+        // Properties
+        // First determine if we need the properties element
+        if (m_propagation || m_msPID != null || ! m_properties.isEmpty()) {
+            Element properties = new Element("properties", "");
+            if (m_propagation) {
+                properties.addAttribute(new Attribute("propagation", "true"));
+            }
+            if (m_msPID != null) {
+                properties.addAttribute(new Attribute("pid", m_msPID));
+            }
+            for (int i = 0; i < m_properties.size(); i++) {
+                Property prop = (Property) m_properties.get(i);
+                properties.addElement(prop.getElement());
+            }
+            element.addElement(properties);
+        }
+        
+        return element;
+    }
+    
+    private void createFactory() {
+        ensureValidity();
+        byte[] clazz = manipulate();
+        m_metadata = generateComponentMetadata();
+        Element meta = m_metadata;
+        meta.addElement(m_manipulation);
+        try {
+            m_factory = new ComponentFactory(m_context, clazz, meta);
+        } catch (ConfigurationException e) {
+            throw new IllegalStateException("An exception occurs during factory initialization
: " + e.getMessage());
+        }
+       
+    }
+    
+    private byte[] manipulate() {
+        Manipulator manipulator = new Manipulator();
+        try {
+            byte[] array = getClassByteArray();
+            byte[] newclazz = manipulator.manipulate(array);
+            m_manipulation = manipulator.getManipulationMetadata();
+            return newclazz;
+        } catch (IOException e) {
+            throw new IllegalStateException("An exception occurs during implementation class
manipulation : " + e.getMessage());
+        }
+    }
+    
+    private byte[] getClassByteArray() throws IOException {
+        String filename = m_classname.replace('.', '/') + ".class";
+        URL url = m_context.getBundle().getResource(filename);
+        if (url == null) {
+            throw new IllegalStateException("An exception occurs during implementation class
manipulation : cannot found the class file " + filename);
+        }
+        InputStream is = url.openStream();
+        if (is == null) {
+            throw new IllegalStateException("An exception occurs during implementation class
manipulation : cannot read the class file " + url);
+        }
+        byte[] b = new byte[is.available()]; 
+        is.read(b); 
+        return b;
+    }
+
+    public PrimitiveComponentType addService(Service svc) {
+        ensureNotInitialized();
+        m_services.add(svc);
+       return this;
+    }
+    
+    public PrimitiveComponentType addDependency(Dependency dep) {
+        ensureNotInitialized();
+        m_dependencies.add(dep);
+        return this;
+    }
+    
+    public PrimitiveComponentType addProperty(Property prop) {
+        ensureNotInitialized();
+        m_properties.add(prop);
+        return this;
+    }
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Property.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Property.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Property.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Property.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,82 @@
+package org.apache.felix.ipojo.api;
+
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+
+public class Property {
+    
+    private String m_name;
+    private String m_field;
+    private String m_value;
+    private String m_method;
+    private boolean m_mandatory;
+    private boolean m_immutable;
+    
+    public Property setName(String name) {
+        m_name = name;
+        return this;
+    }
+    
+    public Property setField(String name) {
+        m_field = name;
+        return this;
+    }
+    
+    public Property setMethod(String name) {
+        m_method = name;
+        return this;
+    }
+    
+    public Property setValue(String name) {
+        m_value = name;
+        return this;
+    }
+    
+    public Property setMandatory(boolean mandatory) {
+        m_mandatory = mandatory;
+        return this;
+    }
+    
+    public Property setImmutable(boolean immutable) {
+        m_immutable = immutable;
+        return this;
+    }
+    
+    public Element getElement() {
+        ensureValidity();
+        Element element = new Element("property", "");
+        if (m_name != null) {
+            element.addAttribute(new Attribute("name", m_name));
+        }
+        if (m_method != null) {
+            element.addAttribute(new Attribute("method", m_method));
+        }
+        if (m_value != null) {
+            element.addAttribute(new Attribute("value", m_value));
+        }
+        if (m_field != null) {
+            element.addAttribute(new Attribute("field", m_field));
+        }
+        if (m_mandatory) {
+            element.addAttribute(new Attribute("mandatory", Boolean.toString(m_mandatory)));
+        }
+        if (m_immutable) {
+            element.addAttribute(new Attribute("immutable", Boolean.toString(m_immutable)));
+        }
+        return element;
+    }
+
+    private void ensureValidity() {
+        // Two cases
+        // Field or Method
+        if (m_field == null && m_method == null) {
+            throw new IllegalStateException("A property must have either a field or a method");
+        }
+        if (m_immutable && m_value == null) {
+            throw new IllegalStateException("A immutable service property must have a value");
+        }
+    }
+    
+    
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Service.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Service.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Service.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/Service.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,98 @@
+package org.apache.felix.ipojo.api;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.ipojo.handlers.providedservice.ProvidedService;
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+
+public class Service {
+    
+    private List m_specifications; // null be default computed. 
+    private int m_strategy = ProvidedService.SINGLETON_STRATEGY;
+    private String m_customStrategy;
+    private List m_properties = new ArrayList();
+    
+    public Element getElement() {
+        ensureValidity();
+        Element element = new Element("provides", "");
+        if (m_specifications != null) {
+            element.addAttribute(new Attribute("specifications", getSpecificationsArray()));
+        }
+        element.addAttribute(new Attribute("strategy", getStringStrategy()));
+        for (int i = 0; i < m_properties.size(); i++) {
+            element.addElement(((ServiceProperty) m_properties.get(i)).getElement());
+        }
+        return element;   
+    }
+
+    
+    private void ensureValidity() {
+        // No check required.
+    }
+
+
+    private String getSpecificationsArray() {
+        if (m_specifications.size() == 1) {
+            return (String) m_specifications.get(0);
+        } else {
+            StringBuffer buffer = new StringBuffer("{");
+            for (int i = 0; i < m_specifications.size(); i++) {
+                if (i != 0) {
+                    buffer.append(',');
+                }
+                buffer.append(m_specifications.get(i));
+            }
+            buffer.append('}');
+            return buffer.toString();
+        }
+    }
+    
+    public Service addProperty(ServiceProperty ps) {
+            m_properties.add(ps);
+            return this;
+    }
+
+    public Service setSpecification(String spec) {
+        m_specifications = new ArrayList(1);
+        m_specifications.add(spec);
+        return this;
+    }
+    
+    public Service setSpecifications(List specs) {
+        m_specifications  = specs;
+        return this;
+    }
+    
+    public Service setCreationStrategy(int strategy) {
+        m_strategy = strategy;
+        return this;
+    }
+    
+    public Service setCreationStrategy(String strategy) {
+        m_strategy = -1; // Custom
+        m_customStrategy = strategy;
+        return this;
+    }
+    
+    private String getStringStrategy() {   
+        switch (m_strategy) {
+            case -1: // Custom policies
+                   return m_customStrategy;
+            case ProvidedService.SINGLETON_STRATEGY:
+                    return "singleton";
+            case  ProvidedService.STATIC_STRATEGY:
+                    return "method";
+            case ProvidedService.SERVICE_STRATEGY:
+                    return "service";
+            case ProvidedService.INSTANCE_STRATEGY:
+                    return "instance";
+            default:
+                throw new IllegalStateException("Unknown creation strategy :  " + m_strategy);
+        }
+    }
+    
+    
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ServiceProperty.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ServiceProperty.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ServiceProperty.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/ServiceProperty.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,82 @@
+package org.apache.felix.ipojo.api;
+
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+
+public class ServiceProperty {
+    
+    private String m_name;
+    private String m_field;
+    private String m_type;
+    private String m_value;
+    private boolean m_mandatory;
+    private boolean m_immutable;
+    
+    public ServiceProperty setName(String name) {
+        m_name = name;
+        return this;
+    }
+    
+    public ServiceProperty setField(String name) {
+        m_field = name;
+        return this;
+    }
+    
+    public ServiceProperty setType(String name) {
+        m_type = name;
+        return this;
+    }
+    
+    public ServiceProperty setValue(String name) {
+        m_value = name;
+        return this;
+    }
+    
+    public ServiceProperty setImmutable(boolean immutable) {
+        m_immutable = immutable;
+        return this;
+    }
+    
+    public ServiceProperty setMandatory(boolean mandatory) {
+        m_mandatory = mandatory;
+        return this;
+    }
+    
+    public Element getElement() {
+        ensureValidity();
+        Element element = new Element("property", "");
+        if (m_name != null) {
+            element.addAttribute(new Attribute("name", m_name));
+        }
+        if (m_type != null) {
+            element.addAttribute(new Attribute("type", m_type));
+        }
+        if (m_value != null) {
+            element.addAttribute(new Attribute("value", m_value));
+        }
+        if (m_field != null) {
+            element.addAttribute(new Attribute("field", m_field));
+        }
+        if (m_mandatory) {
+            element.addAttribute(new Attribute("mandatory", Boolean.toString(m_mandatory)));
+        }
+        if (m_immutable) {
+            element.addAttribute(new Attribute("immutable", Boolean.toString(m_immutable)));
+        }
+        return element;
+    }
+
+    private void ensureValidity() {
+        // Two cases
+        // Field or type
+        if (m_field == null && m_type == null) {
+            throw new IllegalStateException("A service property must have either a field
or a type");
+        }
+        if (m_immutable && m_value == null) {
+            throw new IllegalStateException("A immutable service property must have a value");
+        }
+    }
+    
+    
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/APITest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/APITest.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/APITest.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/APITest.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,191 @@
+package org.apache.felix.ipojo.api.test;
+
+import java.util.Properties;
+
+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.Dependency;
+import org.apache.felix.ipojo.api.PrimitiveComponentType;
+import org.apache.felix.ipojo.api.Property;
+import org.apache.felix.ipojo.api.Service;
+import org.apache.felix.ipojo.api.ServiceProperty;
+import org.osgi.framework.BundleContext;
+
+public class APITest {
+    
+    private BundleContext m_context;
+    private PrimitiveComponentType foo1;
+    
+    // Dependency
+    FooService fs;
+    BarService bs;
+    private PrimitiveComponentType foo2;
+    private PrimitiveComponentType foo3;
+    private PrimitiveComponentType foo4;
+    
+    public APITest(BundleContext bc) {
+        m_context = bc;
+    }
+    
+    public void start() {
+        
+        testFoo1();
+        testFoo2();
+        testFoo3();
+        testFoo4();
+        
+    }
+
+    private void testFoo1() {
+        System.out.println("Try to create a component type");
+        
+        ServiceProperty prop = new ServiceProperty();
+        prop.setName("message");
+        prop.setField("m_message");
+        
+        foo1 = new PrimitiveComponentType()
+            .setBundleContext(m_context)
+            .setClassName(Foo.class.getName())
+            .addService(new Service().addProperty(prop));
+        
+        
+                
+        foo1.start();
+        try {
+            Properties props = new Properties();
+            props.put("message", "hello");
+            foo1.createInstance(props);
+        } catch (UnacceptableConfiguration e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (MissingHandlerException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (ConfigurationException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        
+        System.out.println("fs : " + fs);
+        
+        System.out.println("Invoke 1 : " + fs.getMessage());
+        
+        foo1.stop();
+        
+    }
+    
+    private void testFoo2() {
+        System.out.println("Try to create a second component type");
+        foo2 = new PrimitiveComponentType()
+            .setBundleContext(m_context)
+            .setClassName(Foo.class.getName())
+            .setValidateMethod("start")
+            .setInvalidateMethod("stop");
+                
+        foo2.start();
+        ComponentInstance ci = null;
+        try {
+            ci = foo2.createInstance();
+        } catch (UnacceptableConfiguration e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (MissingHandlerException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (ConfigurationException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        
+        foo2.stop();
+    }
+    
+    private void testFoo3() {
+        System.out.println("Try to create a third component type");
+        foo3 = new PrimitiveComponentType()
+            .setBundleContext(m_context)
+            .setClassName(Foo.class.getName())
+            .addProperty(new Property().setField("m_message"))
+            .addService(new Service());
+
+        
+                
+        foo3.start();
+        ComponentInstance ci = null;
+        try {
+            Properties props = new Properties();
+            props.put("m_message", "bonjour");
+            ci = foo3.createInstance(props);
+        } catch (UnacceptableConfiguration e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (MissingHandlerException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (ConfigurationException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        
+        System.out.println("fs : " + fs);
+        
+        System.out.println("Invoke 2 : " + fs.getMessage());
+        foo3.stop();
+    }
+    
+    private void testFoo4() {
+        foo3.start();
+        
+        foo4 = new PrimitiveComponentType()
+        .setBundleContext(m_context)
+        .setClassName(Bar.class.getName())
+        .addService(new Service().setSpecification(BarService.class.getName()))
+        .addDependency(new Dependency().setField("fs"));
+        
+        
+        foo4.start();
+        
+        foo4.getFactory();
+        try {
+            foo4.createInstance();
+        } catch (UnacceptableConfiguration e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (MissingHandlerException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (ConfigurationException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        
+        try {
+            Properties props = new Properties();
+            props.put("m_message", "hallo");
+            ComponentInstance ci = foo3.createInstance(props);
+        } catch (UnacceptableConfiguration e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (MissingHandlerException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (ConfigurationException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }  
+        
+        System.out.println(bs.getMessage());
+        
+        foo4.stop();
+        foo3.stop();
+        
+        
+    }
+    
+    public void stop() {
+        
+    }
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Bar.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Bar.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Bar.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Bar.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,13 @@
+package org.apache.felix.ipojo.api.test;
+
+public class Bar implements BarService {
+    
+    private FooService fs;
+
+    public String getMessage() {
+       return fs.getMessage();
+    }
+    
+    
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/BarService.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/BarService.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/BarService.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/BarService.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,7 @@
+package org.apache.felix.ipojo.api.test;
+
+public interface BarService {
+    
+    String getMessage();
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Foo.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Foo.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Foo.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/Foo.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,23 @@
+package org.apache.felix.ipojo.api.test;
+
+public class Foo implements FooService {
+    
+    private String m_message;
+    
+    public Foo() {
+        System.out.println("Foo Created !");
+    }
+
+    public String getMessage() {
+        return m_message;
+    }
+    
+    public void start() {
+        System.out.println("Foo2 started...");
+    }
+    
+    public void stop() {
+        System.out.println("Foo2 stopped...");
+    }
+
+}

Added: felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/FooService.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/FooService.java?rev=730247&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/FooService.java
(added)
+++ felix/sandbox/clement/ipojo-utils/org.apache.felix.ipojo.api/src/main/java/org/apache/felix/ipojo/api/test/FooService.java
Tue Dec 30 13:37:00 2008
@@ -0,0 +1,7 @@
+package org.apache.felix.ipojo.api.test;
+
+public interface FooService {
+    
+    String getMessage();
+
+}



Mime
View raw message