felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r745067 - in /felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge: ./ 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/org/apa...
Date Tue, 17 Feb 2009 13:45:08 GMT
Author: clement
Date: Tue Feb 17 13:45:07 2009
New Revision: 745067

URL: http://svn.apache.org/viewvc?rev=745067&view=rev
Log: (empty)

Added:
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/   (with props)
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/metadata.xml
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/pom.xml
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeBridge.java
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeConsumer.java
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapper.java
    felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/resources/

Propchange: felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Feb 17 13:45:07 2009
@@ -0,0 +1,7 @@
+.settings
+
+target
+
+.classpath
+
+.project

Added: felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/metadata.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/metadata.xml?rev=745067&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/metadata.xml (added)
+++ felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/metadata.xml Tue Feb 17 13:45:07
2009
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<iPOJO>
+	<component className="org.apache.felix.ipojo.metatype.bridge.MetatypeBridge" architecture="true"
public="false">
+		<requires filter="(factory.state=1)" optional="true" aggregate="true">
+			<callback type="bind" method="bindFactory"/>
+			<callback type="unbind" method="unbindFactory"/>
+		</requires>
+		<requires field="m_wrapperFactory" filter="(factory.name=MetatypeBundleBridge)"/>
+		<callback transition="invalidate" method="stop"/>
+	</component>
+	<instance component="org.apache.felix.ipojo.metatype.bridge.MetatypeBridge"/>
+	
+	<component className="org.apache.felix.ipojo.metatype.bridge.MetatypeFactoryWrapper"
architecture="false" name="MetatypeBundleBridge">
+		<provides>
+			<property name="bundle.id" type="long"/>
+			<property name="service.pid" type="java.lang.String"/>
+			<property name="factory.name" type="java.lang.String"/>
+		</provides>
+		<properties>
+			<property field="m_factory" name="factory"/>
+		</properties>
+	</component>
+	
+	<component classname="org.apache.felix.ipojo.metatype.bridge.MetatypeConsumer" immediate="true">
+		<requires field="mts"/>
+	</component>
+</iPOJO>
\ No newline at end of file

Added: felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/pom.xml?rev=745067&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/pom.xml (added)
+++ felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/pom.xml Tue Feb 17 13:45:07 2009
@@ -0,0 +1,70 @@
+<project>
+	<modelVersion>4.0.0</modelVersion>
+	<packaging>bundle</packaging>
+	<groupId>ipojo.utils</groupId>
+	<artifactId>org.apache.felix.ipojo.metatype.bridge</artifactId>
+	<version>1.3.0-SNAPSHOT</version>
+	<name>Apache Felix iPOJO Metatype Bridge</name>
+
+	<pluginRepositories>
+		<pluginRepository>
+			<id>apache.snapshots</id>
+			<name>snapshot plugins</name>
+			<url>
+				http://people.apache.org/repo/m2-snapshot-repository
+			</url>
+			<releases>
+				<enabled>false</enabled>
+			</releases>
+			<snapshots>
+				<enabled>true</enabled>
+			</snapshots>
+		</pluginRepository>
+	</pluginRepositories>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.apache.felix</groupId>
+			<artifactId>org.osgi.compendium</artifactId>
+			<version>1.2.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.felix</groupId>
+			<artifactId>org.apache.felix.ipojo</artifactId>
+			<version>1.2.0</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.felix</groupId>
+				<artifactId>maven-bundle-plugin</artifactId>
+				<version>1.4.3</version>
+				<extensions>true</extensions>
+				<configuration>
+					<instructions>
+						<Bundle-SymbolicName>
+							${pom.artifactId}
+						</Bundle-SymbolicName>
+						<Private-Package>
+							org.apache.felix.ipojo.metatype.bridge
+						</Private-Package>
+					</instructions>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.felix</groupId>
+				<artifactId>maven-ipojo-plugin</artifactId>
+				<version>1.2.0</version>
+				<executions>
+					<execution>
+						<goals>
+							<goal>ipojo-bundle</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>

