diff options
Diffstat (limited to 'sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation')
80 files changed, 7018 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/IntrospectionRegistryImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/IntrospectionRegistryImpl.java new file mode 100644 index 0000000000..3dd1328b6a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/IntrospectionRegistryImpl.java @@ -0,0 +1,135 @@ +/* + * 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.core.implementation; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessor; +import org.apache.tuscany.spi.implementation.java.IntrospectionRegistry; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +import org.apache.tuscany.core.util.JavaIntrospectionHelper; + +/** + * Default implementation of the <code>IntrospectionRegistry</code> + * + * @version $Rev$ $Date$ + */ +public class IntrospectionRegistryImpl implements IntrospectionRegistry { + + private Monitor monitor; + private List<ImplementationProcessor> cache = new ArrayList<ImplementationProcessor>(); + + public IntrospectionRegistryImpl() { + } + + public IntrospectionRegistryImpl(Monitor monitor) { + this.monitor = monitor; + } + + @org.apache.tuscany.api.annotation.Monitor + public void setMonitor(Monitor monitor) { + this.monitor = monitor; + } + + public void registerProcessor(ImplementationProcessor processor) { + monitor.register(processor); + cache.add(processor); + } + + public void unregisterProcessor(ImplementationProcessor processor) { + monitor.unregister(processor); + cache.remove(processor); + } + + public PojoComponentType introspect(CompositeComponent parent, Class<?> clazz, + PojoComponentType<JavaMappedService, JavaMappedReference, + JavaMappedProperty<?>> type, + DeploymentContext context) + throws ProcessingException { + for (ImplementationProcessor processor : cache) { + processor.visitClass(parent, clazz, type, context); + } + + for (Constructor<?> constructor : clazz.getConstructors()) { + for (ImplementationProcessor processor : cache) { + processor.visitConstructor(parent, constructor, type, context); + } + } + + Set<Method> methods = JavaIntrospectionHelper.getAllUniquePublicProtectedMethods(clazz); + for (Method method : methods) { + for (ImplementationProcessor processor : cache) { + processor.visitMethod(parent, method, type, context); + } + } + + Set<Field> fields = JavaIntrospectionHelper.getAllPublicAndProtectedFields(clazz); + for (Field field : fields) { + for (ImplementationProcessor processor : cache) { + processor.visitField(parent, field, type, context); + } + } + + Class superClass = clazz.getSuperclass(); + if (superClass != null) { + visitSuperClass(parent, superClass, type, context); + } + + for (ImplementationProcessor processor : cache) { + processor.visitEnd(parent, clazz, type, context); + } + return type; + } + + private void visitSuperClass(CompositeComponent parent, + Class<?> clazz, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) + throws ProcessingException { + if (!Object.class.equals(clazz)) { + for (ImplementationProcessor processor : cache) { + processor.visitSuperClass(parent, clazz, type, context); + } + clazz = clazz.getSuperclass(); + if (clazz != null) { + visitSuperClass(parent, clazz, type, context); + } + } + } + + public static interface Monitor { + void register(ImplementationProcessor processor); + + void unregister(ImplementationProcessor processor); + + void processing(ImplementationProcessor processor); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/PojoAtomicComponent.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/PojoAtomicComponent.java new file mode 100644 index 0000000000..cfa5284efe --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/PojoAtomicComponent.java @@ -0,0 +1,222 @@ +/* + * 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.core.implementation; + +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.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.extension.AtomicComponentExtension; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.RuntimeWire; + +import org.apache.tuscany.core.injection.ArrayMultiplicityObjectFactory; +import org.apache.tuscany.core.injection.EventInvoker; +import org.apache.tuscany.core.injection.FieldInjector; +import org.apache.tuscany.core.injection.Injector; +import org.apache.tuscany.core.injection.InvalidAccessorException; +import org.apache.tuscany.core.injection.ListMultiplicityObjectFactory; +import org.apache.tuscany.core.injection.MethodInjector; +import org.apache.tuscany.core.injection.NoAccessorException; +import org.apache.tuscany.core.injection.ObjectCallbackException; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * Base implementation of an {@link org.apache.tuscany.spi.component.AtomicComponent} whose type is a Java class + * + * @version $$Rev$$ $$Date$$ + */ +public abstract class PojoAtomicComponent extends AtomicComponentExtension { + + protected EventInvoker<Object> initInvoker; + protected EventInvoker<Object> destroyInvoker; + protected PojoObjectFactory<?> instanceFactory; + protected List<String> constructorParamNames; + protected List<Class<?>> serviceInterfaces; + protected Map<String, Member> referenceSites; + protected Map<String, Member> propertySites; + protected Map<String, Member> callbackSites; + protected List<Injector<Object>> injectors; + + public PojoAtomicComponent(String name, PojoConfiguration configuration) { + super(name, + configuration.getParent(), + configuration.getScopeContainer(), + configuration.getWireService(), + configuration.getWorkContext(), + configuration.getScheduler(), + configuration.getInitLevel()); + assert configuration.getInstanceFactory() != null : "Object factory was null"; + initInvoker = configuration.getInitInvoker(); + destroyInvoker = configuration.getDestroyInvoker(); + instanceFactory = configuration.getInstanceFactory(); + constructorParamNames = configuration.getConstructorParamNames(); + serviceInterfaces = configuration.getServiceInterfaces(); + injectors = new ArrayList<Injector<Object>>(); + referenceSites = configuration.getReferenceSite() != null ? configuration.getReferenceSite() + : new HashMap<String, Member>(); + propertySites = configuration.getPropertySites() != null ? configuration.getPropertySites() + : new HashMap<String, Member>(); + callbackSites = configuration.getCallbackSite() != null ? configuration.getCallbackSite() + : new HashMap<String, Member>(); + } + + public List<Class<?>> getServiceInterfaces() { + return serviceInterfaces; + } + + public void init(Object instance) throws TargetException { + if (initInvoker != null) { + try { + initInvoker.invokeEvent(instance); + } catch (ObjectCallbackException e) { + TargetException t = new TargetException("Error initializing component instance", e); + t.setIdentifier(getName()); + throw t; + } + } + } + + public void destroy(Object instance) throws TargetException { + if (destroyInvoker != null) { + try { + destroyInvoker.invokeEvent(instance); + } catch (ObjectCallbackException e) { + TargetException t = new TargetException("Error destroying component instance", e); + t.setIdentifier(getName()); + throw t; + } + } + } + + public Object getTargetInstance() throws TargetException { + return scopeContainer.getInstance(this); + } + + public Object createInstance() throws ObjectCreationException { + Object instance = instanceFactory.getInstance(); + // inject the instance with properties and references + for (Injector<Object> injector : injectors) { + injector.inject(instance); + } + return instance; + } + + public void addPropertyFactory(String name, ObjectFactory<?> factory) { + Member member = propertySites.get(name); + if (member instanceof Field) { + injectors.add(new FieldInjector<Object>((Field) member, factory)); + } else if (member instanceof Method) { + injectors.add(new MethodInjector<Object>((Method) member, factory)); + } + // cycle through constructor param names as well + for (int i = 0; i < constructorParamNames.size(); i++) { + if (name.equals(constructorParamNames.get(i))) { + ObjectFactory[] initializerFactories = instanceFactory.getInitializerFactories(); + initializerFactories[i] = factory; + break; + } + } + //FIXME throw an error if no injection site found + } + + protected void onReferenceWire(OutboundWire wire) { + String name = wire.getReferenceName(); + Member member = referenceSites.get(name); + if (member != null) { + injectors.add(createInjector(member, wire)); + } + // cycle through constructor param names as well + for (int i = 0; i < constructorParamNames.size(); i++) { + if (name.equals(constructorParamNames.get(i))) { + ObjectFactory[] initializerFactories = instanceFactory.getInitializerFactories(); + initializerFactories[i] = createWireFactory(wire); + break; + } + } + //TODO error if ref not set on constructor or ref site + } + + public void onReferenceWires(Class<?> multiplicityClass, List<OutboundWire> wires) { + assert wires.size() > 0 : "Wires were empty"; + String referenceName = wires.get(0).getReferenceName(); + Member member = referenceSites.get(referenceName); + if (member == null) { + if (constructorParamNames.contains(referenceName)) { + // injected on the constructor + + } else { + throw new NoAccessorException(referenceName); + } + } + injectors.add(createMultiplicityInjector(member, multiplicityClass, wires)); + //TODO multiplicity for constructor injection + } + + protected Injector<Object> createInjector(Member member, RuntimeWire wire) { + ObjectFactory<?> factory = createWireFactory(wire); + if (member instanceof Field) { + return new FieldInjector<Object>((Field) member, factory); + } else if (member instanceof Method) { + return new MethodInjector<Object>((Method) member, factory); + } else { + InvalidAccessorException e = new InvalidAccessorException("Member must be a field or method"); + e.setIdentifier(member.getName()); + throw e; + } + } + + protected Injector<Object> createMultiplicityInjector(Member member, + Class<?> interfaceType, + List<OutboundWire> wireFactories) { + List<ObjectFactory<?>> factories = new ArrayList<ObjectFactory<?>>(); + for (OutboundWire wire : wireFactories) { + factories.add(createWireFactory(wire)); + } + if (member instanceof Field) { + Field field = (Field) member; + if (field.getType().isArray()) { + return new FieldInjector<Object>(field, new ArrayMultiplicityObjectFactory(interfaceType, factories)); + } else { + return new FieldInjector<Object>(field, new ListMultiplicityObjectFactory(factories)); + } + } else if (member instanceof Method) { + Method method = (Method) member; + if (method.getParameterTypes()[0].isArray()) { + return new MethodInjector<Object>(method, new ArrayMultiplicityObjectFactory(interfaceType, factories)); + } else { + return new MethodInjector<Object>(method, new ListMultiplicityObjectFactory(factories)); + } + } else { + InvalidAccessorException e = new InvalidAccessorException("Member must be a field or method"); + e.setIdentifier(member.getName()); + throw e; + } + } + + protected abstract ObjectFactory<?> createWireFactory(RuntimeWire wire); + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/PojoConfiguration.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/PojoConfiguration.java new file mode 100644 index 0000000000..6a8c80fb05 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/PojoConfiguration.java @@ -0,0 +1,184 @@ +/* + * 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.core.implementation; + +import java.lang.reflect.Member; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.WireService; + +import org.apache.tuscany.core.injection.EventInvoker; +import org.apache.tuscany.core.injection.Injector; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * Encapsulates confuration for a Java-based atomic component + * + * @version $Rev$ $Date$ + */ +public class PojoConfiguration { + + private CompositeComponent parent; + private ScopeContainer scopeContainer; + private PojoObjectFactory<?> instanceFactory; + private List<String> constructorParamNames = new ArrayList<String>(); + private int initLevel; + private EventInvoker<Object> initInvoker; + private EventInvoker<Object> destroyInvoker; + private List<Injector> propertyInjectors = new ArrayList<Injector>(); + private Map<String, Member> referenceSites = new HashMap<String, Member>(); + private Map<String, Member> propertySites = new HashMap<String, Member>(); + private Map<String, Member> callbackSites = new HashMap<String, Member>(); + private List<Class<?>> serviceInterfaces = new ArrayList<Class<?>>(); + private WireService wireService; + private WorkContext workContext; + private WorkScheduler scheduler; + + public CompositeComponent getParent() { + return parent; + } + + public void setParent(CompositeComponent parent) { + this.parent = parent; + } + + public ScopeContainer getScopeContainer() { + return scopeContainer; + } + + public void setScopeContainer(ScopeContainer scopeContainer) { + this.scopeContainer = scopeContainer; + } + + public List<Class<?>> getServiceInterfaces() { + return serviceInterfaces; + } + + public void addServiceInterface(Class<?> serviceInterface) { + serviceInterfaces.add(serviceInterface); + } + + public PojoObjectFactory<?> getInstanceFactory() { + return instanceFactory; + } + + public void setInstanceFactory(PojoObjectFactory<?> objectFactory) { + this.instanceFactory = objectFactory; + } + + public List<String> getConstructorParamNames() { + return constructorParamNames; + } + + public void setConstructorParamNames(List<String> names) { + constructorParamNames = names; + } + + public void addConstructorParamName(String name) { + constructorParamNames.add(name); + } + + public int getInitLevel() { + return initLevel; + } + + public void setInitLevel(int initLevel) { + this.initLevel = initLevel; + } + + public EventInvoker<Object> getInitInvoker() { + return initInvoker; + } + + public void setInitInvoker(EventInvoker<Object> initInvoker) { + this.initInvoker = initInvoker; + } + + public EventInvoker<Object> getDestroyInvoker() { + return destroyInvoker; + } + + public void setDestroyInvoker(EventInvoker<Object> destroyInvoker) { + this.destroyInvoker = destroyInvoker; + } + + public List<Injector> getPropertyInjectors() { + return propertyInjectors; + } + + public void addPropertyInjector(Injector injector) { + propertyInjectors.add(injector); + } + + public Map<String, Member> getReferenceSite() { + return referenceSites; + } + + public void addReferenceSite(String name, Member member) { + referenceSites.put(name, member); + } + + public Map<String, Member> getCallbackSite() { + return callbackSites; + } + + public void addCallbackSite(String name, Member member) { + callbackSites.put(name, member); + } + + public Map<String, Member> getPropertySites() { + return propertySites; + } + + public void addPropertySite(String name, Member member) { + propertySites.put(name, member); + } + + public WireService getWireService() { + return wireService; + } + + public void setWireService(WireService wireService) { + this.wireService = wireService; + } + + public WorkContext getWorkContext() { + return workContext; + } + + public void setWorkContext(WorkContext workContext) { + this.workContext = workContext; + } + + public WorkScheduler getScheduler() { + return scheduler; + } + + public void setScheduler(WorkScheduler scheduler) { + this.scheduler = scheduler; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractCompositeComponent.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractCompositeComponent.java new file mode 100644 index 0000000000..7949bf4a71 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractCompositeComponent.java @@ -0,0 +1,170 @@ +/* + * 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.core.implementation.composite; + +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.w3c.dom.Document; + +import org.apache.tuscany.spi.builder.Connector; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ObjectRegistrationException; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.extension.CompositeComponentExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; + +import org.apache.tuscany.core.component.ComponentInitException; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.implementation.system.component.SystemSingletonAtomicComponent; + +/** + * The base implementation of a composite context + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractCompositeComponent extends CompositeComponentExtension { + + public static final int DEFAULT_WAIT = 1000 * 60; + + // Blocking latch to ensure the module is initialized exactly once prior to servicing requests + protected CountDownLatch initializeLatch = new CountDownLatch(1); + + protected final Object lock = new Object(); + + // Indicates whether the module context has been initialized + protected boolean initialized; + + protected ScopeContainer scopeContainer; + + + /** + * @param name the name of the SCA composite + * @param parent the SCA composite parent + * @param connector + * @param propertyValues the values of this composite's Properties + */ + public AbstractCompositeComponent(String name, + CompositeComponent parent, + Connector connector, + Map<String, Document> propertyValues) { + super(name, parent, connector, propertyValues); + } + + public void setScopeContainer(ScopeContainer scopeContainer) { + assert this.scopeContainer == null; + this.scopeContainer = scopeContainer; + addListener(scopeContainer); + } + + public <S, I extends S> void registerJavaObject(String name, Class<S> service, I instance) + throws ObjectRegistrationException { + register(new SystemSingletonAtomicComponent<S, I>(name, this, service, instance)); + } + + public void start() { + synchronized (lock) { + if (lifecycleState != UNINITIALIZED && lifecycleState != STOPPED) { + throw new IllegalStateException("Composite not in UNINITIALIZED state"); + } + + if (scopeContainer != null) { + scopeContainer.start(); + } + for (SCAObject child : systemChildren.values()) { + child.start(); + } + for (SCAObject child : children.values()) { + child.start(); + } + initializeLatch.countDown(); + initialized = true; + lifecycleState = INITIALIZED; + } + publish(new CompositeStart(this, this)); + } + + public void stop() { + if (lifecycleState == STOPPED) { + return; + } + + publish(new CompositeStop(this, this)); + for (SCAObject child : children.values()) { + child.stop(); + } + for (SCAObject child : systemChildren.values()) { + child.stop(); + } + if (scopeContainer != null) { + scopeContainer.stop(); + } + + // need to block a start until reset is complete + initializeLatch = new CountDownLatch(2); + lifecycleState = STOPPING; + initialized = false; + // allow initialized to be called + initializeLatch.countDown(); + lifecycleState = STOPPED; + } + + public void publish(Event event) { + if (lifecycleState == STOPPED) { + return; + } + checkInit(); + super.publish(event); + } + + public TargetInvoker createTargetInvoker(String targetName, Operation operation) { + return null; + } + + public TargetInvoker createAsyncTargetInvoker(InboundWire wire, Operation operation) { + return null; + } + + + /** + * Blocks until the module context has been initialized + */ + protected void checkInit() { + if (!initialized) { + try { + /* block until the module has initialized */ + boolean success = initializeLatch.await(AbstractCompositeComponent.DEFAULT_WAIT, + TimeUnit.MILLISECONDS); + if (!success) { + throw new ComponentInitException("Timeout waiting for context to initialize"); + } + } catch (InterruptedException e) { // should not happen + } + } + + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractCompositeReferenceTargetInvoker.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractCompositeReferenceTargetInvoker.java new file mode 100644 index 0000000000..5ef666be76 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractCompositeReferenceTargetInvoker.java @@ -0,0 +1,74 @@ +/* + * 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.core.implementation.composite; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * Base class for dispatching to a Composite Reference. + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractCompositeReferenceTargetInvoker implements TargetInvoker { + + protected Operation operation; + protected boolean cacheable; + + public AbstractCompositeReferenceTargetInvoker(Operation operation) { + assert operation != null : "Operation method cannot be null"; + this.operation = operation; + } + + public boolean isCacheable() { + return cacheable; + } + + public void setCacheable(boolean cacheable) { + this.cacheable = cacheable; + } + + public boolean isOptimizable() { + return isCacheable(); // we only need to check if the scopes are correct + } + + public Object invokeTarget(final Object payload) throws InvocationTargetException { + Object[] args; + if (payload != null && !payload.getClass().isArray()) { + args = new Object[]{payload}; + } else { + args = (Object[]) payload; + } + try { + AbstractOperationOutboundInvocationHandler invocationHandler = getInvocationHandler(); + return invocationHandler.invoke(operation, args); + } catch (Throwable t) { + throw new InvocationTargetException(t); + } + } + + @Override + public AbstractCompositeReferenceTargetInvoker clone() throws CloneNotSupportedException { + return (AbstractCompositeReferenceTargetInvoker) super.clone(); + } + + protected abstract AbstractOperationOutboundInvocationHandler getInvocationHandler(); +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractOperationOutboundInvocationHandler.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractOperationOutboundInvocationHandler.java new file mode 100644 index 0000000000..2ccc3ec729 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/AbstractOperationOutboundInvocationHandler.java @@ -0,0 +1,32 @@ +/* + * 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.core.implementation.composite; + +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.AbstractOutboundInvocationHandler; + +/** + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractOperationOutboundInvocationHandler + extends AbstractOutboundInvocationHandler { + + public abstract Object invoke(Operation operation, Object[] args) throws Throwable; +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeBindlessBuilder.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeBindlessBuilder.java new file mode 100644 index 0000000000..c0466465cf --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeBindlessBuilder.java @@ -0,0 +1,83 @@ +/* + * 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.core.implementation.composite; + +import org.osoa.sca.annotations.Init; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.builder.BindlessBuilder; +import org.apache.tuscany.spi.builder.BuilderRegistry; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.Reference; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.model.BindlessServiceDefinition; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.wire.WireService; + +/** + * The default implementation f a {@link BindlessBuilder} for a composite + */ +public class CompositeBindlessBuilder implements BindlessBuilder { + + protected BuilderRegistry builderRegistry; + protected WireService wireService; + protected WorkContext workContext; + + @Autowire + public void setBuilderRegistry(BuilderRegistry registry) { + this.builderRegistry = registry; + } + + @Autowire + public void setWireService(WireService wireService) { + this.wireService = wireService; + } + + @Autowire + public void setWorkContext(WorkContext workContext) { + this.workContext = workContext; + } + + @Init(eager = true) + public void init() { + builderRegistry.register(this); + } + + public Service build(CompositeComponent parent, + BindlessServiceDefinition definition, + DeploymentContext deploymentContext) { + return new CompositeService(definition.getName(), + definition.getServiceContract().getInterfaceClass(), + parent, + wireService, + workContext); + } + + public Reference build(CompositeComponent parent, + ReferenceDefinition definition, + DeploymentContext deploymentContext) { + return new CompositeReference(definition.getName(), + parent, + wireService, + definition.getServiceContract(), + workContext); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeBuilder.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeBuilder.java new file mode 100644 index 0000000000..dccfc636c4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeBuilder.java @@ -0,0 +1,111 @@ +/* + * 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.core.implementation.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentBuilderExtension; +import org.apache.tuscany.spi.model.Binding; +import org.apache.tuscany.spi.model.BindlessServiceDefinition; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.CompositeComponentType; +import org.apache.tuscany.spi.model.CompositeImplementation; +import org.apache.tuscany.spi.model.Implementation; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.ServiceDefinition; + +/** + * Instantiates a composite component from an assembly definition + * + * @version $Rev$ $Date$ + */ +public class CompositeBuilder extends ComponentBuilderExtension<CompositeImplementation> { + + public Component build(CompositeComponent parent, + ComponentDefinition<CompositeImplementation> componentDefinition, + DeploymentContext deploymentContext) throws BuilderConfigException { + CompositeImplementation implementation = componentDefinition.getImplementation(); + CompositeComponentType<?, ?, ?> componentType = implementation.getComponentType(); + + // create lists of all components, services and references in this composite + List<ComponentDefinition<? extends Implementation<?>>> allComponents = + new ArrayList<ComponentDefinition<? extends Implementation<?>>>(); + allComponents.addAll(componentType.getComponents().values()); + + List<BoundServiceDefinition<? extends Binding>> allBoundServices = + new ArrayList<BoundServiceDefinition<? extends Binding>>(); + List<BindlessServiceDefinition> allBindlessServices = new ArrayList<BindlessServiceDefinition>(); + for (ServiceDefinition serviceDefinition : componentType.getServices().values()) { + if (serviceDefinition instanceof BoundServiceDefinition) { + BoundServiceDefinition<? extends Binding> boundService = + (BoundServiceDefinition<? extends Binding>) serviceDefinition; + allBoundServices.add(boundService); + } else if (serviceDefinition instanceof BindlessServiceDefinition) { + allBindlessServices.add((BindlessServiceDefinition) serviceDefinition); + } + } + + // FIXME is this right? + List<BoundReferenceDefinition<? extends Binding>> allBoundReferences = + new ArrayList<BoundReferenceDefinition<? extends Binding>>(); + List<ReferenceDefinition> allTargetlessReferences = new ArrayList<ReferenceDefinition>(); + + for (Object referenceTarget : componentType.getReferences().values()) { + if (referenceTarget instanceof BoundReferenceDefinition<?>) { + allBoundReferences.add((BoundReferenceDefinition<? extends Binding>) referenceTarget); + } else if (referenceTarget instanceof ReferenceDefinition) { + allTargetlessReferences.add((ReferenceDefinition) referenceTarget); + } + } + + String name = componentDefinition.getName(); + CompositeComponentImpl component = new CompositeComponentImpl(name, parent, connector, null); + for (BoundReferenceDefinition<? extends Binding> referenceDefinition : allBoundReferences) { + component.register(builderRegistry.build(component, referenceDefinition, deploymentContext)); + } + for (BindlessServiceDefinition bindlessServiceDef : allBindlessServices) { + component.register(builderRegistry.build(component, bindlessServiceDef, deploymentContext)); + } + for (ComponentDefinition<? extends Implementation<?>> child : allComponents) { + component.register(builderRegistry.build(component, child, deploymentContext)); + } + for (BoundServiceDefinition<? extends Binding> serviceDefinition : allBoundServices) { + component.register(builderRegistry.build(component, serviceDefinition, deploymentContext)); + } + for (ReferenceDefinition targetlessReferenceDef : allTargetlessReferences) { + component.register(builderRegistry.build(component, targetlessReferenceDef, deploymentContext)); + } + + // HACK: [rfeng] We need a better way to propagate model extensions to SCAObject. + component.getExtensions().putAll(componentType.getExtensions()); + + return component; + } + + protected Class<CompositeImplementation> getImplementationType() { + return CompositeImplementation.class; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImpl.java new file mode 100644 index 0000000000..d7833c8707 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeComponentImpl.java @@ -0,0 +1,65 @@ +/* + * 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.core.implementation.composite; + +import java.util.Map; + +import org.w3c.dom.Document; + +import org.apache.tuscany.spi.builder.Connector; +import org.apache.tuscany.spi.component.CompositeComponent; + +/** + * The standard implementation of a composite component. Autowiring is performed by delegating to the parent composite. + * + * @version $Rev$ $Date$ + */ +public class CompositeComponentImpl extends AbstractCompositeComponent { + private String uri; + + public CompositeComponentImpl(String name, + CompositeComponent parent, + Connector connector, + Map<String, Document> propertyValues) { + this(name, null, parent, connector, propertyValues); + } + + /** + * Constructor specifying name and URI. + * + * @param name the name of this Component + * @param uri the unique identifier for this component + * @param parent this component's parent + * @param connector + * @param propertyValues this composite's Property values + */ + public CompositeComponentImpl(String name, + String uri, + CompositeComponent parent, + Connector connector, + Map<String, Document> propertyValues) { + super(name, parent, connector, propertyValues); + this.uri = uri; + } + + public String getURI() { + return uri; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeComponentTypeLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeComponentTypeLoader.java new file mode 100644 index 0000000000..cac1b231c6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeComponentTypeLoader.java @@ -0,0 +1,67 @@ +/* + * 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.core.implementation.composite; + +import java.net.URL; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.CompositeClassLoader; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.CompositeComponentType; +import org.apache.tuscany.spi.model.CompositeImplementation; + +import org.apache.tuscany.core.deployer.ChildDeploymentContext; + +/** + * Loads a composite component type + * + * @version $Rev$ $Date$ + */ +public class CompositeComponentTypeLoader extends ComponentTypeLoaderExtension<CompositeImplementation> { + public CompositeComponentTypeLoader() { + } + + public CompositeComponentTypeLoader(LoaderRegistry loaderRegistry) { + super(loaderRegistry); + } + + protected Class<CompositeImplementation> getImplementationClass() { + return CompositeImplementation.class; + } + + public void load(CompositeComponent parent, CompositeImplementation implementation, + DeploymentContext deploymentContext) + throws LoaderException { + URL scdlLocation = implementation.getScdlLocation(); + ClassLoader cl = new CompositeClassLoader(implementation.getClassLoader()); + deploymentContext = new ChildDeploymentContext(deploymentContext, cl, scdlLocation); + CompositeComponentType componentType = loadFromSidefile(parent, scdlLocation, deploymentContext); + implementation.setComponentType(componentType); + } + + protected CompositeComponentType loadFromSidefile(CompositeComponent parent, + URL url, + DeploymentContext deploymentContext) + throws LoaderException { + return loaderRegistry.load(parent, url, CompositeComponentType.class, deploymentContext); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeLoader.java new file mode 100644 index 0000000000..312d06cbd2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeLoader.java @@ -0,0 +1,130 @@ +/* + * 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.core.implementation.composite; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.osoa.sca.Version.XML_NAMESPACE_1_0; + +import java.net.URL; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.core.property.PropertyHelper; +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.CompositeClassLoader; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.CompositeComponentType; +import org.apache.tuscany.spi.model.Implementation; +import org.apache.tuscany.spi.model.Include; +import org.apache.tuscany.spi.model.ModelObject; +import org.apache.tuscany.spi.model.Property; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.ServiceDefinition; +import org.apache.tuscany.spi.services.artifact.Artifact; +import org.apache.tuscany.spi.services.artifact.ArtifactRepository; + +/** + * Loads a composite component definition from an XML-based assembly file + * + * @version $Rev$ $Date$ + */ +public class CompositeLoader extends LoaderExtension<CompositeComponentType> { + public static final QName COMPOSITE = new QName(XML_NAMESPACE_1_0, "composite"); + + private final ArtifactRepository artifactRepository; + + public CompositeLoader(@Autowire LoaderRegistry registry, + @Autowire ArtifactRepository artifactRepository) { + super(registry); + this.artifactRepository = artifactRepository; + } + + public QName getXMLType() { + return COMPOSITE; + } + + public CompositeComponentType load(CompositeComponent parent, + XMLStreamReader reader, + DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + CompositeComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> composite = + new CompositeComponentType<ServiceDefinition, ReferenceDefinition, Property<?>>(); + composite.setName(reader.getAttributeValue(null, "name")); + boolean done = false; + while (!done) { + switch (reader.next()) { + case START_ELEMENT: + ModelObject o = registry.load(parent, reader, deploymentContext); + if (o instanceof ServiceDefinition) { + composite.add((ServiceDefinition) o); + } else if (o instanceof ReferenceDefinition) { + composite.add((ReferenceDefinition) o); + } else if (o instanceof Property<?>) { + composite.add((Property<?>) o); + } else if (o instanceof ComponentDefinition<?>) { + composite.add((ComponentDefinition<? extends Implementation<?>>) o); + } else if (o instanceof Include) { + composite.add((Include) o); + } else if (o instanceof Dependency) { + Artifact artifact = ((Dependency) o).getArtifact(); + if (artifactRepository != null) { + // default to jar type if not specified + if (artifact.getType() == null) { + artifact.setType("jar"); + } + artifactRepository.resolve(artifact); + } + if (artifact.getUrl() != null) { + ClassLoader classLoader = deploymentContext.getClassLoader(); + if (classLoader instanceof CompositeClassLoader) { + CompositeClassLoader ccl = (CompositeClassLoader)classLoader; + for (URL dep : artifact.getUrls()) { + ccl.addURL(dep); + } + } + } + } else { + // HACK: [rfeng] Add as an unknown model extension + if (o != null) { + composite.getExtensions().put(o.getClass(), o); + } + } + reader.next(); + break; + case END_ELEMENT: + if (COMPOSITE.equals(reader.getName())) { + done = true; + break; + } + } + } + for (ComponentDefinition<? extends Implementation<?>> c : composite.getComponents().values()) { + PropertyHelper.processProperties(composite, c, deploymentContext); + } + return composite; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReference.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReference.java new file mode 100644 index 0000000000..60f91ec073 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReference.java @@ -0,0 +1,52 @@ +/* + * 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.core.implementation.composite; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.extension.ReferenceExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireService; + +/** + * A bindless reference to a target service in the parent composite + */ +public class CompositeReference extends ReferenceExtension { + + private WorkContext workContext; + + public CompositeReference(String name, + CompositeComponent parent, + WireService wireService, + ServiceContract contract, + WorkContext workContext) { + super(name, contract.getInterfaceClass(), parent, wireService); + this.workContext = workContext; + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + return new CompositeReferenceTargetInvoker(operation, inboundWire, outboundWire, workContext); + } + + public TargetInvoker createCallbackTargetInvoker(ServiceContract contract, Operation operation) { + return new CompositeReferenceCallbackTargetInvoker(operation, inboundWire, workContext); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvoker.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvoker.java new file mode 100644 index 0000000000..5301e024dc --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceCallbackTargetInvoker.java @@ -0,0 +1,64 @@ +/* + * 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.core.implementation.composite; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; + +/** + * + */ +public class CompositeReferenceCallbackTargetInvoker extends AbstractCompositeReferenceTargetInvoker { + + private InboundWire inboundWire; + private WorkContext workContext; + + public CompositeReferenceCallbackTargetInvoker(Operation operation, InboundWire inboundWire, WorkContext context) { + super(operation); + this.inboundWire = inboundWire; + this.workContext = context; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + workContext.setCurrentMessageId(msg.getMessageId()); + workContext.setCurrentCorrelationId(msg.getCorrelationId()); + Object resp = invokeTarget(msg.getBody()); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBodyWithFault(e.getCause()); + } catch (Throwable e) { + msg.setBodyWithFault(e); + } + return msg; + } + + public CompositeReferenceCallbackTargetInvoker clone() throws CloneNotSupportedException { + return (CompositeReferenceCallbackTargetInvoker) super.clone(); + } + + protected AbstractOperationOutboundInvocationHandler getInvocationHandler() { + return new OperationCallbackInvocationHandler(workContext, inboundWire); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceTargetInvoker.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceTargetInvoker.java new file mode 100644 index 0000000000..42c9c7ed82 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeReferenceTargetInvoker.java @@ -0,0 +1,72 @@ +/* + * 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.core.implementation.composite; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; +import org.apache.tuscany.spi.wire.OutboundWire; + +/** + * + */ +public class CompositeReferenceTargetInvoker extends AbstractCompositeReferenceTargetInvoker { + + private InboundWire inboundWire; + private OutboundWire outboundWire; + private WorkContext workContext; + + + public CompositeReferenceTargetInvoker(Operation operation, + InboundWire inboundWire, + OutboundWire outboundWire, + WorkContext workContext) { + super(operation); + this.inboundWire = inboundWire; + this.outboundWire = outboundWire; + this.workContext = workContext; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + inboundWire.addMapping(msg.getMessageId(), msg.getFromAddress()); + workContext.setCurrentMessageId(msg.getMessageId()); + workContext.setCurrentCorrelationId(msg.getCorrelationId()); + Object resp = invokeTarget(msg.getBody()); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBodyWithFault(e.getCause()); + } catch (Throwable e) { + msg.setBodyWithFault(e); + } + return msg; + } + + public CompositeReferenceTargetInvoker clone() throws CloneNotSupportedException { + return (CompositeReferenceTargetInvoker) super.clone(); + } + + protected AbstractOperationOutboundInvocationHandler getInvocationHandler() { + return new OperationOutboundInvocationHandler(outboundWire, workContext); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeService.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeService.java new file mode 100644 index 0000000000..45b1e41d13 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/CompositeService.java @@ -0,0 +1,63 @@ +/* + * 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.core.implementation.composite; + +import org.apache.tuscany.spi.CoreRuntimeException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.extension.ServiceExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireService; + +public class CompositeService extends ServiceExtension { + + private WorkContext workContext; + + public CompositeService(String name, + Class<?> interfaze, + CompositeComponent parent, + WireService wireService, + WorkContext workContext) throws CoreRuntimeException { + super(name, interfaze, parent, wireService); + this.workContext = workContext; + } + + /** + * A service for a remote binding does not need a target invoker, but a bindless service does because it gets wired + * localy from a reference (or from a parent service?!) We just reuse CompositeReferenceTargetInvoker for now since + * it seems the target invoker we need does the same thing, if this is confirmed we should give it a common name + * FIXME !!! Notice that this method is not defined in the SPI !!! + */ + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + return new CompositeReferenceTargetInvoker(operation, inboundWire, outboundWire, workContext); + } + + /** + */ + public TargetInvoker createCallbackTargetInvoker(ServiceContract contract, Operation operation) { + return new CompositeReferenceCallbackTargetInvoker(operation, inboundWire, workContext); + } + + public Object getServiceInstance() throws TargetException { + return interfaze.cast(wireService.createProxy(outboundWire)); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/Dependency.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/Dependency.java new file mode 100644 index 0000000000..808f36072f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/Dependency.java @@ -0,0 +1,39 @@ +/* + * 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.core.implementation.composite; + +import org.apache.tuscany.spi.model.ModelObject; +import org.apache.tuscany.spi.services.artifact.Artifact; + +/** + * A model object that represents a dependency on an external artifact. + * + * @version $Rev$ $Date$ + */ +public class Dependency extends ModelObject { + private Artifact artifact; + + public Artifact getArtifact() { + return artifact; + } + + public void setArtifact(Artifact artifact) { + this.artifact = artifact; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/DependencyLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/DependencyLoader.java new file mode 100644 index 0000000000..643c59bad2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/DependencyLoader.java @@ -0,0 +1,81 @@ +/* + * 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.core.implementation.composite; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.services.artifact.Artifact; + +/** + * Loader for handling <dependency> elements. + * + * @version $Rev$ $Date$ + */ +public class DependencyLoader extends LoaderExtension<Dependency> { + private static final String NS = "http://incubator.apache.org/tuscany/xmlns/1.0-incubator-M2"; + private static final QName DEPENDENCY = new QName(NS, "dependency"); + private static final QName GROUP = new QName(NS, "group"); + private static final QName NAME = new QName(NS, "name"); + private static final QName VERSION = new QName(NS, "version"); + private static final QName CLASSIFIER = new QName(NS, "classifier"); + private static final QName TYPE = new QName(NS, "type"); + + public DependencyLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return DEPENDENCY; + } + + public Dependency load(CompositeComponent parent, + XMLStreamReader reader, + DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + + Artifact artifact = new Artifact(); + while (reader.nextTag() == XMLStreamConstants.START_ELEMENT) { + QName name = reader.getName(); + String text = reader.getElementText(); + if (GROUP.equals(name)) { + artifact.setGroup(text); + } else if (NAME.equals(name)) { + artifact.setName(text); + } else if (VERSION.equals(name)) { + artifact.setVersion(text); + } else if (CLASSIFIER.equals(name)) { + artifact.setClassifier(text); + } else if (TYPE.equals(name)) { + artifact.setType(text); + } + } + Dependency dependency = new Dependency(); + dependency.setArtifact(artifact); + return dependency; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/ImplementationCompositeLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/ImplementationCompositeLoader.java new file mode 100644 index 0000000000..40051d30ba --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/ImplementationCompositeLoader.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.core.implementation.composite; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.Set; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.Version; +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.CompositeClassLoader; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.InvalidValueException; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.loader.MissingResourceException; +import org.apache.tuscany.spi.model.CompositeImplementation; +import org.apache.tuscany.spi.services.artifact.Artifact; +import org.apache.tuscany.spi.services.artifact.ArtifactRepository; + +/** + * Loader that handles an <implementation.composite> element. + * + * @version $Rev$ $Date$ + */ +public class ImplementationCompositeLoader extends LoaderExtension<CompositeImplementation> { + private static final QName IMPLEMENTATION_COMPOSITE = + new QName(Version.XML_NAMESPACE_1_0, "implementation.composite"); + + private final ArtifactRepository artifactRepository; + + public ImplementationCompositeLoader(@Autowire LoaderRegistry registry, + @Autowire ArtifactRepository artifactRepository) { + super(registry); + this.artifactRepository = artifactRepository; + } + + public QName getXMLType() { + return IMPLEMENTATION_COMPOSITE; + } + + public CompositeImplementation load(CompositeComponent parent, + XMLStreamReader reader, + DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + + assert IMPLEMENTATION_COMPOSITE.equals(reader.getName()); + String name = reader.getAttributeValue(null, "name"); + String group = reader.getAttributeValue(null, "group"); + String version = reader.getAttributeValue(null, "version"); + String scdlLocation = reader.getAttributeValue(null, "scdlLocation"); + String jarLocation = reader.getAttributeValue(null, "jarLocation"); + LoaderUtil.skipToEndElement(reader); + + CompositeImplementation impl = new CompositeImplementation(); + impl.setName(name); + if (scdlLocation != null) { + try { + impl.setScdlLocation(new URL(deploymentContext.getScdlLocation(), scdlLocation)); + } catch (MalformedURLException e) { + InvalidValueException ive = new InvalidValueException(scdlLocation, e); + ive.setIdentifier(name); + throw ive; + } + impl.setClassLoader(deploymentContext.getClassLoader()); + } else if (jarLocation != null) { + URL jarUrl; + try { + jarUrl = new URL(deploymentContext.getScdlLocation(), jarLocation); + } catch (MalformedURLException e) { + InvalidValueException ive = new InvalidValueException(jarLocation, e); + ive.setIdentifier(name); + throw ive; + } + try { + impl.setScdlLocation(new URL("jar:" + jarUrl.toExternalForm() + "!/META-INF/sca/default.scdl")); + } catch (MalformedURLException e) { + throw new AssertionError("Could not convert URL to a jar: url"); + } + impl.setClassLoader(new CompositeClassLoader(new URL[]{jarUrl}, deploymentContext.getClassLoader())); + } else if (artifactRepository != null && group != null && version != null) { + Artifact artifact = new Artifact(); + artifact.setGroup(group); + artifact.setName(name); + artifact.setVersion(version); + artifact.setType("jar"); + artifactRepository.resolve(artifact); + if (artifact.getUrl() == null) { + MissingResourceException mre = new MissingResourceException(artifact.toString()); + mre.setIdentifier(name); + throw mre; + } + try { + impl.setScdlLocation(new URL("jar:" + artifact.getUrl() + "!/META-INF/sca/default.scdl")); + } catch (MalformedURLException e) { + throw new AssertionError(e); + } + Set<URL> artifactURLs = artifact.getUrls(); + URL[] urls = new URL[artifactURLs.size()]; + int i = 0; + for (URL artifactURL : artifactURLs) { + urls[i++] = artifactURL; + } + impl.setClassLoader(new CompositeClassLoader(urls, deploymentContext.getClassLoader())); + } + return impl; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/OperationCallbackInvocationHandler.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/OperationCallbackInvocationHandler.java new file mode 100644 index 0000000000..df8e13a58e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/OperationCallbackInvocationHandler.java @@ -0,0 +1,77 @@ +/* + * 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.core.implementation.composite; + +import java.util.Map; + +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * + */ +public class OperationCallbackInvocationHandler extends AbstractOperationOutboundInvocationHandler { + + private WorkContext context; + private InboundWire inboundWire; + private Object messageId; + private Object correlationId; + + public OperationCallbackInvocationHandler(WorkContext context, InboundWire inboundWire) { + this.context = context; + this.inboundWire = inboundWire; + } + + public Object invoke(Object proxy, Operation operation, Object[] args) throws Throwable { + messageId = context.getCurrentMessageId(); + context.setCurrentMessageId(null); + correlationId = context.getCurrentCorrelationId(); + context.setCurrentCorrelationId(null); + Object targetAddress = inboundWire.retrieveMapping(correlationId); + if (targetAddress == null) { + throw new AssertionError("No from address associated with message id [" + correlationId + "]"); + } + //TODO optimize as this is slow in local invocations + Map<Operation<?>, OutboundInvocationChain> sourceCallbackInvocationChains = + inboundWire.getSourceCallbackInvocationChains(targetAddress); + OutboundInvocationChain chain = sourceCallbackInvocationChains.get(operation); + TargetInvoker invoker = chain.getTargetInvoker(); + return invoke(chain, invoker, args); + } + + + public Object invoke(Operation operation, Object[] args) throws Throwable { + return invoke(null, operation, args); + } + + protected Object getFromAddress() { + return (inboundWire.getContainer() == null) ? null : inboundWire.getContainer().getName(); + } + + protected Object getMessageId() { + return messageId; + } + + protected Object getCorrelationId() { + return correlationId; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/OperationOutboundInvocationHandler.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/OperationOutboundInvocationHandler.java new file mode 100644 index 0000000000..ba49cee547 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/composite/OperationOutboundInvocationHandler.java @@ -0,0 +1,127 @@ +/* + * 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.core.implementation.composite; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * + */ +public class OperationOutboundInvocationHandler extends AbstractOperationOutboundInvocationHandler { + + /* + * an association of an operation to chain holder. The holder contains an invocation chain + * and a local clone of the master TargetInvoker. TargetInvokers will be cloned by the handler and placed in the + * holder if they are cacheable. This allows optimizations such as avoiding target resolution when a source refers + * to a target of greater scope since the target reference can be maintained by the invoker. When a target invoker + * is not cacheable, the master associated with the wire chains will be used. + */ + private Map<Operation, ChainHolder> chains; + private WorkContext context; + private Object fromAddress; + private Object messageId; + private Object correlationId; + + public OperationOutboundInvocationHandler(OutboundWire wire, WorkContext context) { + Map<Operation<?>, OutboundInvocationChain> invocationChains = wire.getInvocationChains(); + this.chains = new HashMap<Operation, ChainHolder>(invocationChains.size()); + this.fromAddress = (wire.getContainer() == null) ? null : wire.getContainer().getName(); + // TODO optimize this + for (Map.Entry<Operation<?>, OutboundInvocationChain> entry : invocationChains.entrySet()) { + Operation operation = entry.getKey(); + this.chains.put(operation, new ChainHolder(entry.getValue())); + } + + this.context = context; + } + + public Object invoke(Object proxy, Operation operation, Object[] args) throws Throwable { + ChainHolder holder = chains.get(operation); + if (holder == null) { + TargetException e = new TargetException("Operation not configured"); + e.setIdentifier(operation.getName()); + throw e; + } + OutboundInvocationChain chain = holder.chain; + TargetInvoker invoker; + + if (holder.cachedInvoker == null) { + assert chain != null; + if (chain.getTargetInvoker() == null) { + TargetException e = new TargetException("No target invoker configured for operation"); + e.setIdentifier(chain.getOperation().getName()); + throw e; + } + if (chain.getTargetInvoker().isCacheable()) { + // clone and store the invoker locally + holder.cachedInvoker = (TargetInvoker) chain.getTargetInvoker().clone(); + invoker = holder.cachedInvoker; + } else { + invoker = chain.getTargetInvoker(); + } + } else { + assert chain != null; + invoker = chain.getTargetInvoker(); + } + messageId = context.getCurrentMessageId(); + context.setCurrentMessageId(null); + correlationId = context.getCurrentCorrelationId(); + context.setCurrentCorrelationId(null); + return invoke(chain, invoker, args); + } + + public Object invoke(Operation operation, Object[] args) throws Throwable { + return invoke(null, operation, args); + } + + protected Object getFromAddress() { + return fromAddress; + } + + protected Object getMessageId() { + return messageId; + } + + protected Object getCorrelationId() { + return correlationId; + } + + /** + * A holder used to associate an wire chain with a local copy of a target invoker that was previously cloned from + * the chain master + */ + private class ChainHolder { + + OutboundInvocationChain chain; + TargetInvoker cachedInvoker; + + public ChainHolder(OutboundInvocationChain config) { + this.chain = config; + } + + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/AsyncJavaTargetInvoker.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/AsyncJavaTargetInvoker.java new file mode 100644 index 0000000000..d6e2a35a87 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/AsyncJavaTargetInvoker.java @@ -0,0 +1,98 @@ +/* + * 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.core.implementation.java; + +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.InvocationRuntimeException; +import org.apache.tuscany.spi.wire.Message; + +import org.apache.tuscany.core.wire.PojoTargetInvoker; + +/** + * Responsible for performing a non-blocking dispatch on a Java component implementation instance + * + * @version $Rev$ $Date$ + */ +public class AsyncJavaTargetInvoker extends PojoTargetInvoker { + + private JavaAtomicComponent component; + private InboundWire wire; + private AsyncMonitor monitor; + private WorkContext workContext; + private Object target; + + /** + * Creates a new invoker + * + * @param operation the operation the invoker is associated with + * @param wire + * @param component the target component + * @param monitor the monitor to pass events to + * @param workContext + */ + public AsyncJavaTargetInvoker(Method operation, + InboundWire wire, + JavaAtomicComponent component, + AsyncMonitor monitor, + WorkContext workContext) { + super(operation); + this.wire = wire; + this.component = component; + this.monitor = monitor; + this.workContext = workContext; + } + + public Message invoke(Message msg) throws InvocationRuntimeException { + try { + Object messageId = msg.getMessageId(); + wire.addMapping(messageId, msg.getFromAddress()); + workContext.setCurrentMessageId(null); + workContext.setCurrentCorrelationId(messageId); + invokeTarget(msg.getBody()); + // async so no return value + return null; + } catch (Throwable e) { + // FIXME need to log exceptions + monitor.executionError(e); + return null; + } + } + + public AsyncJavaTargetInvoker clone() throws CloneNotSupportedException { + return (AsyncJavaTargetInvoker) super.clone(); + } + + /** + * Resolves the target service instance or returns a cached one + */ + protected Object getInstance() throws TargetException { + if (!cacheable) { + return component.getTargetInstance(); + } else { + if (target == null) { + target = component.getTargetInstance(); + } + return target; + } + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/AsyncMonitor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/AsyncMonitor.java new file mode 100644 index 0000000000..6d35b32841 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/AsyncMonitor.java @@ -0,0 +1,35 @@ +/* + * 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.core.implementation.java; + +/** + * A monitor used to log events during non-blocking invocations + * <p/> + * REVIEW JFM we should probably generalize async monitor and make it an invocation monitor + * + * @version $$Rev$$ $$Date$$ + */ +public interface AsyncMonitor { + + /** + * Logs an exception thrown during an invocation + */ + void executionError(Throwable e); + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponent.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponent.java new file mode 100644 index 0000000000..96b61dab1c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaAtomicComponent.java @@ -0,0 +1,136 @@ +/* + * 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.core.implementation.java; + +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.TargetNotFoundException; +import static org.apache.tuscany.spi.idl.java.JavaIDLUtils.findMethod; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.RuntimeWire; +import org.apache.tuscany.spi.wire.TargetInvoker; + +import org.apache.tuscany.core.implementation.PojoAtomicComponent; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.injection.CallbackWireObjectFactory; +import org.apache.tuscany.core.injection.FieldInjector; +import org.apache.tuscany.core.injection.Injector; +import org.apache.tuscany.core.injection.InvalidAccessorException; +import org.apache.tuscany.core.injection.MethodInjector; +import org.apache.tuscany.core.injection.WireObjectFactory; + +/** + * The runtime instantiation of Java component implementations + * + * @version $Rev$ $Date$ + */ +public class JavaAtomicComponent extends PojoAtomicComponent { + + private AsyncMonitor monitor; + + public JavaAtomicComponent(String name, + PojoConfiguration configuration, + AsyncMonitor monitor) { + super(name, configuration); + this.scope = configuration.getScopeContainer().getScope(); + this.monitor = monitor; + } + + public Object getServiceInstance(String name) throws TargetException { + InboundWire wire = serviceWires.get(name); + if (wire == null) { + TargetNotFoundException e = new TargetNotFoundException(name); + e.addContextName(getName()); + throw e; + } + return wireService.createProxy(wire); + } + + public Object getServiceInstance() throws TargetException { + if (serviceInterfaces.size() == 0) { + return getTargetInstance(); + } else if (serviceInterfaces.size() == 1) { + return getTargetInstance(); + } else { + throw new TargetException("Component must have exactly one service"); + } + } + + public TargetInvoker createTargetInvoker(String targetName, Operation operation) { + Method[] methods; + if (operation.isCallback()) { + methods = operation.getServiceContract().getCallbackClass().getMethods(); + + } else { + methods = operation.getServiceContract().getInterfaceClass().getMethods(); + } + Method method = findMethod(operation, methods); + return new JavaTargetInvoker(method, this); + } + + public TargetInvoker createAsyncTargetInvoker(InboundWire wire, Operation operation) { + Method method; + if (operation.isCallback()) { + method = findMethod(operation, operation.getServiceContract().getCallbackClass().getMethods()); + + } else { + method = findMethod(operation, operation.getServiceContract().getInterfaceClass().getMethods()); + } + return new AsyncJavaTargetInvoker(method, wire, this, monitor, workContext); + } + + protected void onServiceWire(InboundWire wire) { + String name = wire.getCallbackReferenceName(); + if (name == null) { + // It's ok not to have one, we just do nothing + return; + } + Member member = callbackSites.get(name); + if (member != null) { + injectors.add(createCallbackInjector(member, wire.getServiceContract(), wire)); + } + } + + protected Injector<Object> createCallbackInjector(Member member, ServiceContract<?> contract, + InboundWire inboundWire) { + if (member instanceof Field) { + Field field = (Field) member; + ObjectFactory<?> factory = new CallbackWireObjectFactory(contract, wireService, inboundWire); + return new FieldInjector<Object>(field, factory); + } else if (member instanceof Method) { + Method method = (Method) member; + ObjectFactory<?> factory = new CallbackWireObjectFactory(contract, wireService, inboundWire); + return new MethodInjector<Object>(method, factory); + } else { + InvalidAccessorException e = new InvalidAccessorException("Member must be a field or method"); + e.setIdentifier(member.getName()); + throw e; + } + } + + protected ObjectFactory<?> createWireFactory(RuntimeWire wire) { + return new WireObjectFactory(wire, wireService); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaComponentBuilder.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaComponentBuilder.java new file mode 100644 index 0000000000..2666423b12 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaComponentBuilder.java @@ -0,0 +1,140 @@ +/* + * 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.core.implementation.java; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Member; +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; + +import org.apache.tuscany.api.annotation.Monitor; +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentBuilderExtension; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.PropertyValue; +import org.apache.tuscany.spi.model.Scope; + +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * Builds a Java-based atomic context from a component definition + * + * @version $$Rev$$ $$Date$$ + */ +public class JavaComponentBuilder extends ComponentBuilderExtension<JavaImplementation> { + + private AsyncMonitor monitor; + + @Monitor + public void setMonitor(AsyncMonitor monitor) { + this.monitor = monitor; + } + + @SuppressWarnings("unchecked") + public AtomicComponent build(CompositeComponent parent, + ComponentDefinition<JavaImplementation> definition, + DeploymentContext deployment) + throws BuilderConfigException { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> componentType = + definition.getImplementation().getComponentType(); + + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setParent(parent); + Scope scope = componentType.getImplementationScope(); + if (Scope.MODULE == scope) { + configuration.setScopeContainer(deployment.getModuleScope()); + } else { + configuration.setScopeContainer(scopeRegistry.getScopeContainer(scope)); + } + if (definition.getInitLevel() != null) { + configuration.setInitLevel(definition.getInitLevel()); + } else { + configuration.setInitLevel(componentType.getInitLevel()); + } + Method initMethod = componentType.getInitMethod(); + if (initMethod != null) { + configuration.setInitInvoker(new MethodEventInvoker(initMethod)); + } + Method destroyMethod = componentType.getDestroyMethod(); + if (destroyMethod != null) { + configuration.setDestroyInvoker(new MethodEventInvoker(destroyMethod)); + } + + configuration.setWireService(wireService); + configuration.setWorkContext(workContext); + configuration.setScheduler(workScheduler); + + // setup property injection sites + for (JavaMappedProperty<?> property : componentType.getProperties().values()) { + configuration.addPropertySite(property.getName(), property.getMember()); + } + + // setup reference injection sites + for (JavaMappedReference reference : componentType.getReferences().values()) { + Member member = reference.getMember(); + if (member != null) { + // could be null if the reference is mapped to a constructor + configuration.addReferenceSite(reference.getName(), member); + } + } + // setup constructor injection + ConstructorDefinition<?> ctorDef = componentType.getConstructorDefinition(); + Constructor<?> constr = ctorDef.getConstructor(); + PojoObjectFactory<?> instanceFactory = new PojoObjectFactory(constr); + configuration.setInstanceFactory(instanceFactory); + configuration.getConstructorParamNames().addAll(ctorDef.getInjectionNames()); + + JavaAtomicComponent component = + new JavaAtomicComponent(definition.getName(), configuration, monitor); + + // handle properties + for (PropertyValue<?> property : definition.getPropertyValues().values()) { + ObjectFactory<?> factory = property.getValueFactory(); + if (factory != null) { + component.addPropertyFactory(property.getName(), factory); + } + } + + for (JavaMappedService service : componentType.getServices().values()) { + // setup callback injection sites + if (service.getCallbackReferenceName() != null) { + // Only if there is a callback reference in the service + configuration.addCallbackSite(service.getCallbackReferenceName(), service.getCallbackMember()); + } + } + return component; + } + + protected Class<JavaImplementation> getImplementationType() { + return JavaImplementation.class; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaComponentTypeLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaComponentTypeLoader.java new file mode 100644 index 0000000000..0c4f5537ab --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaComponentTypeLoader.java @@ -0,0 +1,89 @@ +/* + * 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.core.implementation.java; + +import java.net.URL; + +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; + +import org.apache.tuscany.spi.implementation.java.IntrospectionRegistry; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; + +import org.apache.tuscany.spi.implementation.java.Introspector; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +import org.apache.tuscany.core.util.JavaIntrospectionHelper; + +/** + * @version $Rev$ $Date$ + */ +public class JavaComponentTypeLoader extends ComponentTypeLoaderExtension<JavaImplementation> { + private Introspector introspector; + + @Constructor({"registry", "introspector"}) + public JavaComponentTypeLoader(@Autowire LoaderRegistry loaderRegistry, + @Autowire IntrospectionRegistry introspector) { + super(loaderRegistry); + this.introspector = introspector; + } + + @Override + protected Class<JavaImplementation> getImplementationClass() { + return JavaImplementation.class; + } + + public void load(CompositeComponent parent, + JavaImplementation implementation, + DeploymentContext deploymentContext) throws LoaderException { + Class<?> implClass = implementation.getImplementationClass(); + URL resource = implClass.getResource(JavaIntrospectionHelper.getBaseName(implClass) + ".componentType"); + PojoComponentType componentType; + if (resource == null) { + componentType = loadByIntrospection(parent, implementation, deploymentContext); + } else { + componentType = loadFromSidefile(resource, deploymentContext); + } + implementation.setComponentType(componentType); + } + + protected PojoComponentType loadByIntrospection(CompositeComponent parent, + JavaImplementation implementation, + DeploymentContext deploymentContext) throws ProcessingException { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> componentType = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Class<?> implClass = implementation.getImplementationClass(); + introspector.introspect(parent, implClass, componentType, deploymentContext); + return componentType; + } + + protected PojoComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext) throws LoaderException { + return loaderRegistry.load(null, url, PojoComponentType.class, deploymentContext); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaImplementation.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaImplementation.java new file mode 100644 index 0000000000..807db532fe --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaImplementation.java @@ -0,0 +1,37 @@ +/* + * 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.core.implementation.java; + +import org.apache.tuscany.spi.model.AtomicImplementation; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class JavaImplementation extends AtomicImplementation<PojoComponentType> { + private Class<?> implementationClass; + + public Class<?> getImplementationClass() { + return implementationClass; + } + + public void setImplementationClass(Class<?> implementationClass) { + this.implementationClass = implementationClass; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaImplementationLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaImplementationLoader.java new file mode 100644 index 0000000000..2ac8056d74 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaImplementationLoader.java @@ -0,0 +1,62 @@ +/* + * 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.core.implementation.java; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import static org.osoa.sca.Version.XML_NAMESPACE_1_0; +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ModelObject; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.annotation.Autowire; + +public class JavaImplementationLoader extends LoaderExtension { + public static final QName IMPLEMENTATION_JAVA = new QName(XML_NAMESPACE_1_0, "implementation.java"); + + @Constructor({"registry"}) + public JavaImplementationLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + @Override + public QName getXMLType() { + return IMPLEMENTATION_JAVA; + } + + public ModelObject load(CompositeComponent parent, XMLStreamReader reader, DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + assert IMPLEMENTATION_JAVA.equals(reader.getName()); + JavaImplementation implementation = new JavaImplementation(); + String implClass = reader.getAttributeValue(null, "class"); + Class<?> implementationClass = LoaderUtil.loadClass(implClass, deploymentContext.getClassLoader()); + implementation.setImplementationClass(implementationClass); + registry.loadComponentType(parent, implementation, deploymentContext); + LoaderUtil.skipToEndElement(reader); + return implementation; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaTargetInvoker.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaTargetInvoker.java new file mode 100644 index 0000000000..004929fd93 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/java/JavaTargetInvoker.java @@ -0,0 +1,70 @@ +/* + * 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.core.implementation.java; + +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.component.TargetException; + +import org.apache.tuscany.core.wire.PojoTargetInvoker; + +/** + * Uses a component to resolve an implementation instance based on the current thread component + * + * @version $Rev$ $Date$ + */ +public class JavaTargetInvoker extends PojoTargetInvoker { + + private JavaAtomicComponent component; + private Object target; + + /** + * Creates a new invoker + * + * @param operation the operation the invoker is associated with + * @param component the target component + */ + public JavaTargetInvoker(Method operation, JavaAtomicComponent component) { + super(operation); + assert component != null : "No atomic component specified"; + this.component = component; + } + + /** + * Resolves the target service instance or returns a cached one + */ + protected Object getInstance() throws TargetException { + if (!cacheable) { + return component.getTargetInstance(); + } else { + if (target == null) { + target = component.getTargetInstance(); + } + return target; + } + } + + public JavaTargetInvoker clone() throws CloneNotSupportedException { + JavaTargetInvoker invoker = (JavaTargetInvoker) super.clone(); + invoker.target = null; + invoker.cacheable = this.cacheable; + invoker.component = this.component; + return invoker; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java new file mode 100644 index 0000000000..6a91bbefe4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when constructor parameters cannot be unambiguously resolved to a property or reference + * + * @version $Rev$ $Date$ + */ +public class AmbiguousConstructorException extends ProcessingException { + public AmbiguousConstructorException() { + } + + public AmbiguousConstructorException(String message) { + super(message); + } + + public AmbiguousConstructorException(String message, Throwable cause) { + super(message, cause); + } + + public AmbiguousConstructorException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java new file mode 100644 index 0000000000..b363d46337 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ConstructorProcessor.java @@ -0,0 +1,111 @@ +/* + * 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.core.implementation.processor; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.util.List; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; + + +/** + * Handles processing of a constructor decorated with {@link org.osoa.sca.annotations.Constructor} + * + * @version $Rev$ $Date$ + */ +@SuppressWarnings("unchecked") +public class ConstructorProcessor extends ImplementationProcessorExtension { + + private ImplementationProcessorService service; + + public ConstructorProcessor(@Autowire ImplementationProcessorService service) { + this.service = service; + } + + public <T> void visitClass(CompositeComponent parent, Class<T> clazz, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + Constructor[] ctors = clazz.getConstructors(); + boolean found = false; + for (Constructor constructor : ctors) { + if (constructor.getAnnotation(org.osoa.sca.annotations.Constructor.class) != null) { + if (found) { + DuplicateConstructorException e = + new DuplicateConstructorException("Multiple constructors marked with @Constructor"); + e.setIdentifier(constructor.getDeclaringClass().getName()); + throw e; + } + found = true; + } + } + } + + public <T> void visitConstructor(CompositeComponent parent, Constructor<T> constructor, + PojoComponentType<JavaMappedService, JavaMappedReference, + JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + org.osoa.sca.annotations.Constructor annotation = + constructor.getAnnotation(org.osoa.sca.annotations.Constructor.class); + if (annotation == null) { + return; + } + ConstructorDefinition<?> definition = type.getConstructorDefinition(); + if (definition != null && !definition.getConstructor().equals(constructor)) { + DuplicateConstructorException e = + new DuplicateConstructorException("Multiple constructor definitions found"); + e.setIdentifier(constructor.getDeclaringClass().getName()); + throw e; + } else if (definition == null) { + definition = new ConstructorDefinition(constructor); + } + Class<?>[] params = constructor.getParameterTypes(); + String[] names = annotation.value(); + Annotation[][] annotations = constructor.getParameterAnnotations(); + List<String> injectionNames = definition.getInjectionNames(); + for (int i = 0; i < params.length; i++) { + Class<?> param = params[i]; + Annotation[] paramAnnotations = annotations[i]; + try { + if (!service.processParam(param, paramAnnotations, names, i, type, injectionNames)) { + String name = (i < names.length) ? names[i] : ""; + service.addName(injectionNames, i, name); + } + } catch (ProcessingException + e) { + e.setIdentifier(constructor.toString()); + throw e; + } + } + if (names.length != 0 && names[0].length() != 0 && names.length != params.length) { + throw new InvalidConstructorException("Names in @Constructor do not match number of parameters"); + } + type.setConstructorDefinition(definition); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ContextProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ContextProcessor.java new file mode 100644 index 0000000000..326bfb05f4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ContextProcessor.java @@ -0,0 +1,95 @@ +/* + * 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.core.implementation.processor; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.osoa.sca.CompositeContext; +import org.osoa.sca.RequestContext; +import org.osoa.sca.annotations.Context; + +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.core.util.JavaIntrospectionHelper; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; + +/** + * Processes {@link @Context} annotations on a component implementation and adds a {@link JavaMappedProperty} to the + * component type which will be used to inject the appropriate context + * + * @version $Rev$ $Date$ + */ +public class ContextProcessor extends ImplementationProcessorExtension { + + public void visitMethod(CompositeComponent parent, Method method, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) + throws ProcessingException { + if (method.getAnnotation(Context.class) == null) { + return; + } + if (method.getParameterTypes().length != 1) { + IllegalContextException e = new IllegalContextException("Context setter must have one parameter"); + e.setIdentifier(method.toString()); + throw e; + } + Class<?> paramType = method.getParameterTypes()[0]; + if (CompositeContext.class.equals(paramType)) { + String name = method.getName(); + if (name.startsWith("set")) { + name = JavaIntrospectionHelper.toPropertyName(name); + } + JavaMappedProperty property = new JavaMappedProperty(); + property.setName(name); + property.setMember(method); + throw new UnsupportedOperationException(); + // TODO pass in composite context + //SingletonObjectFactory factory = new SingletonObjectFactory(compositeContext); + //property.setDefaultValueFactory(factory); + //type.getProperties().put(name,property); + } else if (RequestContext.class.equals(paramType)) { + String name = method.getName(); + if (name.startsWith("set")) { + name = JavaIntrospectionHelper.toPropertyName(name); + } + JavaMappedProperty property = new JavaMappedProperty(); + property.setName(name); + property.setMember(method); + throw new UnsupportedOperationException(); + // TODO pass in request context + //property.setDefaultValueFactory(factory); + //type.getProperties().put(name,property); + } else { + throw new UnknownContextTypeException(paramType.getName()); + } + + } + + public void visitField(CompositeComponent parent, Field field, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + super.visitField(parent, field, type, context); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DestroyProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DestroyProcessor.java new file mode 100644 index 0000000000..a25902566f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DestroyProcessor.java @@ -0,0 +1,65 @@ +/* + * 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.core.implementation.processor; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import org.osoa.sca.annotations.Destroy; + +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; + +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; + +/** + * Processes the {@link @Destroy} annotation on a component implementation and updates the component type with the + * decorated destructor method + * + * @version $Rev$ $Date$ + */ +public class DestroyProcessor extends ImplementationProcessorExtension { + + public void visitMethod(CompositeComponent parent, Method method, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) + throws ProcessingException { + Destroy annotation = method.getAnnotation(Destroy.class); + if (annotation == null) { + return; + } + if (method.getParameterTypes().length != 0) { + IllegalDestructorException e = new IllegalDestructorException("Destructor must not have argments"); + e.setIdentifier(method.toString()); + throw e; + } + if (type.getDestroyMethod() != null) { + throw new DuplicateDestructorException("More than one destructor found on implementation"); + } + if (Modifier.isProtected(method.getModifiers())) { + method.setAccessible(true); + } + type.setDestroyMethod(method); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateConstructorException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateConstructorException.java new file mode 100644 index 0000000000..909d0ef124 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateConstructorException.java @@ -0,0 +1,45 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when more than one component implementation constructor is annotated with {@link + * org.osoa.sca.annotations.Constructor} + * + * @version $Rev$ $Date$ + */ +public class DuplicateConstructorException extends ProcessingException { + + public DuplicateConstructorException() { + } + + public DuplicateConstructorException(String message) { + super(message); + } + + public DuplicateConstructorException(String message, Throwable cause) { + super(message, cause); + } + + public DuplicateConstructorException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateDestructorException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateDestructorException.java new file mode 100644 index 0000000000..732dd926c3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateDestructorException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when an implementation is annotated multiple times with {@link org.osoa.sca.annotations.Destroy} + * + * @version $Rev$ $Date$ + */ +public class DuplicateDestructorException extends ProcessingException { + public DuplicateDestructorException() { + } + + public DuplicateDestructorException(String message) { + super(message); + } + + public DuplicateDestructorException(String message, Throwable cause) { + super(message, cause); + } + + public DuplicateDestructorException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateInitException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateInitException.java new file mode 100644 index 0000000000..6795a7bfce --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateInitException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when an implementation is annotated multiple times with {@link @org.osoa.sca.annotations.Init} + * + * @version $Rev$ $Date$ + */ +public class DuplicateInitException extends ProcessingException { + public DuplicateInitException() { + } + + public DuplicateInitException(String message) { + super(message); + } + + public DuplicateInitException(String message, Throwable cause) { + super(message, cause); + } + + public DuplicateInitException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateReferenceException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateReferenceException.java new file mode 100644 index 0000000000..da6ceaa472 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/DuplicateReferenceException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when an implementation has more than one reference injection site with the same name + * + * @version $Rev$ $Date$ + */ +public class DuplicateReferenceException extends ProcessingException { + public DuplicateReferenceException() { + } + + public DuplicateReferenceException(String message) { + super(message); + } + + public DuplicateReferenceException(String message, Throwable cause) { + super(message, cause); + } + + public DuplicateReferenceException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java new file mode 100644 index 0000000000..27ecccafb3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java @@ -0,0 +1,521 @@ +/* + * 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.core.implementation.processor; + +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllInterfaces; +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllPublicAndProtectedFields; +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllUniquePublicProtectedMethods; +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getBaseName; +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.model.OverrideOptions; +import org.osoa.sca.annotations.Remotable; +import org.osoa.sca.annotations.Service; + +/** + * Heuristically evaluates an un-annotated Java implementation type to determine services, references, and properties + * according to the algorithm described in the SCA Java Client and Implementation Model Specification <p/> TODO + * Implement: + * <p/> + * When no service inteface is annotated, need to calculate a single service comprising all public methods that are not + * reference or property injection sites. If that service can be exactly mapped to an interface implemented by the class + * then the service interface will be defined in terms of that interface. + * + * @version $Rev$ $Date$ + */ +public class HeuristicPojoProcessor extends ImplementationProcessorExtension { + + private ImplementationProcessorService implService; + + public HeuristicPojoProcessor(@Autowire ImplementationProcessorService service) { + this.implService = service; + } + + public <T> void visitEnd( + CompositeComponent parent, + Class<T> clazz, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + Map<String, JavaMappedService> services = type.getServices(); + if (services.isEmpty()) { + // heuristically determine the service + // TODO finish algorithm + Set<Class> interfaces = getAllInterfaces(clazz); + if (interfaces.size() == 0) { + // class is the interface + JavaMappedService service; + try { + service = implService.createService(clazz); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + type.getServices().put(service.getName(), service); + } else if (interfaces.size() == 1) { + JavaMappedService service; + try { + service = implService.createService(interfaces.iterator().next()); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + type.getServices().put(service.getName(), service); + } + } + Set<Method> methods = getAllUniquePublicProtectedMethods(clazz); + if (!type.getReferences().isEmpty() || !type.getProperties().isEmpty()) { + // references and properties have been explicitly defined + if (type.getServices().isEmpty()) { + calculateServiceInterface(clazz, type, methods); + if (type.getServices().isEmpty()) { + throw new ServiceTypeNotFoundException(clazz.getName()); + } + } + evaluateConstructor(type, clazz); + return; + } + + // heuristically determine the properties references + // make a first pass through all public methods with one param + for (Method method : methods) { + if (method.getParameterTypes().length != 1 || !Modifier.isPublic(method.getModifiers()) + || !method.getName().startsWith("set") + || method.getReturnType() != void.class) { + continue; + } + if (!isInServiceInterface(method, services)) { + String name = toPropertyName(method.getName()); + // avoid duplicate property or ref names + if (!type.getProperties().containsKey(name) && !type.getReferences().containsKey(name)) { + Class<?> param = method.getParameterTypes()[0]; + Type genericType = method.getGenericParameterTypes()[0]; + if (isReferenceType(genericType)) { + type.add(createReference(name, method, param)); + } else { + type.add(createProperty(name, method, param)); + } + } + } + } + // second pass for protected methods with one param + for (Method method : methods) { + if (method.getParameterTypes().length != 1 || !Modifier.isProtected(method.getModifiers()) + || !method.getName().startsWith("set") + || method.getReturnType() != void.class) { + continue; + } + Class<?> param = method.getParameterTypes()[0]; + String name = toPropertyName(method.getName()); + // avoid duplicate property or ref names + if (!type.getProperties().containsKey(name) && !type.getReferences().containsKey(name)) { + if (isReferenceType(param)) { + type.add(createReference(name, method, param)); + } else { + type.add(createProperty(name, method, param)); + } + } + } + Set<Field> fields = getAllPublicAndProtectedFields(clazz); + for (Field field : fields) { + Class<?> paramType = field.getType(); + if (isReferenceType(paramType)) { + type.add(createReference(field.getName(), field, paramType)); + } else { + type.add(createProperty(field.getName(), field, paramType)); + } + } + evaluateConstructor(type, clazz); + } + + /** + * Determines the constructor to use based on the component type's references and properties + * + * @param type the component type + * @param clazz the implementation class corresponding to the component type + * @throws NoConstructorException if no suitable constructor is found + * @throws AmbiguousConstructorException if the parameters of a constructor cannot be unambiguously mapped to + * references and properties + */ + @SuppressWarnings("unchecked") + private <T> void evaluateConstructor( + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + Class<T> clazz) throws ProcessingException { + // determine constructor if one is not annotated + ConstructorDefinition<?> definition = type.getConstructorDefinition(); + Constructor constructor; + boolean explict = false; + if (definition != null + && definition.getConstructor().getAnnotation(org.osoa.sca.annotations.Constructor.class) != null) { + // the constructor was already defined explicitly + return; + } else if (definition != null) { + explict = true; + constructor = definition.getConstructor(); + } else { + // no definition, heuristically determine constructor + Constructor[] constructors = clazz.getConstructors(); + if (constructors.length == 0) { + NoConstructorException e = new NoConstructorException("No public constructor for class"); + e.setIdentifier(clazz.getName()); + throw e; + } else if (constructors.length == 1) { + constructor = constructors[0]; + } else { + // FIXME multiple constructors, none yet done + Constructor<T> selected = null; + int sites = type.getProperties().size() + type.getReferences().size(); + for (Constructor<T> ctor : constructors) { + if (ctor.getParameterTypes().length == 0) { + selected = ctor; + } + if (ctor.getParameterTypes().length == sites) { + // TODO finish + // selected = constructor; + // select constructor + // break; + } + } + if (selected == null) { + throw new NoConstructorException(); + } + constructor = selected; + definition = new ConstructorDefinition<T>(selected); + type.setConstructorDefinition(definition); + // return; + } + definition = new ConstructorDefinition<T>(constructor); + type.setConstructorDefinition(definition); + } + Class[] params = constructor.getParameterTypes(); + if (params.length == 0) { + return; + } + List<String> paramNames = definition.getInjectionNames(); + Map<String, JavaMappedProperty<?>> props = type.getProperties(); + Map<String, JavaMappedReference> refs = type.getReferences(); + Annotation[][] annotations = constructor.getParameterAnnotations(); + if (!explict) { + // the constructor wasn't defined by an annotation, so check to see if any of the params have an annotation + // which we can impute as explicitly defining the constructor, e.g. @Property, @Reference, or @Autowire + explict = implService.injectionAnnotationsPresent(annotations); + } + if (explict) { + for (int i = 0; i < params.length; i++) { + Class param = params[i]; + implService.processParam(param, annotations[i], new String[0], i, type, paramNames); + } + } else { + if (!implService.areUnique(params)) { + throw new AmbiguousConstructorException("Cannot resolve non-unique parameter types, use @Constructor"); + } + if (!calcPropRefUniqueness(props.values(), refs.values())) { + throw new AmbiguousConstructorException("Cannot resolve non-unique parameter types, use @Constructor"); + } + boolean empty = props.size() + refs.size() == 0; + if (!empty) { + // the constructor param types must unambiguously match defined reference or property types + for (Class param : params) { + String name = findReferenceOrProperty(param, props, refs); + if (name == null) { + throw new AmbiguousConstructorException(param.getName()); + } + paramNames.add(name); + } + } else { + // heuristically determine refs and props from the parameter types + for (Class<?> param : params) { + String name = getBaseName(param).toLowerCase(); + if (isReferenceType(param)) { + refs.put(name, createReference(name, null, param)); + } else { + props.put(name, createProperty(name, null, param)); + } + paramNames.add(name); + } + } + } + } + + /** + * Returns true if the union of the given collections of properties and references have unique Java types + */ + private boolean calcPropRefUniqueness( + Collection<JavaMappedProperty<?>> props, + Collection<JavaMappedReference> refs) { + + Class[] classes = new Class[props.size() + refs.size()]; + int i = 0; + for (JavaMappedProperty<?> property : props) { + classes[i] = property.getJavaType(); + i++; + } + for (JavaMappedReference reference : refs) { + classes[i] = reference.getServiceContract().getInterfaceClass(); + i++; + } + return implService.areUnique(classes); + } + + /** + * Unambiguously finds the reference or property associated with the given type + * + * @return the name of the reference or property if found, null if not + * @throws AmbiguousConstructorException if the constructor parameter cannot be resolved to a property or reference + */ + private String findReferenceOrProperty( + Class<?> type, + Map<String, JavaMappedProperty<?>> props, + Map<String, JavaMappedReference> refs) throws AmbiguousConstructorException { + + String name = null; + for (JavaMappedProperty<?> property : props.values()) { + if (property.getJavaType().equals(type)) { + if (name != null) { + AmbiguousConstructorException e = + new AmbiguousConstructorException("Ambiguous property or reference for constructor type"); + e.setIdentifier(type.getName()); + throw e; + } + name = property.getName(); + // do not break since ambiguities must be checked, i.e. more than one prop or ref of the same type + } + } + for (JavaMappedReference reference : refs.values()) { + if (reference.getServiceContract().getInterfaceClass().equals(type)) { + if (name != null) { + AmbiguousConstructorException e = + new AmbiguousConstructorException("Ambiguous property or reference for constructor type"); + e.setIdentifier(type.getName()); + throw e; + } + name = reference.getName(); + // do not break since ambiguities must be checked, i.e. more than one prop or ref of the same type + } + } + return name; + } + + /** + * Returns true if a given type is reference according to the SCA specification rules for determining reference + * types + */ + private boolean isReferenceType(Type operationType) { + Class<?> rawType; + Class<?> referenceType = null; + if (operationType instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) operationType; + rawType = (Class<?>) parameterizedType.getRawType(); + Type[] typeArgs = parameterizedType.getActualTypeArguments(); + if (typeArgs.length == 1) { + referenceType = (Class<?>) typeArgs[0]; + } + } else { + rawType = (Class<?>) operationType; + } + if (rawType.isArray()) { + referenceType = rawType.getComponentType(); + } else if (Collection.class.isAssignableFrom(rawType) && referenceType == null) { + return true; + } + if (referenceType != null) { + return referenceType.getAnnotation(Remotable.class) != null + || referenceType.getAnnotation(Service.class) != null; + } else { + return rawType.getAnnotation(Remotable.class) != null || rawType.getAnnotation(Service.class) != null; + } + } + + /** + * Returns true if the given operation is defined in the collection of service interfaces + */ + private boolean isInServiceInterface(Method operation, Map<String, JavaMappedService> services) { + for (JavaMappedService service : services.values()) { + Class<?> clazz = service.getServiceContract().getInterfaceClass(); + if (operation.getDeclaringClass().equals(clazz)) { + return true; + } + Method[] methods = service.getServiceContract().getInterfaceClass().getMethods(); + for (Method method : methods) { + if (operation.getName().equals(method.getName()) + && operation.getParameterTypes().length == method.getParameterTypes().length) { + Class<?>[] methodTypes = method.getParameterTypes(); + for (int i = 0; i < operation.getParameterTypes().length; i++) { + Class<?> paramType = operation.getParameterTypes()[i]; + if (!paramType.equals(methodTypes[i])) { + break; + } else if (i == operation.getParameterTypes().length - 1) { + return true; + } + } + } + } + } + return false; + } + + /** + * Creates a mapped reference + * + * @param name the reference name + * @param member the injection site the reference maps to + * @param paramType the service interface of the reference + */ + private JavaMappedReference createReference(String name, Member member, Class<?> paramType) + throws ProcessingException { + return implService.createReference(name, member, paramType); + } + + /** + * Creates a mapped property + * + * @param name the property name + * @param member the injection site the reference maps to + * @param paramType the property type + */ + private <T> JavaMappedProperty<T> createProperty(String name, Member member, Class<T> paramType) { + JavaMappedProperty<T> property = new JavaMappedProperty<T>(); + property.setName(name); + property.setMember(member); + property.setOverride(OverrideOptions.MAY); + property.setJavaType(paramType); + return property; + } + + /** + * Populates a component type with a service whose interface type is determined by examining all implemented + * interfaces of the given class and chosing one whose operations match all of the class's non-property and + * non-reference methods + * + * @param clazz the class to examine + * @param type the component type + * @param methods all methods in the class to examine + */ + private void calculateServiceInterface( + Class<?> clazz, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + Set<Method> methods) throws ProcessingException { + List<Method> nonPropRefMethods = new ArrayList<Method>(); + // Map<String, JavaMappedService> services = type.getServices(); + Map<String, JavaMappedReference> references = type.getReferences(); + Map<String, JavaMappedProperty<?>> properties = type.getProperties(); + // calculate methods that are not properties or references + for (Method method : methods) { + String name = toPropertyName(method.getName()); + if (!references.containsKey(name) && !properties.containsKey(name)) { + nonPropRefMethods.add(method); + } + } + // determine if an implemented interface matches all of the non-property and non-reference methods + Class[] interfaces = clazz.getInterfaces(); + if (interfaces.length == 0) { + return; + } + for (Class interfaze : interfaces) { + if (analyzeInterface(interfaze, nonPropRefMethods)) { + JavaMappedService service; + try { + service = implService.createService(interfaze); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + type.getServices().put(service.getName(), service); + } + } + } + + /** + * Determines if the methods of a given interface match the given list of methods + * + * @param interfaze the interface to examine + * @param nonPropRefMethods the list of methods to match against + * @return true if the interface matches + */ + private boolean analyzeInterface(Class<?> interfaze, List<Method> nonPropRefMethods) { + Method[] interfaceMethods = interfaze.getMethods(); + if (nonPropRefMethods.size() != interfaceMethods.length) { + return false; + } + for (Method method : nonPropRefMethods) { + boolean found = false; + for (Method interfaceMethod : interfaceMethods) { + if (interfaceMethod.getName().equals(method.getName())) { + Class<?>[] interfaceParamTypes = interfaceMethod.getParameterTypes(); + Class<?>[] methodParamTypes = method.getParameterTypes(); + if (interfaceParamTypes.length == methodParamTypes.length) { + if (interfaceParamTypes.length == 0) { + found = true; + } else { + for (int i = 0; i < methodParamTypes.length; i++) { + Class<?> param = methodParamTypes[i]; + if (!param.equals(interfaceParamTypes[i])) { + break; + } + if (i == methodParamTypes.length - 1) { + found = true; + } + } + } + } + if (found) { + break; + } + } + } + if (!found) { + return false; + } + } + return true; + } + +} + +/* + * 1) public setter methods that are not included in any service interface 2) protected setter methods 3) public or + * protected fields unless there is a setter method for the same name If the type associated with the member is an array + * or a java.util.Collection, then the basetype will be the element type of the array or the parameterized type of the + * Collection, otherwise the basetype will be the member type. If the basetype is an interface with an @Remotable or + * @Service annotation then the member will be defined as a reference, otherwise it will be defined as a property. + * + * + */ diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalCallbackReferenceException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalCallbackReferenceException.java new file mode 100644 index 0000000000..7ad5b7e526 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalCallbackReferenceException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an illegcal use of {@link org.osoa.sca.annotations.Callback} on a reference + * + * @version $Rev$ $Date$ + */ +public class IllegalCallbackReferenceException extends ProcessingException { + public IllegalCallbackReferenceException() { + } + + public IllegalCallbackReferenceException(String message) { + super(message); + } + + public IllegalCallbackReferenceException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalCallbackReferenceException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalContextException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalContextException.java new file mode 100644 index 0000000000..9c64b3dee8 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalContextException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an illegal signature for a method decorated with {@link org.osoa.sca.annotations.Context} + * + * @version $Rev$ $Date$ + */ +public class IllegalContextException extends ProcessingException { + public IllegalContextException() { + } + + public IllegalContextException(String message) { + super(message); + } + + public IllegalContextException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalContextException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalDestructorException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalDestructorException.java new file mode 100644 index 0000000000..b412a38bb9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalDestructorException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an illegal signature for a method decorated with {@link org.osoa.sca.annotations.Destroy} + * + * @version $Rev$ $Date$ + */ +public class IllegalDestructorException extends ProcessingException { + public IllegalDestructorException() { + } + + public IllegalDestructorException(String message) { + super(message); + } + + public IllegalDestructorException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalDestructorException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalInitException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalInitException.java new file mode 100644 index 0000000000..615a81926c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalInitException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an illegal signature for a method decorated with {@link @org.osoa.sca.annotations.Init} + * + * @version $Rev$ $Date$ + */ +public class IllegalInitException extends ProcessingException { + public IllegalInitException() { + } + + public IllegalInitException(String message) { + super(message); + } + + public IllegalInitException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalInitException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalReferenceException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalReferenceException.java new file mode 100644 index 0000000000..10a75baaa5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalReferenceException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an illegal reference definition in a component type + * + * @version $Rev$ $Date$ + */ +public class IllegalReferenceException extends ProcessingException { + public IllegalReferenceException() { + } + + public IllegalReferenceException(String message) { + super(message); + } + + public IllegalReferenceException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalReferenceException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalServiceDefinitionException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalServiceDefinitionException.java new file mode 100644 index 0000000000..60fd13a3e5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/IllegalServiceDefinitionException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an illegal use of the {@link @org.osoa.sca.annotations.Service} annotation + * + * @version $Rev$ $Date$ + */ +public class IllegalServiceDefinitionException extends ProcessingException { + public IllegalServiceDefinitionException() { + } + + public IllegalServiceDefinitionException(String message) { + super(message); + } + + public IllegalServiceDefinitionException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalServiceDefinitionException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java new file mode 100644 index 0000000000..2c2b40bc3e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ImplementationProcessorServiceImpl.java @@ -0,0 +1,339 @@ +/* + * 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.core.implementation.processor; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Member; +import java.util.List; +import javax.xml.namespace.QName; + +import org.osoa.sca.annotations.Callback; +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Remotable; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.implementation.java.DuplicatePropertyException; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.model.OverrideOptions; +import org.apache.tuscany.spi.model.ServiceContract; + +import org.apache.tuscany.core.idl.java.IllegalCallbackException; +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getBaseName; + +/** + * The default implementation of an <code>ImplementationProcessorService</code> + * + * @version $Rev$ $Date$ + */ +public class ImplementationProcessorServiceImpl implements ImplementationProcessorService { + private JavaInterfaceProcessorRegistry registry; + + public ImplementationProcessorServiceImpl(@Autowire JavaInterfaceProcessorRegistry registry) { + this.registry = registry; + } + + public JavaMappedService createService(Class<?> interfaze) throws InvalidServiceContractException { + JavaMappedService service = new JavaMappedService(); + service.setName(getBaseName(interfaze)); + service.setRemotable(interfaze.getAnnotation(Remotable.class) != null); + ServiceContract<?> contract = registry.introspect(interfaze); + service.setServiceContract(contract); + return service; + } + + public void processCallback(Class<?> interfaze, ServiceContract<?> contract) throws IllegalCallbackException { + Callback callback = interfaze.getAnnotation(Callback.class); + if (callback != null && !Void.class.equals(callback.value())) { + Class<?> callbackClass = callback.value(); + contract.setCallbackClass(callbackClass); + contract.setCallbackName(getBaseName(callbackClass)); + } else if (callback != null && Void.class.equals(callback.value())) { + IllegalCallbackException e = new IllegalCallbackException( + "Callback annotation must specify an interface on service type"); + e.setIdentifier(interfaze.getName()); + throw e; + } + } + + public boolean areUnique(Class[] collection) { + if (collection.length == 0) { + return true; + } + return areUnique(collection, 0); + } + + public void addName(List<String> names, int pos, String name) { + if (names.size() < pos) { + for (int i = 0; i < pos; i++) { + names.add(i, ""); + } + names.add(name); + } else if (names.size() > pos) { + names.remove(pos); + names.add(pos, name); + } else { + names.add(pos, name); + } + } + + public boolean processParam( + Class<?> param, + Annotation[] paramAnnotations, + String[] constructorNames, + int pos, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + List<String> injectionNames) throws ProcessingException { + boolean processed = false; + for (Annotation annot : paramAnnotations) { + if (Autowire.class.equals(annot.annotationType())) { + processed = true; + processAutowire(annot, constructorNames, pos, param, type, injectionNames); + } else if (Property.class.equals(annot.annotationType())) { + processed = true; + processProperty(annot, constructorNames, pos, type, param, injectionNames); + } else if (Reference.class.equals(annot.annotationType())) { + processed = true; + processReference(annot, constructorNames, pos, type, param, injectionNames); + } + } + return processed; + } + + public boolean injectionAnnotationsPresent(Annotation[][] annots) { + for (Annotation[] annotations : annots) { + for (Annotation annotation : annotations) { + Class<? extends Annotation> annotType = annotation.annotationType(); + if (annotType.equals(Autowire.class) || annotType.equals(Property.class) + || annotType.equals(Reference.class)) { + return true; + } + } + } + return false; + } + + /** + * Determines if all the members of a collection have unique types + * + * @param collection the collection to analyze + * @param start the position in the collection to start + * @return true if the types are unique + */ + private boolean areUnique(Class[] collection, int start) { + Object compare = collection[start]; + for (int i = start + 1; i < collection.length; i++) { + if (compare.equals(collection[i])) { + return false; + } + } + if (start + 1 < collection.length) { + return areUnique(collection, start + 1); + } else { + return true; + } + } + + /** + * Processes autowire metadata for a constructor parameter + * + * @param annot the autowire annotation + * @param constructorNames the parameter names as specified in an {@link org.osoa.sca.annotations.Constructor} + * annotation + * @param pos the position of the parameter in the constructor's parameter list + * @param param the parameter type + * @param type the component type associated with the implementation being processed + * @param injectionNames the collection of injection names to update + * @throws InvalidAutowireException + * @throws InvalidConstructorException + */ + private void processAutowire( + Annotation annot, + String[] constructorNames, + int pos, + Class<?> param, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + List<String> injectionNames) throws InvalidAutowireException, InvalidConstructorException { + // the param is marked as an autowire + Autowire autowireAnnot = (Autowire) annot; + JavaMappedReference reference = new JavaMappedReference(); + reference.setAutowire(true); + String name = autowireAnnot.name(); + if (name == null || name.length() == 0) { + if (constructorNames.length > 1 && (constructorNames.length < pos + 1 || constructorNames[pos] == null)) { + throw new InvalidAutowireException("Names in @Constructor and autowire parameter do not match at " + + (pos + 1)); + } else if (constructorNames.length == 1 && constructorNames[0].length() == 0) { + // special case when @Constructor present with all autowire params not specifying any name + name = param.getName() + String.valueOf(pos); + } else if (constructorNames.length == 1 + && (constructorNames.length < pos + 1 || constructorNames[pos] == null)) { + throw new InvalidAutowireException("Names in @Constructor and autowire parameter do not match at " + + (pos + 1)); + } else if (constructorNames.length == 1 && constructorNames[0].length() > 0) { + name = constructorNames[pos]; + } else if (constructorNames.length == 0 || constructorNames[pos].length() == 0) { + name = param.getName() + String.valueOf(pos); + } else { + name = constructorNames[pos]; + } + } else if (pos < constructorNames.length && constructorNames[pos] != null + && constructorNames[pos].length() != 0 && !name.equals(constructorNames[pos])) { + throw new InvalidConstructorException("Name specified by @Constructor does not match autowire name at " + + (pos + 1)); + } + reference.setName(name); + + reference.setRequired(autowireAnnot.required()); + + ServiceContract<?> contract = new JavaServiceContract(); + contract.setInterfaceClass(param); + reference.setServiceContract(contract); + type.getReferences().put(name, reference); + addName(injectionNames, pos, name); + } + + /** + * Processes parameter metadata for a constructor parameter + * + * @param annot the parameter annotation + * @param constructorNames the parameter names as specified in an {@link org.osoa.sca.annotations.Constructor} + * annotation + * @param pos the position of the parameter in the constructor's parameter list + * @param type the component type associated with the implementation being processed + * @param param the parameter type + * @param explicitNames the collection of injection names to update + * @throws ProcessingException + */ + private <T> void processProperty( + Annotation annot, + String[] constructorNames, + int pos, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + Class<T> param, + List<String> explicitNames) throws ProcessingException { + // TODO multiplicity + // the param is marked as a property + Property propAnnot = (Property) annot; + JavaMappedProperty<T> property = new JavaMappedProperty<T>(); + String name = propAnnot.name(); + if (name == null || name.length() == 0) { + if (constructorNames.length < pos + 1 || constructorNames[pos] == null + || constructorNames[pos].length() == 0) { + throw new InvalidPropertyException("No name specified for property parameter " + (pos + 1)); + } + name = constructorNames[pos]; + } else if (pos < constructorNames.length && constructorNames[pos] != null + && constructorNames[pos].length() != 0 && !name.equals(constructorNames[pos])) { + throw new InvalidConstructorException("Name specified by @Constructor does not match property name at " + + (pos + 1)); + } + if (type.getProperties().get(name) != null) { + throw new DuplicatePropertyException(name); + } + property.setName(name); + property.setOverride(OverrideOptions.valueOf(propAnnot.override().toUpperCase())); + + property.setXmlType(QName.valueOf(propAnnot.xmlType())); + property.setJavaType(param); + type.getProperties().put(name, property); + addName(explicitNames, pos, name); + } + + /** + * Processes reference metadata for a constructor parameter + * + * @param annot the parameter annotation + * @param constructorNames the parameter names as specified in an {@link org.osoa.sca.annotations.Constructor} + * annotation + * @param pos the position of the parameter in the constructor's parameter list + * @param type the component type associated with the implementation being processed + * @param param the parameter type + * @param explicitNames the collection of injection names to update + * @throws ProcessingException + */ + private void processReference( + Annotation annot, + String[] constructorNames, + int pos, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + Class<?> param, + List<String> explicitNames) throws ProcessingException { + + // TODO multiplicity + // the param is marked as a reference + Reference refAnnotation = (Reference) annot; + JavaMappedReference reference = new JavaMappedReference(); + String name = refAnnotation.name(); + if (name == null || name.length() == 0) { + if (constructorNames.length < pos + 1 || constructorNames[pos] == null + || constructorNames[pos].length() == 0) { + throw new InvalidReferenceException("No name specified for reference parameter " + (pos + 1)); + } + name = constructorNames[pos]; + } else if (pos < constructorNames.length && constructorNames[pos] != null + && constructorNames[pos].length() != 0 && !name.equals(constructorNames[pos])) { + throw new InvalidConstructorException("Name specified by @Constructor does not match reference name at " + + (pos + 1)); + } + if (type.getReferences().get(name) != null) { + throw new DuplicateReferenceException(name); + } + reference.setName(name); + reference.setRequired(refAnnotation.required()); + try { + ServiceContract<?> contract = registry.introspect(param); + reference.setServiceContract(contract); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + type.getReferences().put(name, reference); + addName(explicitNames, pos, name); + } + + public JavaMappedReference createReference(String name, Member member, Class<?> paramType) + throws ProcessingException { + JavaMappedReference reference = new JavaMappedReference(); + reference.setName(name); + reference.setMember(member); + reference.setRequired(false); + ServiceContract contract = null; + try { + contract = registry.introspect(paramType); + } catch (InvalidServiceContractException e1) { + throw new ProcessingException(e1); + } + try { + processCallback(paramType, contract); + } catch (IllegalCallbackException e) { + throw new ProcessingException(e); + } + reference.setServiceContract(contract); + return reference; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InitProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InitProcessor.java new file mode 100644 index 0000000000..cf32e8b855 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InitProcessor.java @@ -0,0 +1,65 @@ +/* + * 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.core.implementation.processor; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import org.osoa.sca.annotations.Init; + +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; + +/** + * Processes the {@link @Init} annotation on a component implementation and updates the component type with the + * decorated initializer method + * + * @version $Rev$ $Date$ + */ +public class InitProcessor extends ImplementationProcessorExtension { + + public void visitMethod(CompositeComponent parent, Method method, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) + throws ProcessingException { + Init annotation = method.getAnnotation(Init.class); + if (annotation == null) { + return; + } + if (method.getParameterTypes().length != 0) { + IllegalInitException e = new IllegalInitException("Initializer must not have argments"); + e.setIdentifier(method.toString()); + throw e; + } + if (type.getInitMethod() != null) { + throw new DuplicateInitException("More than one initializer found on implementaton"); + } + if (Modifier.isProtected(method.getModifiers())) { + method.setAccessible(true); + } + type.setEagerInit(annotation.eager()); + type.setInitMethod(method); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidAutowireException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidAutowireException.java new file mode 100644 index 0000000000..1a46b22d8d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidAutowireException.java @@ -0,0 +1,44 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an invalid usage of {@link org.apache.tuscany.spi.annotation.Autowire} + * + * @version $Rev$ $Date$ + */ +public class InvalidAutowireException extends ProcessingException { + + public InvalidAutowireException() { + } + + public InvalidAutowireException(String message) { + super(message); + } + + public InvalidAutowireException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidAutowireException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidConstructorException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidConstructorException.java new file mode 100644 index 0000000000..ded7b8886a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidConstructorException.java @@ -0,0 +1,44 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an invalid constructor definition, e.g. when the number of injection names specified in {@link + * org.osoa.sca.annotations.Constructor} do not match the number of actual constructor parameters + * + * @version $Rev$ $Date$ + */ +public class InvalidConstructorException extends ProcessingException { + public InvalidConstructorException() { + } + + public InvalidConstructorException(String message) { + super(message); + } + + public InvalidConstructorException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidConstructorException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidPropertyException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidPropertyException.java new file mode 100644 index 0000000000..7a45d22605 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidPropertyException.java @@ -0,0 +1,44 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an invalid usage of {@link org.osoa.sca.annotations.Property} + * + * @version $Rev$ $Date$ + */ +public class InvalidPropertyException extends ProcessingException { + + public InvalidPropertyException() { + } + + public InvalidPropertyException(String message) { + super(message); + } + + public InvalidPropertyException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidPropertyException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidReferenceException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidReferenceException.java new file mode 100644 index 0000000000..634acaaaed --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidReferenceException.java @@ -0,0 +1,44 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Denotes an invalid usage of {@link org.osoa.sca.annotations.Reference} + * + * @version $Rev$ $Date$ + */ +public class InvalidReferenceException extends ProcessingException { + + public InvalidReferenceException() { + } + + public InvalidReferenceException(String message) { + super(message); + } + + public InvalidReferenceException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidReferenceException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidServiceType.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidServiceType.java new file mode 100644 index 0000000000..36936acbfb --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/InvalidServiceType.java @@ -0,0 +1,44 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when a service type specified by an {@link org.osoa.sca.annotations.Service} annotation is invalid, e.g. it is + * not an interface + * + * @version $Rev$ $Date$ + */ +public class InvalidServiceType extends ProcessingException { + public InvalidServiceType() { + } + + public InvalidServiceType(String message) { + super(message); + } + + public InvalidServiceType(String message, Throwable cause) { + super(message, cause); + } + + public InvalidServiceType(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/MonitorProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/MonitorProcessor.java new file mode 100644 index 0000000000..e7acce2973 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/MonitorProcessor.java @@ -0,0 +1,57 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.implementation.java.AbstractPropertyProcessor; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; + +import org.apache.tuscany.api.annotation.Monitor; +import org.apache.tuscany.core.injection.SingletonObjectFactory; +import org.apache.tuscany.host.MonitorFactory; + +/** + * Processes an {@link @Monitor} annotation, updating the component type with corresponding {@link + * org.apache.tuscany.spi.implementation.java.JavaMappedProperty} + * + * @version $Rev$ $Date$ + */ +public class MonitorProcessor extends AbstractPropertyProcessor<Monitor> { + private MonitorFactory monitorFactory; + + public MonitorProcessor(@Autowire MonitorFactory monitorFactory, @Autowire ImplementationProcessorService service) { + super(Monitor.class, service); + this.monitorFactory = monitorFactory; + } + + protected String getName(Monitor annotation) { + return null; + } + + protected <T> void initProperty(JavaMappedProperty<T> property, + Monitor annotation, + CompositeComponent parent, + DeploymentContext context) { + Class<T> javaType = property.getJavaType(); + property.setDefaultValueFactory(new SingletonObjectFactory<T>(monitorFactory.getMonitor(javaType))); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/NoConstructorException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/NoConstructorException.java new file mode 100644 index 0000000000..78a9c6b503 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/NoConstructorException.java @@ -0,0 +1,44 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when a suitable constructor for a component implementation cannot be found + * + * @version $Rev$ $Date$ + */ +public class NoConstructorException extends ProcessingException { + + public NoConstructorException() { + } + + public NoConstructorException(String message) { + super(message); + } + + public NoConstructorException(String message, Throwable cause) { + super(message, cause); + } + + public NoConstructorException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/PropertyProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/PropertyProcessor.java new file mode 100644 index 0000000000..09b3c7383d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/PropertyProcessor.java @@ -0,0 +1,78 @@ +/* + * 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.core.implementation.processor; + +import java.lang.reflect.Constructor; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.databinding.extension.SimpleTypeMapperExtension; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.TypeInfo; +import org.apache.tuscany.spi.implementation.java.AbstractPropertyProcessor; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.model.OverrideOptions; +import org.osoa.sca.annotations.Property; + +/** + * Processes an {@link @Property} annotation, updating the component type with corresponding {@link JavaMappedProperty} + * + * @version $Rev$ $Date$ + */ +public class PropertyProcessor extends AbstractPropertyProcessor<Property> { + private SimpleTypeMapperExtension typeMapper = new SimpleTypeMapperExtension(); + + public PropertyProcessor(@Autowire ImplementationProcessorService service) { + super(Property.class, service); + } + + protected String getName(Property annotation) { + return annotation.name(); + } + + protected <T> void initProperty(JavaMappedProperty<T> property, + Property annotation, + CompositeComponent parent, + DeploymentContext context) { + property.setOverride(OverrideOptions.valueOf(annotation.override().toUpperCase())); + String xmlType = annotation.xmlType(); + if (xmlType != null && xmlType.length() != 0) { + property.setXmlType(QName.valueOf(annotation.xmlType())); + } else { + TypeInfo type = typeMapper.getXMLType(property.getJavaType()); + if (type != null) { + property.setXmlType(type.getQName()); + } + } + } + + public <T> void visitConstructor(CompositeComponent parent, Constructor<T> constructor, + PojoComponentType<JavaMappedService, JavaMappedReference, + JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + // override since heuristic pojo processor evalautes properties + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java new file mode 100644 index 0000000000..d96f31023b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ReferenceProcessor.java @@ -0,0 +1,155 @@ +/* + * 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.core.implementation.processor; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.osoa.sca.annotations.Reference; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.model.ServiceContract; + +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName; + +/** + * Processes an {@link @Reference} annotation, updating the component type with corresponding {@link + * org.apache.tuscany.spi.implementation.java.JavaMappedReference} + * + * @version $Rev$ $Date$ + */ +public class ReferenceProcessor extends ImplementationProcessorExtension { + + private JavaInterfaceProcessorRegistry regsitry; + + public ReferenceProcessor(@Autowire JavaInterfaceProcessorRegistry registry) { + this.regsitry = registry; + } + + public void visitMethod(CompositeComponent parent, Method method, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) + throws ProcessingException { + Reference annotation = method.getAnnotation(Reference.class); + Autowire autowire = method.getAnnotation(Autowire.class); + boolean isAutowire = autowire != null; + if (annotation == null && !isAutowire) { + return; //Not a reference or autowire annotation. + } + if (method.getParameterTypes().length != 1) { + IllegalReferenceException e = new IllegalReferenceException("Setter must have one parameter"); + e.setIdentifier(method.toString()); + throw e; + } + //process autowire required first let reference override. or if conflicting should this fault? + boolean required = false; + if (isAutowire) { + required = autowire.required(); + } + + String name = null; + + if (annotation != null) { + if (annotation.name() != null && annotation.name().length() > 0) { + name = annotation.name(); + } + required = annotation.required(); + } + if (name == null) { + name = method.getName(); + if (method.getName().startsWith("set")) { + name = toPropertyName(method.getName()); + } + } + if (type.getReferences().get(name) != null) { + throw new DuplicateReferenceException(name); + } + + JavaMappedReference reference = new JavaMappedReference(); + reference.setMember(method); + reference.setAutowire(isAutowire); + reference.setRequired(required); + reference.setName(name); + ServiceContract contract; + try { + contract = regsitry.introspect(method.getParameterTypes()[0]); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + reference.setServiceContract(contract); + type.getReferences().put(name, reference); + } + + public void visitField(CompositeComponent parent, Field field, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + Reference annotation = field.getAnnotation(Reference.class); + boolean autowire = field.getAnnotation(Autowire.class) != null; + if (annotation == null && !autowire) { + return; + } + String name = field.getName(); + boolean required = false; + if (annotation != null) { + if (annotation.name() != null) { + name = annotation.name(); + } + required = annotation.required(); + } + if (name.length() == 0) { + name = field.getName(); + } + if (type.getReferences().get(name) != null) { + throw new DuplicateReferenceException(name); + } + JavaMappedReference reference = new JavaMappedReference(); + reference.setMember(field); + reference.setRequired(required); + reference.setAutowire(autowire); + reference.setName(name); + ServiceContract contract; + try { + contract = regsitry.introspect(field.getType()); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + reference.setServiceContract(contract); + type.getReferences().put(name, reference); + } + + public <T> void visitConstructor(CompositeComponent parent, Constructor<T> constructor, + PojoComponentType<JavaMappedService, JavaMappedReference, + JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ScopeProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ScopeProcessor.java new file mode 100644 index 0000000000..3827a82652 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ScopeProcessor.java @@ -0,0 +1,63 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.model.Scope; + +/** + * Processes the {@link Scope} annotation and updates the component type with the corresponding implmentation scope + * + * @version $Rev$ $Date$ + */ +public class ScopeProcessor extends ImplementationProcessorExtension { + + public <T> void visitClass(CompositeComponent parent, Class<T> clazz, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) + throws ProcessingException { + org.osoa.sca.annotations.Scope annotation = clazz.getAnnotation(org.osoa.sca.annotations.Scope.class); + if (annotation == null) { + type.setImplementationScope(Scope.STATELESS); + return; + } + //FIXME deal with eager init + String name = annotation.value(); + Scope scope; + if ("MODULE".equals(name)) { + scope = Scope.MODULE; + } else if ("SESSION".equals(name)) { + scope = Scope.SESSION; + } else if ("REQUEST".equals(name)) { + scope = Scope.REQUEST; + } else if ("COMPOSITE".equals(name)) { + scope = Scope.COMPOSITE; + } else { + scope = new Scope(name); + } + type.setImplementationScope(scope); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceProcessor.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceProcessor.java new file mode 100644 index 0000000000..5e4e561228 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceProcessor.java @@ -0,0 +1,161 @@ +/* + * 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.core.implementation.processor; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Set; + +import org.osoa.sca.annotations.Callback; +import org.osoa.sca.annotations.Remotable; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.idl.InvalidServiceContractException; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.model.ServiceContract; + +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllInterfaces; +import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName; + +/** + * Processes an {@link org.osoa.sca.annotations.Service} annotation and updates the component type with corresponding + * {@link JavaMappedService}s. Also processes related {@link org.osoa.sca.annotations.Callback} annotations. + * + * @version $Rev$ $Date$ + */ +public class ServiceProcessor extends ImplementationProcessorExtension { + + private ImplementationProcessorService implService; + + public ServiceProcessor(@Autowire ImplementationProcessorService implService) { + this.implService = implService; + } + + public <T> void visitClass(CompositeComponent parent, Class<T> clazz, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + org.osoa.sca.annotations.Service annotation = clazz.getAnnotation(org.osoa.sca.annotations.Service.class); + if (annotation == null) { + // scan intefaces for remotable + Set<Class> interfaces = getAllInterfaces(clazz); + for (Class<?> interfaze : interfaces) { + if (interfaze.getAnnotation(Remotable.class) != null) { + JavaMappedService service; + try { + service = implService.createService(interfaze); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + type.getServices().put(service.getName(), service); + } + } + return; + } + Class<?>[] interfaces = annotation.interfaces(); + if (interfaces.length == 0) { + Class<?> interfaze = annotation.value(); + if (Void.class.equals(interfaze)) { + throw new IllegalServiceDefinitionException("No interfaces specified"); + } else { + interfaces = new Class<?>[1]; + interfaces[0] = interfaze; + } + } + for (Class<?> interfaze : interfaces) { + if (!interfaze.isInterface()) { + InvalidServiceType e = new InvalidServiceType("Service must be an interface"); + e.setIdentifier(interfaze.getName()); + throw e; + } + JavaMappedService service; + try { + service = implService.createService(interfaze); + } catch (InvalidServiceContractException e) { + throw new ProcessingException(e); + } + type.getServices().put(service.getName(), service); + } + } + + + public void visitMethod(CompositeComponent parent, + Method method, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + + Callback annotation = method.getAnnotation(Callback.class); + if (annotation == null) { + return; + } + if (method.getParameterTypes().length != 1) { + IllegalCallbackReferenceException e = + new IllegalCallbackReferenceException("Setter must have one parameter"); + e.setIdentifier(method.toString()); + throw e; + } + String name = toPropertyName(method.getName()); + JavaMappedService callbackService = null; + Class<?> callbackClass = method.getParameterTypes()[0]; + for (JavaMappedService service : type.getServices().values()) { + ServiceContract serviceContract = service.getServiceContract(); + if (serviceContract.getCallbackClass().equals(callbackClass)) { + callbackService = service; + } + } + if (callbackService == null) { + throw new IllegalCallbackReferenceException("Callback type does not match a service callback interface"); + } + callbackService.setCallbackReferenceName(name); + callbackService.setCallbackMember(method); + } + + public void visitField(CompositeComponent parent, Field field, + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type, + DeploymentContext context) throws ProcessingException { + + Callback annotation = field.getAnnotation(Callback.class); + if (annotation == null) { + return; + } + String name = field.getName(); + JavaMappedService callbacksService = null; + Class<?> callbackClass = field.getType(); + for (JavaMappedService service : type.getServices().values()) { + ServiceContract serviceContract = service.getServiceContract(); + if (serviceContract.getCallbackClass().equals(callbackClass)) { + callbacksService = service; + } + } + if (callbacksService == null) { + throw new IllegalCallbackReferenceException("Callback type does not match a service callback interface"); + } + callbacksService.setCallbackReferenceName(name); + callbacksService.setCallbackMember(field); + } + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceTypeNotFoundException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceTypeNotFoundException.java new file mode 100644 index 0000000000..a84e8f3850 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/ServiceTypeNotFoundException.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.processor; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * Thrown when a service interface cannot be determined based on a heuristic evaluation of an implementation + * + * @version $Rev$ $Date$ + */ +public class ServiceTypeNotFoundException extends ProcessingException { + public ServiceTypeNotFoundException() { + } + + public ServiceTypeNotFoundException(String message) { + super(message); + } + + public ServiceTypeNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public ServiceTypeNotFoundException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnknownContextTypeException.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnknownContextTypeException.java new file mode 100644 index 0000000000..875f10a4fd --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnknownContextTypeException.java @@ -0,0 +1,41 @@ +/* + * 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.core.implementation.processor; + +/** + * Thrown when a method or field marked with {@link org.osoa.sca.annotations.Context} takes an unknown type + * + * @version $Rev$ $Date$ + */ +public class UnknownContextTypeException extends IllegalContextException { + public UnknownContextTypeException() { + } + + public UnknownContextTypeException(String message) { + super(message); + } + + public UnknownContextTypeException(String message, Throwable cause) { + super(message, cause); + } + + public UnknownContextTypeException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemBindingBuilder.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemBindingBuilder.java new file mode 100644 index 0000000000..059ae0e768 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemBindingBuilder.java @@ -0,0 +1,85 @@ +/* + * 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.core.implementation.system.builder; + +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.builder.BindingBuilder; +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; +import org.apache.tuscany.spi.wire.InboundWire; + +import org.apache.tuscany.core.implementation.system.component.SystemReference; +import org.apache.tuscany.core.implementation.system.component.SystemReferenceImpl; +import org.apache.tuscany.core.implementation.system.component.SystemService; +import org.apache.tuscany.core.implementation.system.component.SystemServiceImpl; +import org.apache.tuscany.core.implementation.system.model.SystemBinding; +import org.apache.tuscany.core.implementation.system.wire.SystemInboundWire; +import org.apache.tuscany.core.implementation.system.wire.SystemInboundWireImpl; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundAutowire; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWire; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWireImpl; + +/** + * Creates {@link SystemService}s and {@link org.apache.tuscany.core.implementation.system.component.SystemReference}s + * by evaluating an assembly definition + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemBindingBuilder implements BindingBuilder<SystemBinding> { + + public SystemService build(CompositeComponent parent, + BoundServiceDefinition<SystemBinding> boundServiceDefinition, + DeploymentContext deploymentContext) { + Class<?> interfaze = boundServiceDefinition.getServiceContract().getInterfaceClass(); + QualifiedName targetName = new QualifiedName(boundServiceDefinition.getTarget().getPath()); + Component target = (Component) parent.getSystemChild(targetName.getPartName()); + if (target == null) { + throw new BuilderConfigException("Target not found: [" + targetName + ']'); + } + String name = boundServiceDefinition.getName(); + InboundWire inboundWire = + new SystemInboundWireImpl(name, interfaze, target); + SystemOutboundWire outboundWire = + new SystemOutboundWireImpl(name, targetName, interfaze); + SystemService service = new SystemServiceImpl(boundServiceDefinition.getName(), parent); + service.setInboundWire(inboundWire); + service.setOutboundWire(outboundWire); + return service; + } + + public SystemReference build(CompositeComponent parent, + BoundReferenceDefinition<SystemBinding> boundReferenceDefinition, + DeploymentContext deploymentContext) { + CompositeComponent autowireComponent = parent.getParent(); + Class<?> interfaze = boundReferenceDefinition.getServiceContract().getInterfaceClass(); + String name = boundReferenceDefinition.getName(); + SystemReferenceImpl reference = new SystemReferenceImpl(name, interfaze, parent); + SystemInboundWire inboundWire = new SystemInboundWireImpl(name, interfaze); + String refName = boundReferenceDefinition.getName(); + boolean required = boundReferenceDefinition.isRequired(); + SystemOutboundWire outboundWire = new SystemOutboundAutowire(refName, interfaze, autowireComponent, required); + reference.setInboundWire(inboundWire); + reference.setOutboundWire(outboundWire); + return reference; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilder.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilder.java new file mode 100644 index 0000000000..ceff57f9f9 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemComponentBuilder.java @@ -0,0 +1,174 @@ +/* + * 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.core.implementation.system.builder; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Map; + +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentBuilderExtension; +import org.apache.tuscany.spi.implementation.java.ConstructorDefinition; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.PropertyValue; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.ReferenceTarget; +import org.apache.tuscany.spi.model.ServiceDefinition; +import org.apache.tuscany.spi.wire.OutboundWire; + +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.implementation.system.model.SystemImplementation; +import org.apache.tuscany.core.implementation.system.wire.SystemInboundWire; +import org.apache.tuscany.core.implementation.system.wire.SystemInboundWireImpl; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundAutowire; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWireImpl; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * Produces system atomic components from a component definition + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemComponentBuilder extends ComponentBuilderExtension<SystemImplementation> { + + protected Class<SystemImplementation> getImplementationType() { + return SystemImplementation.class; + } + + @SuppressWarnings("unchecked") + public AtomicComponent build(CompositeComponent parent, + ComponentDefinition<SystemImplementation> definition, + DeploymentContext deploymentContext) throws BuilderConfigException { + PojoComponentType<ServiceDefinition, JavaMappedReference, JavaMappedProperty<?>> componentType = + definition.getImplementation().getComponentType(); + + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setParent(parent); + configuration.setScopeContainer(deploymentContext.getModuleScope()); + if (definition.getInitLevel() != null) { + configuration.setInitLevel(definition.getInitLevel()); + } else { + configuration.setInitLevel(componentType.getInitLevel()); + } + Method initMethod = componentType.getInitMethod(); + if (initMethod != null) { + configuration.setInitInvoker(new MethodEventInvoker<Object>(initMethod)); + } + Method destroyMethod = componentType.getDestroyMethod(); + if (destroyMethod != null) { + configuration.setDestroyInvoker(new MethodEventInvoker<Object>(destroyMethod)); + } + for (ServiceDefinition serviceDefinition : componentType.getServices().values()) { + configuration.addServiceInterface(serviceDefinition.getServiceContract().getInterfaceClass()); + } + // setup property injection sites + for (JavaMappedProperty<?> property : componentType.getProperties().values()) { + configuration.addPropertySite(property.getName(), property.getMember()); + } + // setup reference injection sites + for (JavaMappedReference reference : componentType.getReferences().values()) { + Member member = reference.getMember(); + if (member != null) { + // could be null if the reference is mapped to a constructor + configuration.addReferenceSite(reference.getName(), member); + } + } + // setup constructor injection + ConstructorDefinition<?> ctorDef = componentType.getConstructorDefinition(); + Constructor<?> constr = ctorDef.getConstructor(); + PojoObjectFactory<?> instanceFactory = new PojoObjectFactory(constr); + configuration.setInstanceFactory(instanceFactory); + configuration.getConstructorParamNames().addAll(ctorDef.getInjectionNames()); + SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(definition.getName(), configuration); + // handle properties + Map<String, PropertyValue<?>> propertyValues = definition.getPropertyValues(); + processProperties(propertyValues, componentType.getProperties().values(), component); + // handle inbound wires + for (ServiceDefinition serviceDefinition : componentType.getServices().values()) { + Class<?> interfaze = serviceDefinition.getServiceContract().getInterfaceClass(); + String name = serviceDefinition.getName(); + SystemInboundWire wire = new SystemInboundWireImpl(name, interfaze, component); + component.addInboundWire(wire); + } + // handle references + processReferences(definition, componentType.getReferences(), parent, component); + // FIXME we need a way to build configuration references from autowires in the loader to eliminate this eval + for (ReferenceDefinition reference : componentType.getReferences().values()) { + if (reference.isAutowire()) { + Class interfaze = reference.getServiceContract().getInterfaceClass(); + OutboundWire wire = + new SystemOutboundAutowire(reference.getName(), interfaze, parent, reference.isRequired()); + component.addOutboundWire(wire); + } + } + return component; + } + + private void processReferences(ComponentDefinition<SystemImplementation> definition, + Map<String, JavaMappedReference> references, + CompositeComponent parent, + SystemAtomicComponentImpl component) { + // no proxies needed for system components + for (ReferenceTarget target : definition.getReferenceTargets().values()) { + String referenceName = target.getReferenceName(); + JavaMappedReference referenceDefiniton = references.get(referenceName); + Class interfaze = referenceDefiniton.getServiceContract().getInterfaceClass(); + OutboundWire wire; + if (referenceDefiniton.isAutowire()) { + boolean required = referenceDefiniton.isRequired(); + wire = new SystemOutboundAutowire(referenceName, interfaze, parent, required); + } else { + //FIXME support multiplicity! + assert target.getTargets().size() == 1 : "Multiplicity not yet implemented"; + QualifiedName targetName = new QualifiedName(target.getTargets().get(0).getPath()); + wire = new SystemOutboundWireImpl(referenceName, targetName, interfaze); + } + component.addOutboundWire(wire); + } + } + + private void processProperties(Map<String, PropertyValue<?>> propertyValues, + Collection<JavaMappedProperty<?>> properties, + SystemAtomicComponentImpl component) { + for (JavaMappedProperty<?> property : properties) { + PropertyValue value = propertyValues.get(property.getName()); + ObjectFactory<?> factory; + if (value != null) { + factory = value.getValueFactory(); + } else { + factory = property.getDefaultValueFactory(); + } + if (factory != null) { + component.addPropertyFactory(property.getName(), factory); + } + } + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemCompositeBuilder.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemCompositeBuilder.java new file mode 100644 index 0000000000..77fc5bf17c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/builder/SystemCompositeBuilder.java @@ -0,0 +1,108 @@ +/* + * 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.core.implementation.system.builder; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.builder.BuilderRegistry; +import org.apache.tuscany.spi.builder.Connector; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentBuilderExtension; +import org.apache.tuscany.spi.model.Binding; +import org.apache.tuscany.spi.model.BoundServiceDefinition; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.CompositeComponentType; +import org.apache.tuscany.spi.model.Implementation; +import org.apache.tuscany.spi.model.ServiceDefinition; + +import org.apache.tuscany.core.implementation.composite.CompositeComponentImpl; +import org.apache.tuscany.core.implementation.system.model.SystemCompositeImplementation; + +/** + * Produces system composite components by evaluating an assembly. + * + * @version $Rev$ $Date$ + */ +public class SystemCompositeBuilder extends ComponentBuilderExtension<SystemCompositeImplementation> { + public SystemCompositeBuilder() { + } + + public SystemCompositeBuilder(BuilderRegistry builderRegistry, Connector connector) { + this.builderRegistry = builderRegistry; + this.connector = connector; + } + + protected Class<SystemCompositeImplementation> getImplementationType() { + return SystemCompositeImplementation.class; + } + + public Component build(CompositeComponent parent, + ComponentDefinition<SystemCompositeImplementation> componentDefinition, + DeploymentContext deploymentContext) throws BuilderConfigException { + SystemCompositeImplementation impl = componentDefinition.getImplementation(); + CompositeComponentType<?, ?, ?> componentType = impl.getComponentType(); + + // create lists of all components and services in this composite + List<ComponentDefinition<? extends Implementation<?>>> allComponents = + new ArrayList<ComponentDefinition<? extends Implementation<?>>>(); + allComponents.addAll(componentType.getComponents().values()); + + List<BoundServiceDefinition<? extends Binding>> allBoundServices = + new ArrayList<BoundServiceDefinition<? extends Binding>>(); + for (ServiceDefinition serviceDefinition : componentType.getServices().values()) { + if (serviceDefinition instanceof BoundServiceDefinition) { + BoundServiceDefinition<? extends Binding> boundService = + (BoundServiceDefinition<? extends Binding>) serviceDefinition; + allBoundServices.add(boundService); + } + } + + /* + // add in components and services from included composites + for (Include include : componentType.getIncludes().values()) { + CompositeComponentType<?, ?, ?> included = include.getIncluded(); + allComponents.addAll(included.getComponents().values()); + for (ServiceDefinition serviceDefinition : included.getServices().values()) { + if (serviceDefinition instanceof BoundServiceDefinition) { + BoundServiceDefinition<? extends Binding> boundService = + (BoundServiceDefinition<? extends Binding>) serviceDefinition; + allBoundServices.add(boundService); + } + } + } + */ + + // create the composite component + String name = componentDefinition.getName(); + CompositeComponent component = new CompositeComponentImpl(name, parent, connector, null); + for (ComponentDefinition<? extends Implementation> childComponentDefinition : allComponents) { + component.register(builderRegistry.build(component, childComponentDefinition, deploymentContext)); + } + + for (BoundServiceDefinition<? extends Binding> serviceDefinition : allBoundServices) { + component.register(builderRegistry.build(component, serviceDefinition, deploymentContext)); + } + return component; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemAtomicComponentImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemAtomicComponentImpl.java new file mode 100644 index 0000000000..cc9926280c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemAtomicComponentImpl.java @@ -0,0 +1,66 @@ +/* + * 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.core.implementation.system.component; + +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.SystemAtomicComponent; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.wire.RuntimeWire; +import org.apache.tuscany.spi.wire.TargetInvoker; + +import org.apache.tuscany.core.implementation.PojoAtomicComponent; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWire; + +/** + * Default implementation of a system atomic context + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemAtomicComponentImpl extends PojoAtomicComponent implements SystemAtomicComponent { + + public SystemAtomicComponentImpl(String name, PojoConfiguration configuration) { + super(name, configuration); + scope = Scope.MODULE; + } + + public Object getServiceInstance(String name) throws TargetException { + return getTargetInstance(); + } + + public Object getServiceInstance() throws TargetException { + return getTargetInstance(); + } + + public TargetInvoker createTargetInvoker(String targetName, Operation operation) { + return null; + } + + public boolean isSystem() { + return true; + } + + protected ObjectFactory<?> createWireFactory(RuntimeWire wire) { + assert wire instanceof SystemOutboundWire : "Wire must be an instance of " + SystemOutboundWire.class.getName(); + SystemOutboundWire systemWire = (SystemOutboundWire) wire; + return new SystemWireObjectFactory(systemWire); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemReference.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemReference.java new file mode 100644 index 0000000000..5c15edb957 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemReference.java @@ -0,0 +1,30 @@ +/* + * 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.core.implementation.system.component; + +import org.apache.tuscany.spi.component.Reference; + +/** + * A marker for SCA references configured with the + * {@link org.apache.tuscany.core.implementation.system.model.SystemBinding} + * + * @version $$Rev$$ $$Date$$ + */ +public interface SystemReference extends Reference { +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemReferenceImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemReferenceImpl.java new file mode 100644 index 0000000000..a9a23b028c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemReferenceImpl.java @@ -0,0 +1,115 @@ +/* + * 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.core.implementation.system.component; + +import org.apache.tuscany.spi.component.AbstractSCAObject; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireInvocationHandler; + +import org.apache.tuscany.core.implementation.system.wire.SystemInboundWire; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWire; + +/** + * Default implementation of a reference configured with the + * {@link org.apache.tuscany.core.implementation.system.model.SystemBinding} + * + * @version $Rev$ $Date$ + */ +public class SystemReferenceImpl extends AbstractSCAObject implements SystemReference { + + protected SystemInboundWire inboundWire; + protected SystemOutboundWire outboundWire; + protected Class<?> referenceInterface; + + + public SystemReferenceImpl(String name, Class<?> referenceInterface, CompositeComponent parent) { + super(name, parent); + assert referenceInterface != null : "Reference interface was null"; + this.referenceInterface = referenceInterface; + } + + public Scope getScope() { + return Scope.COMPOSITE; + } + + public void setInboundWire(InboundWire wire) { + assert wire instanceof SystemInboundWire : "Wire must be a " + SystemInboundWire.class.getName(); + this.inboundWire = (SystemInboundWire) wire; + } + + public InboundWire getInboundWire() { + return inboundWire; + } + + public OutboundWire getOutboundWire() { + return outboundWire; + } + + public void setOutboundWire(OutboundWire wire) { + assert wire instanceof SystemOutboundWire : "Wire must be a " + SystemOutboundWire.class.getName(); + this.outboundWire = (SystemOutboundWire) wire; + } + + public Class<?> getInterface() { + return referenceInterface; + } + + public void setInterface(Class<?> referenceInterface) { + this.referenceInterface = referenceInterface; + } + + public Object getServiceInstance() throws TargetException { + return referenceInterface.cast(inboundWire.getTargetService()); + } + + public WireInvocationHandler getHandler() throws TargetException { + throw new UnsupportedOperationException(); + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + throw new UnsupportedOperationException(); + } + + public TargetInvoker createCallbackTargetInvoker(ServiceContract contract, Operation operation) { + throw new UnsupportedOperationException(); + } + + public TargetInvoker createAsyncTargetInvoker(OutboundWire wire, Operation operation) { + throw new UnsupportedOperationException(); + } + + public ServiceContract<?> getBindingServiceContract() { + throw new UnsupportedOperationException(); + } + + public void setBindingServiceContract(ServiceContract<?> serviceContract) { + throw new UnsupportedOperationException(); + } + + public boolean isSystem() { + return true; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemService.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemService.java new file mode 100644 index 0000000000..95999c5045 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemService.java @@ -0,0 +1,31 @@ +/* + * 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.core.implementation.system.component; + +import org.apache.tuscany.spi.component.Service; + +/** + * Marker for services configured with the {@link org.apache.tuscany.core.implementation.system.model.SystemBinding} + * + * @version $$Rev$$ $$Date$$ + */ +public interface SystemService extends Service { + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemServiceImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemServiceImpl.java new file mode 100644 index 0000000000..1a8cd490ae --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemServiceImpl.java @@ -0,0 +1,102 @@ +/* + * 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.core.implementation.system.component; + +import org.apache.tuscany.spi.CoreRuntimeException; +import org.apache.tuscany.spi.component.AbstractSCAObject; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireInvocationHandler; + +import org.apache.tuscany.core.implementation.system.wire.SystemInboundWire; +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWire; + +/** + * Default implementation for services configured with the + * {@link org.apache.tuscany.core.implementation.system.model.SystemBinding} + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemServiceImpl extends AbstractSCAObject implements SystemService { + + protected SystemInboundWire inboundWire; + protected SystemOutboundWire outboundWire; + + public SystemServiceImpl(String name, CompositeComponent parent) throws CoreRuntimeException { + super(name, parent); + } + + public Scope getScope() { + return Scope.COMPOSITE; + } + + public InboundWire getInboundWire() { + return inboundWire; + } + + public void setInboundWire(InboundWire wire) { + assert wire instanceof SystemInboundWire : "wire must be a " + SystemInboundWire.class.getName(); + this.inboundWire = (SystemInboundWire) wire; + } + + public OutboundWire getOutboundWire() { + return outboundWire; + } + + public void setOutboundWire(OutboundWire wire) { + assert wire instanceof SystemOutboundWire : "wire must be a " + SystemOutboundWire.class.getName(); + this.outboundWire = (SystemOutboundWire) wire; + } + + public Class<?> getInterface() { + return inboundWire.getServiceContract().getInterfaceClass(); + } + + public WireInvocationHandler getHandler() { + // system services do not proxy + throw new UnsupportedOperationException(); + } + + public Object getServiceInstance() throws TargetException { + return inboundWire.getTargetService(); + } + + + public TargetInvoker createCallbackTargetInvoker(ServiceContract contract, Operation operation) { + throw new UnsupportedOperationException(); + } + + public ServiceContract<?> getBindingServiceContract() { + throw new UnsupportedOperationException(); + } + + public void setBindingServiceContract(ServiceContract<?> serviceContract) { + throw new UnsupportedOperationException(); + } + + public boolean isSystem() { + return true; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemSingletonAtomicComponent.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemSingletonAtomicComponent.java new file mode 100644 index 0000000000..a40cc9785c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemSingletonAtomicComponent.java @@ -0,0 +1,131 @@ +/* + * 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.core.implementation.system.component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.component.AbstractSCAObject; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.SystemAtomicComponent; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; + +/** + * An {@link org.apache.tuscany.spi.component.AtomicComponent} used when registering objects directly into a composite + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemSingletonAtomicComponent<S, T extends S> extends AbstractSCAObject + implements SystemAtomicComponent { + + private T instance; + private List<Class<?>> serviceInterfaces; + + public SystemSingletonAtomicComponent(String name, CompositeComponent parent, Class<S> interfaze, T instance) { + super(name, parent); + this.instance = instance; + serviceInterfaces = new ArrayList<Class<?>>(1); + serviceInterfaces.add(interfaze); + } + + public List<Class<?>> getServiceInterfaces() { + return serviceInterfaces; + } + + public Scope getScope() { + return Scope.MODULE; + } + + public boolean isEagerInit() { + return false; + } + + public int getInitLevel() { + return 0; + } + + public T getTargetInstance() throws TargetException { + return instance; + } + + public Object getServiceInstance(String name) throws TargetException { + return getTargetInstance(); + } + + public Object getServiceInstance() throws TargetException { + return getTargetInstance(); + } + + public void init(Object instance) throws TargetException { + + } + + public void destroy(Object instance) throws TargetException { + + } + + public Object createInstance() throws ObjectCreationException { + throw new UnsupportedOperationException(); + } + + public void addInboundWire(InboundWire wire) { + throw new UnsupportedOperationException(); + } + + public Map<String, InboundWire> getInboundWires() { + return Collections.emptyMap(); + } + + public InboundWire getInboundWire(String serviceName) { + return null; + } + + public void addOutboundWire(OutboundWire wire) { + throw new UnsupportedOperationException(); + } + + public void addOutboundWires(Class<?> multiplicityClass, List<OutboundWire> wires) { + throw new UnsupportedOperationException(); + } + + public Map<String, List<OutboundWire>> getOutboundWires() { + return Collections.emptyMap(); + } + + + public TargetInvoker createTargetInvoker(String targetName, Operation operation) { + return null; + } + + public TargetInvoker createAsyncTargetInvoker(InboundWire wire, Operation operation) { + return null; + } + + public boolean isSystem() { + return true; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemWireObjectFactory.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemWireObjectFactory.java new file mode 100644 index 0000000000..2399bee076 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemWireObjectFactory.java @@ -0,0 +1,43 @@ +/* + * 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.core.implementation.system.component; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; + +import org.apache.tuscany.core.implementation.system.wire.SystemOutboundWire; + +/** + * Uses a system wire to return an object instance + * + * @version $Rev$ $Date$ + */ +public class SystemWireObjectFactory implements ObjectFactory { + + private SystemOutboundWire wire; + + public SystemWireObjectFactory(SystemOutboundWire factory) { + this.wire = factory; + } + + public Object getInstance() throws ObjectCreationException { + return wire.getTargetService(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemBindingLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemBindingLoader.java new file mode 100644 index 0000000000..4db167a852 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemBindingLoader.java @@ -0,0 +1,59 @@ +/* + * 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.core.implementation.system.loader; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.core.implementation.system.model.SystemBinding; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.annotation.Autowire; + +/** + * Loads a system binding specified in an XML-based assembly + * + * @version $Rev$ $Date$ + */ +public class SystemBindingLoader extends LoaderExtension<SystemBinding> { + public static final QName SYSTEM_BINDING = + new QName("http://incubator.apache.org/tuscany/xmlns/system/1.0-incubator-M2", "binding.system"); + + @Constructor({"registry"}) + public SystemBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return SYSTEM_BINDING; + } + + public SystemBinding load(CompositeComponent parent, XMLStreamReader reader, DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + LoaderUtil.skipToEndElement(reader); + return new SystemBinding(); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemComponentTypeLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemComponentTypeLoader.java new file mode 100644 index 0000000000..90165c21c0 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemComponentTypeLoader.java @@ -0,0 +1,97 @@ +/* + * 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.core.implementation.system.loader; + +import java.net.URL; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension; +import org.apache.tuscany.spi.implementation.java.IntrospectionRegistry; +import org.apache.tuscany.spi.implementation.java.Introspector; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.JavaMappedReference; +import org.apache.tuscany.spi.implementation.java.JavaMappedService; +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; + +import org.apache.tuscany.core.implementation.system.model.SystemImplementation; +import org.apache.tuscany.core.util.JavaIntrospectionHelper; + +/** + * Loads a system component type + * + * @version $Rev$ $Date$ + */ +public class SystemComponentTypeLoader extends ComponentTypeLoaderExtension<SystemImplementation> { + private Introspector introspector; + + public SystemComponentTypeLoader() { + } + + public SystemComponentTypeLoader(Introspector introspector) { + this.introspector = introspector; + } + + public SystemComponentTypeLoader(LoaderRegistry loaderRegistry, Introspector introspector) { + super(loaderRegistry); + this.introspector = introspector; + } + + //FIXME autowire to support multiple interfaces + @Autowire + public void setIntrospector(IntrospectionRegistry introspector) { + this.introspector = introspector; + } + + public void load(CompositeComponent parent, SystemImplementation implementation, + DeploymentContext deploymentContext) throws LoaderException { + Class<?> implClass = implementation.getImplementationClass(); + URL sidefile = implClass.getResource(JavaIntrospectionHelper.getBaseName(implClass) + ".componentType"); + PojoComponentType componentType; + if (sidefile == null) { + componentType = loadByIntrospection(parent, implementation, deploymentContext); + } else { + componentType = loadFromSidefile(sidefile, deploymentContext); + } + implementation.setComponentType(componentType); + } + + protected Class<SystemImplementation> getImplementationClass() { + return SystemImplementation.class; + } + + protected PojoComponentType loadByIntrospection(CompositeComponent parent, + SystemImplementation implementation, + DeploymentContext deploymentContext) throws ProcessingException { + PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> componentType = + new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>(); + Class<?> implClass = implementation.getImplementationClass(); + introspector.introspect(parent, implClass, componentType, deploymentContext); + return componentType; + } + + + protected PojoComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext) throws LoaderException { + return loaderRegistry.load(null, url, PojoComponentType.class, deploymentContext); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemCompositeComponentTypeLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemCompositeComponentTypeLoader.java new file mode 100644 index 0000000000..74ce87673e --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemCompositeComponentTypeLoader.java @@ -0,0 +1,70 @@ +/* + * 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.core.implementation.system.loader; + +import java.net.URL; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.CompositeComponentType; + +import org.apache.tuscany.core.deployer.ChildDeploymentContext; +import org.apache.tuscany.core.implementation.system.model.SystemCompositeImplementation; + +/** + * Loads a system composite component type + * + * @version $Rev$ $Date$ + */ +public class SystemCompositeComponentTypeLoader extends ComponentTypeLoaderExtension<SystemCompositeImplementation> { + public SystemCompositeComponentTypeLoader() { + } + + public SystemCompositeComponentTypeLoader(LoaderRegistry loaderRegistry) { + super(loaderRegistry); + } + + protected Class<SystemCompositeImplementation> getImplementationClass() { + return SystemCompositeImplementation.class; + } + + public void load(CompositeComponent parent, SystemCompositeImplementation implementation, + DeploymentContext deploymentContext) + throws LoaderException { + URL scdlLocation = implementation.getScdlLocation(); + if (scdlLocation == null) { + throw new LoaderException("SCDL location not found"); + } + ClassLoader cl = implementation.getClassLoader(); + deploymentContext = new ChildDeploymentContext(deploymentContext, cl, scdlLocation); + CompositeComponentType componentType = loadFromSidefile(parent, scdlLocation, deploymentContext); + implementation.setComponentType(componentType); + } + + + protected CompositeComponentType loadFromSidefile(CompositeComponent parent, + URL url, + DeploymentContext deploymentContext) + throws LoaderException { + return loaderRegistry.load(parent, url, CompositeComponentType.class, deploymentContext); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemImplementationLoader.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemImplementationLoader.java new file mode 100644 index 0000000000..7db92e07ef --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/loader/SystemImplementationLoader.java @@ -0,0 +1,69 @@ +/* + * 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.core.implementation.system.loader; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.annotations.Constructor; + +import org.apache.tuscany.core.implementation.system.model.SystemImplementation; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.annotation.Autowire; + +/** + * Loads information for a system implementation + * + * @version $Rev$ $Date$ + */ +public class SystemImplementationLoader extends LoaderExtension<SystemImplementation> { + public static final QName SYSTEM_IMPLEMENTATION = + new QName("http://incubator.apache.org/tuscany/xmlns/system/1.0-incubator-M2", "implementation.system"); + + @Constructor({"registry"}) + public SystemImplementationLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public SystemImplementation load(CompositeComponent parent, + XMLStreamReader reader, + DeploymentContext deploymentContext + ) + throws XMLStreamException, LoaderException { + assert SYSTEM_IMPLEMENTATION.equals(reader.getName()); + SystemImplementation implementation = new SystemImplementation(); + String implClass = reader.getAttributeValue(null, "class"); + Class<?> implementationClass = LoaderUtil.loadClass(implClass, deploymentContext.getClassLoader()); + implementation.setImplementationClass(implementationClass); + registry.loadComponentType(parent, implementation, deploymentContext); + LoaderUtil.skipToEndElement(reader); + return implementation; + } + + public QName getXMLType() { + return SYSTEM_IMPLEMENTATION; + } + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemBinding.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemBinding.java new file mode 100644 index 0000000000..36a439c61b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemBinding.java @@ -0,0 +1,29 @@ +/* + * 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.core.implementation.system.model; + +import org.apache.tuscany.spi.model.Binding; + +/** + * A by-reference binding used by runtime system services + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemBinding extends Binding { +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemCompositeImplementation.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemCompositeImplementation.java new file mode 100644 index 0000000000..e634609c5b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemCompositeImplementation.java @@ -0,0 +1,57 @@ +/* + * 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.core.implementation.system.model; + +import java.net.URL; + +import org.apache.tuscany.spi.model.CompositeComponentType; +import org.apache.tuscany.spi.model.Implementation; + +/** + * Represents a system composite type + * + * @version $Rev$ $Date$ + */ +public class SystemCompositeImplementation extends Implementation<CompositeComponentType> { + private URL scdlLocation; + private ClassLoader classLoader; + + public SystemCompositeImplementation() { + } + + public SystemCompositeImplementation(CompositeComponentType componentType) { + super(componentType); + } + + public URL getScdlLocation() { + return scdlLocation; + } + + public void setScdlLocation(URL scdlLocation) { + this.scdlLocation = scdlLocation; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemImplementation.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemImplementation.java new file mode 100644 index 0000000000..ce48ed3fe5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemImplementation.java @@ -0,0 +1,51 @@ +/* + * 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.core.implementation.system.model; + +import org.apache.tuscany.spi.implementation.java.PojoComponentType; +import org.apache.tuscany.spi.model.AtomicImplementation; + +/** + * Represents the system composite implementation + * + * @version $Rev$ $Date$ + */ +public class SystemImplementation extends AtomicImplementation<PojoComponentType> { + private Class<?> implementationClass; + + public SystemImplementation(PojoComponentType componentType, Class<?> implementationClass) { + super(componentType); + this.implementationClass = implementationClass; + } + + public SystemImplementation() { + } + + public SystemImplementation(Class<?> implementationClass) { + this.implementationClass = implementationClass; + } + + public Class<?> getImplementationClass() { + return implementationClass; + } + + public void setImplementationClass(Class<?> implementationClass) { + this.implementationClass = implementationClass; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundWire.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundWire.java new file mode 100644 index 0000000000..188a813800 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundWire.java @@ -0,0 +1,32 @@ +/* + * 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.core.implementation.system.wire; + +import org.apache.tuscany.spi.wire.InboundWire; + +/** + * Specified by a {@link org.apache.tuscany.core.implementation.system.model.SystemBinding}, a specialized inbound wire + * that returns a direct reference to the target. + * + * @version $$Rev$$ $$Date$$ + */ +public interface SystemInboundWire extends InboundWire { + + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundWireImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundWireImpl.java new file mode 100644 index 0000000000..7938f3ad48 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemInboundWireImpl.java @@ -0,0 +1,159 @@ +/* + * 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.core.implementation.system.wire; + +import java.util.Collections; +import java.util.Map; + +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; +import org.apache.tuscany.spi.wire.OutboundWire; + +/** + * Default implementation of an inbound system wire + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemInboundWireImpl implements SystemInboundWire { + private String serviceName; + private ServiceContract serviceContract; + private Component component; + private SystemOutboundWire wire; + private SCAObject container; + private Class<?> interfaze; + + /** + * Constructs a new inbound wire + * + * @param serviceName the name of the service the inbound wire represents + * @param interfaze the service interface + * @param target the target context the wire is connected to + */ + public SystemInboundWireImpl(String serviceName, Class<?> interfaze, Component target) { + this.serviceName = serviceName; + this.component = target; + serviceContract = new JavaServiceContract(interfaze); + this.interfaze = interfaze; + } + + public SystemInboundWireImpl(String serviceName, Class<?> interfaze) { + this(serviceName, interfaze, null); + } + + public ServiceContract getServiceContract() { + return serviceContract; + } + + public void setServiceContract(ServiceContract serviceContract) { + this.serviceContract = serviceContract; + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + throw new UnsupportedOperationException(); + } + + public Object getTargetService() throws TargetException { + if (wire != null) { + return interfaze.cast(wire.getTargetService()); + } + return interfaze.cast(component.getServiceInstance(serviceName)); + } + + public Class[] getImplementedInterfaces() { + return new Class[0]; + } + + public Map<Operation<?>, InboundInvocationChain> getInvocationChains() { + return Collections.emptyMap(); + } + + public void addInvocationChain(Operation<?> operation, InboundInvocationChain chain) { + throw new UnsupportedOperationException(); + } + + public void addInvocationChains(Map chains) { + throw new UnsupportedOperationException(); + } + + public Map<Operation<?>, OutboundInvocationChain> getSourceCallbackInvocationChains(Object targetAddr) { + throw new UnsupportedOperationException(); + } + + public void addSourceCallbackInvocationChains(Object targetAddr, + Map<Operation<?>, + OutboundInvocationChain> chains) { + throw new UnsupportedOperationException(); + } + + public void addSourceCallbackInvocationChain(Object targetAddr, Operation operation, + OutboundInvocationChain chain) { + throw new UnsupportedOperationException(); + } + + public void addInterface(Class claz) { + throw new UnsupportedOperationException(); + } + + public String getCallbackReferenceName() { + return null; + } + + public void setCallbackReferenceName(String callbackReferenceName) { + throw new UnsupportedOperationException(); + } + + public boolean isOptimizable() { + return true; // system wires are always optimizable + } + + public void setTargetWire(OutboundWire wire) { + assert wire instanceof SystemOutboundWire : "wire must be a " + SystemOutboundWireImpl.class.getName(); + this.wire = (SystemOutboundWire) wire; + } + + public SCAObject getContainer() { + return container; + } + + public void setContainer(SCAObject container) { + this.container = container; + } + + public void addMapping(Object messageId, Object fromAddress) { + throw new UnsupportedOperationException(); + } + + public Object retrieveMapping(Object messageId) { + return null; + } + + public void removeMapping(Object messageId) { + throw new UnsupportedOperationException(); + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundAutowire.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundAutowire.java new file mode 100644 index 0000000000..ff4cad9795 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundAutowire.java @@ -0,0 +1,152 @@ +/* + * 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.core.implementation.system.wire; + +import java.util.Collections; +import java.util.Map; + +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.TargetNotFoundException; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; + +import org.apache.tuscany.core.wire.OutboundAutowire; + +/** + * A specialization of <code>OutboundAutowire</code> that returns a direct reference to the target + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemOutboundAutowire implements OutboundAutowire, SystemOutboundWire { + private String referenceName; + private ServiceContract serviceContract; + private CompositeComponent component; + private final boolean required; + private SCAObject container; + + public SystemOutboundAutowire(String referenceName, Class<?> interfaze, CompositeComponent component, + boolean required) { + + this.referenceName = referenceName; + this.component = component; + serviceContract = new JavaServiceContract(interfaze); + this.required = required; + } + + public ServiceContract getServiceContract() { + return serviceContract; + } + + public void setServiceContract(ServiceContract serviceContract) { + this.serviceContract = serviceContract; + } + + public String getReferenceName() { + return referenceName; + } + + public void setReferenceName(String referenceName) { + this.referenceName = referenceName; + } + + public QualifiedName getTargetName() { + return null; + } + + public void setTargetName(QualifiedName targetName) { + } + + public Object getTargetService() throws TargetException { + Class<?> interfaze = serviceContract.getInterfaceClass(); + Object service = component.resolveSystemInstance(interfaze); + if (service == null && required) { + TargetNotFoundException e = new TargetNotFoundException("Autowire target not found"); + e.setIdentifier(interfaze.getName()); + throw e; + } + return service; + } + + public void setCallbackInterface(Class<?> interfaze) { + throw new UnsupportedOperationException(); + } + + public Class<?> getCallbackInterface() { + throw new UnsupportedOperationException(); + } + + public void addCallbackInterface(Class<?> claz) { + throw new UnsupportedOperationException(); + } + + public Class[] getImplementedCallbackInterfaces() { + throw new UnsupportedOperationException(); + } + + public void setTargetWire(InboundWire wire) { + throw new UnsupportedOperationException(); + } + + public Map<Operation<?>, OutboundInvocationChain> getInvocationChains() { + return Collections.emptyMap(); + } + + public void addInvocationChain(Operation operation, OutboundInvocationChain chains) { + throw new UnsupportedOperationException(); + } + + public void addInvocationChains(Map<Operation<?>, OutboundInvocationChain> chains) { + throw new UnsupportedOperationException(); + } + + public Map<Operation<?>, InboundInvocationChain> getTargetCallbackInvocationChains() { + throw new UnsupportedOperationException(); + } + + public void addTargetCallbackInvocationChains(Map<Operation<?>, InboundInvocationChain> chains) { + throw new UnsupportedOperationException(); + } + + public void addTargetCallbackInvocationChain(Operation operation, InboundInvocationChain chain) { + throw new UnsupportedOperationException(); + } + + public void addInterface(Class claz) { + throw new UnsupportedOperationException(); + } + + public boolean isOptimizable() { + return true; + } + + public SCAObject getContainer() { + return container; + } + + public void setContainer(SCAObject container) { + this.container = container; + } +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundWire.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundWire.java new file mode 100644 index 0000000000..bf9ccdd5a1 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundWire.java @@ -0,0 +1,31 @@ +/* + * 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.core.implementation.system.wire; + +import org.apache.tuscany.spi.wire.OutboundWire; + +/** + * Specified by a {@link org.apache.tuscany.core.implementation.system.model.SystemBinding}, a specialized outbound wire + * that returns a direct reference to the target. + * + * @version $$Rev$$ $$Date$$ + */ +public interface SystemOutboundWire extends OutboundWire { + +} diff --git a/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundWireImpl.java b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundWireImpl.java new file mode 100644 index 0000000000..ae20b89b55 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-M2/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/wire/SystemOutboundWireImpl.java @@ -0,0 +1,146 @@ +/* + * 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.core.implementation.system.wire; + +import java.util.Collections; +import java.util.Map; + +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.InboundInvocationChain; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundInvocationChain; + +/** + * Default implementation of a system outbound wire + * + * @version $$Rev$$ $$Date$$ + */ +public class SystemOutboundWireImpl implements SystemOutboundWire { + private String referenceName; + private QualifiedName targetName; + private ServiceContract serviceContract; + private SystemInboundWire targetWire; + private SCAObject container; + private Class<?> interfaze; + + public SystemOutboundWireImpl(String referenceName, QualifiedName targetName, Class<?> interfaze) { + this.referenceName = referenceName; + this.targetName = targetName; + serviceContract = new JavaServiceContract(interfaze); + this.interfaze = interfaze; + } + + public ServiceContract getServiceContract() { + return serviceContract; + } + + public void setServiceContract(ServiceContract serviceContract) { + this.serviceContract = serviceContract; + } + + public String getReferenceName() { + return referenceName; + } + + public void setReferenceName(String referenceName) { + this.referenceName = referenceName; + } + + public QualifiedName getTargetName() { + return targetName; + } + + public void setTargetName(QualifiedName targetName) { + this.targetName = targetName; + } + + public Object getTargetService() throws TargetException { + if (targetWire == null) { + throw new TargetException("No target wire connected to source wire"); + } + return interfaze.cast(targetWire.getTargetService()); + } + + public void setCallbackInterface(Class<?> interfaze) { + throw new UnsupportedOperationException(); + } + + public Class<?> getCallbackInterface() { + throw new UnsupportedOperationException(); + } + + public void addCallbackInterface(Class<?> claz) { + throw new UnsupportedOperationException(); + } + + public Class[] getImplementedCallbackInterfaces() { + throw new UnsupportedOperationException(); + } + + public Map<Operation<?>, OutboundInvocationChain> getInvocationChains() { + return Collections.emptyMap(); + } + + public void addInvocationChain(Operation operation, OutboundInvocationChain chains) { + throw new UnsupportedOperationException(); + } + + public void addInvocationChains(Map chains) { + throw new UnsupportedOperationException(); + } + + public Map<Operation<?>, InboundInvocationChain> getTargetCallbackInvocationChains() { + throw new UnsupportedOperationException(); + } + + public void addTargetCallbackInvocationChains(Map<Operation<?>, InboundInvocationChain> chains) { + throw new UnsupportedOperationException(); + } + + public void addTargetCallbackInvocationChain(Operation operation, InboundInvocationChain chain) { + throw new UnsupportedOperationException(); + } + + public void addInterface(Class claz) { + throw new UnsupportedOperationException(); + } + + public void setTargetWire(InboundWire wire) { + assert wire instanceof SystemInboundWire : "wire must be a " + SystemInboundWire.class.getName(); + this.targetWire = (SystemInboundWire) wire; + } + + public boolean isOptimizable() { + return true; + } + + public SCAObject getContainer() { + return container; + } + + public void setContainer(SCAObject container) { + this.container = container; + } + +} |