diff options
Diffstat (limited to 'tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config')
10 files changed, 0 insertions, 1254 deletions
diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ComponentTypeIntrospector.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ComponentTypeIntrospector.java deleted file mode 100644 index c80a8f31ba..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ComponentTypeIntrospector.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config; - -import org.apache.tuscany.model.assembly.ComponentType; - -/** - * Interface for implementations that are able create SCA definitions - * by inspecting Java classes. - * - * @version $Rev$ $Date$ - */ -public interface ComponentTypeIntrospector { - /** - * Create a componentType definition by introspecting a Java Class. - * - * @param implClass the class to inspect - * @return a componentType definition - * @throws ConfigurationException if the Class does not define a valid component type - */ - ComponentType introspect(Class<?> implClass) throws ConfigurationException; - -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ConfigurationException.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ConfigurationException.java deleted file mode 100644 index 96365311f2..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ConfigurationException.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config; - -import org.apache.tuscany.common.TuscanyException; - - -/** - * Base class for exceptions that pertain to configuration. - * - * @version $Rev$ $Date$ - */ -public class ConfigurationException extends TuscanyException { - public ConfigurationException() { - } - - public ConfigurationException(String message) { - super(message); - } - - public ConfigurationException(String message, Throwable cause) { - super(message, cause); - } - - public ConfigurationException(Throwable cause) { - super(cause); - } -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ConfigurationLoadException.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ConfigurationLoadException.java deleted file mode 100644 index 3055dc2fd2..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ConfigurationLoadException.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config; - -import java.io.IOException; - -/** - * Exception indicating that there was a problem loading a configuration resource. - * - * @version $Rev$ $Date$ - */ -public class ConfigurationLoadException extends ConfigurationException { - - /** - * Constructor specifying the resource that was being loaded and the IOException that resulted. - * These are returned as the message and cause respectively. - * - * @param resource the resource being loaded - * @param cause the IOException that occurred - */ - public ConfigurationLoadException(String resource, IOException cause) { - super(resource, cause); - } - - /** - * Constructor specifying the resource that was being loaded. - * - * @param resource the resource being loaded - */ - public ConfigurationLoadException(String resource) { - super(resource); - } -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ImplementationCache.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ImplementationCache.java deleted file mode 100644 index 595f499797..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ImplementationCache.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config; - -import javax.xml.namespace.QName; - -import org.apache.tuscany.model.assembly.ComponentImplementation; - -/** - * Cache of introspected implementations. - * - * @version $Rev$ $Date$ - */ -public interface ImplementationCache { - /** - * Return an implementation from a given namespace. - * - * @param type the namespace that defines the type of implementation - * @param name the name of an implementation in that namespace - * @return the implementation or null if it is not present in the cache - */ - ComponentImplementation get(QName type, String name); - - /** - * Add an implementation to the cache - * - * @param type the namespace that defines the type of implementation - * @param name the name of an implementation in that namespace - * @param implementation the introspected implementation - */ - void put(QName type, String name, ComponentImplementation implementation); -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/InvalidRootElementException.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/InvalidRootElementException.java deleted file mode 100644 index 19df4784cd..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/InvalidRootElementException.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config; - -/** - * Configuration exception that indicates the root element in an XML file was not the one expected. - * - * @version $Rev$ $Date$ - */ -public class InvalidRootElementException extends ConfigurationException { - private final String resource; - private final String element; - - /** - * Constructor specifying the location of the resource and the element that was expected. - * - * @param resource the resource - * @param element the expected root element - */ - public InvalidRootElementException(String resource, String element) { - super(); - this.resource = resource; - this.element = element; - } - - public String getMessage() { - return "XML document element in resource " + resource + " is not a " + element; - } - - public String getResource() { - return resource; - } - - public String getElement() { - return element; - } -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/JavaIntrospectionHelper.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/JavaIntrospectionHelper.java deleted file mode 100644 index c10ff82310..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/JavaIntrospectionHelper.java +++ /dev/null @@ -1,349 +0,0 @@ -package org.apache.tuscany.core.config; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Implements various reflection-related operations - * - * @version $Rev$ $Date$ - */ -public class JavaIntrospectionHelper { - - private static final Class[] EMPTY_CLASS_ARRY = new Class[0]; - - // ---------------------------------- - // Constructors - // ---------------------------------- - - /** - * Hide the constructor - */ - private JavaIntrospectionHelper() { - } - - // ---------------------------------- - // Methods - // ---------------------------------- - - /** - * Returns a collection of public, private, protected, or default fields declared by a class or one of its - * supertypes - */ - public static Set<Field> getAllFields(Class pClass) { - return getAllFields(pClass, new HashSet<Field>()); - } - - /** - * Recursively evaluates the type hierachy to return all fields on a given type - * - * @spec This raises an interesting issue - do we allow injection on private supertype fields in a subtype even if - * they are annotated? - */ - private static Set<Field> getAllFields(Class pClass, Set<Field> fields) { - if (pClass == null || pClass.isArray() || Object.class.equals(pClass)) { - return fields; - } - fields = getAllFields(pClass.getSuperclass(), fields); - Field[] declaredFields = pClass.getDeclaredFields(); - for (int i = 0; i < declaredFields.length; i++) { - declaredFields[i].setAccessible(true); // ignore Java accessibility - fields.add(declaredFields[i]); - } - return fields; - } - - /** - * Returns a collection of public, private, protected, or default methods declared by a class or one of its - * supertypes. Note that overriden methods will not be returned in the collection (i.e. only the method override - * will be). <p/> This method can potentially be expensive as reflection information is not cached. It is assumed - * that this method will be used during a configuration phase. - */ - public static Set<Method> getAllUniqueMethods(Class pClass) { - return getAllUniqueMethods(pClass, new HashSet<Method>()); - } - - /** - * Recursively evaluates the type hierarchy to return all unique methods - */ - private static Set<Method> getAllUniqueMethods(Class pClass, Set<Method> methods) { - if (pClass == null || pClass.isArray() || Object.class.equals(pClass)) { - return methods; - } - // we first evaluate methods of the subclass and then move to the parent - Method[] declaredMethods = pClass.getDeclaredMethods(); - for (int i = 0; i < declaredMethods.length; i++) { - if (methods.size() == 0) { - methods.add(declaredMethods[i]); - } else { - List temp = new ArrayList(); - boolean matched = false; - for (Method method : methods) { - // only add if not already in the set from a supclass (i.e. the - // method is not overrided) - if (exactMethodMatch(declaredMethods[i], method)) { - matched = true; - break; - } - } - if (!matched) { - // TODO ignore Java accessibility - declaredMethods[i].setAccessible(true); - temp.add(declaredMethods[i]); - - } - methods.addAll(temp); - temp.clear(); - } - } - // evaluate class hierarchy - this is done last to track inherited methods - methods = getAllUniqueMethods(pClass.getSuperclass(), methods); - return methods; - } - - /** - * Finds the closest matching field with the given name, that is, a field of the exact specified type or, - * alternately, of a supertype. - * - * @param name the name of the field - * @param type the field type - * @param fields the collection of fields to search - * @return the matching field or null if not found - * @throws NoSuchFieldException if no field found - */ - public static Field findClosestMatchingField(String name, Class type, Set<Field> fields) { - Field candidate = null; - for (Field field : fields) { - if (field.getName().equals(name)) { - if (field.getType().equals(type)) { - return field; // exact match - } else if (field.getType().isAssignableFrom(type) - || (field.getType().isPrimitive() && primitiveAssignable(field.getType(), type))) { - // We could have the situation where a field parameter is a primitive and the demarshalled value is - // an object counterpart (e.g. Integer and int) - // @spec issue - // either an interface or super class, so keep a reference until - // we know there are no closer types - candidate = field; - } - } - } - if (candidate != null) { - return candidate; - } else { - return null; - } - } - - /** - * Finds the closest matching method with the given name, that is, a method taking the exact parameter types or, - * alternately, parameter supertypes. - * - * @param name the name of the method - * @param types the method parameter types - * @param methods the collection of methods to search - * @return the matching method or null if not found - * @throws NoSuchFieldException if no field found - */ - public static Method findClosestMatchingMethod(String name, Class[] types, Set<Method> methods) { - if (types == null) { - types = EMPTY_CLASS_ARRY; - } - Method candidate = null; - for (Method method : methods) { - if (method.getName().equals(name) && method.getParameterTypes().length == types.length) { - Class[] params = method.getParameterTypes(); - boolean disqualify = false; - boolean exactMatch = true; - for (int i = 0; i < params.length; i++) { - if (!params[i].equals(types[i]) && !params[i].isAssignableFrom(types[i])) { - // no match - disqualify = true; - exactMatch = false; - break; - } else if (!params[i].equals(types[i]) && params[i].isAssignableFrom(types[i])) { - // not exact match - exactMatch = false; - } - } - if (disqualify) { - continue; - } else if (exactMatch) { - return method; - } else { - candidate = method; - } - } - } - if (candidate != null) { - return candidate; - } else { - return null; - } - } - - /** - * Returns a field or method defined in the given class or its superclasses matching a literal name and parameter - * types <p/> This method can potentially be expensive as reflection information is not cached. It is assumed that - * this method will be used during a configuration phase. - * - * @param clazz the class to introspect - * @param propertName the literal name of the property (i.e. JavaBean conventions are not applied) - * @param paramTypes the parameter types for a method or null for fields or methods with no parameters - * @return the field, method or null - */ - public static AccessibleObject getBeanProperty(Class clazz, String propertName, Class[] paramTypes) { - - Set<Method> methods = getAllUniqueMethods(clazz); - for (Method method : methods) { - if (method.getName().equals(propertName)) { - Class[] types = method.getParameterTypes(); - if (types.length == 0 && paramTypes == null) { - return method; - } else if (types.length != 0 && paramTypes == null) { - break; - } else if (types.length == paramTypes.length) { - for (int n = 0; n < types.length - 1; n++) { - if (!types[n].equals(paramTypes[n]) || !types[n].isAssignableFrom(paramTypes[n])) { - break; - } - } - return method; - } - } - } - - Set<Field> fields = getAllFields(clazz); - for (Field field : fields) { - if (field.getName().equals(propertName)) { - return field; - } - } - return null; - } - - /** - * Determines if two methods "match" - that is, they have the same method names and exact parameter types (one is - * not a supertype of the other) - */ - public static boolean exactMethodMatch(Method method1, Method method2) { - if (!method1.getName().equals(method2.getName())) { - return false; - } - Class[] types1 = method1.getParameterTypes(); - Class[] types2 = method2.getParameterTypes(); - if (types1.length == 0 && types2.length == 0) { - return true; - } else if (types1.length == types2.length) { - for (int n = 0; n < types1.length; n++) { - if (!types1[n].equals(types2[n])) { - return false; - } - } - return true; - } - return false; - } - - public static Constructor getDefaultConstructor(Class clazz) throws NoSuchMethodException { - return clazz.getConstructor((Class[]) null); - } - - /** - * Loads a class corresponding to the class name using the current context class loader. - * - * @throws ClassNotFoundException if the class was not found on the classpath - */ - public static Class loadClass(String pName) throws ClassNotFoundException { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - return Class.forName(pName, true, loader); - } - - /** - * Returns the simple name of a class - i.e. the class name devoid of its package qualifier - * - * @param implClass - * @return - */ - public static String getBaseName(Class<?> implClass) { - String baseName = implClass.getName(); - int lastDot = baseName.lastIndexOf('.'); - if (lastDot != -1) { - baseName = baseName.substring(lastDot + 1); - } - return baseName; - } - - public static boolean isImmutable(Class clazz) { - return (String.class == clazz || clazz.isPrimitive() || Number.class.isAssignableFrom(clazz) - || Boolean.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz) || Byte.class - .isAssignableFrom(clazz)); - } - - /** - * Takes a property name and converts it to a getter method name according to JavaBean conventions. For example, - * property <code>foo<code> is returned as <code>getFoo</code> - */ - public static String toGetter(String name) { - return "get" + name.toUpperCase().substring(0, 1) + name.substring(1); - } - - /** - * Takes a setter or getter method name and converts it to a property name according to JavaBean conventions. For - * example, <code>setFoo(var)</code> is returned as property <code>foo<code> - */ - public static String toPropertyName(String name) { - return name.substring(3, 4).toLowerCase() + name.substring(4); - } - - /** - * Takes a property name and converts it to a setter method name according to JavaBean conventions. For example, the - * property <code>foo<code> is returned as <code>setFoo(var)</code> - */ - public static String toSetter(String name) { - return "set" + name.toUpperCase().substring(0, 1) + name.substring(1); - } - - /** - * Compares a two types, assuming one is a primitive, to dtermine if the other is its object counterpart - */ - private static boolean primitiveAssignable(Class memberType, Class param) { - if (memberType == Integer.class) { - return (param == Integer.TYPE); - } else if (memberType == Double.class) { - return (param == Double.TYPE); - } else if (memberType == Float.class) { - return (param == Float.TYPE); - } else if (memberType == Short.class) { - return (param == Short.TYPE); - } else if (memberType == Character.class) { - return (param == Character.TYPE); - } else if (memberType == Boolean.class) { - return (param == Boolean.TYPE); - } else if (memberType == Byte.class) { - return (param == Byte.TYPE); - } else if (param == Integer.class) { - return (memberType == Integer.TYPE); - } else if (param == Double.class) { - return (memberType == Double.TYPE); - } else if (param == Float.class) { - return (memberType == Float.TYPE); - } else if (param == Short.class) { - return (memberType == Short.TYPE); - } else if (param == Character.class) { - return (memberType == Character.TYPE); - } else if (param == Boolean.class) { - return (memberType == Boolean.TYPE); - } else if (param == Byte.class) { - return (memberType == Byte.TYPE); - } else { - return false; - } - } -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/MissingResourceException.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/MissingResourceException.java deleted file mode 100644 index 7b40ccbefa..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/MissingResourceException.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config; - -/** - * Exception that indicates an expected resource could not be found. - * - * @version $Rev$ $Date$ - */ -public class MissingResourceException extends ConfigurationException { - /** - * Constructor that indicates which resource could not be found. - * The supplied parameter is also returned as the message. - * - * @param resource the resource that could not be found - */ - public MissingResourceException(String resource) { - super(resource); - } - - /** - * Return the name of the expected resource. - * - * @return the name of the expected resource - */ - public String getResource() { - return getMessage(); - } -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ModuleComponentConfigurationLoader.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ModuleComponentConfigurationLoader.java deleted file mode 100644 index a8e2718222..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/ModuleComponentConfigurationLoader.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config; - -import java.util.Collection; - -import org.apache.tuscany.model.assembly.ModuleComponent; - -/** - * Interface for loading configuration information from some external - * form into a Tuscany logical model. - * - * @version $Rev: 368822 $ $Date: 2006-01-13 10:54:38 -0800 (Fri, 13 Jan 2006) $ - */ -public interface ModuleComponentConfigurationLoader { - - /** - * Load a SCDL module component. - * - * @param name the name of the module component - * @param uri - * @return a new module component definition - * @throws ConfigurationException if there was a problem loading the module component. - */ - ModuleComponent loadModuleComponent(String name, String uri) throws ConfigurationLoadException; - - /** - * Load a System SCDL module component. - * - * @param name the name of the module component - * @param uri - * @return a new module component definition - * @throws ConfigurationException if there was a problem loading the module component. - */ - ModuleComponent loadSystemModuleComponent(String name, String uri) throws ConfigurationLoadException; - - /** - * Load a SCDL module component. - * - * @param name the name of the module component - * @param uri - * @param url - * @return a new module component definition - * @throws ConfigurationException if there was a problem loading the module component. - */ - ModuleComponent loadModuleComponent(String name, String uri, String url) throws ConfigurationLoadException; - - /** - * Load a SCDL module component. - * - * @param name the name of the module component - * @param uri - * @param url - * @param urls - * @return a new module component definition - * @throws ConfigurationException if there was a problem loading the module component. - */ - ModuleComponent loadModuleComponent(String name, String uri, String url, Collection<String> urls) throws ConfigurationLoadException; - -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/impl/Java5ComponentTypeIntrospector.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/impl/Java5ComponentTypeIntrospector.java deleted file mode 100644 index 8799969028..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/impl/Java5ComponentTypeIntrospector.java +++ /dev/null @@ -1,423 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config.impl; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.osoa.sca.annotations.Callback; -import org.osoa.sca.annotations.Remotable; - -import org.apache.tuscany.core.config.ComponentTypeIntrospector; -import org.apache.tuscany.core.config.ConfigurationException; -import org.apache.tuscany.core.config.JavaIntrospectionHelper; -import org.apache.tuscany.model.assembly.AssemblyFactory; -import org.apache.tuscany.model.assembly.ComponentType; -import org.apache.tuscany.model.assembly.Multiplicity; -import org.apache.tuscany.model.assembly.Property; -import org.apache.tuscany.model.assembly.Reference; -import org.apache.tuscany.model.assembly.Scope; -import org.apache.tuscany.model.assembly.Service; -import org.apache.tuscany.model.assembly.ServiceContract; -import org.apache.tuscany.model.types.java.JavaServiceContract; - -/** - * Introspects Java annotation-based metata data - * - * @version $Rev$ $Date$ - */ -public class Java5ComponentTypeIntrospector implements ComponentTypeIntrospector { - private final AssemblyFactory factory; - - public Java5ComponentTypeIntrospector(AssemblyFactory factory) { - this.factory = factory; - } - - /** - * Returns a component type for the given class - * - * @throws ConfigurationException - */ - public ComponentType introspect(Class<?> implClass) throws ConfigurationException { - ComponentType compType = factory.createComponentType(); - introspectServices(compType, implClass); - introspectAnnotatedMembers(compType, implClass); - - // if implementation is not annotated and no annotated members were found, add public fields and setters - if (!implClass.isAnnotationPresent(org.osoa.sca.annotations.Service.class) && compType.getProperties().isEmpty() - && compType.getReferences().isEmpty()) { - introspectMembers(compType, implClass); - } - - // FIXME scopes should be handled at the interface level - if (compType != null) { - Scope scope = getScope(implClass); - for (Iterator<Service> i = compType.getServices().iterator(); i.hasNext();) { - ServiceContract intf = i.next().getServiceContract(); - if (intf != null) - intf.setScope(scope); - } - } - - return compType; - } - - /** - * Returns the scope for a given class - * - */ - private static Scope getScope(Class<?> implClass) { - org.osoa.sca.annotations.Scope scope = implClass.getAnnotation(org.osoa.sca.annotations.Scope.class); - if (scope == null) { - // scope was not defined on the implementation class, look for annotated interfaces - Class<?>[] interfaces = implClass.getInterfaces(); - for (int i = 0; i < interfaces.length; i++) { - scope = interfaces[i].getAnnotation(org.osoa.sca.annotations.Scope.class); - } - } - if (scope == null) { - return Scope.INSTANCE; - } - - if ("MODULE".equalsIgnoreCase(scope.value())) { - return (Scope.MODULE); - } else if ("SESSION".equalsIgnoreCase(scope.value())) { - return (Scope.SESSION); - } else if ("REQUEST".equalsIgnoreCase(scope.value())) { - return (Scope.REQUEST); - } else { - return (Scope.INSTANCE); - } - } - - /** - * Adds the supported services for a component implementation type to its component type - * - * @param compType the component type being generated - * @param implClass the component implementation type class - * @throws ConfigurationException - */ - protected void introspectServices(ComponentType compType, Class<?> implClass) throws ConfigurationException { - List<Service> services = compType.getServices(); - assert services.isEmpty() : "componentType already has services defined"; - - // add services defined in an @Service annotation - org.osoa.sca.annotations.Service serviceAnnotation = implClass.getAnnotation(org.osoa.sca.annotations.Service.class); - if (serviceAnnotation != null) { - Class<?>[] interfaces = serviceAnnotation.interfaces(); - Class<?> value = serviceAnnotation.value(); - if (interfaces.length > 0) { - if (!Void.class.equals(value)) { - throw new IllegalArgumentException("Both interfaces and value specified in @Service on " - + implClass.getName()); - } - for (int i = 0; i < interfaces.length; i++) { - Class<?> intf = interfaces[i]; - addService(services, intf); - } - return; - } else if (!Void.class.equals(value)) { - addService(services, value); - return; - } - } - - // no @Service annotation, add all implemented interfaces with an @Remotable annotation - Class[] interfaces = implClass.getInterfaces(); - for (int i = 0; i < interfaces.length; i++) { - Class<?> intf = interfaces[i]; - if (intf.isAnnotationPresent(Remotable.class)) { - addService(services, intf); - } - } - - // if no Remotable interfaces were specified, the class itself is the service - if (services.isEmpty()) { - addService(services, implClass); - } - } - - /** - * Recursively adds supported services to a component type by walking the class hierarchy - * - * @throws ConfigurationException - */ - protected void addService(List<Service> services, Class<?> serviceClass) throws ConfigurationException { - JavaServiceContract javaInterface = factory.createJavaServiceContract(); - javaInterface.setInterface(serviceClass); - Callback callback = serviceClass.getAnnotation(Callback.class); - if (callback != null && !Void.class.equals(callback.value())) { - javaInterface.setCallbackInterface(callback.value()); - } - - String name = JavaIntrospectionHelper.getBaseName(serviceClass); - Service service = factory.createService(); - service.setName(name); - service.setServiceContract(javaInterface); - services.add(service); - } - - /** - * Root method for determining public field and method metadata - * - * @throws ConfigurationException - */ - protected void introspectAnnotatedMembers(ComponentType compType, Class<?> implClass) throws ConfigurationException { - - introspectPublicFields(compType, implClass); - introspectPrivateFields(compType, implClass); - - introspectPublicMethods(compType, implClass); - introspectPrivateMethods(compType, implClass); - } - - /** - * Introspects metdata for all public fields and methods for a class hierarchy - * - * @throws ConfigurationException - */ - protected void introspectMembers(ComponentType compType, Class<?> implClass) throws ConfigurationException { - List<Property> properties = compType.getProperties(); - List<Reference> references = compType.getReferences(); - - // inspect public fields from class and all superclasses - Field[] fields = implClass.getFields(); - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; - if (field.getType().isAnnotationPresent(Remotable.class)) { - addReference(references, field); - } else { - addProperty(properties, field); - } - } - - // add public methods from class and all superclasses - Method[] methods = implClass.getMethods(); - for (int i = 0; i < methods.length; i++) { - Method method = methods[i]; - if (Void.class.equals(method.getReturnType()) && method.getName().startsWith("set") - && method.getParameterTypes().length == 1 - && !method.getParameterTypes()[0].isAnnotationPresent(Remotable.class)) { - String name = method.getName(); - name = Character.toLowerCase(name.charAt(3)) + name.substring(4); - Class<?> type = method.getParameterTypes()[0]; - if (type.isAnnotationPresent(Remotable.class)) { - addReference(references, name, type, false); - } else { - addProperty(properties, name, type, false); - } - } - } - } - - private void introspectPublicFields(ComponentType compType, Class<?> implClass) throws ConfigurationException { - List<Property> properties = compType.getProperties(); - List<Reference> references = compType.getReferences(); - - // inspect public fields from class and all superclasses - Field[] fields = implClass.getFields(); - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; - if (field.isAnnotationPresent(org.osoa.sca.annotations.Property.class)) { - addProperty(properties, field); - } else if (field.isAnnotationPresent(org.osoa.sca.annotations.Reference.class)) { - addReference(references, field); - } - } - } - - private void introspectPrivateFields(ComponentType compType, Class<?> implClass) throws ConfigurationException { - List<Property> properties = compType.getProperties(); - List<Reference> references = compType.getReferences(); - - // inspect private fields declared in class - Field[] fields = implClass.getDeclaredFields(); - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; - if (!Modifier.isPrivate(field.getModifiers())) { - continue; - } - if (field.isAnnotationPresent(org.osoa.sca.annotations.Property.class)) { - addProperty(properties, field); - } else if (field.isAnnotationPresent(org.osoa.sca.annotations.Reference.class)) { - addReference(references, field); - } - } - } - - private void introspectPublicMethods(ComponentType compType, Class<?> implClass) throws ConfigurationException { - List<Property> properties = compType.getProperties(); - List<Reference> references = compType.getReferences(); - - // add public methods from class and all superclasses - Method[] methods = implClass.getMethods(); - for (int i = 0; i < methods.length; i++) { - Method method = methods[i]; - if (method.isAnnotationPresent(org.osoa.sca.annotations.Property.class)) { - addProperty(properties, method); - } else if (method.isAnnotationPresent(org.osoa.sca.annotations.Reference.class)) { - addReference(references, method); - } - } - } - - private void introspectPrivateMethods(ComponentType compType, Class<?> implClass) throws ConfigurationException { - List<Property> properties = compType.getProperties(); - List<Reference> references = compType.getReferences(); - - // add private methods declared on class - Method[] methods = implClass.getDeclaredMethods(); - for (int i = 0; i < methods.length; i++) { - Method method = methods[i]; - if (!Modifier.isPrivate(method.getModifiers())) { - continue; - } - if (method.isAnnotationPresent(org.osoa.sca.annotations.Property.class)) { - addProperty(properties, method); - } else if (method.isAnnotationPresent(org.osoa.sca.annotations.Reference.class)) { - addReference(references, method); - } - } - } - - protected void addProperty(List<Property> properties, Field field) throws ConfigurationException { - String name; - boolean required; - org.osoa.sca.annotations.Property annotation = field.getAnnotation(org.osoa.sca.annotations.Property.class); - if (annotation != null) { - name = annotation.name(); - if (name.length() == 0) { - name = field.getName(); - } - required = annotation.required(); - } else { - name = field.getName(); - required = false; - } - addProperty(properties, name, field.getType(), required); - } - - protected void addProperty(List<Property> properties, Method method) throws ConfigurationException { - if (!Void.class.equals(method.getReturnType())) { - throw new ConfigurationException("Property setter method does not return void: " + method.toString()); - } - Class<?>[] params = method.getParameterTypes(); - if (params.length != 1) { - throw new ConfigurationException("Property setter method does not have 1 parameter: " + method.toString()); - } - - String name; - boolean required; - org.osoa.sca.annotations.Property annotation = method.getAnnotation(org.osoa.sca.annotations.Property.class); - if (annotation != null) { - name = annotation.name(); - required = annotation.required(); - } else { - name = ""; - required = false; - } - if (name.length() == 0) { - name = method.getName(); - if (name.length() > 3 && name.startsWith("set")) { - name = Character.toLowerCase(name.charAt(3)) + name.substring(4); - } - } - addProperty(properties, name, params[0], required); - } - - protected void addProperty(List<Property> properties, String name, Class<?> type, boolean required) - throws ConfigurationException { - Property prop = factory.createProperty(); - prop.setName(name); - prop.setType(type); - prop.setRequired(required); - - // a java.util.Map is not a "many" - prop.setMany(type.isArray() || Collection.class.isAssignableFrom(type)); - - // todo how is the default specified using annotations? - prop.setDefaultValue(null); - - properties.add(prop); - } - - protected void addReference(List<Reference> references, Field field) throws ConfigurationException { - String name; - boolean required; - org.osoa.sca.annotations.Reference annotation = field.getAnnotation(org.osoa.sca.annotations.Reference.class); - if (annotation != null) { - name = annotation.name(); - if (name.length() == 0) { - name = field.getName(); - } - required = annotation.required(); - } else { - name = field.getName(); - required = false; - } - addReference(references, name, field.getType(), required); - } - - protected void addReference(List<Reference> references, Method method) throws ConfigurationException { - if (!Void.TYPE.equals(method.getReturnType())) { - throw new ConfigurationException("Reference setter method does not return void: " + method.toString()); - } - Class<?>[] params = method.getParameterTypes(); - if (params.length != 1) { - throw new ConfigurationException("Reference setter method does not have 1 parameter: " + method.toString()); - } - - String name; - boolean required; - org.osoa.sca.annotations.Reference annotation = method.getAnnotation(org.osoa.sca.annotations.Reference.class); - if (annotation != null) { - name = annotation.name(); - required = annotation.required(); - } else { - name = ""; - required = false; - } - if (name.length() == 0) { - name = method.getName(); - if (name.length() > 3 && name.startsWith("set")) { - name = Character.toLowerCase(name.charAt(3)) + name.substring(4); - } - } - addReference(references, name, params[0], required); - } - - protected void addReference(List<Reference> references, String name, Class<?> type, boolean required) - throws ConfigurationException { - Reference ref = factory.createReference(); - ref.setName(name); - boolean many = type.isArray() || Collection.class.isAssignableFrom(type); - Multiplicity multiplicity; - if (required) - multiplicity = many ? Multiplicity.ONE_N : Multiplicity.ONE_ONE; - else - multiplicity = many ? Multiplicity.ZERO_N : Multiplicity.ZERO_ONE; - ref.setMultiplicity(multiplicity); - ServiceContract javaInterface = factory.createJavaServiceContract(); - javaInterface.setInterface(type); - ref.setServiceContract(javaInterface); - references.add(ref); - } -} diff --git a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/impl/ModuleComponentConfigurationLoaderImpl.java b/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/impl/ModuleComponentConfigurationLoaderImpl.java deleted file mode 100644 index bed4b0ac40..0000000000 --- a/tags/java-stable-20060304/sca/core/src/main/java/org/apache/tuscany/core/config/impl/ModuleComponentConfigurationLoaderImpl.java +++ /dev/null @@ -1,142 +0,0 @@ -/** - * - * Copyright 2005 The Apache Software Foundation or its licensors, as applicable. - * - * Licensed 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.core.config.impl; - -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.apache.tuscany.common.resource.ResourceLoader; -import org.apache.tuscany.core.config.ConfigurationLoadException; -import org.apache.tuscany.core.config.ModuleComponentConfigurationLoader; -import org.apache.tuscany.model.assembly.AssemblyFactory; -import org.apache.tuscany.model.assembly.AssemblyModelContext; -import org.apache.tuscany.model.assembly.Module; -import org.apache.tuscany.model.assembly.ModuleComponent; -import org.apache.tuscany.model.assembly.ModuleFragment; -import org.apache.tuscany.model.assembly.loader.AssemblyModelLoader; - -/** - */ -public class ModuleComponentConfigurationLoaderImpl implements ModuleComponentConfigurationLoader { - - private static final String SCA_MODULE_FILE_NAME = "sca.module"; - //FIXME can fragments have a variable prefix name? - private static final String SCA_FRAGMENT_FILE_NAME = "sca.fragment"; - private static final String SYSTEM_MODULE_FILE_NAME = "system.module"; - //FIXME can fragments have a variable prefix name? - private static final String SYSTEM_FRAGMENT_FILE_NAME = "system.fragment"; - - private AssemblyModelContext modelContext; - private ResourceLoader resourceLoader; - private AssemblyFactory assemblyFactory; - private AssemblyModelLoader modelLoader; - - /** - * Constructor - */ - public ModuleComponentConfigurationLoaderImpl(AssemblyModelContext modelContext) { - this.modelContext=modelContext; - this.modelLoader=this.modelContext.getAssemblyLoader(); - this.assemblyFactory=this.modelContext.getAssemblyFactory(); - this.resourceLoader=this.modelContext.getApplicationResourceLoader(); - } - - /** - * @see org.apache.tuscany.model.assembly.loader.AssemblyModelLoader#loadModuleComponent(java.lang.String, java.lang.String) - */ - public ModuleComponent loadSystemModuleComponent(String name, String uri) throws ConfigurationLoadException { - return loadModuleComponent(SYSTEM_MODULE_FILE_NAME, SYSTEM_FRAGMENT_FILE_NAME, name, uri); - } - - /** - * @see org.apache.tuscany.model.assembly.loader.AssemblyModelLoader#loadModuleComponent(java.lang.String, java.lang.String) - */ - public ModuleComponent loadModuleComponent(String name, String uri) throws ConfigurationLoadException { - return loadModuleComponent(SCA_MODULE_FILE_NAME, SCA_FRAGMENT_FILE_NAME, name, uri); - } - - /** - * Load a module component. - */ - private ModuleComponent loadModuleComponent(String moduleFileName, String fragmentFileName, String name, String uri) throws ConfigurationLoadException { - - // Load the sca.module file - URL moduleUrl; - try { - moduleUrl = resourceLoader.getResource(moduleFileName); - } catch (IOException e) { - throw new ConfigurationLoadException(moduleFileName, e); - } - if (moduleUrl == null) { - throw new ConfigurationLoadException(moduleFileName); - } - String moduleUri=moduleUrl.toString(); - - // Load the sca.fragment files - Iterator<URL> i; - try { - i = resourceLoader.getAllResources(fragmentFileName); - } catch (IOException e) { - throw new ConfigurationLoadException(fragmentFileName, e); - } - List<String> moduleFragmentUris=new ArrayList<String>(); - for (; i.hasNext(); ) { - URL url=i.next(); - moduleFragmentUris.add(url.toString()); - } - - return loadModuleComponent(name, uri, moduleUri, moduleFragmentUris); - } - - /** - * @see org.apache.tuscany.core.config.ModuleComponentConfigurationLoader#loadModuleComponent(java.lang.String, java.lang.String, java.lang.String) - */ - public ModuleComponent loadModuleComponent(String name, String uri, String url) throws ConfigurationLoadException { - return loadModuleComponent( name, uri, url, (Collection)null); - } - - /** - * @see org.apache.tuscany.core.config.ModuleComponentConfigurationLoader#loadModuleComponent(java.lang.String, java.lang.String, java.lang.String, java.util.Collection) - */ - public ModuleComponent loadModuleComponent(String name, String uri, String moduleUri, Collection<String> moduleFragmentUris) throws ConfigurationLoadException { - - // Load the module file - Module module=modelLoader.loadModule(moduleUri); - - // Load the sca.fragment files - if (moduleFragmentUris!=null) { - for (String moduleFragmentUri : moduleFragmentUris) { - ModuleFragment moduleFragment=modelLoader.loadModuleFragment(moduleFragmentUri); - module.getModuleFragments().add(moduleFragment); - } - } - - // Create the module component - ModuleComponent moduleComponent=assemblyFactory.createModuleComponent(); - moduleComponent.setName(name); - moduleComponent.setURI(uri); - moduleComponent.setComponentImplementation(module); - moduleComponent.initialize(modelContext); - - return moduleComponent; - } - -} |