/* * 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)); } } 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)); } } 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; } }