From e5b7380c874745c989d1816b8f552504f038e1bc Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 26 Sep 2013 20:33:20 +0000 Subject: 2.0 branch for possible maintenance release git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1526672 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/databinding/sdo/DataObject2Node.java | 75 ++++++ .../sca/databinding/sdo/DataObject2String.java | 64 +++++ .../sdo/DataObject2XMLStreamReader.java | 82 +++++++ .../tuscany/sca/databinding/sdo/ImportSDO.java | 79 +++++++ .../sca/databinding/sdo/ImportSDOProcessor.java | 215 +++++++++++++++++ .../sca/databinding/sdo/Node2DataObject.java | 76 ++++++ .../sca/databinding/sdo/SDOContextHelper.java | 258 +++++++++++++++++++++ .../sca/databinding/sdo/SDODataBinding.java | 174 ++++++++++++++ .../sca/databinding/sdo/SDOSimpleTypeMapper.java | 82 +++++++ .../tuscany/sca/databinding/sdo/SDOTypeHelper.java | 256 ++++++++++++++++++++ .../tuscany/sca/databinding/sdo/SDOTypes.java | 107 +++++++++ .../sca/databinding/sdo/SDOWrapperHandler.java | 192 +++++++++++++++ .../sca/databinding/sdo/String2DataObject.java | 61 +++++ .../sca/databinding/sdo/XMLDocument2String.java | 65 ++++++ .../sdo/XMLDocument2XMLStreamReader.java | 69 ++++++ .../sdo/XMLStreamReader2DataObject.java | 72 ++++++ .../sdo/XMLStreamReader2XMLDocument.java | 66 ++++++ .../databinding/sdo/xml/SDOTypesModelResolver.java | 234 +++++++++++++++++++ .../sca/databinding/sdo/xml/SDOTypesProcessor.java | 109 +++++++++ 19 files changed, 2336 insertions(+) create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2Node.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2String.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReader.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDO.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOProcessor.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/Node2DataObject.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOSimpleTypeMapper.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypeHelper.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypes.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/String2DataObject.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2String.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2XMLStreamReader.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2DataObject.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2XMLDocument.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesModelResolver.java create mode 100644 sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesProcessor.java (limited to 'sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany') diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2Node.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2Node.java new file mode 100644 index 0000000000..80f4602fac --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2Node.java @@ -0,0 +1,75 @@ +/* + * 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.databinding.sdo; + +import javax.xml.namespace.QName; +import javax.xml.transform.dom.DOMResult; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; + + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + +public class DataObject2Node extends BaseTransformer implements + PullTransformer { + + public Node transform(DataObject source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, true); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + QName elementName = SDOContextHelper.getElement(context); + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + DOMResult result = new DOMResult(doc); + XMLDocument xmlDoc = xmlHelper.createDocument(source, elementName.getNamespaceURI(), elementName.getLocalPart()); + xmlHelper.save(xmlDoc, result, null); + return doc.getDocumentElement(); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return DataObject.class; + } + + @Override + protected Class getTargetType() { + return Node.class; + } + + @Override + public int getWeight() { + return 40; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2String.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2String.java new file mode 100644 index 0000000000..eaaf153dd5 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2String.java @@ -0,0 +1,64 @@ +/* + * 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.databinding.sdo; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLHelper; + +public class DataObject2String extends BaseTransformer implements + PullTransformer { + + public String transform(DataObject source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, true); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + QName elementName = SDOContextHelper.getElement(context); + return xmlHelper.save(source, elementName.getNamespaceURI(), elementName.getLocalPart()); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return DataObject.class; + } + + @Override + protected Class getTargetType() { + return String.class; + } + + @Override + public int getWeight() { + return 40; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReader.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReader.java new file mode 100644 index 0000000000..7d66810ca0 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/DataObject2XMLStreamReader.java @@ -0,0 +1,82 @@ +/* + * 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.databinding.sdo; + +import java.security.AccessController; +import java.security.PrivilegedAction; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sdo.api.XMLStreamHelper; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + +public class DataObject2XMLStreamReader extends BaseTransformer implements + PullTransformer { + + public XMLStreamReader transform(final DataObject source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, true); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext); + final QName elementName = SDOContextHelper.getElement(context); + final XMLHelper xmlHelper = helperContext.getXMLHelper(); + // Allow privileged access to read properties. REquires java.util.PropertyPermission + // XML.load.form.lax read in security policy. + XMLDocument document = AccessController.doPrivileged(new PrivilegedAction() { + public XMLDocument run() { + return xmlHelper.createDocument(source, elementName.getNamespaceURI(), elementName.getLocalPart()); + } + }); + + return streamHelper.createXMLStreamReader(document); + } catch (XMLStreamException e) { + // TODO: Add context to the exception + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return DataObject.class; + } + + @Override + protected Class getTargetType() { + return XMLStreamReader.class; + } + + @Override + public int getWeight() { + return 10; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDO.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDO.java new file mode 100644 index 0000000000..d59698cdb3 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDO.java @@ -0,0 +1,79 @@ +/* + * 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.databinding.sdo; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Base; +import org.apache.tuscany.sca.assembly.impl.BaseImpl; + +import commonj.sdo.helper.HelperContext; + +/** + * The model object for import.sdo + * + * @version $Rev$ $Date$ + */ +public class ImportSDO extends BaseImpl { + public static final QName IMPORT_SDO = + new QName(Base.SCA11_TUSCANY_NS, "import.sdo"); + + private HelperContext helperContext; + private String factoryClassName; + private String schemaLocation; + + public ImportSDO(HelperContext helperContext) { + super(); + this.helperContext = helperContext; + setUnresolved(true); + } + + public HelperContext getHelperContext() { + return helperContext; + } + + /** + * @return the factoryClassName + */ + public String getFactoryClassName() { + return factoryClassName; + } + + /** + * @param factoryClassName the factoryClassName to set + */ + public void setFactoryClassName(String factoryClassName) { + this.factoryClassName = factoryClassName; + } + + /** + * @return the schemaLocation + */ + public String getSchemaLocation() { + return schemaLocation; + } + + /** + * @param schemaLocation the schemaLocation to set + */ + public void setSchemaLocation(String schemaLocation) { + this.schemaLocation = schemaLocation; + } +} \ No newline at end of file diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOProcessor.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOProcessor.java new file mode 100644 index 0000000000..117d61ff26 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOProcessor.java @@ -0,0 +1,215 @@ +/* + * 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.databinding.sdo; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static org.apache.tuscany.sca.databinding.sdo.ImportSDO.IMPORT_SDO; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLConnection; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; +import org.apache.tuscany.sca.monitor.impl.ProblemImpl; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XSDHelper; + +/** + * Loader that handles <import.sdo> elements. + * + * @version $Rev$ $Date$ + * @deprecated + */ +@Deprecated +public class ImportSDOProcessor implements StAXArtifactProcessor { + + private ContributionFactory contributionFactory; + + public ImportSDOProcessor(ExtensionPointRegistry registry) { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + new ProblemImpl(this.getClass().getName(), "databinding-sdo-validation-messages", Severity.ERROR, + message, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "databinding-sdo-validation-messages", Severity.ERROR, message,model, message, ex); + monitor.problem(problem); + } + } + + public QName getXMLType() { + return IMPORT_SDO; + } + + public ImportSDO read(XMLStreamReader reader,ProcessorContext context) throws ContributionReadException, XMLStreamException { + assert IMPORT_SDO.equals(reader.getName()); + + // FIXME: How do we associate the application HelperContext with the one + // imported by the composite + ImportSDO importSDO = new ImportSDO(SDOContextHelper.getDefaultHelperContext()); + String factoryName = reader.getAttributeValue(null, "factory"); + if (factoryName != null) { + importSDO.setFactoryClassName(factoryName); + } + String location = reader.getAttributeValue(null, "location"); + if (location != null) { + importSDO.setSchemaLocation(location); + } + + // Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && ImportSDO.IMPORT_SDO.equals(reader.getName())) { + break; + } + } + return importSDO; + } + + private void importFactory(ImportSDO importSDO, ModelResolver resolver,ProcessorContext context) throws ContributionResolveException { + String factoryName = importSDO.getFactoryClassName(); + if (factoryName != null) { + ClassReference reference = new ClassReference(factoryName); + ClassReference resolved = resolver.resolveModel(ClassReference.class, reference,context); + if (resolved != null && !resolved.isUnresolved()) { + try { + Class factoryClass = resolved.getJavaClass(); + register(factoryClass, importSDO.getHelperContext()); + importSDO.setUnresolved(false); + } catch (Exception e) { + ContributionResolveException ce = new ContributionResolveException(e); + error(context.getMonitor(), "ContributionResolveException", resolver, ce); + //throw ce; + } + } else { + error(context.getMonitor(), "FailToResolveClass", resolver, factoryName); + //ContributionResolveException loaderException = + //new ContributionResolveException("Fail to resolve class: " + factoryName); + //throw loaderException; + } + } + } + + private static void register(Class factoryClass, HelperContext helperContext) throws Exception { + Field field = factoryClass.getField("INSTANCE"); + Object factory = field.get(null); + Method method = factory.getClass().getMethod("register", new Class[] {HelperContext.class}); + method.invoke(factory, new Object[] {helperContext}); + + // HelperContext defaultContext = HelperProvider.getDefaultContext(); + // method.invoke(factory, new Object[] {defaultContext}); + } + + private void importWSDL(ImportSDO importSDO, ModelResolver resolver,ProcessorContext context) throws ContributionResolveException { + String location = importSDO.getSchemaLocation(); + if (location != null) { + try { + Artifact artifact = contributionFactory.createArtifact(); + artifact.setURI(location); + artifact = resolver.resolveModel(Artifact.class, artifact,context); + if (artifact.getLocation() != null) { + String wsdlURL = artifact.getLocation(); + URLConnection connection = new URL(wsdlURL).openConnection(); + connection.setUseCaches(false); + InputStream xsdInputStream = connection.getInputStream(); + try { + XSDHelper xsdHelper = importSDO.getHelperContext().getXSDHelper(); + xsdHelper.define(xsdInputStream, wsdlURL); + } finally { + xsdInputStream.close(); + } + importSDO.setUnresolved(false); + } else { + error(context.getMonitor(), "FailToResolveLocation", resolver, location); + //ContributionResolveException loaderException = new ContributionResolveException("Fail to resolve location: " + location); + //throw loaderException; + } + } catch (IOException e) { + ContributionResolveException ce = new ContributionResolveException(e); + error(context.getMonitor(), "ContributionResolveException", resolver, ce); + //throw ce; + } + } + } + + public QName getArtifactType() { + return ImportSDO.IMPORT_SDO; + } + + public void write(ImportSDO model, XMLStreamWriter outputSource,ProcessorContext context) throws ContributionWriteException { + // Not implemented as is deprecated + } + + public Class getModelType() { + return ImportSDO.class; + } + + public void resolve(ImportSDO importSDO, ModelResolver resolver,ProcessorContext context) throws ContributionResolveException { + importFactory(importSDO, resolver,context); + importWSDL(importSDO, resolver,context); + if (!importSDO.isUnresolved()) { + resolver.addModel(importSDO,context); + } + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/Node2DataObject.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/Node2DataObject.java new file mode 100644 index 0000000000..a7fc9aeb5c --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/Node2DataObject.java @@ -0,0 +1,76 @@ +/* + * 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.databinding.sdo; + +import javax.xml.transform.dom.DOMSource; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + +public class Node2DataObject extends BaseTransformer implements PullTransformer { + +private DOMHelper helper; + +public Node2DataObject(ExtensionPointRegistry registry) { + super(); + helper = DOMHelper.getInstance(registry); +} + public DataObject transform(Node source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, false); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + Document doc = helper.promote(source); + DOMSource domSource = new DOMSource(doc); + XMLDocument xmlDoc = xmlHelper.load(domSource, doc.getDocumentURI(), null); + return xmlDoc.getRootObject(); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return Node.class; + } + + @Override + protected Class getTargetType() { + return DataObject.class; + } + + @Override + public int getWeight() { + return 40; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java new file mode 100644 index 0000000000..23063317e7 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java @@ -0,0 +1,258 @@ +/* + * 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.databinding.sdo; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import javax.xml.datatype.DatatypeFactory; +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.common.java.collection.LRUCache; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.databinding.util.DataTypeHelper; +import org.apache.tuscany.sca.extensibility.ClassLoaderContext; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.apache.tuscany.sdo.api.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.impl.HelperProvider; + +/** + * Helper class to get TypeHelper from the context + * + * @version $Rev$ $Date$ + */ +public final class SDOContextHelper { + private static final LRUCache cache = new LRUCache(1024); + + private static ExtensionPointRegistry registry = null; + private static HelperContext defaultHelperContext; + + private SDOContextHelper() { + } + + /** + * Static method used to set the registry used to locate the SDO implementation provider + * MUST be called before using any other methods on this class + * @param theRegistry + */ + public static void setRegistry( ExtensionPointRegistry theRegistry ) { + registry = theRegistry; + } // end setRegistry + + public static HelperContext getHelperContext(TransformationContext context, boolean source) { + if (context == null) { + return getDefaultHelperContext(); + } + + HelperContext helperContext = null; + Operation op = source ? context.getSourceOperation() : context.getTargetOperation(); + if (op == null) { + DataType dt = source ? context.getSourceDataType() : context.getTargetDataType(); + + helperContext = dt.getMetaData(HelperContext.class); + if (helperContext != null) { + return helperContext; + } + helperContext = SDOUtil.createHelperContext(); + boolean found = register(helperContext, dt); + if (!found) { + helperContext = getDefaultHelperContext(); + } + dt.setMetaData(HelperContext.class, helperContext); + return helperContext; + } else { + return getHelperContext(op); + } + + } + + public static HelperContext getHelperContext(Operation op) { + if (op == null) { + return getDefaultHelperContext(); + } + + HelperContext helperContext = op.getInputType().getMetaData(HelperContext.class); + + if (helperContext != null) { + return helperContext; + } + + // Use the default HelperContext until a type is registered later on + helperContext = getDefaultHelperContext(); + + /* + List dataTypes = DataTypeHelper.getDataTypes(op, false); + boolean found = false; + for (DataType d : dataTypes) { + if (register(helperContext, d)) { + found = true; + } + } + if (!found) { + helperContext = getDefaultHelperContext(); + } + */ + op.getInputType().setMetaData(HelperContext.class, helperContext); + return helperContext; + } + + /** + * @param helperContext + * @param dataType + * @return + */ + private static boolean register(HelperContext helperContext, DataType dataType) { + if (dataType == null) { + return false; + } + Set> classes = DataTypeHelper.findClasses(dataType); + boolean found = false; + for (Class cls : classes) { + found = register(helperContext, dataType.getPhysical()) || found; + } + return found; + } + + /** + * FIXME: [rfeng] This is a hack to get the factory out a SDO class + * @param helperContext + * @param javaType + */ + + public static boolean register(HelperContext helperContext, Class javaType) { + if (javaType == null || DataObject.class == javaType) { + return false; + } + try { + Type type = helperContext.getTypeHelper().getType(javaType); + return register(helperContext, type); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public static boolean register(HelperContext helperContext, Type type) { + if (type != null && (!type.isDataType())) { + try { + Method method = type.getClass().getMethod("getEPackage"); + Object factory = method.invoke(type, new Object[] {}); + method = factory.getClass().getMethod("register", HelperContext.class); + method.invoke(factory, new Object[] {helperContext}); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } else { + return false; + } + } + + public static HelperContext getDefaultHelperContext( ) { + // Return a chached value if available... + if( defaultHelperContext != null ) return defaultHelperContext; + + // Try to set up TCCL so that SDO Helper Provider service discovery works in OSGi + if( registry == null ) return null; + + ClassLoader oldTccl = + ClassLoaderContext.setContextClassLoader(SDOContextHelper.class.getClassLoader(), + registry.getServiceDiscovery(), + // SDO Helper Provider + "commonj.sdo.impl.HelperProvider" + ); + try { + // Load the HelperProvider (using the new TCCL) and get the default HelperContext + // cache the returned HelperContext... + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + HelperProvider.setDefaultInstance(tccl); + defaultHelperContext = HelperProvider.getDefaultContext(); + return defaultHelperContext; + } catch (Exception e ){ + e.printStackTrace(); + return null; + } finally { + if (oldTccl != null) { + Thread.currentThread().setContextClassLoader(oldTccl); + } + } // end try + } // end getDefaultHelperContext() + + public static QName getElement(TransformationContext context) { + if (context == null) { + return SDODataBinding.ROOT_ELEMENT; + } + DataType dataType = context.getTargetDataType(); + Object logical = dataType.getLogical(); + QName elementName = null; + if (logical instanceof XMLType) { + XMLType xmlType = (XMLType)logical; + QName element = xmlType.getElementName(); + if (element != null) { + elementName = element; + } + } + if (elementName == null) { + // Try source type + dataType = context.getSourceDataType(); + logical = dataType.getLogical(); + if (logical instanceof XMLType) { + XMLType xmlType = (XMLType)logical; + QName element = xmlType.getElementName(); + if (element != null) { + elementName = element; + } + } + } + if (elementName != null) { + return elementName; + } else { + return SDODataBinding.ROOT_ELEMENT; + } + } + + public static String generateSchema(HelperContext context, Class[] classes) { + TypeHelper typeHelper = context.getTypeHelper(); + List types = new ArrayList(); + for (Class cls : classes) { + Type type = typeHelper.getType(cls); + if (type != null) { + types.add(type); + } + } + return generateSchema(context, types); + } + + public static String generateSchema(HelperContext context, List types) { + // FIXME: SDO throws IllegalArgumentException for types generated from existing XSDs + return context.getXSDHelper().generate(types); + } +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java new file mode 100644 index 0000000000..ef46a2b3c7 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java @@ -0,0 +1,174 @@ +/* + * 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.databinding.sdo; + +import java.lang.reflect.ParameterizedType; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Collection; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.BaseDataBinding; +import org.apache.tuscany.sca.databinding.WrapperHandler; +import org.apache.tuscany.sca.databinding.XMLTypeHelper; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.apache.tuscany.sdo.api.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.CopyHelper; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; + +/** + * SDO Databinding + * + * @version $Rev$ $Date$ + */ +public class SDODataBinding extends BaseDataBinding { + public static final String NAME = DataObject.class.getName(); + + public static final String ROOT_NAMESPACE = "commonj.sdo"; + public static final QName ROOT_ELEMENT = new QName(ROOT_NAMESPACE, "dataObject"); + + private WrapperHandler wrapperHandler; + private XMLTypeHelper xmlTypeHelper; + + public SDODataBinding( ExtensionPointRegistry registry ) { + super(NAME, DataObject.class); + SDOContextHelper.setRegistry( registry ); + wrapperHandler = new SDOWrapperHandler(); + xmlTypeHelper = new SDOTypeHelper(); + } + + @Override + public boolean introspect(DataType dataType, final Operation operation) { + Class javaType = dataType.getPhysical(); + // Allow privileged access to read system properties. Requires PropertyPermission + // java.specification.version read in security policy. + final HelperContext context = AccessController.doPrivileged(new PrivilegedAction() { + public HelperContext run() { + return SDOContextHelper.getHelperContext(operation); + } + }); + + final Type type = context.getTypeHelper().getType(javaType); + if (type == null) { + // FIXME: Need a better to test dynamic SDO + // TUSCANY-3298: get underlying element type for collections + boolean isMany = false; + if (Collection.class.isAssignableFrom(javaType)) { + java.lang.reflect.Type genericType = dataType.getGenericType(); + if (genericType instanceof ParameterizedType) { + java.lang.reflect.Type actualType = ((ParameterizedType)genericType).getActualTypeArguments()[0]; + if (actualType instanceof Class) { + javaType = (Class)actualType; + isMany = true; + } + } + } + if (DataObject.class.isAssignableFrom(javaType)) { + // Dynamic SDO + dataType.setDataBinding(getName()); + // TUSCANY-3298: use XMLType many value to indicate a collection + Object logical = dataType.getLogical(); + if (logical == null) { + if (!isMany) { + dataType.setLogical(XMLType.UNKNOWN); + } else { + XMLType xmlType = new XMLType(null, null); + xmlType.setMany(true); + dataType.setLogical(xmlType); + } + } else if (logical instanceof XMLType && isMany) { + ((XMLType)logical).setMany(true); + } + return true; + } + return false; + } + if (type.isDataType()) { + // FIXME: Ignore simple types? + return false; + } + + // Found a SDO type, replace the default context with a private one + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + if (context == SDOContextHelper.getDefaultHelperContext()) { + HelperContext newContext = SDOUtil.createHelperContext(); + SDOContextHelper.register(newContext, type); + if (operation != null) { + operation.getInputType().setMetaData(HelperContext.class, newContext); + } + } else { + SDOContextHelper.register(context, type); + } + return null; + } + }); + + String namespace = type.getURI(); + String name = context.getXSDHelper().getLocalName(type); + QName xmlType = new QName(namespace, name); + dataType.setDataBinding(getName()); + QName elementName = null; + Object logical = dataType.getLogical(); + if (logical instanceof XMLType) { + elementName = ((XMLType)logical).getElementName(); + } + dataType.setLogical(new XMLType(elementName, xmlType)); + + return true; + } + + @Override + public WrapperHandler getWrapperHandler() { + return wrapperHandler; + } + + @Override + public XMLTypeHelper getXMLTypeHelper() { + // return new SDOTypeHelper(); + return xmlTypeHelper; + } + + @Override + public Object copy(Object arg, DataType dataType, DataType targetDataType, Operation operation, Operation targetOperation) { + HelperContext context = SDOContextHelper.getHelperContext(operation); + CopyHelper copyHelper = context.getCopyHelper(); + if (arg instanceof XMLDocument) { + XMLDocument document = (XMLDocument)arg; + DataObject dataObject = copyHelper.copy(document.getRootObject()); + return context.getXMLHelper().createDocument(dataObject, + document.getRootElementURI(), + document.getRootElementName()); + } else if (arg instanceof DataObject) { + return context.getCopyHelper().copy((DataObject)arg); + } else { + return super.copy(arg, dataType, targetDataType, operation, targetOperation); + } + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOSimpleTypeMapper.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOSimpleTypeMapper.java new file mode 100644 index 0000000000..7e26990b32 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOSimpleTypeMapper.java @@ -0,0 +1,82 @@ +/* + * 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.databinding.sdo; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.databinding.SimpleTypeMapper; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sca.interfacedef.util.TypeInfo; + +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + +/** + * SDO Java/XML mapping for simple XSD types + * + * @version $Rev$ $Date$ + */ +public class SDOSimpleTypeMapper implements SimpleTypeMapper { + public static final String URI_2001_SCHEMA_XSD = "http://www.w3.org/2001/XMLSchema"; + + public SDOSimpleTypeMapper() { + super(); + } + + public Object toJavaObject(QName typeName, String value, TransformationContext context) { + Type type = null; + if (URI_2001_SCHEMA_XSD.equals(typeName.getNamespaceURI())) { + type = SDOUtil.getXSDSDOType(typeName.getLocalPart()); + } else { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, false); + TypeHelper typeHelper = helperContext.getTypeHelper(); + type = typeHelper.getType(typeName.getNamespaceURI(), typeName.getLocalPart()); + } + return SDOUtil.createFromString(type, value); + } + + public String toXMLLiteral(QName typeName, Object obj, TransformationContext context) { + Type type = null; + if (URI_2001_SCHEMA_XSD.equals(typeName.getNamespaceURI())) { + type = SDOUtil.getXSDSDOType(typeName.getLocalPart()); + } else { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, true); + TypeHelper typeHelper = helperContext.getTypeHelper(); + type = typeHelper.getType(typeName.getNamespaceURI(), typeName.getLocalPart()); + } + return SDOUtil.convertToString(type, obj); + } + + public Class getJavaType(QName xmlType) { + + return null; + } + + public TypeInfo getXMLType(Class javaType) { + return null; + } + + public boolean isSimpleXSDType(QName xmlType) { + return false; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypeHelper.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypeHelper.java new file mode 100644 index 0000000000..4c2075fcc4 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypeHelper.java @@ -0,0 +1,256 @@ +/* + * 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.databinding.sdo; + +import java.beans.Introspector; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.XMLTypeHelper; +import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.util.JavaXMLMapper; +import org.apache.tuscany.sca.interfacedef.util.TypeInfo; +import org.apache.tuscany.sca.interfacedef.util.WrapperInfo; +import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.apache.tuscany.sca.xsd.XSDFactory; +import org.apache.tuscany.sca.xsd.XSDefinition; +import org.oasisopen.sca.ServiceRuntimeException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSParser; + +import commonj.sdo.Type; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XSDHelper; + +public class SDOTypeHelper implements XMLTypeHelper { + private static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"; + + private TypeHelper typeHelper; + private XSDHelper xsdHelper; + + private ProcessorContext context; + // private Map> xsdTypesMap = new HashMap>(); + // private Map> typesMap = new HashMap>(); + + public SDOTypeHelper( ProcessorContext context ) { + super(); + this.context=context; + //Should we use this.context to get helper objects ??? + typeHelper = SDOContextHelper.getDefaultHelperContext().getTypeHelper(); + xsdHelper = SDOContextHelper.getDefaultHelperContext().getXSDHelper(); + } + //Should we remove this constructor???? otherwise we context gets created + public SDOTypeHelper() { + this(null); + /* + super(); + this.context=null; + //Should we use this.context to get helper objects ??? + typeHelper = SDOContextHelper.getDefaultHelperContext().getTypeHelper(); + xsdHelper = SDOContextHelper.getDefaultHelperContext().getXSDHelper(); + */ + } + public TypeInfo getTypeInfo(Class javaType, Object logical) { + QName xmlType = JavaXMLMapper.getXMLType(javaType); + if (xmlType != null) { + return new TypeInfo(xmlType, true, null); + } else { + // introspect(javaType, xsdTypesMap, typesMap); + if (logical instanceof XMLType) { + xmlType = ((XMLType)logical).getTypeName(); + } + if (xmlType == null) { + // TUSCANY-3298: dynamic SDO or collection of dynamic SDO + return new TypeInfo(SimpleTypeMapperImpl.XSD_ANYTYPE, true, null); + } + return new TypeInfo(xmlType, false, null); + } + } + + private void introspect(Class javaType, Map> xsdTypesMap, Map> typesMap) { + Type type = typeHelper.getType(javaType); + if (type == null) { + return; + } + if (xsdHelper.isXSD(type)) { + addToMap(xsdTypesMap, type); + } else { + addToMap(typesMap, type); + } + } + + private void addToMap(Map> map, Type type) { + List types = map.get(type.getURI()); + if (types == null) { + types = new ArrayList(); + map.put(type.getURI(), types); + } + if (!types.contains(type)) { + types.add(type); + } + } + + /* + public List getSchemaDefinitions(XSDFactory factory, ModelResolver resolver) { + List definitions = new ArrayList(); + generateSDOSchemas(definitions, factory, typesMap); + addResolvedXSDs(definitions, factory, resolver, xsdTypesMap); + return definitions; + } + */ + + private void generateSDOSchemas(List definitions, XSDFactory factory, Map> map) { + for (Map.Entry> entry : map.entrySet()) { + List types = entry.getValue(); + String ns = entry.getKey(); + generateSchema(definitions, factory, types, ns); + } + } + + private void generateSchema(List definitions, XSDFactory factory, List types, String ns) { + String schema = xsdHelper.generate(types); + DOMImplementationRegistry registry = null; + try { + registry = DOMImplementationRegistry.newInstance(); + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + DOMImplementation impl = registry.getDOMImplementation("XML 3.0"); + DOMImplementationLS ls = (DOMImplementationLS)impl.getFeature("LS", "3.0"); + LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, SCHEMA_NS); + LSInput input = ls.createLSInput(); + input.setCharacterStream(new StringReader(schema)); + Document document = parser.parse(input); + XSDefinition definition = factory.createXSDefinition(); + definition.setUnresolved(true); + definition.setDocument(document); + definition.setNamespace(ns); + definitions.add(definition); + } + + private void addResolvedXSDs(List definitions, + XSDFactory factory, + ModelResolver resolver, + Map> map) { + for (Map.Entry> entry : map.entrySet()) { + XSDefinition definition = factory.createXSDefinition(); + definition.setUnresolved(true); + definition.setNamespace(entry.getKey()); + //FIXME: set location URI + + XSDefinition resolved = resolver.resolveModel(XSDefinition.class, definition,context); + if (resolved.getSchema() == null) { + //FIXME: create a checked exception and propagate it back up to the activator + throw new RuntimeException("No XSD found for namespace " + entry.getKey()); + } + // make sure all the required types are defined in the resolved schema + for (Type type : entry.getValue()) { + String name = xsdHelper.getLocalName(type); + QName typeName = null; + if (name.endsWith("_._type")) { + // FIXME: Anonymous tyype + name = name.substring(0, name.length() - "_._type".length()); + typeName = new QName(type.getURI(), name); + if (resolved.getXmlSchemaElement(typeName) == null) { + //FIXME: create a checked exception and propagate it back up to the activator + throw new RuntimeException("No XSD found for " + typeName.toString()); + } + } else { + typeName = new QName(type.getURI(), name); + if (resolved.getXmlSchemaType(typeName) == null) { + //FIXME: create a checked exception and propagate it back up to the activator + throw new RuntimeException("No XSD found for " + typeName.toString()); + } + } + + } + definitions.add(resolved); + } + } + + private static List getDataTypes(Interface intf) { + List dataTypes = new ArrayList(); + for (Operation op : intf.getOperations()) { + + WrapperInfo inputWrapperInfo = op.getInputWrapper(); + WrapperInfo outputWrapperInfo = op.getOutputWrapper(); + + if ((inputWrapperInfo != null) && (outputWrapperInfo != null)) { + DataType dt1 = inputWrapperInfo.getWrapperType(); + if (dt1 != null) { + dataTypes.add(dt1); + } + DataType dt2 = outputWrapperInfo.getWrapperType(); + if (dt2 != null) { + dataTypes.add(dt2); + } + } else { + for (DataType dt1 : op.getInputType().getLogical()) { + dataTypes.add(dt1); + } + DataType dt2 = op.getOutputType(); + if (dt2 != null) { + dataTypes.add(dt2); + } + for (DataType dt3 : op.getFaultTypes()) { + DataType dt4 = dt3.getLogical(); + if (dt4 != null) { + dataTypes.add(dt4); + } + } + } + } + return dataTypes; + } + + public List getSchemaDefinitions(XSDFactory factory, ModelResolver resolver, Interface intf) { + return getSchemaDefinitions(factory, resolver, getDataTypes(intf)); + } + + public List getSchemaDefinitions(XSDFactory factory, ModelResolver resolver, List dataTypes) { + Map> xsdTypesMap = new HashMap>(); + Map> typesMap = new HashMap>(); + for (DataType d : dataTypes) { + if (SDODataBinding.NAME.equals(d.getDataBinding())) { + introspect(d.getPhysical(), xsdTypesMap, typesMap); + } + } + List definitions = new ArrayList(); + generateSDOSchemas(definitions, factory, typesMap); + addResolvedXSDs(definitions, factory, resolver, xsdTypesMap); + return definitions; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypes.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypes.java new file mode 100644 index 0000000000..1ef689185a --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOTypes.java @@ -0,0 +1,107 @@ +/* + * 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.databinding.sdo; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Base; + +import commonj.sdo.Type; + +/** + * The model object for sdo.types + * + * @version $Rev$ $Date$ + * + * This extends the META-INF/sca-contribution.xml to register SDO types from static factory classes or WSDL/XSD files + * + * <contribution xmlns="http://tuscany.apache.org/xmlns/sca/1.1"
+ *     xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0">
+ *     <tuscany:sdo.types namespace="http://helloworld" location="wsdl/helloworld.wsdl"/>
+ * </contribution> + * + */ +public class SDOTypes { + public static final QName SDO_TYPES = new QName(Base.SCA11_TUSCANY_NS, "sdo.types"); + + private String factory; + private String schemaLocation; + private String namespace; + + private List types = new ArrayList(); + + private boolean unresolved; + + public SDOTypes() { + super(); + setUnresolved(true); + } + + public boolean isUnresolved() { + return unresolved; + } + + public void setUnresolved(boolean undefined) { + this.unresolved = undefined; + } + + /** + * @return the factoryClassName + */ + public String getFactory() { + return factory; + } + + /** + * @param factoryClassName the factoryClassName to set + */ + public void setFactory(String factoryClassName) { + this.factory = factoryClassName; + } + + /** + * @return the schemaLocation + */ + public String getSchemaLocation() { + return schemaLocation; + } + + /** + * @param schemaLocation the schemaLocation to set + */ + public void setSchemaLocation(String schemaLocation) { + this.schemaLocation = schemaLocation; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public List getTypes() { + return types; + } +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java new file mode 100644 index 0000000000..167be60640 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java @@ -0,0 +1,192 @@ +/* + * 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.databinding.sdo; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.databinding.WrapperHandler; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.util.ElementInfo; +import org.apache.tuscany.sca.interfacedef.util.TypeInfo; +import org.apache.tuscany.sca.interfacedef.util.WrapperInfo; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Sequence; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XSDHelper; + +/** + * SDO Wrapper Handler + * + * @version $Rev$ $Date$ + */ +public class SDOWrapperHandler implements WrapperHandler { + + public Object create(Operation operation, boolean input) { + WrapperInfo inputWrapperInfo = operation.getInputWrapper(); + WrapperInfo outputWrapperInfo = operation.getOutputWrapper(); + + ElementInfo element = input ? inputWrapperInfo.getWrapperElement() : + outputWrapperInfo.getWrapperElement(); + + HelperContext helperContext = SDOContextHelper.getHelperContext(operation); + Type sdoType = getSDOType(helperContext, element); + if (sdoType != null) { + DataFactory dataFactory = helperContext.getDataFactory(); + return dataFactory.create(sdoType); + } + return null; + } + + public void setChildren(Object wrapper, Object[] childObjects, Operation operation, boolean input) { + WrapperInfo inputWrapperInfo = operation.getInputWrapper(); + WrapperInfo outputWrapperInfo = operation.getOutputWrapper(); + + List childElements = input? inputWrapperInfo.getChildElements(): + outputWrapperInfo.getChildElements(); + + for (int i = 0; i < childElements.size(); i++) { + setChild(wrapper, i, childElements.get(i), childObjects[i]); + } + } + + /** + * @see org.apache.tuscany.sca.databinding.WrapperHandler#setChild(java.lang.Object, int, ElementInfo, + * java.lang.Object) + */ + public void setChild(Object wrapper, int i, ElementInfo childElement, Object value) { + DataObject wrapperDO = + (wrapper instanceof XMLDocument) ? ((XMLDocument)wrapper).getRootObject() : (DataObject)wrapper; + String name = childElement.getQName().getLocalPart(); + if (childElement.isMany()) { + // FIXME: If we look up by name, we need to make sure the WrapperInfo has the correct element names + wrapperDO.getList(i).addAll((Collection)value); + } else { + wrapperDO.set(i, value); + } + } + + @SuppressWarnings("unchecked") + public List getChildren(Object wrapper, Operation operation, boolean input) { + DataObject wrapperDO = + (wrapper instanceof XMLDocument) ? ((XMLDocument)wrapper).getRootObject() : (DataObject)wrapper; + List properties = wrapperDO.getInstanceProperties(); + List elements = new ArrayList(); + Type type = wrapperDO.getType(); + if (type.isSequenced()) { + // Add values in the sequence + Sequence sequence = wrapperDO.getSequence(); + for (int i = 0; i < sequence.size(); i++) { + // Skip mixed text + if (sequence.getProperty(i) != null) { + elements.add(sequence.getValue(i)); + } + } + } else { + for (Property p : properties) { + Object child = wrapperDO.get(p); + if (p.isMany()) { + for (Object c : (Collection)child) { + elements.add(c); + } + } else { + elements.add(child); + } + } + } + return elements; + } + + /** + * @see org.apache.tuscany.sca.databinding.WrapperHandler#getWrapperType(Operation, boolean) + */ + public DataType getWrapperType(Operation operation, boolean input) { + WrapperInfo inputWrapperInfo = operation.getInputWrapper(); + WrapperInfo outputWrapperInfo = operation.getOutputWrapper(); + + ElementInfo element = input ? inputWrapperInfo.getWrapperElement() : + outputWrapperInfo.getWrapperElement(); + + HelperContext helperContext = SDOContextHelper.getHelperContext(operation); + Type sdoType = getSDOType(helperContext, element); + if (sdoType != null) { + // Check if child elements matches + Class physical = sdoType.getInstanceClass(); + DataType wrapperType = + new DataTypeImpl(SDODataBinding.NAME, physical, new XMLType(element)); + return wrapperType; + } else { + return null; + } + } + + /** + * @param helperContext + * @param element + * @return + */ + private Type getSDOType(HelperContext helperContext, ElementInfo element) { + XSDHelper xsdHelper = helperContext.getXSDHelper(); + Type sdoType = null; + Property prop = + xsdHelper.getGlobalProperty(element.getQName().getNamespaceURI(), element.getQName().getLocalPart(), true); + if (prop != null) { + sdoType = prop.getType(); + } else { + TypeInfo type = element.getType(); + QName typeName = type != null ? type.getQName() : null; + if (typeName != null) { + sdoType = helperContext.getTypeHelper().getType(typeName.getNamespaceURI(), typeName.getLocalPart()); + } + } + return sdoType; + } + + /** + * @see org.apache.tuscany.sca.databinding.WrapperHandler#isInstance(java.lang.Object, Operation, boolean) + */ + public boolean isInstance(Object wrapper, Operation operation, boolean input) { + WrapperInfo inputWrapperInfo = operation.getInputWrapper(); + WrapperInfo outputWrapperInfo = operation.getOutputWrapper(); + + ElementInfo element = input ? inputWrapperInfo.getWrapperElement() : + outputWrapperInfo.getWrapperElement(); + + // List childElements = + // input ? wrapperInfo.getInputChildElements() : wrapperInfo.getOutputChildElements(); + HelperContext helperContext = SDOContextHelper.getHelperContext(operation); + Type sdoType = getSDOType(helperContext, element); + if (sdoType != null) { + return sdoType.isInstance(wrapper); + } + return false; + } +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/String2DataObject.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/String2DataObject.java new file mode 100644 index 0000000000..a48e357960 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/String2DataObject.java @@ -0,0 +1,61 @@ +/* + * 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.databinding.sdo; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLHelper; + +public class String2DataObject extends BaseTransformer implements + PullTransformer { + + public DataObject transform(String source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, false); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + return xmlHelper.load(source).getRootObject(); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return String.class; + } + + @Override + protected Class getTargetType() { + return DataObject.class; + } + + @Override + public int getWeight() { + return 50; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2String.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2String.java new file mode 100644 index 0000000000..d73f714384 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2String.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.databinding.sdo; + +import java.io.StringWriter; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + +public class XMLDocument2String extends BaseTransformer implements + PullTransformer { + + public String transform(XMLDocument source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, true); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + StringWriter writer = new StringWriter(); + xmlHelper.save(source, writer, null); + return writer.toString(); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return XMLDocument.class; + } + + @Override + protected Class getTargetType() { + return String.class; + } + + @Override + public int getWeight() { + return 40; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2XMLStreamReader.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2XMLStreamReader.java new file mode 100644 index 0000000000..2f41fb1957 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLDocument2XMLStreamReader.java @@ -0,0 +1,69 @@ +/* + * 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.databinding.sdo; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sdo.api.XMLStreamHelper; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; + +public class XMLDocument2XMLStreamReader extends BaseTransformer implements + PullTransformer { + /** + * @param source + * @param context + * @return + */ + public XMLStreamReader transform(XMLDocument source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, true); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext); + return streamHelper.createXMLStreamReader(source); + } catch (XMLStreamException e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return XMLDocument.class; + } + + @Override + protected Class getTargetType() { + return XMLStreamReader.class; + } + + @Override + public int getWeight() { + return 10; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2DataObject.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2DataObject.java new file mode 100644 index 0000000000..fbc13e6fed --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2DataObject.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.databinding.sdo; + +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sdo.api.XMLStreamHelper; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; + +public class XMLStreamReader2DataObject extends BaseTransformer implements + PullTransformer { + + public DataObject transform(XMLStreamReader source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, false); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext); + // The XMLStreamHelper requires that the reader is positioned at + // START_ELEMENT + while (source.getEventType() != XMLStreamConstants.START_ELEMENT && source.hasNext()) { + source.next(); + } + DataObject target = streamHelper.loadObject(source); + source.close(); + return target; + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getTargetType() { + return DataObject.class; + } + + @Override + protected Class getSourceType() { + return XMLStreamReader.class; + } + + @Override + public int getWeight() { + return 15; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2XMLDocument.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2XMLDocument.java new file mode 100644 index 0000000000..658fe115a5 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/XMLStreamReader2XMLDocument.java @@ -0,0 +1,66 @@ +/* + * 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.databinding.sdo; + +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sdo.api.SDOUtil; +import org.apache.tuscany.sdo.api.XMLStreamHelper; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; + +public class XMLStreamReader2XMLDocument extends BaseTransformer implements + PullTransformer { + + public XMLDocument transform(XMLStreamReader source, TransformationContext context) { + if (source == null) { + return null; + } + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context, false); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext); + XMLDocument target = streamHelper.load(source); + source.close(); + return target; + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getTargetType() { + return XMLDocument.class; + } + + @Override + protected Class getSourceType() { + return XMLStreamReader.class; + } + + @Override + public int getWeight() { + return 15; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesModelResolver.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesModelResolver.java new file mode 100644 index 0000000000..020a8edd7d --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesModelResolver.java @@ -0,0 +1,234 @@ +/* + * 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.databinding.sdo.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.databinding.sdo.SDOTypes; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; +import org.apache.tuscany.sca.monitor.impl.ProblemImpl; +import org.apache.tuscany.sca.xsd.XSDFactory; +import org.apache.tuscany.sca.xsd.XSDefinition; +import org.apache.tuscany.sdo.api.SDOUtil; + +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XSDHelper; + +/** + * SDO types model resolver that aggregates the SDO type registration for an SCA contribution + */ +public class SDOTypesModelResolver implements ModelResolver { + private Contribution contribution; + private HelperContext helperContext; + private List sdoTypes = new ArrayList(); + private ContributionFactory contributionFactory; + private XSDFactory xsdFactory; + private ProcessorContext context; + + public SDOTypesModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) { + super(); + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + this.xsdFactory = modelFactories.getFactory(XSDFactory.class); + this.contribution = contribution; + } + + public void addModel(Object resolved, ProcessorContext context) { + if (helperContext == null) { + helperContext = SDOUtil.createHelperContext(); + } + this.context = context; + SDOTypes types = (SDOTypes)resolved; + try { + loadSDOTypes(types, contribution.getModelResolver()); + } catch (ContributionResolveException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + sdoTypes.add(types); + } + + public Object removeModel(Object resolved, ProcessorContext context) { + SDOTypes types = (SDOTypes)resolved; + + return sdoTypes.remove(types); + } + + public T resolveModel(Class modelClass, T unresolved, ProcessorContext context) { + SDOTypes types = (SDOTypes)unresolved; + String ns = types.getNamespace(); + this.context = context; + for (SDOTypes t : sdoTypes) { + if (t.getNamespace().equals(types.getNamespace())) { + try { + loadSDOTypes(types, contribution.getModelResolver()); + } catch (ContributionResolveException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return (T)t; + } + } + return (T)types; + } + + private static void register(Class factoryClass, HelperContext helperContext) throws Exception { + Field field = factoryClass.getField("INSTANCE"); + Object factory = field.get(null); + Method method = factory.getClass().getMethod("register", new Class[] {HelperContext.class}); + method.invoke(factory, new Object[] {helperContext}); + } + + private void defineFromFactory(SDOTypes importSDO, ModelResolver resolver) throws ContributionResolveException { + String factoryName = importSDO.getFactory(); + if (factoryName != null) { + ClassReference reference = new ClassReference(factoryName); + ClassReference resolved = resolver.resolveModel(ClassReference.class, reference, context); + if (resolved != null && !resolved.isUnresolved()) { + try { + Class factoryClass = resolved.getJavaClass(); + // Get the namespace + Field field = factoryClass.getField("NAMESPACE_URI"); + importSDO.setNamespace((String)field.get(null)); + register(factoryClass, helperContext); + importSDO.setUnresolved(false); + } catch (Exception e) { + ContributionResolveException ce = new ContributionResolveException(e); + error(context.getMonitor(), "ContributionResolveException", resolver, ce); + //throw ce; + } + } else { + error(context.getMonitor(), "FailToResolveClass", resolver, factoryName); + //ContributionResolveException loaderException = + //new ContributionResolveException("Fail to resolve class: " + factoryName); + //throw loaderException; + } + } + } + + private void defineFromXSD(SDOTypes importSDO, ModelResolver resolver) throws ContributionResolveException { + String location = importSDO.getSchemaLocation(); + if (location != null) { + try { + Artifact artifact = contributionFactory.createArtifact(); + artifact.setURI(location); + artifact = resolver.resolveModel(Artifact.class, artifact, context); + if (artifact.getLocation() != null) { + String wsdlURL = artifact.getLocation(); + URLConnection connection = new URL(wsdlURL).openConnection(); + connection.setUseCaches(false); + InputStream xsdInputStream = connection.getInputStream(); + try { + XSDHelper xsdHelper = helperContext.getXSDHelper(); + List sdoTypes = xsdHelper.define(xsdInputStream, wsdlURL); + for (Type t : sdoTypes) { + importSDO.setNamespace(t.getURI()); + break; + } + importSDO.getTypes().addAll(sdoTypes); + } finally { + xsdInputStream.close(); + } + importSDO.setUnresolved(false); + } else { + error(context.getMonitor(), "FailToResolveLocation", resolver, location); + //ContributionResolveException loaderException = new ContributionResolveException("Fail to resolve location: " + location); + //throw loaderException; + } + } catch (IOException e) { + ContributionResolveException ce = new ContributionResolveException(e); + error(context.getMonitor(), "ContributionResolveException", resolver, ce); + //throw ce; + } + } else { + String ns = importSDO.getNamespace(); + if (ns != null) { + XSDefinition xsd = xsdFactory.createXSDefinition(); + xsd.setUnresolved(true); + xsd.setNamespace(ns); + xsd = resolver.resolveModel(XSDefinition.class, xsd, context); + if (!xsd.isUnresolved()) { + XSDHelper xsdHelper = helperContext.getXSDHelper(); + xsdHelper.define(xsd.getLocation().toString()); + } + } + } + } + + private void loadSDOTypes(SDOTypes types, ModelResolver resolver) throws ContributionResolveException { + synchronized (types) { + if (types.isUnresolved()) { + defineFromFactory(types, resolver); + defineFromXSD(types, resolver); + types.setUnresolved(false); + } + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = + new ProblemImpl(this.getClass().getName(), "databinding-sdo-validation-messages", Severity.ERROR, + message, model, message, ex); + monitor.problem(problem); + } + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + new ProblemImpl(this.getClass().getName(), "databinding-sdo-validation-messages", Severity.ERROR, + message, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesProcessor.java b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesProcessor.java new file mode 100644 index 0000000000..b851d38dae --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/xml/SDOTypesProcessor.java @@ -0,0 +1,109 @@ +/* + * 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.databinding.sdo.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.sdo.SDOTypes; + + +/** + * Loader that handles <import.sdo> elements. + * + * @version $Rev$ $Date$ + */ +public class SDOTypesProcessor implements StAXArtifactProcessor { + + public SDOTypesProcessor(ExtensionPointRegistry registry) { + } + + public QName getXMLType() { + return SDOTypes.SDO_TYPES; + } + + public SDOTypes read(XMLStreamReader reader,ProcessorContext context) throws ContributionReadException, XMLStreamException { + assert SDOTypes.SDO_TYPES.equals(reader.getName()); + + // FIXME: How do we associate the application HelperContext with the one + // imported by the composite + SDOTypes sdoTypes = new SDOTypes(); + String factoryName = reader.getAttributeValue(null, "factory"); + if (factoryName != null) { + sdoTypes.setFactory(factoryName); + } + String location = reader.getAttributeValue(null, "location"); + if (location != null) { + sdoTypes.setSchemaLocation(location); + } + String ns = reader.getAttributeValue(null, "namespace"); + sdoTypes.setNamespace(ns); + + // Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && SDOTypes.SDO_TYPES.equals(reader.getName())) { + break; + } + } + return sdoTypes; + } + + public QName getArtifactType() { + return SDOTypes.SDO_TYPES; + } + + public void write(SDOTypes model, XMLStreamWriter writer,ProcessorContext context) throws ContributionWriteException { + try { + writer.writeStartElement(SDOTypes.SDO_TYPES.getNamespaceURI(), SDOTypes.SDO_TYPES.getLocalPart()); + if (model.getNamespace() != null) { + writer.writeAttribute("namespace", model.getNamespace()); + } + if (model.getSchemaLocation() != null) { + writer.writeAttribute("location", model.getSchemaLocation()); + } + if (model.getFactory() != null) { + writer.writeAttribute("factory", model.getFactory()); + } + writer.writeEndElement(); + } catch (XMLStreamException e) { + throw new ContributionWriteException(e); + } + } + + public Class getModelType() { + return SDOTypes.class; + } + + public void resolve(SDOTypes types, ModelResolver resolver,ProcessorContext context) throws ContributionResolveException { + // Defer the resolution to SDOTypesResolver which aggragates the type registrations into an instance of HelperContext + resolver.addModel(types,context); + } + +} -- cgit v1.2.3