summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main')
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaAssemblyFactory.java32
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaImplementation.java37
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaAssemblyFactoryImpl.java42
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaImplementationImpl.java97
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaComponentContextBuilder.java348
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaTargetWireBuilder.java85
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/config/JavaComponentRuntimeConfiguration.java151
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/context/JavaComponentContext.java206
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/AbstractJavaComponentInvoker.java92
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/ScopedJavaComponentInvoker.java86
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/StaticJavaComponentTargetInvoker.java50
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/loader/JavaSCDLModelLoader.java81
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/org/apache/tuscany/container/java/Messages.properties25
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/system.fragment38
-rw-r--r--sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/test/sca.module75
15 files changed, 1445 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaAssemblyFactory.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaAssemblyFactory.java
new file mode 100644
index 0000000000..3ed37e70ff
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaAssemblyFactory.java
@@ -0,0 +1,32 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.assembly;
+
+import org.apache.tuscany.model.assembly.AssemblyFactory;
+
+
+/**
+ * The Factory for the model.
+ */
+public interface JavaAssemblyFactory extends AssemblyFactory {
+
+ /**
+ * Returns a new JavaImplementation.
+ */
+ JavaImplementation createJavaImplementation();
+
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaImplementation.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaImplementation.java
new file mode 100644
index 0000000000..e8cf6b4014
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/JavaImplementation.java
@@ -0,0 +1,37 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.assembly;
+
+import org.apache.tuscany.model.assembly.ComponentImplementation;
+
+
+/**
+ * Represents a java implementation.
+ */
+public interface JavaImplementation extends ComponentImplementation {
+
+ /**
+ * Returns the implementation class.
+ */
+ Class getImplementationClass();
+
+ /**
+ * Sets the implementation class.
+ */
+ void setImplementationClass(Class value);
+
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaAssemblyFactoryImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaAssemblyFactoryImpl.java
new file mode 100644
index 0000000000..c8f29a2140
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaAssemblyFactoryImpl.java
@@ -0,0 +1,42 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.assembly.impl;
+
+import org.apache.tuscany.container.java.assembly.JavaAssemblyFactory;
+import org.apache.tuscany.container.java.assembly.JavaImplementation;
+import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl;
+
+/**
+ * A factory for the model.
+ */
+public class JavaAssemblyFactoryImpl extends AssemblyFactoryImpl implements JavaAssemblyFactory {
+
+ /**
+ * Constructor
+ */
+ public JavaAssemblyFactoryImpl() {
+ super();
+ }
+
+ /**
+ * @see org.apache.tuscany.container.java.assembly.JavaAssemblyFactory#createJavaImplementation()
+ */
+ public JavaImplementation createJavaImplementation() {
+ return new JavaImplementationImpl();
+ }
+
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaImplementationImpl.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaImplementationImpl.java
new file mode 100644
index 0000000000..5ed0109b1d
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/assembly/impl/JavaImplementationImpl.java
@@ -0,0 +1,97 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.assembly.impl;
+
+import java.net.URL;
+
+import org.apache.tuscany.container.java.assembly.JavaAssemblyFactory;
+import org.apache.tuscany.container.java.assembly.JavaImplementation;
+import org.apache.tuscany.core.config.impl.Java5ComponentTypeIntrospector;
+import org.apache.tuscany.core.config.ComponentTypeIntrospector;
+import org.apache.tuscany.core.config.ConfigurationException;
+import org.apache.tuscany.core.config.JavaIntrospectionHelper;
+import org.apache.tuscany.model.assembly.AssemblyModelContext;
+import org.apache.tuscany.model.assembly.ComponentType;
+import org.apache.tuscany.model.assembly.impl.ComponentImplementationImpl;
+
+/**
+ * An implementation of JavaImplementation.
+ */
+public class JavaImplementationImpl extends ComponentImplementationImpl implements JavaImplementation {
+
+ private Class<?> implementationClass;
+
+ /**
+ * Constructor
+ */
+ protected JavaImplementationImpl() {
+ }
+
+ /**
+ * @see org.apache.tuscany.container.java.assembly.JavaImplementation#getImplementationClass()
+ */
+ public Class getImplementationClass() {
+ return implementationClass;
+ }
+
+ /**
+ * @see org.apache.tuscany.container.java.assembly.JavaImplementation#setImplementationClass(java.lang.Class)
+ */
+ public void setImplementationClass(Class value) {
+ checkNotFrozen();
+ implementationClass=value;
+ }
+
+ /**
+ * @see org.apache.tuscany.model.assembly.AssemblyModelObject#initialize(org.apache.tuscany.model.assembly.AssemblyModelContext)
+ */
+ public void initialize(AssemblyModelContext modelContext) {
+ if (isInitialized())
+ return;
+
+ // Initialize the component type
+ ComponentType componentType=getComponentType();
+ if (componentType==null) {
+ componentType=createComponentType(modelContext, implementationClass);
+ setComponentType(componentType);
+ }
+
+ super.initialize(modelContext);
+ }
+
+ /**
+ * Create the component type
+ * @param modelContext
+ * @param implClass
+ */
+ private static ComponentType createComponentType(AssemblyModelContext modelContext, Class<?> implClass) {
+ String baseName = JavaIntrospectionHelper.getBaseName(implClass);
+ URL componentTypeFile = implClass.getResource(baseName + ".componentType");
+ if (componentTypeFile != null) {
+ return modelContext.getAssemblyLoader().loadComponentType(componentTypeFile.toString());
+ } else {
+ JavaAssemblyFactory factory = new JavaAssemblyFactoryImpl();
+ ComponentTypeIntrospector introspector = new Java5ComponentTypeIntrospector(factory);
+ try {
+ return introspector.introspect(implClass);
+ } catch (ConfigurationException e) {
+ throw new IllegalArgumentException("Unable to introspect implementation class: " + implClass.getName(), e);
+ }
+ }
+ }
+
+ }
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaComponentContextBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaComponentContextBuilder.java
new file mode 100644
index 0000000000..9ac030dfb9
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaComponentContextBuilder.java
@@ -0,0 +1,348 @@
+package org.apache.tuscany.container.java.builder;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.tuscany.container.java.assembly.JavaImplementation;
+import org.apache.tuscany.container.java.config.JavaComponentRuntimeConfiguration;
+import org.apache.tuscany.core.builder.BuilderConfigException;
+import org.apache.tuscany.core.builder.BuilderException;
+import org.apache.tuscany.core.builder.NoAccessorException;
+import org.apache.tuscany.core.builder.RuntimeConfigurationBuilder;
+import org.apache.tuscany.core.builder.impl.HierarchicalBuilder;
+import org.apache.tuscany.core.builder.impl.ProxyObjectFactory;
+import org.apache.tuscany.core.config.JavaIntrospectionHelper;
+import org.apache.tuscany.core.context.AggregateContext;
+import org.apache.tuscany.core.context.QualifiedName;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.FactoryInitException;
+import org.apache.tuscany.core.injection.FieldInjector;
+import org.apache.tuscany.core.injection.Injector;
+import org.apache.tuscany.core.injection.MethodEventInvoker;
+import org.apache.tuscany.core.injection.MethodInjector;
+import org.apache.tuscany.core.injection.SDOObjectFactory;
+import org.apache.tuscany.core.injection.SingletonObjectFactory;
+import org.apache.tuscany.core.invocation.InvocationConfiguration;
+import org.apache.tuscany.core.invocation.MethodHashMap;
+import org.apache.tuscany.core.invocation.ProxyConfiguration;
+import org.apache.tuscany.core.invocation.impl.InvokerInterceptor;
+import org.apache.tuscany.core.invocation.spi.ProxyFactory;
+import org.apache.tuscany.core.invocation.spi.ProxyFactoryFactory;
+import org.apache.tuscany.core.message.MessageFactory;
+import org.apache.tuscany.core.runtime.RuntimeContext;
+import org.apache.tuscany.core.system.annotation.Autowire;
+import org.apache.tuscany.model.assembly.AssemblyModelObject;
+import org.apache.tuscany.model.assembly.ConfiguredProperty;
+import org.apache.tuscany.model.assembly.ConfiguredReference;
+import org.apache.tuscany.model.assembly.ConfiguredService;
+import org.apache.tuscany.model.assembly.Scope;
+import org.apache.tuscany.model.assembly.Service;
+import org.apache.tuscany.model.assembly.ServiceContract;
+import org.apache.tuscany.model.assembly.SimpleComponent;
+import org.osoa.sca.annotations.ComponentName;
+import org.osoa.sca.annotations.Context;
+import org.osoa.sca.annotations.Destroy;
+import org.osoa.sca.annotations.Init;
+
+import commonj.sdo.DataObject;
+
+/**
+ * Builds runtime configurations for component implementations that map to
+ * {@link org.apache.tuscany.container.java.assembly.JavaImplementation}. The logical model is then decorated with the
+ * runtime configuration.
+ *
+ * @see org.apache.tuscany.core.builder.RuntimeConfiguration
+ *
+ * @version $Rev: 368822 $ $Date: 2006-01-13 10:54:38 -0800 (Fri, 13 Jan 2006) $
+ */
+@org.osoa.sca.annotations.Scope("MODULE")
+public class JavaComponentContextBuilder implements RuntimeConfigurationBuilder<AggregateContext> {
+
+ private RuntimeContext runtimeContext;
+
+ private ProxyFactoryFactory proxyFactoryFactory;
+
+ private MessageFactory messageFactory;
+
+ /* the top-level builder responsible for evaluating policies */
+ private HierarchicalBuilder policyBuilder = new HierarchicalBuilder();
+
+ @Init(eager = true)
+ public void init() {
+ runtimeContext.addBuilder(this);
+ }
+
+ /**
+ * @param runtimeContext The runtimeContext to set.
+ */
+ @Autowire
+ public void setRuntimeContext(RuntimeContext runtimeContext) {
+ this.runtimeContext = runtimeContext;
+ }
+
+ /**
+ * Sets the factory used to construct proxies implmementing the business interface required by a reference
+ */
+ @Autowire
+ public void setProxyFactoryFactory(ProxyFactoryFactory factory) {
+ this.proxyFactoryFactory = factory;
+ }
+
+ /**
+ * Sets the factory used to construct invocation messages
+ *
+ * @param msgFactory
+ */
+ @Autowire
+ public void setMessageFactory(MessageFactory msgFactory) {
+ this.messageFactory = msgFactory;
+ }
+
+ /**
+ * Adds a builder responsible for creating source-side and target-side invocation chains for a reference. The
+ * reference builder may be hierarchical, containing other child reference builders that operate on specific
+ * metadata used to construct and invocation chain.
+ */
+ public void addPolicyBuilder(RuntimeConfigurationBuilder builder) {
+ policyBuilder.addBuilder(builder);
+ }
+
+ // ----------------------------------
+ // Constructors
+ // ----------------------------------
+
+ public JavaComponentContextBuilder() {
+ }
+
+ // ----------------------------------
+ // Methods
+ // ----------------------------------
+
+ public void build(AssemblyModelObject modelObject, AggregateContext parentContext) throws BuilderException {
+ if (!(modelObject instanceof SimpleComponent)) {
+ return;
+ }
+ SimpleComponent component = (SimpleComponent) modelObject;
+ if (component.getComponentImplementation() instanceof JavaImplementation) {
+ JavaImplementation javaImpl = (JavaImplementation) component.getComponentImplementation();
+ // FIXME scope
+ Scope scope = component.getComponentImplementation().getComponentType().getServices().get(0).getServiceContract()
+ .getScope();
+ Class implClass = null;
+ Set<Field> fields;
+ Set<Method> methods;
+ try {
+ implClass = javaImpl.getImplementationClass();
+ fields = JavaIntrospectionHelper.getAllFields(implClass);
+ methods = JavaIntrospectionHelper.getAllUniqueMethods(implClass);
+ String name = component.getName();
+ Constructor ctr = implClass.getConstructor((Class[]) null);
+
+ List<Injector> injectors = new ArrayList();
+
+ EventInvoker initInvoker = null;
+ boolean eagerInit = false;
+ EventInvoker destroyInvoker = null;
+ for (Field field : fields) {
+ ComponentName compName = field.getAnnotation(ComponentName.class);
+ if (compName != null) {
+ Injector injector = new FieldInjector(field, new SingletonObjectFactory(name));
+ injectors.add(injector);
+ }
+ Context context = field.getAnnotation(Context.class);
+ if (context != null) {
+ Injector injector = new FieldInjector(field, new SingletonObjectFactory(parentContext));
+ injectors.add(injector);
+ }
+ }
+ for (Method method : methods) {
+ Init init = method.getAnnotation(Init.class);
+ if (init != null && initInvoker == null) {
+ initInvoker = new MethodEventInvoker(method);
+ eagerInit = init.eager();
+ continue;
+ }
+ // @spec - should we allow the same method to have @init and
+ // @destroy?
+ Destroy destroy = method.getAnnotation(Destroy.class);
+ if (destroy != null && destroyInvoker == null) {
+ destroyInvoker = new MethodEventInvoker(method);
+ continue;
+ }
+ ComponentName compName = method.getAnnotation(ComponentName.class);
+ if (compName != null) {
+ Injector injector = new MethodInjector(method, new SingletonObjectFactory(name));
+ injectors.add(injector);
+ }
+ Context context = method.getAnnotation(Context.class);
+ if (context != null) {
+ Injector injector = new MethodInjector(method, new SingletonObjectFactory(parentContext));
+ injectors.add(injector);
+ }
+ }
+ // handle properties
+ List<ConfiguredProperty> configuredProperties = component.getConfiguredProperties();
+ if (configuredProperties != null) {
+ for (ConfiguredProperty property : configuredProperties) {
+ Injector injector = createPropertyInjector(property, fields, methods);
+ injectors.add(injector);
+ }
+ }
+ JavaComponentRuntimeConfiguration config = new JavaComponentRuntimeConfiguration(name, JavaIntrospectionHelper
+ .getDefaultConstructor(implClass), eagerInit, initInvoker, destroyInvoker, scope);
+ component.getComponentImplementation().setRuntimeConfiguration(config);
+
+ // create target-side invocation chains for each service offered by the implementation
+ for (ConfiguredService configuredService : component.getConfiguredServices()) {
+ Service service = configuredService.getService();
+ ServiceContract serviceContract = service.getServiceContract();
+ Map<Method, InvocationConfiguration> iConfigMap = new MethodHashMap();
+ ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory();
+ Set<Method> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface());
+ for (Method method : javaMethods) {
+ InvocationConfiguration iConfig = new InvocationConfiguration(method);
+ iConfigMap.put(method, iConfig);
+ }
+ QualifiedName qName = new QualifiedName(component.getName() + QualifiedName.NAME_SEPARATOR + service.getName());
+ ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface()
+ .getClassLoader(), messageFactory);
+ proxyFactory.setBusinessInterface(serviceContract.getInterface());
+ proxyFactory.setProxyConfiguration(pConfiguration);
+ config.addTargetProxyFactory(service.getName(), proxyFactory);
+ configuredService.setProxyFactory(proxyFactory);
+ if (policyBuilder != null) {
+ // invoke the reference builder to handle target-side metadata
+ policyBuilder.build(configuredService, parentContext);
+ }
+ // add tail interceptor
+ for (InvocationConfiguration iConfig : (Collection<InvocationConfiguration>) iConfigMap.values()) {
+ iConfig.addTargetInterceptor(new InvokerInterceptor());
+ }
+
+ }
+
+ // handle references
+ List<ConfiguredReference> configuredReferences = component.getConfiguredReferences();
+ if (configuredReferences != null) {
+ for (ConfiguredReference reference : configuredReferences) {
+ ProxyFactory proxyFactory = proxyFactoryFactory.createProxyFactory();
+ ServiceContract serviceContract = reference.getReference().getServiceContract();
+ Map<Method, InvocationConfiguration> iConfigMap = new HashMap();
+ Set<Method> javaMethods = JavaIntrospectionHelper.getAllUniqueMethods(serviceContract.getInterface());
+ for (Method method : javaMethods) {
+ InvocationConfiguration iConfig = new InvocationConfiguration(method);
+ iConfigMap.put(method, iConfig);
+ }
+ String targetCompName = reference.getTargetConfiguredServices().get(0).getAggregatePart().getName();
+ String targetSerivceName = reference.getTargetConfiguredServices().get(0).getService().getName();
+
+ QualifiedName qName = new QualifiedName(targetCompName + "/" + targetSerivceName);
+ // QualifiedName qName = new QualifiedName(reference.getAggregatePart().getName() + "/"
+ // + reference.getPort().getName());
+ ProxyConfiguration pConfiguration = new ProxyConfiguration(qName, iConfigMap, serviceContract.getInterface().getClassLoader(), messageFactory);
+ proxyFactory.setBusinessInterface(serviceContract.getInterface());
+ proxyFactory.setProxyConfiguration(pConfiguration);
+ config.addSourceProxyFactory(reference.getReference().getName(), proxyFactory);
+ reference.setProxyFactory(proxyFactory);
+ if (policyBuilder != null) {
+ // invoke the reference builder to handle metadata associated with the reference
+ policyBuilder.build(reference, parentContext);
+ }
+ Injector injector = createReferenceInjector(reference.getReference().getName(), proxyFactory, fields,
+ methods);
+ injectors.add(injector);
+ }
+ }
+ config.setSetters(injectors);
+ } catch (BuilderException e) {
+ e.addContextName(component.getName());
+ e.addContextName(parentContext.getName());
+ throw e;
+ } catch (NoSuchMethodException e) {
+ BuilderConfigException ce = new BuilderConfigException("Class does not have a no-arg constructor", e);
+ ce.setIdentifier(implClass.getName());
+ ce.addContextName(component.getName());
+ ce.addContextName(parentContext.getName());
+ throw ce;
+ }
+ }
+ }
+
+ // ----------------------------------
+ // Private methods
+ // ----------------------------------
+
+ /**
+ * Creates an <code>Injector</code> for component properties
+ */
+ private Injector createPropertyInjector(ConfiguredProperty property, Set<Field> fields, Set<Method> methods)
+ throws NoAccessorException {
+ Object value = property.getValue();
+ String propName = property.getProperty().getName();
+ Class type = value.getClass();
+
+ // There is no efficient way to do this
+ Method method = null;
+ Field field = JavaIntrospectionHelper.findClosestMatchingField(propName, type, fields);
+ if (field == null) {
+ method = JavaIntrospectionHelper.findClosestMatchingMethod(propName, new Class[] { type }, methods);
+ if (method == null) {
+ throw new NoAccessorException(propName);
+ }
+ }
+ Injector injector = null;
+ if (value instanceof DataObject) {
+ if (field != null) {
+ injector = new FieldInjector(field, new SDOObjectFactory((DataObject) value));
+ } else {
+ injector = new MethodInjector(method, new SDOObjectFactory((DataObject) value));
+ }
+ } else if (JavaIntrospectionHelper.isImmutable(type)) {
+ if (field != null) {
+ injector = new FieldInjector(field, new SingletonObjectFactory(value));
+ } else {
+ injector = new MethodInjector(method, new SingletonObjectFactory(value));
+ }
+ }
+ return injector;
+
+ }
+
+ /**
+ * Creates an <code>Injector</code> for service references
+ */
+ private Injector createReferenceInjector(String refName, ProxyFactory proxyFactory, Set<Field> fields, Set<Method> methods)
+ throws NoAccessorException, BuilderConfigException {
+ Method method = null;
+ Field field = JavaIntrospectionHelper.findClosestMatchingField(refName, proxyFactory.getBusinessInterface(), fields);
+ if (field == null) {
+ method = JavaIntrospectionHelper.findClosestMatchingMethod(refName,
+ new Class[] { proxyFactory.getBusinessInterface() }, methods);
+ if (method == null) {
+ throw new NoAccessorException(refName);
+ }
+ }
+ Injector injector;
+ try {
+ if (field != null) {
+ injector = new FieldInjector(field, new ProxyObjectFactory(proxyFactory));
+ } else {
+ injector = new MethodInjector(method, new ProxyObjectFactory(proxyFactory));
+ }
+ } catch (FactoryInitException e) {
+ BuilderConfigException ce = new BuilderConfigException("Error configuring reference", e);
+ ce.setIdentifier(refName);
+ throw ce;
+ }
+ return injector;
+
+ }
+
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaTargetWireBuilder.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaTargetWireBuilder.java
new file mode 100644
index 0000000000..5df160cb54
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/builder/JavaTargetWireBuilder.java
@@ -0,0 +1,85 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.tuscany.container.java.builder;
+
+import org.apache.tuscany.container.java.config.JavaComponentRuntimeConfiguration;
+import org.apache.tuscany.container.java.invocation.ScopedJavaComponentInvoker;
+import org.apache.tuscany.core.builder.BuilderConfigException;
+import org.apache.tuscany.core.builder.WireBuilder;
+import org.apache.tuscany.core.context.ScopeContext;
+import org.apache.tuscany.core.invocation.InvocationConfiguration;
+import org.apache.tuscany.core.invocation.spi.ProxyFactory;
+import org.apache.tuscany.core.runtime.RuntimeContext;
+import org.apache.tuscany.core.system.annotation.Autowire;
+import org.osoa.sca.annotations.Init;
+import org.osoa.sca.annotations.Scope;
+
+/**
+ * Completes a wire to a Java-based target component by adding a scoped java invoker to the source chain
+ *
+ * @version $Rev$ $Date$
+ */
+@Scope("MODULE")
+public class JavaTargetWireBuilder implements WireBuilder {
+
+ private RuntimeContext runtimeContext;
+
+ @Autowire
+ public void setRuntimeContext(RuntimeContext context) {
+ runtimeContext = context;
+ }
+
+ public JavaTargetWireBuilder() {
+ }
+
+ @Init(eager=true)
+ public void init() {
+ runtimeContext.addBuilder(this);
+ }
+
+ public void connect(ProxyFactory sourceFactory, ProxyFactory targetFactory, Class targetType, boolean downScope,
+ ScopeContext targetScopeContext) throws BuilderConfigException {
+ if (!(JavaComponentRuntimeConfiguration.class.isAssignableFrom(targetType))) {
+ return;
+ }
+ for (InvocationConfiguration sourceInvocationConfig : sourceFactory.getProxyConfiguration().getInvocationConfigurations()
+ .values()) {
+ ScopedJavaComponentInvoker invoker = new ScopedJavaComponentInvoker(sourceFactory.getProxyConfiguration()
+ .getTargetName(), sourceInvocationConfig.getMethod(), targetScopeContext);
+ if (downScope) {
+ // the source scope is shorter than the target, so the invoker can cache the target instance
+ invoker.setCacheable(false);
+ } else {
+ invoker.setCacheable(true); //TODO set to true
+ }
+ sourceInvocationConfig.setTargetInvoker(invoker);
+ }
+ }
+
+ public void completeTargetChain(ProxyFactory targetFactory, Class targetType, ScopeContext targetScopeContext)
+ throws BuilderConfigException {
+ // TODO implement.
+ // if (!(JavaComponentRuntimeConfiguration.class.isAssignableFrom(targetType))) {
+ // return;
+ // }
+ // for (InvocationConfiguration targetInvocationConfig :
+ // targetFactory.getProxyConfiguration().getInvocationConfigurations()
+ // .values()) {
+ // ScopedJavaComponentInvoker invoker = new ScopedJavaComponentInvoker(targetFactory.getProxyConfiguration()
+ // .getTargetName(), ((JavaOperationType) targetInvocationConfig.getOperationType()).getJavaMethod(),
+ // targetScopeContext);
+ // targetInvocationConfig.setTargetInvoker(invoker);
+ // }
+ }
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/config/JavaComponentRuntimeConfiguration.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/config/JavaComponentRuntimeConfiguration.java
new file mode 100644
index 0000000000..bfe6dc25d5
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/config/JavaComponentRuntimeConfiguration.java
@@ -0,0 +1,151 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.tuscany.container.java.config;
+
+import java.lang.reflect.Constructor;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tuscany.container.java.context.JavaComponentContext;
+import org.apache.tuscany.core.builder.ContextCreationException;
+import org.apache.tuscany.core.builder.RuntimeConfiguration;
+import org.apache.tuscany.core.context.SimpleComponentContext;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.Injector;
+import org.apache.tuscany.core.injection.PojoObjectFactory;
+import org.apache.tuscany.core.invocation.spi.ProxyFactory;
+import org.apache.tuscany.model.assembly.Scope;
+
+/**
+ * A RuntimeConfiguration that handles POJO component implementation types
+ *
+ * @version $Rev$ $Date$
+ */
+public class JavaComponentRuntimeConfiguration implements RuntimeConfiguration<SimpleComponentContext> {
+
+ // the component name as configured in the hosting module
+ private String name;
+
+ // the implementation type constructor
+ private Constructor ctr;
+
+ // injectors for properties, references and other metadata values such as
+ private List<Injector> setters;
+
+ // an invoker for a method decorated with @Init
+ private EventInvoker init;
+
+ // whether the component should be eagerly initialized when its scope starts
+ private boolean eagerInit;
+
+ // an invoker for a method decorated with @Destroy
+ private EventInvoker destroy;
+
+ // the scope of the implementation instance
+ private Scope scope;
+
+ private boolean stateless;
+
+ // ----------------------------------
+ // Constructors
+ // ----------------------------------
+
+ /**
+ * Creates the runtime configuration
+ *
+ * @param name the SCDL name of the component the context refers to
+ * @param ctr the implementation type constructor
+ * @param setters a collection of <code>Injectors</code> used to configure properties, references and other meta
+ * data values on implementation instances
+ * @param eagerInit whether the component should be eagerly initialized
+ * @param init an <code>Invoker</code> pointing to a method on the implementation type decorated with
+ * <code>@Init</code>
+ * @param destroy an <code>Invoker</code> pointing to a method on the implementation type decorated with
+ * <code>@Destroy</code>
+ * @param scope the scope of the component implementation type
+ */
+ public JavaComponentRuntimeConfiguration(String name, Constructor ctr, List<Injector> setters, boolean eagerInit,
+ EventInvoker init, EventInvoker destroy, Scope scope) {
+ assert (name != null) : "Name was null";
+ assert (ctr != null) : "Constructor was null";
+ this.name = name;
+ this.ctr = ctr;
+ this.setters = setters;
+ this.eagerInit = eagerInit;
+ this.init = init;
+ this.destroy = destroy;
+ this.scope = scope;
+ stateless = (scope == Scope.INSTANCE);
+ }
+
+ public JavaComponentRuntimeConfiguration(String name, Constructor ctr, boolean eagerInit, EventInvoker init,
+ EventInvoker destroy, Scope scope) {
+ this(name, ctr, null, eagerInit, init, destroy, scope);
+ }
+
+ // ----------------------------------
+ // Methods
+ // ----------------------------------
+
+ public String getName() {
+ return name;
+ }
+
+ public Scope getScope() {
+ return scope;
+ }
+
+ public SimpleComponentContext createInstanceContext() throws ContextCreationException {
+ PojoObjectFactory objectFactory = new PojoObjectFactory(ctr, null, setters);
+ return new JavaComponentContext(name, objectFactory, eagerInit, init, destroy, stateless);
+ }
+
+ private Map<String, ProxyFactory> targetProxyFactories = new HashMap();
+
+ public void addTargetProxyFactory(String serviceName, ProxyFactory factory) {
+ targetProxyFactories.put(serviceName, factory);
+ }
+
+ public ProxyFactory getTargetProxyFactory(String serviceName) {
+ return targetProxyFactories.get(serviceName);
+ }
+
+ public Map<String, ProxyFactory> getTargetProxyFactories() {
+ return targetProxyFactories;
+ }
+
+ private Map<String, ProxyFactory> sourceProxyFactories = new HashMap();
+
+ public void addSourceProxyFactory(String referenceName, ProxyFactory factory) {
+ sourceProxyFactories.put(referenceName, factory);
+ }
+
+ public ProxyFactory getSourceProxyFactory(String referenceName) {
+ return sourceProxyFactories.get(referenceName);
+ }
+
+ public Map<String, ProxyFactory> getSourceProxyFactories() {
+ return sourceProxyFactories;
+ }
+
+ public void setSetters(List<Injector> setters) {
+ this.setters = setters;
+ }
+
+ public void prepare(){
+
+ }
+
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/context/JavaComponentContext.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/context/JavaComponentContext.java
new file mode 100644
index 0000000000..0b4a800f1f
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/context/JavaComponentContext.java
@@ -0,0 +1,206 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.context;
+
+import java.util.Iterator;
+
+import org.apache.tuscany.core.builder.ObjectFactory;
+import org.apache.tuscany.core.context.AbstractContext;
+import org.apache.tuscany.core.context.Context;
+import org.apache.tuscany.core.context.ContextInitException;
+import org.apache.tuscany.core.context.LifecycleEventListener;
+import org.apache.tuscany.core.context.QualifiedName;
+import org.apache.tuscany.core.context.SimpleComponentContext;
+import org.apache.tuscany.core.context.TargetException;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.Injector;
+import org.apache.tuscany.core.injection.ObjectCallbackException;
+import org.apache.tuscany.core.injection.ObjectCreationException;
+
+/**
+ * Manages Java service component implementation instances
+ *
+ * @version $Rev$ $Date$
+ */
+public class JavaComponentContext extends AbstractContext implements SimpleComponentContext {
+
+ private boolean eagerInit;
+
+ private EventInvoker initInvoker;
+
+ private EventInvoker destroyInvoker;
+
+ private Injector componentName;
+
+ private Injector moduleContext;
+
+ private boolean stateless;
+
+ // the cached target instance
+ private Object cachedTargetInstance;
+
+ // creates a new implementation instance with injected references and properties
+ private ObjectFactory objectFactory;
+
+ // ----------------------------------
+ // Constructors
+ // ----------------------------------
+
+ public JavaComponentContext(String name, ObjectFactory objectFactory, boolean eagerInit, EventInvoker initInvoker,
+ EventInvoker destroyInvoker, boolean stateless) {
+ super(name);
+ assert (objectFactory != null) : "Object factory was null";
+ if (eagerInit == true && initInvoker == null) {
+ ContextInitException e = new ContextInitException("No intialization method found for implementation");
+ e.setIdentifier(getName());
+ throw e;
+ }
+ this.objectFactory = objectFactory;
+
+ this.eagerInit = eagerInit;
+ this.initInvoker = initInvoker;
+ this.destroyInvoker = destroyInvoker;
+ this.stateless = stateless;
+ }
+
+ // ----------------------------------
+ // Methods
+ // ----------------------------------
+
+ public void setName(String name) {
+ super.setName(name);
+ }
+
+ protected int type;
+
+ public int getType() {
+ return type;
+ }
+
+ public void setType(int type) {
+ this.type = type;
+ }
+
+ public synchronized Object getInstance(QualifiedName qName) throws TargetException {
+ return getInstance(qName, true);
+ }
+
+ public synchronized Object getInstance(QualifiedName qName, boolean notify) throws TargetException {
+ //TODO implement returning of proxy and invocation chain for service
+ if (cachedTargetInstance != null) {
+ return cachedTargetInstance; // already cached, just return
+ }
+
+ if (getLifecycleState() == ERROR || getLifecycleState() == CONFIG_ERROR) {
+ return null;
+ }
+ synchronized (this) {
+ try {
+ Object instance = objectFactory.getInstance();
+ startInstance(instance);
+ if (notify) {
+ for (Iterator iter = contextListener.iterator(); iter.hasNext();) {
+ LifecycleEventListener listener = (LifecycleEventListener) iter.next();
+ listener.onInstanceCreate(this);
+ }
+ }
+ setLifecycleState(RUNNING);
+ if (stateless) {
+ return instance;
+ } else {
+ // cache the actual instance
+ cachedTargetInstance = instance;
+ return cachedTargetInstance;
+ }
+ } catch (ObjectCreationException e) {
+ setLifecycleState(Context.ERROR);
+ TargetException te = new TargetException("Error creating instance for component", e);
+ te.setIdentifier(getName());
+ throw te;
+ }
+ }
+
+ }
+
+ public Object getImplementationInstance() throws TargetException{
+ //TODO refactor when getInstance() returns a proxy
+ return getInstance(null);
+ }
+
+ public Object getImplementationInstance(boolean notify) throws TargetException{
+ //TODO refactor when getInstance() returns a proxy
+ return getInstance(null,notify);
+ }
+
+
+ public boolean isEagerInit() {
+ return eagerInit;
+ }
+
+ public boolean isDestroyable() {
+ return (destroyInvoker != null);
+ }
+
+ // ----------------------------------
+ // Lifecycle methods
+ // ----------------------------------
+
+ public void start() throws ContextInitException {
+ if (getLifecycleState() != UNINITIALIZED && getLifecycleState() != STOPPED) {
+ throw new IllegalStateException("Component must be in UNINITIALIZED state [" + getLifecycleState() + "]");
+ }
+ if (objectFactory == null) {
+ setLifecycleState(ERROR);
+ ContextInitException e = new ContextInitException("Object factory not found");
+ e.setIdentifier(getName());
+ throw e;
+ }
+ setLifecycleState(INITIALIZED);
+ }
+
+ public void stop() {
+ if (cachedTargetInstance != null) {
+ if (destroyInvoker != null) {
+ try {
+ destroyInvoker.invokeEvent(cachedTargetInstance);
+ } catch (ObjectCallbackException e) {
+ TargetException te = new TargetException(e.getCause());
+ te.setIdentifier(getName());
+ throw te;
+ }
+ }
+ }
+ setLifecycleState(STOPPED);
+ }
+
+ // ----------------------------------
+ // Private methods
+ // ----------------------------------
+ private void startInstance(Object instance) throws TargetException {
+ try {
+ // handle @Init
+ if (initInvoker != null) {
+ initInvoker.invokeEvent(instance);
+ }
+ } catch (ObjectCallbackException e) {
+ TargetException te = new TargetException("Error initializing instance", e);
+ te.setIdentifier(getName());
+ throw te;
+ }
+ }
+
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/AbstractJavaComponentInvoker.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/AbstractJavaComponentInvoker.java
new file mode 100644
index 0000000000..0109416e75
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/AbstractJavaComponentInvoker.java
@@ -0,0 +1,92 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.invocation;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Set;
+
+import org.apache.tuscany.core.config.JavaIntrospectionHelper;
+import org.apache.tuscany.core.context.TargetException;
+import org.apache.tuscany.core.invocation.Interceptor;
+import org.apache.tuscany.core.invocation.InvocationRuntimeException;
+import org.apache.tuscany.core.invocation.TargetInvoker;
+import org.apache.tuscany.core.message.Message;
+
+/**
+ * Base class for dispatching to a Java based component implementation. Subclasses implement a strategy for resolving
+ * implementation instances.
+ *
+ * @version $Rev$ $Date$
+ */
+public abstract class AbstractJavaComponentInvoker implements TargetInvoker {
+
+ protected Method operation;
+
+ public AbstractJavaComponentInvoker(Method operation) {
+ assert (operation != null) : "Operation method cannot be null";
+ this.operation = operation;
+ }
+
+ public Object invokeTarget(Object payload) throws InvocationTargetException {
+ try {
+ Object instance = getInstance();
+ if (!operation.getDeclaringClass().isInstance(instance)) {
+ Set methods = JavaIntrospectionHelper.getAllUniqueMethods(instance.getClass());
+ Method newOperation = JavaIntrospectionHelper.findClosestMatchingMethod(operation.getName(), operation
+ .getParameterTypes(), methods);
+ if (newOperation != null)
+ operation = newOperation;
+ }
+ if (payload != null && !payload.getClass().isArray()) {
+ return operation.invoke(instance, payload);
+ } else {
+ return operation.invoke(instance, (Object[]) payload);
+ }
+ } catch (IllegalAccessException e) {
+ throw new InvocationRuntimeException(e);
+ }
+ }
+
+ public Message invoke(Message msg) {
+ try {
+ Object resp = invokeTarget(msg.getBody());
+ msg.setBody(resp);
+ } catch (InvocationTargetException e) {
+ msg.setBody(e.getCause());
+ } catch (Throwable e) {
+ msg.setBody(e);
+ }
+ return msg;
+ }
+
+ protected abstract Object getInstance() throws TargetException;
+
+ public void setNext(Interceptor next) {
+ throw new IllegalStateException("This interceptor must be the last interceptor in an interceptor chain");
+ }
+
+ public Object clone(){
+ try {
+ AbstractJavaComponentInvoker clone = (AbstractJavaComponentInvoker) super.clone();
+ clone.operation = this.operation;
+ return clone;
+ } catch (CloneNotSupportedException e) {
+ return null; // will not happen
+ }
+ }
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/ScopedJavaComponentInvoker.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/ScopedJavaComponentInvoker.java
new file mode 100644
index 0000000000..4d69ba51f5
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/ScopedJavaComponentInvoker.java
@@ -0,0 +1,86 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.invocation;
+
+import java.lang.reflect.Method;
+
+import org.apache.tuscany.core.context.QualifiedName;
+import org.apache.tuscany.core.context.ScopeContext;
+import org.apache.tuscany.core.context.TargetException;
+
+/**
+ * Uses a scope container to resolve an implementation instance based on the current thread context
+ *
+ * @version $Rev$ $Date$
+ */
+public class ScopedJavaComponentInvoker extends AbstractJavaComponentInvoker {
+
+ private ScopeContext container;
+
+ private QualifiedName name;
+
+ private Object target;
+
+ public boolean cacheable;
+
+ public ScopedJavaComponentInvoker(QualifiedName serviceName, Method operation, ScopeContext container) {
+ super(operation);
+ assert (serviceName != null) : "No service name specified";
+ assert (container != null) : "No scope container specified";
+ name = serviceName;
+ this.container = container;
+ }
+
+ /**
+ * Returns whether the target is cacheable.
+ */
+ public boolean isCacheable() {
+ return cacheable;
+ }
+
+ /**
+ * Sets whether the target service instance may be cached by the invoker. This is a possible optimization when a
+ * wire is configured for a "down-scope" reference, i.e. a reference from a source of a shorter lifetime to a source
+ * of greater lifetime.
+ */
+ public void setCacheable(boolean val) {
+ cacheable = val;
+ }
+
+ /**
+ * Resolves the target service instance or returns a cached one
+ */
+ protected Object getInstance() throws TargetException {
+ if (!cacheable) {
+ return container.getInstance(name);
+ } else {
+ if (target == null) {
+ target = container.getInstance(name);
+ }
+ return target;
+ }
+ }
+
+ public Object clone() {
+ ScopedJavaComponentInvoker invoker = (ScopedJavaComponentInvoker) super.clone();
+ invoker.target = null;
+ invoker.cacheable = this.cacheable;
+ invoker.container = this.container;
+ invoker.name = this.name;
+ return invoker;
+ }
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/StaticJavaComponentTargetInvoker.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/StaticJavaComponentTargetInvoker.java
new file mode 100644
index 0000000000..8e0342beb7
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/invocation/StaticJavaComponentTargetInvoker.java
@@ -0,0 +1,50 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.java.invocation;
+
+import java.lang.reflect.Method;
+
+/**
+ * Caches component instances that do not need to be resolved for every invocation, e.g. an invocation originating from
+ * a lesser scope intended for a target with a wider scope
+ *
+ * @version $Rev$ $Date$
+ */
+public class StaticJavaComponentTargetInvoker extends AbstractJavaComponentInvoker {
+
+ private Object instance;
+
+ public StaticJavaComponentTargetInvoker(Method operation, Object instance) {
+ super(operation);
+ assert (instance != null) : "Instance cannot be null";
+ this.instance = instance;
+ }
+
+ protected Object getInstance() {
+ return instance;
+ }
+
+ public boolean isCacheable() {
+ return true;
+ }
+
+ public Object clone() {
+ StaticJavaComponentTargetInvoker invoker = (StaticJavaComponentTargetInvoker) super.clone();
+ invoker.instance = null;
+ return invoker;
+ }
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/loader/JavaSCDLModelLoader.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/loader/JavaSCDLModelLoader.java
new file mode 100644
index 0000000000..e1c50a9ff4
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/java/org/apache/tuscany/container/java/loader/JavaSCDLModelLoader.java
@@ -0,0 +1,81 @@
+package org.apache.tuscany.container.java.loader;
+
+import org.apache.tuscany.container.java.assembly.JavaAssemblyFactory;
+import org.apache.tuscany.container.java.assembly.JavaImplementation;
+import org.apache.tuscany.container.java.assembly.impl.JavaAssemblyFactoryImpl;
+import org.apache.tuscany.core.loader.SCDLModelLoaderRegistry;
+import org.apache.tuscany.core.runtime.RuntimeContext;
+import org.apache.tuscany.core.system.annotation.Autowire;
+import org.apache.tuscany.model.assembly.AssemblyModelContext;
+import org.apache.tuscany.model.assembly.AssemblyModelObject;
+import org.apache.tuscany.model.scdl.loader.SCDLModelLoader;
+import org.osoa.sca.annotations.Destroy;
+import org.osoa.sca.annotations.Init;
+
+/**
+ * Populates the assembly model from an SCDL model
+ */
+@org.osoa.sca.annotations.Scope("MODULE")
+public class JavaSCDLModelLoader implements SCDLModelLoader {
+
+ private RuntimeContext runtimeContext;
+ private SCDLModelLoaderRegistry loaderRegistry;
+ private JavaAssemblyFactory javaFactory;
+
+ /**
+ * Constructs a new JavaSCDLModelLoader.
+ */
+ public JavaSCDLModelLoader() {
+ this.javaFactory=new JavaAssemblyFactoryImpl();
+ }
+
+ /**
+ * @param runtimeContext The runtimeContext to set.
+ */
+ @Autowire
+ public void setRuntimeContext(RuntimeContext runtimeContext) {
+ this.runtimeContext = runtimeContext;
+ }
+
+// @Reference
+ public void setLoaderRegistry(SCDLModelLoaderRegistry registry) {
+ this.loaderRegistry = registry;
+ }
+
+ @Init(eager=true)
+ public void init() {
+ runtimeContext.addLoader(this);
+// loaderRegistry.registerLoader(this);
+ }
+
+ @Destroy
+ public void destroy() {
+// loaderRegistry.unregisterLoader(this);
+ }
+
+ /**
+ * @see org.apache.tuscany.model.scdl.loader.SCDLModelLoader#load(org.apache.tuscany.model.assembly.AssemblyModelContext, java.lang.Object)
+ */
+ public AssemblyModelObject load(AssemblyModelContext modelContext, Object object) {
+ if (object instanceof org.apache.tuscany.model.scdl.JavaImplementation) {
+ org.apache.tuscany.model.scdl.JavaImplementation scdlJavaImplementation=(org.apache.tuscany.model.scdl.JavaImplementation)object;
+ JavaImplementation implementation=javaFactory.createJavaImplementation();
+
+ //FIXME Temp set the current app classloader on the class to load the impl class
+ ClassLoader ccl=Thread.currentThread().getContextClassLoader();
+ Class implementationClass;
+ try {
+ Thread.currentThread().setContextClassLoader(modelContext.getApplicationResourceLoader().getClassLoader());
+ implementationClass=modelContext.getApplicationResourceLoader().loadClass(scdlJavaImplementation.getClass_());
+ } catch (ClassNotFoundException e) {
+ throw new IllegalArgumentException(e);
+ } finally {
+ Thread.currentThread().setContextClassLoader(ccl);
+ }
+ implementation.setImplementationClass(implementationClass);
+
+ return implementation;
+ } else
+ return null;
+ }
+}
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/org/apache/tuscany/container/java/Messages.properties b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/org/apache/tuscany/container/java/Messages.properties
new file mode 100644
index 0000000000..4581e44bff
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/org/apache/tuscany/container/java/Messages.properties
@@ -0,0 +1,25 @@
+# Copyright (c) 2005 The Apache Software Foundation or its licensors, as applicable.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ====================================================================
+# To code developer:
+# Do NOT change the properties between this line and the
+# "%%% END OF TRANSLATED PROPERTIES %%%" line.
+# Make a new property name, append to the end of the file and change
+# the code to use the new property.
+# ====================================================================
+
+# =====================================================================
+# %%% END OF TRANSLATED PROPERTIES %%%
+# =====================================================================
+# NLS_MESSAGEFORMAT_ALL
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/system.fragment b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/system.fragment
new file mode 100644
index 0000000000..e29d165b0b
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/resources/system.fragment
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="ASCII"?>
+<!--
+ Copyright (c) 2005 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<moduleFragment xmlns="http://www.osoa.org/xmlns/sca/0.9" xmlns:v="http://www.osoa.org/xmlns/sca/values/0.9"
+ xmlns:system="http://org.apache.tuscany/xmlns/system/0.9"
+ name="org.apache.tuscany.container.java">
+
+ <component name="org.apache.tuscany.container.java.builder.JavaComponentContextBuilder">
+ <system:implementation.system class="org.apache.tuscany.container.java.builder.JavaComponentContextBuilder"/>
+ </component>
+
+ <component name="org.apache.tuscany.container.java.builder.JavaTargetWireBuilder">
+ <system:implementation.system class="org.apache.tuscany.container.java.builder.JavaTargetWireBuilder"/>
+ </component>
+
+ <component name="org.apache.tuscany.container.java.loader.JavaSCDLModelLoader">
+ <system:implementation.system class="org.apache.tuscany.container.java.loader.JavaSCDLModelLoader"/>
+<!--
+ <references>
+ <v:loaderRegistry>org.apache.tuscany.core.loader.SCDLModelLoaderRegistry</v:loaderRegistry>
+ </references>
+-->
+ </component>
+
+</moduleFragment>
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/test/sca.module b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/test/sca.module
new file mode 100644
index 0000000000..e1f10097f4
--- /dev/null
+++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.java/src/main/test/sca.module
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="ASCII"?>
+<!--
+ Copyright (c) 2005 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<module xmlns="http://www.osoa.org/xmlns/sca/0.9" xmlns:v="http://www.osoa.org/xmlns/sca/values/0.9"
+
+ name="sample.myvaluemodule">
+ <!--
+ <entryPoint name="MyValueService">
+ <interface.java interface="services.myvalue.MyValueService"/>
+ <binding.ws port="http://webservice.myvalue#MyValueWebService"/>
+ <reference>MyValueServiceComponent</reference>
+ </entryPoint>
+
+ <component name="MyValueServiceComponent">
+ <implementation.java class="services.myvalue.MyValueServiceImpl"/>
+ <properties>
+ <v:currency>EURO</v:currency>
+ </properties>
+ <references>
+ <v:customerService>CustomerServiceComponent</v:customerService>
+ <v:stockQuoteService>StockQuoteMediatorComponent</v:stockQuoteService>
+ </references>
+ </component>
+
+ <component name="StockQuoteMediatorComponent">
+ <implementation.java class="services.mediator.StockQuoteMediatorImpl"/>
+ <references>
+ <v:stockQuoteService>StockQuoteService</v:stockQuoteService>
+ </references>
+ </component>
+
+ <component name="CustomerServiceComponent">
+ <implementation.java class="services.customer.CustomerServiceImpl"/>
+ </component>
+
+ <component name="StockQuoteService">
+ <implementation.java class="services.stockquote.StockQuoteServiceImpl"/>
+ </component>
+ -->
+ <component name="TestService">
+ <implementation.java class="test.org.apache.tuscany.container.java.components.ModuleScopeComponentImpl"/>
+ <properties>
+ <v:foo>MyFoo</v:foo>
+ <v:mInt>1</v:mInt>
+ </properties>
+ <references>
+ <v:mGenericComponent>TestService2</v:mGenericComponent>
+ </references>
+ </component>
+ <component name="TestService2">
+ <implementation.java class="test.org.apache.tuscany.container.java.components.ModuleScopeComponentImpl"/>
+ </component>
+
+ <!--
+ <externalService name="StockQuoteService">
+ <interface.java interface="services.stockquote.StockQuoteService"/>
+ <binding.ws port="http://webservice.stockquote#StockQuoteWebService"/>
+ </externalService>
+ -->
+
+</module>
+ \ No newline at end of file