From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../container/groovy/GroovyAtomicComponent.java | 96 +++++++++++++ .../container/groovy/GroovyComponentBuilder.java | 110 +++++++++++++++ .../container/groovy/GroovyComponentType.java | 47 +++++++ .../groovy/GroovyComponentTypeLoader.java | 73 ++++++++++ .../container/groovy/GroovyConfiguration.java | 149 +++++++++++++++++++++ .../container/groovy/GroovyImplementation.java | 73 ++++++++++ .../tuscany/container/groovy/GroovyInvoker.java | 87 ++++++++++++ .../container/groovy/ImplementationLoader.java | 108 +++++++++++++++ 8 files changed, 743 insertions(+) create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentType.java create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentTypeLoader.java create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyConfiguration.java create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyImplementation.java create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyInvoker.java create mode 100644 sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java (limited to 'sandbox/old/contrib/implementation-groovy/container/src/main/java') diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java new file mode 100644 index 0000000000..9969647a3b --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyAtomicComponent.java @@ -0,0 +1,96 @@ +/* + * 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.container.groovy; + +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.TargetResolutionException; +import org.apache.tuscany.spi.extension.AtomicComponentExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; + +import groovy.lang.GroovyObject; + +/** + * The Groovy atomic component implementation. Groovy implementations may be "scripts" or classes. + * + * @version $Rev$ $Date$ + */ +public class GroovyAtomicComponent extends AtomicComponentExtension { + private final Class groovyClass; + //FIXME properties should move up to AtomicComponentExtension + private final Map properties; + + public GroovyAtomicComponent(GroovyConfiguration configuration) { + super(configuration.getName(), + configuration.getParent(), + configuration.getWireService(), + configuration.getWorkContext(), + null, + configuration.getMonitor(), + configuration.getInitLevel()); + + this.groovyClass = configuration.getGroovyClass(); + this.properties = new HashMap(); + assert groovyClass != null; + } + + public TargetInvoker createTargetInvoker(String targetName, Operation operation, InboundWire callbackWire) { + return new GroovyInvoker(operation.getName(), this, callbackWire, workContext, monitor); + } + + public Object createInstance() throws ObjectCreationException { + GroovyObject instance; + try { + instance = groovyClass.newInstance(); + } catch (IllegalAccessException e) { + throw new ObjectCreationException(e); + } catch (InstantiationException e) { + throw new ObjectCreationException(e); + } + + // inject properties + for (Map.Entry property : properties.entrySet()) { + instance.setProperty(property.getKey(), property.getValue().getInstance()); + } + + // inject references + for (List referenceWires : getOutboundWires().values()) { + for (OutboundWire wire : referenceWires) { + Class clazz = wire.getServiceContract().getInterfaceClass(); + instance.setProperty(wire.getReferenceName(), wireService.createProxy(clazz, wire)); + } + } + return instance; + } + + public GroovyObject getTargetInstance() throws TargetResolutionException { + return (GroovyObject) scopeContainer.getInstance(this); + } + + public void addPropertyFactory(String name, ObjectFactory factory) { + properties.put(name, factory); + } +} diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java new file mode 100644 index 0000000000..0e615977cb --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentBuilder.java @@ -0,0 +1,110 @@ +/* + * 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.container.groovy; + +import groovy.lang.GroovyClassLoader; +import groovy.lang.GroovyObject; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.spi.ObjectFactory; +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.ComponentDefinition; +import org.apache.tuscany.spi.model.Property; +import org.apache.tuscany.spi.model.ServiceDefinition; +import org.codehaus.groovy.control.CompilationFailedException; + +/** + * Extension point for creating {@link GroovyAtomicComponent}s from an assembly configuration + * + * @version $$Rev$$ $$Date$$ + */ +public class GroovyComponentBuilder extends ComponentBuilderExtension { + + protected Class getImplementationType() { + return GroovyImplementation.class; + } + + @SuppressWarnings("unchecked") + public Component build(CompositeComponent parent, + ComponentDefinition componentDefinition, + DeploymentContext deploymentContext) + throws BuilderConfigException { + + String name = componentDefinition.getName(); + GroovyImplementation implementation = componentDefinition.getImplementation(); + GroovyComponentType componentType = implementation.getComponentType(); + + int initLevel = componentType.getInitLevel(); + + // get list of serviceBindings provided by this component + Collection collection = componentType.getServices().values(); + List> services = new ArrayList>(collection.size()); + for (ServiceDefinition serviceDefinition : collection) { + services.add(serviceDefinition.getServiceContract().getInterfaceClass()); + } + + // get the Groovy classloader for this deployment component + GroovyClassLoader groovyClassLoader = (GroovyClassLoader) deploymentContext.getExtension("groovy.classloader"); + if (groovyClassLoader == null) { + groovyClassLoader = new GroovyClassLoader(implementation.getApplicationLoader()); + deploymentContext.putExtension("groovy.classloader", groovyClassLoader); + } + + // create the implementation class for the script + Class groovyClass; + try { + String script = implementation.getScript(); + // REVIEW JFM can we cache the class? + groovyClass = groovyClassLoader.parseClass(script); + } catch (CompilationFailedException e) { + throw new BuilderConfigException(name, e); + } + // TODO deal with init and destroy + + GroovyConfiguration configuration = new GroovyConfiguration(); + configuration.setName(name); + configuration.setGroovyClass(groovyClass); + configuration.setParent(parent); + + configuration.setWireService(wireService); + configuration.setWorkContext(workContext); + configuration.setInitLevel(initLevel); + configuration.setServices(services); + configuration.setMonitor(monitor); + GroovyAtomicComponent component = new GroovyAtomicComponent(configuration); + + // handle properties + for (Property property : componentType.getProperties().values()) { + ObjectFactory factory = property.getDefaultValueFactory(); + if (factory != null) { + component.addPropertyFactory(property.getName(), factory); + } + } + return component; + } + + +} diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentType.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentType.java new file mode 100644 index 0000000000..91b80827a7 --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentType.java @@ -0,0 +1,47 @@ +/* + * 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.container.groovy; + +import org.apache.tuscany.spi.model.ComponentType; +import org.apache.tuscany.spi.model.Property; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceDefinition; + +/** + * Model object representing a Groovy component type + * + * @version $$Rev$$ $$Date$$ + */ +public class GroovyComponentType extends ComponentType> { + private Scope lifecycleScope; + + public Scope getLifecycleScope() { + return lifecycleScope; + } + + public void setLifecycleScope(Scope lifecycleScope) { + this.lifecycleScope = lifecycleScope; + } + + public GroovyComponentType() { + implementationScope = Scope.COMPOSITE; + } + +} diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentTypeLoader.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentTypeLoader.java new file mode 100644 index 0000000000..f811440027 --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyComponentTypeLoader.java @@ -0,0 +1,73 @@ +/* + * 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.container.groovy; + +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.model.Scope; + +/** + * @version $Rev$ $Date$ + */ +public class GroovyComponentTypeLoader extends ComponentTypeLoaderExtension { + + @Override + protected Class getImplementationClass() { + return GroovyImplementation.class; + } + + public void load(CompositeComponent parent, GroovyImplementation implementation, DeploymentContext context) + throws LoaderException { + URL resource = implementation.getApplicationLoader().getResource(getSideFileName(implementation)); + GroovyComponentType componentType; + if (resource == null) { + //TODO this should be replaced by loadFromIntrospection, + componentType = new GroovyComponentType(); + } else { + componentType = loadFromSidefile(resource, context); + } + + // for now, default to composite + componentType.setLifecycleScope(Scope.COMPOSITE); + implementation.setComponentType(componentType); + } + + protected GroovyComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext) + throws LoaderException { + GroovyComponentType ct = new GroovyComponentType(); + return (GroovyComponentType)loaderRegistry.load(null,ct, url, GroovyComponentType.class, deploymentContext); + } + + private String getSideFileName(GroovyImplementation implementation) { + String baseName = getResourceName(implementation); + int lastDot = baseName.lastIndexOf('.'); + if (lastDot != -1) { + baseName = baseName.substring(0, lastDot); + } + return baseName + ".componentType"; + } + + protected String getResourceName(GroovyImplementation implementation) { + return implementation.getScriptResourceName(); + } +} diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyConfiguration.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyConfiguration.java new file mode 100644 index 0000000000..49fa4c2a41 --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyConfiguration.java @@ -0,0 +1,149 @@ +/* + * 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.container.groovy; + +import groovy.lang.GroovyObject; + +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.WorkContext; +import org.apache.tuscany.spi.extension.ExecutionMonitor; +import org.apache.tuscany.spi.wire.WireService; + +/** + * Encapsulates confuration for a Groovy-based atomic component + * + * @version $Rev$ $Date$ + */ +public class GroovyConfiguration { + + private CompositeComponent parent; + private int initLevel; + private Map referenceSites = new HashMap(); + private Map propertySites = new HashMap(); + private Map callbackSites = new HashMap(); + private List> serviceInterfaces = new ArrayList>(); + private WireService wireService; + private WorkContext workContext; + private String name; + private Class groovyClass; + private List> services; + private ExecutionMonitor monitor; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Class getGroovyClass() { + return groovyClass; + } + + public void setGroovyClass(Class groovyClass) { + this.groovyClass = groovyClass; + } + + public CompositeComponent getParent() { + return parent; + } + + public void setParent(CompositeComponent parent) { + this.parent = parent; + } + + public List> getServiceInterfaces() { + return serviceInterfaces; + } + + public void addServiceInterface(Class serviceInterface) { + serviceInterfaces.add(serviceInterface); + } + + public int getInitLevel() { + return initLevel; + } + + public void setInitLevel(int initLevel) { + this.initLevel = initLevel; + } + + public List> getServices() { + return services; + } + + public void setServices(List> services) { + this.services = services; + } + + public Map getReferenceSite() { + return referenceSites; + } + + public void addReferenceSite(String name, Member member) { + referenceSites.put(name, member); + } + + public Map getCallbackSite() { + return callbackSites; + } + + public void addCallbackSite(String name, Member member) { + callbackSites.put(name, member); + } + + public Map 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 ExecutionMonitor getMonitor() { + return monitor; + } + + public void setMonitor(ExecutionMonitor monitor) { + this.monitor = monitor; + } +} diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyImplementation.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyImplementation.java new file mode 100644 index 0000000000..0a48507a11 --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyImplementation.java @@ -0,0 +1,73 @@ +/* + * 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.container.groovy; + +import org.apache.tuscany.spi.model.AtomicImplementation; + +/** + * Model object for a Groovy implementation. + */ +public class GroovyImplementation extends AtomicImplementation { + + //the Groovy source to be executed + private String script; + + // the application class loader, we need this to construct a GroovyClassLoader in GroovyComponentBuilder + private ClassLoader applicationLoader; + + // TODO , Suggest to change the current script to scriptContent, acutally, we need script file name to get component side file name. + private String scriptResourceName; + + public String getScriptResourceName() { + return scriptResourceName; + } + + public void setScriptResourceName(String scriptResourceName) { + this.scriptResourceName = scriptResourceName; + } + + /** + * Return Application class loader to be executed. + */ + public ClassLoader getApplicationLoader() { + return applicationLoader; + } + + /** + * Sets the Application class loader to be executed. + */ + public void setApplicationLoader(ClassLoader applicationLoader) { + this.applicationLoader = applicationLoader; + } + + /** + * Returns the Groovy source to be executed. + */ + public String getScript() { + return script; + } + + /** + * Sets the Groovy source to be executed. + */ + public void setScript(String script) { + this.script = script; + } + +} diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyInvoker.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyInvoker.java new file mode 100644 index 0000000000..20492e5fc2 --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/GroovyInvoker.java @@ -0,0 +1,87 @@ +/* + * 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.container.groovy; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.extension.ExecutionMonitor; +import org.apache.tuscany.spi.extension.TargetInvokerExtension; +import org.apache.tuscany.spi.wire.InboundWire; + +import groovy.lang.GroovyObject; + +/** + * Dispatches to a Groovy implementation instance + * + * @version $$Rev$$ $$Date$$ + */ +public class GroovyInvoker extends TargetInvokerExtension { + + protected GroovyAtomicComponent component; + protected String operation; + protected boolean cacheable; + + public GroovyInvoker(String operation, + GroovyAtomicComponent component, + InboundWire wire, + WorkContext context, + ExecutionMonitor monitor) { + super(wire, context, monitor); + this.component = component; + this.operation = operation; + } + + public boolean isCacheable() { + return cacheable; + } + + public void setCacheable(boolean cacheable) { + this.cacheable = cacheable; + } + + public boolean isOptimizable() { + return false; + } + + /** + * Dispatches to the the target. TODO support conversational dispatch + */ + public Object invokeTarget(final Object payload, final short sequence) throws InvocationTargetException { + GroovyObject target = null; + try { + target = component.getTargetInstance(); + } catch (TargetException e) { + throw new InvocationTargetException(e); + } + Object[] args = (Object[]) payload; + try { + return target.invokeMethod(operation, args); + } catch (Exception ex) { + throw new InvocationTargetException(ex); + } + } + + public GroovyInvoker clone() throws CloneNotSupportedException { + return (GroovyInvoker) super.clone(); + } + + +} diff --git a/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.java new file mode 100644 index 0000000000..a025d0d7eb --- /dev/null +++ b/sandbox/old/contrib/implementation-groovy/container/src/main/java/org/apache/tuscany/container/groovy/ImplementationLoader.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.container.groovy; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; +import javax.xml.namespace.QName; +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.loader.LoaderUtil; +import org.apache.tuscany.spi.loader.MissingResourceException; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * Loader for handling elements. + * + * @version $Rev$ $Date$ + */ +public class ImplementationLoader extends LoaderExtension { + private static final QName IMPLEMENTATION_GROOVY = + new QName("http://tuscany.apache.org/xmlns/groovy/1.0", "implementation.groovy"); + + public ImplementationLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return IMPLEMENTATION_GROOVY; + } + + public GroovyImplementation load(CompositeComponent parent, + ModelObject object, XMLStreamReader reader, + DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + + String script = reader.getAttributeValue(null, "script"); + if (script == null) { + throw new MissingResourceException("No script supplied"); + } + String source = loadSource(deploymentContext.getClassLoader(), script); + + LoaderUtil.skipToEndElement(reader); + + GroovyImplementation implementation = new GroovyImplementation(); + implementation.setScript(source); + implementation.setApplicationLoader(deploymentContext.getClassLoader()); + implementation.setScriptResourceName(script); + + return implementation; + } + + protected String loadSource(ClassLoader cl, String resource) throws LoaderException { + URL url = cl.getResource(resource); + if (url == null) { + throw new MissingResourceException(resource); + } + InputStream is; + try { + is = url.openStream(); + } catch (IOException e) { + throw new MissingResourceException(resource, e); + } + try { + Reader reader = new InputStreamReader(is, "UTF-8"); + char[] buffer = new char[1024]; + StringBuilder source = new StringBuilder(); + int count; + while ((count = reader.read(buffer)) > 0) { + source.append(buffer, 0, count); + } + return source.toString(); + } catch (IOException e) { + throw new LoaderException(resource, e); + } finally { + try { + is.close(); + } catch (IOException e) { + // ignore + } + } + } +} -- cgit v1.2.3