summaryrefslogtreecommitdiffstats
path: root/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension')
-rw-r--r--sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/config/AbstractContextFactory.java143
-rw-r--r--sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/context/AbstractComponentContext.java114
-rw-r--r--sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java160
3 files changed, 417 insertions, 0 deletions
diff --git a/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/config/AbstractContextFactory.java b/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/config/AbstractContextFactory.java
new file mode 100644
index 0000000000..cf0e722c62
--- /dev/null
+++ b/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/config/AbstractContextFactory.java
@@ -0,0 +1,143 @@
+/**
+ *
+ * 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.extension.config;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tuscany.core.builder.ContextFactory;
+import org.apache.tuscany.core.context.AtomicContext;
+import org.apache.tuscany.core.context.CompositeContext;
+import org.apache.tuscany.core.wire.SourceWireFactory;
+import org.apache.tuscany.core.wire.TargetWireFactory;
+import org.apache.tuscany.model.assembly.Scope;
+
+/**
+ * Abstract context factory.
+ */
+public abstract class AbstractContextFactory<T extends AtomicContext> implements ContextFactory<T> {
+
+ // Context scope
+ private Scope scope;
+
+ // Context name
+ private String name;
+
+ // SOurce proxy factories
+ private List<SourceWireFactory> sourceProxyFactories = new ArrayList<SourceWireFactory>();
+
+ // Target proxy factories
+ private Map<String, TargetWireFactory> targetProxyFactories = new HashMap<String, TargetWireFactory>();
+
+ /**
+ * Initializes the context factory.
+ *
+ * @param name Context name.
+ * @param scope Context scope.
+ * @param groovyScript Groovy script to run.
+ */
+ public AbstractContextFactory(String name, Scope scope) {
+ this.name = name;
+ this.scope = scope;
+ }
+
+ /**
+ * Returns the context scope.
+ * @return Context scope.
+ */
+ public final Scope getScope() {
+ return scope;
+ }
+
+ /**
+ * Returns the context name.
+ * @return Context name.
+ */
+ public final String getName() {
+ return name;
+ }
+
+ /**
+ * Adds a property.
+ *
+ * @param propertyName Property name.
+ * @param value Property value.
+ */
+ public final void addProperty(String propertyName, Object value) {
+ }
+
+ /**
+ * Adds a target proxy factory.
+ *
+ * @param serviceName Service name.
+ * @param factory Target proxy factory.
+ */
+ public final void addTargetWireFactory(String serviceName, TargetWireFactory factory) {
+ targetProxyFactories.put(serviceName, factory);
+ }
+
+ /**
+ * Gets a target proxy factory by name.
+ *
+ * @param serviceName Service name.
+ * @return Target wire factory for the specified service.
+ */
+ public final TargetWireFactory getTargetWireFactory(String serviceName) {
+ return targetProxyFactories.get(serviceName);
+ }
+
+ /**
+ * Returns all the target wire factories.
+ * @return All the target wire factories for the context.
+ */
+ public final Map<String, TargetWireFactory> getTargetWireFactories() {
+ return targetProxyFactories;
+ }
+
+ /**
+ * Adds a source wire factory.
+ *
+ * @param referenceName Reference name.
+ * @param factory SOurce wire factory.
+ */
+ public final void addSourceWireFactory(String referenceName, SourceWireFactory factory) {
+ sourceProxyFactories.add(factory);
+ }
+
+ /**
+ * Adds all source wire factories.
+ */
+ public final void addSourceWireFactories(String referenceName, Class referenceInterface, List<SourceWireFactory> factory, boolean multiplicity) {
+ sourceProxyFactories.addAll(factory);
+ }
+
+ /**
+ * Returns all the source wire factories.
+ */
+ public final List<SourceWireFactory> getSourceWireFactories() {
+ return sourceProxyFactories;
+ }
+
+ /**
+ * Notification that the parent factory has been activated.
+ */
+ public final void prepare(CompositeContext parent) {
+ }
+
+}
diff --git a/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/context/AbstractComponentContext.java b/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/context/AbstractComponentContext.java
new file mode 100644
index 0000000000..866a233c66
--- /dev/null
+++ b/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/context/AbstractComponentContext.java
@@ -0,0 +1,114 @@
+/**
+ *
+ * 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.extension.context;
+
+import java.util.Map;
+
+import org.apache.tuscany.core.context.AtomicContext;
+import org.apache.tuscany.core.context.CoreRuntimeException;
+import org.apache.tuscany.core.context.QualifiedName;
+import org.apache.tuscany.core.context.TargetException;
+import org.apache.tuscany.core.context.event.InstanceCreated;
+import org.apache.tuscany.core.context.impl.AbstractContext;
+import org.apache.tuscany.core.wire.ProxyCreationException;
+import org.apache.tuscany.core.wire.TargetWireFactory;
+import org.apache.tuscany.core.wire.WireFactory;
+
+/**
+ * Abstract component context.
+ *
+ */
+public abstract class AbstractComponentContext extends AbstractContext implements AtomicContext {
+
+ private Map<String, TargetWireFactory> targetProxyFactories;
+
+ public AbstractComponentContext(String name, Map<String, TargetWireFactory> targetProxyFactories) {
+ super(name);
+ this.targetProxyFactories = targetProxyFactories;
+ }
+
+ public Object getInstance(QualifiedName qName) throws TargetException {
+ return getInstance(qName, true);
+ }
+
+ /**
+ * Init method.
+ */
+ public void init() throws TargetException {
+ }
+
+ /**
+ * Detsroy method.
+ */
+ public void destroy() throws TargetException {
+ }
+
+ private synchronized Object getInstance(QualifiedName qName, boolean notify) throws TargetException {
+ String portName = qName.getPortName();
+ WireFactory targetFactory;
+ if (portName != null) {
+ targetFactory = targetProxyFactories.get(portName);
+ } else {
+ targetFactory = targetProxyFactories.values().iterator().next();
+ }
+ if (targetFactory == null) {
+ TargetException e = new TargetException("Target service not found");
+ e.setIdentifier(qName.getPortName());
+ e.addContextName(getName());
+ throw e;
+ }
+ try {
+ Object proxy = targetFactory.createProxy();
+ if (notify) {
+ publish(new InstanceCreated(this));
+ }
+ return proxy;
+ } catch (ProxyCreationException e) {
+ TargetException te = new TargetException("Error returning target", e);
+ e.setIdentifier(qName.getPortName());
+ e.addContextName(getName());
+ throw te;
+ }
+ }
+
+ /**
+ * Whether this context supports eager init.
+ */
+ public boolean isEagerInit() {
+ return false;
+ }
+
+ /**
+ * Whether this context is destroyable.
+ */
+ public boolean isDestroyable() {
+ return false;
+ }
+
+ /**
+ * Starts the context.
+ */
+ public void start() throws CoreRuntimeException {
+ }
+
+ /**
+ * Stops the context.
+ */
+ public void stop() throws CoreRuntimeException {
+ }
+
+}
diff --git a/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java b/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java
new file mode 100644
index 0000000000..ed325e087b
--- /dev/null
+++ b/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java
@@ -0,0 +1,160 @@
+/**
+ *
+ * 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.extension.loader;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.tuscany.core.config.ConfigurationLoadException;
+import org.apache.tuscany.core.config.InvalidRootElementException;
+import org.apache.tuscany.core.config.MissingResourceException;
+import org.apache.tuscany.core.config.SidefileLoadException;
+import org.apache.tuscany.core.loader.LoaderContext;
+import org.apache.tuscany.core.loader.StAXElementLoader;
+import org.apache.tuscany.core.loader.StAXLoaderRegistry;
+import org.apache.tuscany.core.loader.assembly.AssemblyConstants;
+import org.apache.tuscany.core.system.annotation.Autowire;
+import org.apache.tuscany.model.assembly.ComponentType;
+import org.apache.tuscany.model.assembly.Implementation;
+import org.osoa.sca.annotations.Scope;
+
+/**
+ * Abstract implementation loader.
+ *
+ */
+@Scope("MODULE")
+public abstract class AbstractImplementationLoader<T extends Implementation> implements StAXElementLoader<T> {
+
+ // Injected Stax loader registry.
+ protected StAXLoaderRegistry registry;
+
+ // XML input factory.
+ protected XMLInputFactory xmlFactory;
+
+ /**
+ * Initializes the XML input factory.
+ *
+ */
+ public AbstractImplementationLoader() {
+ xmlFactory = XMLInputFactory.newInstance();
+ }
+
+ /**
+ * Injection method for Stax loader registry.
+ * @param registry Stax loader registry.
+ */
+ @Autowire
+ public void setRegistry(StAXLoaderRegistry registry) {
+ this.registry = registry;
+ }
+
+ /**
+ * Loads the Groovy implementation.
+ *
+ * @param Stax XML stream reader.
+ * @param loaderContext Loader context.
+ * @return Groovy implementation.
+ */
+ public T load(XMLStreamReader reader, LoaderContext loaderContext) throws XMLStreamException, ConfigurationLoadException {
+
+ T assemblyObject = getAssemblyObject(reader, loaderContext);
+
+ URL componentTypeFile = getSideFile(reader, loaderContext);
+ ComponentType componentType = loadComponentType(componentTypeFile, loaderContext);
+
+ assemblyObject.setComponentType(componentType);
+
+ return assemblyObject;
+
+ }
+
+ /**
+ * Required to be implemented by the concrete classes.
+ * @return Implementation object.
+ */
+ protected abstract T getAssemblyObject(XMLStreamReader reader, LoaderContext loaderContext);
+
+ /**
+ * Gets the side file.
+ *
+ * @param reader Reader for the module file.
+ * @param loaderContext Loader context.
+ * @return Side file Url.
+ * @throws MissingResourceException
+ */
+ protected abstract URL getSideFile(XMLStreamReader reader, LoaderContext loaderContext) throws MissingResourceException;
+
+ /**
+ * Loads the SIDE file to get the component information.
+ *
+ * @param scriptFile SCript file name.
+ * @param loaderContext Loader context.
+ * @return Component information.
+ * @throws SidefileLoadException
+ * @throws MissingResourceException
+ */
+ private ComponentType loadComponentType(URL componentTypeFile, LoaderContext loaderContext) throws SidefileLoadException, MissingResourceException{
+
+ XMLStreamReader reader = null;
+ InputStream is = null;
+
+ try {
+ is = componentTypeFile.openStream();
+ reader = xmlFactory.createXMLStreamReader(is);
+ reader.nextTag();
+ if (!AssemblyConstants.COMPONENT_TYPE.equals(reader.getName())) {
+ InvalidRootElementException e = new InvalidRootElementException(AssemblyConstants.COMPONENT_TYPE, reader.getName());
+ e.setResourceURI(componentTypeFile.toString());
+ throw e;
+ }
+ return (ComponentType) registry.load(reader, loaderContext);
+ } catch (IOException e) {
+ SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
+ sfe.setResourceURI(componentTypeFile.toString());
+ throw sfe;
+ } catch (XMLStreamException e) {
+ SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
+ sfe.setResourceURI(componentTypeFile.toString());
+ throw sfe;
+ } catch (ConfigurationLoadException e) {
+ SidefileLoadException sfe = new SidefileLoadException(e.getMessage());
+ sfe.setResourceURI(componentTypeFile.toString());
+ throw sfe;
+ } finally {
+ try {
+ if(reader != null) {
+ reader.close();
+ }
+ } catch (XMLStreamException e) {
+ // ignore
+ }
+ try {
+ if(is != null) {
+ is.close();
+ }
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+
+}