Added: felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeBridge.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeBridge.java?rev=745067&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeBridge.java
(added)
+++ felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeBridge.java
Tue Feb 17 13:45:07 2009
@@ -0,0 +1,73 @@
+package org.apache.felix.ipojo.metatype.bridge;
+
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.InstanceManager;
+import org.osgi.framework.Bundle;
+
+/**
+ * Creates on the fly ManagedServiceFactory implementing MetatypeProvider.
+ * The created MSF just delegates calls on the wrapped one.
+ */
+public class MetatypeBridge {
+    
+    /**
+     * Store created wrapper per iPOJO Factories.
+     */
+    private Map/*Factory, ComponentInstance<MetatypeBundleBridge>*/ m_bridges = new
HashMap();
+    
+    /**
+     * Wrapper Factory
+     */
+    private Factory m_wrapperFactory; // injected (only valid factories)
+    
+    public synchronized void bindFactory(Factory factory) {
+        Bundle bundle = factory.getComponentDescription().getBundleContext().getBundle();
+        InstanceManager instance = (InstanceManager) m_bridges.get(factory);
+        if (instance == null) {
+            // Create a new instance for this factory
+            Dictionary prop = new Properties();
+            prop.put("bundle.id", new Long(bundle.getBundleId()));
+            prop.put("factory", factory);
+            prop.put("factory.name", factory.getName());
+            prop.put("service.pid", factory.getName()+ "-WRAPPER");
+
+            try {
+                instance = (InstanceManager) m_wrapperFactory.createComponentInstance(prop);
+                m_bridges.put(factory, instance);
+            } catch (Exception e) {
+                System.out.println("ERROR : Cannot create a bride instance : " + e.getMessage());
+                e.printStackTrace();
+                return;
+            } 
+        }
+    }
+
+    public synchronized void unbindFactory(Factory factory) {
+        InstanceManager instance = (InstanceManager) m_bridges.remove(factory);
+        if (instance == null) {
+            // Ignore ...
+            return;
+        } else {
+            instance.dispose();
+        }
+        
+    }
+    
+    public void stop() {
+        // Cleanup
+        Collection instances = m_bridges.values();
+        Iterator it = instances.iterator();
+        while(it.hasNext()) {
+            ((InstanceManager) it.next()).dispose();
+        }
+        m_bridges.clear();
+    }
+    
+}

Added: felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeConsumer.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeConsumer.java?rev=745067&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeConsumer.java
(added)
+++ felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeConsumer.java
Tue Feb 17 13:45:07 2009
@@ -0,0 +1,22 @@
+package org.apache.felix.ipojo.metatype.bridge;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.metatype.MetaTypeInformation;
+import org.osgi.service.metatype.MetaTypeService;
+
+public class MetatypeConsumer {
+    
+    MetaTypeService mts; // injected
+    
+    public MetatypeConsumer(BundleContext bc) {
+        Bundle bundle = bc.getBundle(27); // 27 is cool
+        MetaTypeInformation mty = mts.getMetaTypeInformation(bundle);
+        String[] pids = mty.getFactoryPids();
+        System.out.println("Metatype for the bundle 27:");
+        for (int i = 0; i < pids.length; i++) {
+            System.out.println("\t" + pids[i]);
+        }
+    }
+
+}

