Return-Path: Delivered-To: apmail-felix-commits-archive@www.apache.org Received: (qmail 63635 invoked from network); 17 Feb 2009 13:45:36 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 17 Feb 2009 13:45:36 -0000 Received: (qmail 22008 invoked by uid 500); 17 Feb 2009 13:45:36 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 21990 invoked by uid 500); 17 Feb 2009 13:45:36 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 21981 invoked by uid 99); 17 Feb 2009 13:45:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Feb 2009 05:45:36 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Feb 2009 13:45:28 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 60C142388A3D; Tue, 17 Feb 2009 13:45:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@felix.apache.org From: clement@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090217134508.60C142388A3D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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 @@ + + 4.0.0 + bundle + ipojo.utils + org.apache.felix.ipojo.metatype.bridge + 1.3.0-SNAPSHOT + Apache Felix iPOJO Metatype Bridge + + + + apache.snapshots + snapshot plugins + + http://people.apache.org/repo/m2-snapshot-repository + + + false + + + true + + + + + + + org.apache.felix + org.osgi.compendium + 1.2.0 + + + org.apache.felix + org.apache.felix.ipojo + 1.2.0 + + + + + + + org.apache.felix + maven-bundle-plugin + 1.4.3 + true + + + + ${pom.artifactId} + + + org.apache.felix.ipojo.metatype.bridge + + + + + + org.apache.felix + maven-ipojo-plugin + 1.2.0 + + + + ipojo-bundle + + + + + + + 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*/ 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()); + } + +}