From 9fb2fb275d3f69a287ac4f1c7d55190fb7a7bb3b Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 6 Oct 2008 08:32:38 +0000 Subject: Work in progress. Started to clean up cross-bundle dependencies on ContributionService implementation class, the various CompositeBuilder implementation classes and the ContributionDependencyBuilder implementation class. Added extension points for ContributionBuilders and CompositeBuilders to have them discovered and loaded by the Equinox ServiceDiscovery like other extension points. Moved injection of monitors and SCA definitions to the build methods instead of the constructors to remove references to these implementation constructors. Simplified NodeImpl a bit to remove references to other runtime implementation classes. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@702000 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/core/DefaultFactoryExtensionPoint.java | 133 +++++++++++++++++++++ .../tuscany/sca/core/FactoryExtensionPoint.java | 54 +++++++++ ...g.apache.tuscany.sca.core.FactoryExtensionPoint | 18 +++ 3 files changed, 205 insertions(+) create mode 100644 branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java create mode 100644 branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/FactoryExtensionPoint.java create mode 100644 branches/sca-equinox/modules/extensibility/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.FactoryExtensionPoint (limited to 'branches/sca-equinox/modules/extensibility/src') diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java new file mode 100644 index 0000000000..1d0da12f91 --- /dev/null +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/DefaultFactoryExtensionPoint.java @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.core; + +import java.lang.reflect.Constructor; +import java.util.HashMap; + +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; + + + +/** + * Default implementation of a model factory extension point. + * + * @version $Rev$ $Date$ + */ +public class DefaultFactoryExtensionPoint implements FactoryExtensionPoint { + + private HashMap, Object> factories = new HashMap, Object>(); + + /** + * Constructs a new DefaultModelFactoryExtensionPoint. + */ + public DefaultFactoryExtensionPoint() { + } + + /** + * Add a model factory extension. + * + * @param factory The factory to add + */ + public void addFactory(Object factory) { + Class[] interfaces = factory.getClass().getInterfaces(); + if (interfaces.length == 0) { + Class sc = factory.getClass().getSuperclass(); + if (sc != Object.class) { + factories.put(sc, factory); + } + } else { + for (int i = 0; i[] interfaces = factory.getClass().getInterfaces(); + if (interfaces.length == 0) { + Class sc = factory.getClass().getSuperclass(); + if (sc != Object.class) { + factories.remove(sc); + } + } else { + for (int i = 0; i T getFactory(Class factoryInterface) { + Object factory = factories.get(factoryInterface); + if (factory == null) { + + if (factoryInterface.isInterface()) { + + // Dynamically load a factory class declared under META-INF/services + try { + ServiceDeclaration factoryDeclaration = ServiceDiscovery.getInstance().getFirstServiceDeclaration(factoryInterface.getName()); + if (factoryDeclaration != null) { + Class factoryClass = factoryDeclaration.loadClass(); + + try { + // Default empty constructor + Constructor constructor = factoryClass.getConstructor(); + factory = constructor.newInstance(); + } catch (NoSuchMethodException e) { + + // Constructor taking the model factory extension point + Constructor constructor = factoryClass.getConstructor(FactoryExtensionPoint.class); + factory = constructor.newInstance(this); + } + + // Cache the loaded factory + addFactory(factory); + } + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } else { + + // Call the newInstance static method on the factory abstract class + try { + factory = ServiceDiscovery.getInstance().newFactoryClassInstance(factoryInterface.getName()); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + + // Cache the factory + addFactory(factory); + } + } + return factoryInterface.cast(factory); + } + +} diff --git a/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/FactoryExtensionPoint.java b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/FactoryExtensionPoint.java new file mode 100644 index 0000000000..4d38cb8afe --- /dev/null +++ b/branches/sca-equinox/modules/extensibility/src/main/java/org/apache/tuscany/sca/core/FactoryExtensionPoint.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.core; + +/** + * An extension point for model factories. Model factories are provided to + * abstract the classes that represent artifacts in the assembly model away + * from their creation mechanism. When the runtime needs to extend the model + * as it reads in contributed artifacts it looks up the factory for the + * artifact required in this registry + * + * @version $Rev$ $Date$ + */ +public interface FactoryExtensionPoint { + + /** + * Add a model factory extension. + * + * @param factory The factory to add + */ + void addFactory(Object factory); + + /** + * Remove a model factory extension. + * + * @param factory The factory to remove + */ + void removeFactory(Object factory); + + /** + * Get a factory implementing the given interface. + * @param factoryInterface the lookup key (factory interface) + * @return The factory + */ + T getFactory(Class factoryInterface); + +} diff --git a/branches/sca-equinox/modules/extensibility/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.FactoryExtensionPoint b/branches/sca-equinox/modules/extensibility/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.FactoryExtensionPoint new file mode 100644 index 0000000000..3c135aa267 --- /dev/null +++ b/branches/sca-equinox/modules/extensibility/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.FactoryExtensionPoint @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +org.apache.tuscany.sca.core.DefaultFactoryExtensionPoint -- cgit v1.2.3