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 --- .../loader/AbstractImplementationLoader.java | 160 +++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 sandbox/container.ruby/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java (limited to 'sandbox/container.ruby/src/main/java/org/apache/tuscany/container/extension/loader') diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/extension/loader/AbstractImplementationLoader.java new file mode 100644 index 0000000000..f5115a5b41 --- /dev/null +++ b/sandbox/container.ruby/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 implements StAXElementLoader { + + // 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 Ruby implementation. + * + * @param Stax XML stream reader. + * @param loaderContext Loader context. + * @return Ruby 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 + } + } + } + +} -- cgit v1.2.3