diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:26:33 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:26:33 +0000 |
commit | a40e527938d76ba71f211da7e327adb50384ba69 (patch) | |
tree | cb8f99f1727122b040a3f0fbb6649292b6a74302 /sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils | |
parent | 968721109881107520d7aefa91d7fcc0519d7739 (diff) |
Moving 1.x tags
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835157 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils')
7 files changed, 716 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractBinding.java b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractBinding.java new file mode 100644 index 0000000000..3686412331 --- /dev/null +++ b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractBinding.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.extension.helper.utils; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.Binding; + +/** + * Helper for implementing Bindings, implements all the + * standard getters and setters on the binding interface. + */ +public class AbstractBinding implements Binding { + + private String name; + private String uri; + private List<Object> extensions; + private boolean unresolved; + + public String getName() { + return name; + } + + public String getURI() { + return uri; + } + + public void setName(String name) { + this.name = name; + } + + public void setURI(String uri) { + this.uri = uri; + } + + public List<Object> getExtensions() { + return extensions; + } + + public boolean isUnresolved() { + return unresolved; + } + + public void setUnresolved(boolean unresolved) { + this.unresolved = unresolved; + } + +} diff --git a/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractImplementation.java b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractImplementation.java new file mode 100644 index 0000000000..bc3c94b0ff --- /dev/null +++ b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractImplementation.java @@ -0,0 +1,114 @@ +/* + * 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.sca.extension.helper.utils; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.assembly.ConstrainingType; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; + +/** + * Helper for implementing Implementations, implements all the + * standard getters and setters on the Implementation interface. + */ +public abstract class AbstractImplementation implements Implementation { + + private List<Service> services = new ArrayList<Service>(); + private List<Reference> references = new ArrayList<Reference>(); + private List<Property> properties = new ArrayList<Property>(); + private ConstrainingType constrainingType; + private String uri; + private boolean unresolved = true; + + public AbstractImplementation() { + } + + public List<Property> getProperties() { + return properties; + } + + public List<Reference> getReferences() { + return references; + } + + public List<Service> getServices() { + return services; + } + + public String getURI() { + return uri; + } + + public void setURI(String uri) { + this.uri = uri; + } + + public ConstrainingType getConstrainingType() { + return constrainingType; + } + + public void setConstrainingType(ConstrainingType constrainingType) { + this.constrainingType = constrainingType; + } + + public List<Object> getExtensions() { + // TODO what is this for? + return null; + } + + public boolean isUnresolved() { + return unresolved; + } + + public void setUnresolved(boolean unresolved) { + this.unresolved = unresolved; + } + + public Service getService(String name) { + for (Service service : getServices()) { + if (service.getName().equals(name)) { + return service; + } + } + return null; + } + + public Reference getReference(String name) { + for (Reference reference : getReferences()) { + if (reference.getName().equals(name)) { + return reference; + } + } + return null; + } + + public Property getProptery(String name) { + for (Property property : getProperties()) { + if (property.getName().equals(name)) { + return property; + } + } + return null; + } +} diff --git a/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractStAXArtifactProcessor.java b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractStAXArtifactProcessor.java new file mode 100644 index 0000000000..197429a8cc --- /dev/null +++ b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/AbstractStAXArtifactProcessor.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.extension.helper.utils; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.impl.InterfaceContractImpl; +import org.apache.tuscany.sca.interfacedef.impl.InterfaceImpl; + +/** + * TODO: couldn't something like this class be provided by the runtime? + * Each impl shouldn't have to have their own .componentType merging code + */ +public abstract class AbstractStAXArtifactProcessor<I extends Implementation> implements StAXArtifactProcessor<I> { + + protected AssemblyFactory assemblyFactory; + + public AbstractStAXArtifactProcessor(AssemblyFactory assemblyFactory) { + this.assemblyFactory = assemblyFactory; + } + + public void resolve(I model, ModelResolver resolver) throws ContributionResolveException { + + addSideFileComponentType(model.getURI(), model, resolver); + + if (model instanceof DynamicImplementation) { + // if no services have been defined then add a dynamic one + if (model.getServices().size() < 1) { + Service dynamicService = createDynamicService(); + model.getServices().add(dynamicService); + } + } + + model.setUnresolved(false); + } + + protected void addSideFileComponentType(String name, Implementation impl, ModelResolver resolver) { + if (name == null) { + return; + } + int lastDot = name.lastIndexOf('.'); + if (lastDot < 0) { + return; + } + String sideFileName = name.substring(0, lastDot) + ".componentType"; + + ComponentType componentType = assemblyFactory.createComponentType(); + componentType.setURI(sideFileName); + componentType.setUnresolved(true); + + componentType = resolver.resolveModel(ComponentType.class, componentType); + + if (!componentType.isUnresolved()) { + for (Reference reference : componentType.getReferences()) { + impl.getReferences().add(reference); + } + for (Service service : componentType.getServices()) { + impl.getServices().add(service); + } + for (Property property : componentType.getProperties()) { + impl.getProperties().add(property); + } + if (componentType.getConstrainingType() != null) { + impl.setConstrainingType(componentType.getConstrainingType()); + } + } + } + + protected Service createDynamicService() { + Service dynamicService = assemblyFactory.createService(); + dynamicService.setName("$dynamic$"); + InterfaceContract dynamicInterfaceContract = new InterfaceContractImpl() {}; + Interface dynamicInterface = new InterfaceImpl() { + @Override + public boolean isDynamic() { + return true; + } + }; + Operation dynamicOperation = assemblyFactory.createOperation(); + dynamicOperation.setDynamic(true); + dynamicInterface.getOperations().add(dynamicOperation); + dynamicInterfaceContract.setInterface(dynamicInterface); + dynamicService.setInterfaceContract(dynamicInterfaceContract); + + return dynamicService; + } + +}
\ No newline at end of file diff --git a/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/DefaultPropertyValueObjectFactory.java b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/DefaultPropertyValueObjectFactory.java new file mode 100644 index 0000000000..d68e2dcb6b --- /dev/null +++ b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/DefaultPropertyValueObjectFactory.java @@ -0,0 +1,229 @@ +/* + * 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.sca.extension.helper.utils; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.databinding.SimpleTypeMapper; +import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * TODO: Shouldn't this class be provided by the runtime? + */ +public class DefaultPropertyValueObjectFactory implements PropertyValueObjectFactory { + // protected DataBindingRegistry dbRegistry = new DataBindingRegistryImpl(); + protected Mediator mediator = null; + protected SimpleTypeMapper simpleTypeMapper = new SimpleTypeMapperImpl(); + boolean isSimpleType; + + public DefaultPropertyValueObjectFactory(Mediator mediator) { + this.mediator = mediator; + } + + /* (non-Javadoc) + * @see org.apache.tuscany.sca.extension.helper.utils.PVOF#createValueFactory(org.apache.tuscany.sca.assembly.Property) + */ + public ObjectFactory createValueFactory(Property property) { + isSimpleType = isSimpleType(property); + Document doc = (Document)property.getValue(); + Element rootElement = doc.getDocumentElement(); + + //FIXME : since scripts use dynamic types we need to generate a dynamic java type using the + //xml structure of the property value. Should this be done in the JavaBeansDataBinding... + Class javaType = null; + + if (property.isMany()) { + if (isSimpleType) { + String value = ""; + if (rootElement.getChildNodes().getLength() > 0) { + value = rootElement.getChildNodes().item(0).getTextContent(); + } + List<String> values = + getSimplePropertyValues(value, javaType); + return new ListObjectFactoryImpl(property, + values, + isSimpleType, + javaType); + } else { + return new ListObjectFactoryImpl(property, + getComplexPropertyValues(doc), + isSimpleType, + javaType); + } + } else { + if (isSimpleType) { + String value = ""; + if (rootElement.getChildNodes().getLength() > 0) { + value = rootElement.getChildNodes().item(0).getTextContent(); + } + return new ObjectFactoryImpl(property, + value, + isSimpleType, + javaType); + } else { + Object value = getComplexPropertyValues(doc).get(0); + return new ObjectFactoryImpl(property, + value, + isSimpleType, + javaType); + } + + } + } + + private boolean isSimpleType(Property property) { + if (property.getXSDType() != null) { + return SimpleTypeMapperImpl.isSimpleXSDType(property.getXSDType()); + } else { + if (property instanceof Document) { + Document doc = (Document)property; + Element element = doc.getDocumentElement(); + if (element.getChildNodes().getLength() == 1 && + element.getChildNodes().item(0).getNodeType() == Node.TEXT_NODE) { + return true; + } + } + } + return false; + } + + private List<String> getSimplePropertyValues(String concatenatedValue, Class javaType) { + List<String> propValues = new ArrayList<String>(); + StringTokenizer st = null; + if ( javaType.getName().equals("java.lang.String")) { + st = new StringTokenizer(concatenatedValue, "\""); + } else { + st = new StringTokenizer(concatenatedValue); + } + String aToken = null; + while (st.hasMoreTokens()) { + aToken = st.nextToken(); + if (aToken.trim().length() > 0) { + propValues.add(aToken); + } + } + return propValues; + } + + private List<Node> getComplexPropertyValues(Document document) { + Element rootElement = document.getDocumentElement(); + List<Node> propValues = new ArrayList<Node>(); + for (int count = 0 ; count < rootElement.getChildNodes().getLength() ; ++count) { + if (rootElement.getChildNodes().item(count).getNodeType() == Node.ELEMENT_NODE) { + propValues.add(rootElement.getChildNodes().item(count)); + } + } + return propValues; + } + + public abstract class ObjectFactoryImplBase implements ObjectFactory { + protected SimpleTypeMapper simpleTypeMapper = new SimpleTypeMapperImpl(); + protected Property property; + protected Object propertyValue; + protected Class javaType; + protected DataType<XMLType> sourceDataType; + protected DataType<?> targetDataType; + boolean isSimpleType; + + public ObjectFactoryImplBase(Property property, Object propertyValue, boolean isSimpleType, Class javaType) { + + this.isSimpleType = isSimpleType; + this.property = property; + this.propertyValue = propertyValue; + this.javaType = javaType; + //FIXME : fix this when we have managed to generate dynamic java types + /*sourceDataType = + new DataTypeImpl<XMLType>(DOMDataBinding.NAME, Node.class, + new XMLType(null, this.property.getXSDType())); + TypeInfo typeInfo = null; + if (this.property.getXSDType() != null) { + if (SimpleTypeMapperExtension.isSimpleXSDType(this.property.getXSDType())) { + typeInfo = new TypeInfo(property.getXSDType(), true, null); + } else { + typeInfo = new TypeInfo(property.getXSDType(), false, null); + } + } else { + typeInfo = new TypeInfo(property.getXSDType(), false, null); + } + + XMLType xmlType = new XMLType(typeInfo); + String dataBinding = null; //(String)property.getExtensions().get(DataBinding.class.getName()); + if (dataBinding != null) { + targetDataType = new DataTypeImpl<XMLType>(dataBinding, javaType, xmlType); + } else { + targetDataType = new DataTypeImpl<XMLType>(dataBinding, javaType, xmlType); + mediator.getDataBindingRegistry().introspectType(targetDataType, null); + }*/ + } + } + + public class ObjectFactoryImpl extends ObjectFactoryImplBase { + public ObjectFactoryImpl(Property property, Object propertyValue, boolean isSimpleType, Class javaType) { + super(property, propertyValue, isSimpleType, javaType); + } + + @SuppressWarnings("unchecked") + public Object getInstance() throws ObjectCreationException { + if (isSimpleType) { + return simpleTypeMapper.toJavaObject(property.getXSDType(), (String)propertyValue, null); + } else { + return mediator.mediate(propertyValue, sourceDataType, targetDataType, null); + //return null; + } + } + } + + public class ListObjectFactoryImpl extends ObjectFactoryImplBase { + public ListObjectFactoryImpl(Property property, List<?>propertyValues, boolean isSimpleType, Class javaType) { + super(property, propertyValues, isSimpleType, javaType); + } + + @SuppressWarnings("unchecked") + public List<?> getInstance() throws ObjectCreationException { + if (isSimpleType) { + List<Object> values = new ArrayList<Object>(); + for (String aValue : (List<String>)propertyValue) { + values.add(simpleTypeMapper.toJavaObject(property.getXSDType(), aValue, null)); + } + return values; + } else { + List instances = new ArrayList(); + for (Node aValue : (List<Node>)propertyValue) { + instances.add(mediator.mediate(aValue, + sourceDataType, + targetDataType, + null)); + } + return instances; + } + } + } +} +
\ No newline at end of file diff --git a/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/DynamicImplementation.java b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/DynamicImplementation.java new file mode 100644 index 0000000000..010a647ee7 --- /dev/null +++ b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/DynamicImplementation.java @@ -0,0 +1,92 @@ +/* + * 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.sca.extension.helper.utils; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.builder.ComponentPreProcessor; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +public class DynamicImplementation extends AbstractImplementation implements ComponentPreProcessor { + + public void preProcess(Component component) { + RuntimeComponent rtc = (RuntimeComponent) component; + + for (Service service : rtc.getServices()) { + if (getService(service.getName()) == null) { + getServices().add(createService(service)); + } + } + + Service dynamicService = getService("$dynamic$"); + if (dynamicService != null && getServices().size() > 1) { + getServices().remove(dynamicService); + dynamicService = null; + } + + for (Reference reference : rtc.getReferences()) { + if (getReference(reference.getName()) == null) { + getReferences().add(createReference(reference)); + } + } + + for (Property property : rtc.getProperties()) { + if (getProptery(property.getName()) == null) { + getProperties().add(createProperty(property)); + } + } + + // TODO: support properties + } + + protected Service createService(Service service) { + Service newService; + try { + newService = (Service)service.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(e); // should not ever happen + } + + return newService; + } + + protected Reference createReference(Reference reference) { + Reference newReference; + try { + newReference = (Reference)reference.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(e); // should not ever happen + } + return newReference; + } + + protected Property createProperty(Property property) { + Property newProperty; + try { + newProperty = (Property)property.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(e); // should not ever happen + } + return newProperty; + } + +} diff --git a/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/PropertyValueObjectFactory.java b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/PropertyValueObjectFactory.java new file mode 100644 index 0000000000..8f032c68f5 --- /dev/null +++ b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/PropertyValueObjectFactory.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.extension.helper.utils; + +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +public interface PropertyValueObjectFactory { + + public abstract ObjectFactory createValueFactory(Property property); + +} diff --git a/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/ResourceHelper.java b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/ResourceHelper.java new file mode 100644 index 0000000000..042752b405 --- /dev/null +++ b/sca-java-1.x/tags/1.0.1/modules/extension-helper/src/main/java/org/apache/tuscany/sca/extension/helper/utils/ResourceHelper.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.extension.helper.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; + +/** + * TODO: Shouldn't this be using the contrabution service? + */ +public class ResourceHelper { + + public static String readResource(String scriptName) { + + //FIXME The classloader should be passed in + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL scriptSrcUrl = cl.getResource(scriptName); + if (scriptSrcUrl == null) { + throw new RuntimeException("No script: " + scriptName); + } + + InputStream is; + try { + is = scriptSrcUrl.openStream(); + } catch (IOException e) { + throw new RuntimeException(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 RuntimeException(e); + } finally { + try { + is.close(); + } catch (IOException e) { + // ignore + } + } + } + +} |