From e5b7380c874745c989d1816b8f552504f038e1bc Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 26 Sep 2013 20:33:20 +0000 Subject: 2.0 branch for possible maintenance release git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1526672 13f79535-47bb-0310-9956-ffa450edef68 --- .../invocation/JavaInstanceFactoryProvider.java | 204 +++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 sca-java-2.x/branches/2.0/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java (limited to 'sca-java-2.x/branches/2.0/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java') diff --git a/sca-java-2.x/branches/2.0/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java b/sca-java-2.x/branches/2.0/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java new file mode 100644 index 0000000000..e89b618cfe --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java @@ -0,0 +1,204 @@ +/* + * 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.implementation.java.invocation; + +import java.lang.annotation.ElementType; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.context.InstanceFactory; +import org.apache.tuscany.sca.implementation.java.context.InstanceFactoryProvider; +import org.apache.tuscany.sca.implementation.java.context.ReflectiveInstanceFactory; +import org.apache.tuscany.sca.implementation.java.injection.ArrayMultiplicityObjectFactory; +import org.apache.tuscany.sca.implementation.java.injection.FieldInjector; +import org.apache.tuscany.sca.implementation.java.injection.Injector; +import org.apache.tuscany.sca.implementation.java.injection.InvalidAccessorException; +import org.apache.tuscany.sca.implementation.java.injection.ListMultiplicityObjectFactory; +import org.apache.tuscany.sca.implementation.java.injection.MethodInjector; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; + +/** + * Encapsulates configuration for a Java-based atomic component + * + * @version $Rev$ $Date$ + */ +public class JavaInstanceFactoryProvider implements InstanceFactoryProvider { + private JavaImplementation definition; + private ProxyFactory proxyService; + + private final List injectionSites; + private final EventInvoker initInvoker; + private final EventInvoker destroyInvoker; + private final Map factories = new HashMap(); + private final List callbackInjectionSites; + + public JavaInstanceFactoryProvider(JavaImplementation definition) { + this.definition = definition; + this.initInvoker = definition.getInitMethod() == null ? null : new MethodEventInvoker(definition + .getInitMethod()); + this.destroyInvoker = definition.getDestroyMethod() == null ? null : new MethodEventInvoker(definition + .getDestroyMethod()); + injectionSites = new ArrayList(); + callbackInjectionSites = new ArrayList(); + } + + ProxyFactory getProxyFactory() { + return proxyService; + } + + void setProxyFactory(ProxyFactory proxyService) { + this.proxyService = proxyService; + } + + /** + * @return the definition + */ + JavaImplementation getImplementation() { + return definition; + } + + @SuppressWarnings("unchecked") + public InstanceFactory createFactory() { + ObjectFactory[] initArgs = getConstructorArgs(); + Injector[] injectors = getInjectors(false); + Injector[] callbackInjectors = getInjectors(true); + return new ReflectiveInstanceFactory((Constructor)definition.getConstructor().getConstructor(), + initArgs, injectors, callbackInjectors, initInvoker, destroyInvoker); + } + + + + private ObjectFactory[] getConstructorArgs() { + JavaConstructorImpl constructor = definition.getConstructor(); + ObjectFactory[] initArgs = new ObjectFactory[constructor.getParameters().length]; + for (int i = 0; i < initArgs.length; i++) { + ObjectFactory factory = (ObjectFactory)factories.get(constructor.getParameters()[i]); + assert factory != null; + initArgs[i] = factory; + } + return initArgs; + } + + + + @SuppressWarnings("unchecked") + private Injector[] getInjectors(boolean callback) { + List sites = null; + if ( callback ) + sites = callbackInjectionSites; + else + sites = injectionSites; + + // work around JDK1.5 issue with allocating generic arrays + Injector[] injectors = new Injector[sites.size()]; + + int i = 0; + for (JavaElementImpl element : sites) { + Object obj = factories.get(element); + if (obj != null) { + if (obj instanceof ObjectFactory) { + ObjectFactory factory = (ObjectFactory)obj; + Member member = (Member)element.getAnchor(); + if (element.getElementType() == ElementType.FIELD) { + injectors[i++] = new FieldInjector((Field)member, factory); + } else if (element.getElementType() == ElementType.PARAMETER && member instanceof Method) { + injectors[i++] = new MethodInjector((Method)member, factory); + } else if (member instanceof Constructor) { + // Ignore + } else { + throw new AssertionError(String.valueOf(element)); + } + } else { + injectors[i++] = createMultiplicityInjector(element, (List>)obj); + } + } + } + return injectors; + } + + private Injector createMultiplicityInjector(JavaElementImpl element, List> factories) { + Class interfaceType = JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); + + if (element.getAnchor() instanceof Field) { + Field field = (Field)element.getAnchor(); + if (field.getType().isArray()) { + return new FieldInjector(field, new ArrayMultiplicityObjectFactory(interfaceType, factories)); + } else { + return new FieldInjector(field, new ListMultiplicityObjectFactory(factories, field.getType())); + } + } else if (element.getAnchor() instanceof Method) { + Method method = (Method)element.getAnchor(); + if (method.getParameterTypes()[0].isArray()) { + return new MethodInjector(method, new ArrayMultiplicityObjectFactory(interfaceType, factories)); + } else { + return new MethodInjector(method, new ListMultiplicityObjectFactory(factories, method.getParameterTypes()[0])); + } + } else { + throw new InvalidAccessorException("Member must be a field or method: " + element.getName()); + } + } + + @SuppressWarnings("unchecked") + public Class getImplementationClass() { + return (Class)definition.getJavaClass(); + } + + public void setObjectFactory(JavaElementImpl element, ObjectFactory objectFactory) { + factories.put(element, objectFactory); + } + + public void setObjectFactories(JavaElementImpl element, List> objectFactory) { + factories.put(element, objectFactory); + } + + /** + * @return the injectionSites + */ + List getInjectionSites() { + return injectionSites; + } + + /** + * @return the callbackInjectionSites + */ + public List getCallbackInjectionSites() { + return callbackInjectionSites; + } + + /** + * @return the factories + */ + Map getFactories() { + return factories; + } + + + +} -- cgit v1.2.3