Added: felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapper.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapper.java?rev=745067&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapper.java
(added)
+++ felix/sandbox/clement/ipojo-utils/iPOJO-Metatype-Bridge/src/main/java/org/apache/felix/ipojo/metatype/bridge/MetatypeFactoryWrapper.java
Tue Feb 17 13:45:07 2009
@@ -0,0 +1,229 @@
+package org.apache.felix.ipojo.metatype.bridge;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+
+import org.apache.felix.ipojo.IPojoFactory;
+import org.apache.felix.ipojo.architecture.ComponentTypeDescription;
+import org.apache.felix.ipojo.architecture.PropertyDescription;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+import org.osgi.service.metatype.AttributeDefinition;
+import org.osgi.service.metatype.MetaTypeProvider;
+import org.osgi.service.metatype.ObjectClassDefinition;
+
+/**
+ * ManagedServiceFactory wrapper.
+ * This class delegates call to the wrapped managed service factory.
+ * However, it provides a Metatype description for the factory configurations.
+ */
+public class MetatypeFactoryWrapper implements MetaTypeProvider, ManagedServiceFactory {
+    
+    /**
+     * Wrapped Factory.
+     */
+    private IPojoFactory m_factory; //Injected
+
+    private class ObjectClassDefinitionImpl implements ObjectClassDefinition {
+        
+        private ComponentTypeDescription m_description;
+        
+        List m_attributes = new ArrayList();
+        
+        public ObjectClassDefinitionImpl(ComponentTypeDescription desc) {
+            m_description = desc;
+            PropertyDescription[] props = desc.getProperties();
+            for (int i = 0; i < props.length; i++) {
+                m_attributes.add(new AttributeDefinitionImpl(props[i], m_description.getName()));
+            }
+        }
+
+        public AttributeDefinition[] getAttributeDefinitions(int filter) {
+            List list = new ArrayList();
+            switch(filter) {
+                case ObjectClassDefinition.OPTIONAL :
+                    for (int i = 0; i < m_attributes.size(); i++) {
+                        AttributeDefinitionImpl att = (AttributeDefinitionImpl) m_attributes.get(i);
+                        if (! att.m_isImmutable && ! att.m_isMandatory) {
+                            list.add(att);
+                        }
+                    }
+                    break;
+                case ObjectClassDefinition.REQUIRED:
+                    for (int i = 0; i < m_attributes.size(); i++) {
+                        AttributeDefinitionImpl att = (AttributeDefinitionImpl) m_attributes.get(i);
+                        if (! att.m_isImmutable && att.m_isMandatory) {
+                            list.add(att);
+                        }
+                    }
+                    break;
+                 default :
+                     for (int i = 0; i < m_attributes.size(); i++) {
+                         AttributeDefinitionImpl att = (AttributeDefinitionImpl) m_attributes.get(i);
+                         if (! att.m_isImmutable) {
+                             list.add(att);
+                         }
+                     }
+            }
+            return (AttributeDefinition[]) list.toArray(new AttributeDefinition[list.size()]);
   
+        }
+
+        public String getDescription() {
+            return m_description.getName();
+        }
+
+        public String getID() {
+            return m_description.getName();
+        }
+
+        public InputStream getIcon(int arg0) throws IOException {
+            return null;
+        }
+
+        public String getName() {
+            return m_description.getName();
+        }
+        
+    }
+    
+    private class AttributeDefinitionImpl implements AttributeDefinition {
+        
+        private PropertyDescription m_desc;
+        private String m_typeName;
+
+        private int m_type;
+        private int m_cardinality;
+        private boolean m_isMandatory;
+        private boolean m_isImmutable;
+        
+        public AttributeDefinitionImpl(PropertyDescription desc, String typeName) {
+            m_desc = desc;
+            m_typeName = typeName;
+            m_isMandatory = desc.isMandatory();
+            m_isImmutable = desc.isImmutable();
+            String type = m_desc.getType();
+            String internType = null;
+            if (type.endsWith("[]")) {
+                // Array
+                m_cardinality = Integer.MAX_VALUE;
+                int index = type.indexOf('[');
+                internType = type.substring(0, index);
+            } else if (type.equals(java.util.Vector.class.getName())) {
+                m_cardinality = Integer.MIN_VALUE;
+                internType = String.class.getName();
+            } else {
+                m_cardinality = 0;
+                internType = type;
+            }
+            m_type = getTypeByInternType(internType);
+        }
+
+        private int getTypeByInternType(String internType) {
+            //STRING,LONG,INTEGER, CHAR,BYTE,DOUBLE,FLOAT, BOOLEAN
+
+            if (internType.equalsIgnoreCase("string") || internType.equals(String.class.getName()))
{
+                return AttributeDefinition.STRING;
+            }
+            if (internType.equals("boolean")) {
+                return AttributeDefinition.BOOLEAN;
+            }
+            if (internType.equals("float")) {
+                return AttributeDefinition.FLOAT;
+            }
+            if (internType.equals("double")) {
+                return AttributeDefinition.DOUBLE;
+            }
+            if (internType.equals("byte")) {
+                return AttributeDefinition.BYTE;
+            }
+            if (internType.equals("char")) {
+                return AttributeDefinition.CHARACTER;
+            }
+            if (internType.equals("int")) {
+                return AttributeDefinition.INTEGER;
+            }
+            if (internType.equals("long")) {
+                return AttributeDefinition.LONG;
+            }
+            System.out.println("WARNING : Cannot determine the type of " + internType);
+            return -1;
+        }
+
+        public int getCardinality() {
+            return m_cardinality;
+        }
+
+        public String[] getDefaultValue() {
+           if (m_desc.getValue() != null) {
+               if (getCardinality() == 0) {
+                   return new String[] { m_desc.getValue() };
+               } else if (m_cardinality > 0) {
+                   return org.apache.felix.ipojo.parser.ParseUtils.parseArrays(m_desc.getValue());
+               } else if (m_cardinality < 0) {
+                  //TODO handle vector case
+                   return new String[] { m_desc.getValue() };
+               }
+           }
+           return null;
+        }
+
+        public String getDescription() {
+            return m_desc.getName();
+        }
+
+        public String getID() {
+            return m_typeName + "-" + m_desc.getName();
+
+        }
+
+        public String getName() {
+            return m_desc.getName();
+        }
+
+        public String[] getOptionLabels() {
+            return null;
+        }
+
+        public String[] getOptionValues() {
+            return null;
+        }
+
+        public int getType() {
+           return m_type;
+        }
+
+        public String validate(String arg0) {
+            return null; // No validation
+        }
+        
+        
+        
+    }
+
+    public void deleted(String arg0) {
+        m_factory.deleted(arg0);
+    }
+
+    public String getName() {
+       return m_factory.getName();
+    }
+
+    public void updated(String arg0, Dictionary arg1)
+            throws ConfigurationException {
+        m_factory.updated(arg0, arg1);
+        
+    }
+
+    public String[] getLocales() {
+        return null;
+    }
+
+    public ObjectClassDefinition getObjectClassDefinition(String id,
+            String loc) {
+        return new ObjectClassDefinitionImpl(m_factory.getComponentDescription());
+    }
+
+}



Mime
View raw message