diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-13 01:42:27 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-13 01:42:27 +0000 |
commit | 3c7c4a749baafcf375f4785a7668d3a25c9063e3 (patch) | |
tree | a66b8b031c5dc6c7744f44b44b6bcd371bd0b8df /sca-java-1.x/trunk/modules/implementation-xquery | |
parent | eb03ff672236cddf65533f39b83ddd5e2984a2bb (diff) |
Moving 1.x trunk
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835700 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/trunk/modules/implementation-xquery')
16 files changed, 1672 insertions, 0 deletions
diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/LICENSE b/sca-java-1.x/trunk/modules/implementation-xquery/LICENSE new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/NOTICE b/sca-java-1.x/trunk/modules/implementation-xquery/NOTICE new file mode 100644 index 0000000000..25bb89c9b2 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/NOTICE @@ -0,0 +1,6 @@ +${pom.name} +Copyright (c) 2005 - 2009 The Apache Software Foundation + +This product includes software developed by +The Apache Software Foundation (http://www.apache.org/). + diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/pom.xml b/sca-java-1.x/trunk/modules/implementation-xquery/pom.xml new file mode 100644 index 0000000000..135bc43a65 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/pom.xml @@ -0,0 +1,105 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-modules</artifactId> + <version>1.6-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>tuscany-implementation-xquery</artifactId> + <name>Apache Tuscany SCA XQuery Implementation Extension</name> + + <dependencies> + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-assembly</artifactId> + <version>1.6-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-interface-java-xml</artifactId> + <version>1.6-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-implementation-java-runtime</artifactId> + <version>1.6-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-databinding-saxon</artifactId> + <version>1.6-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-databinding-jaxb</artifactId> + <version>1.6-SNAPSHOT</version> + <scope>runtime</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-core-spi</artifactId> + <version>1.6-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-host-embedded</artifactId> + <version>1.6-SNAPSHOT</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.5</version> + <scope>test</scope> + </dependency> + + </dependencies> + + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + + <configuration> + <instructions> + <Bundle-Version>${tuscany.version}</Bundle-Version> + <Bundle-SymbolicName>org.apache.tuscany.sca.implementation.xquery</Bundle-SymbolicName> + <Bundle-Description>${pom.name}</Bundle-Description> + <Export-Package>org.apache.tuscany.sca.implementation.xquery*</Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementation.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementation.java new file mode 100644 index 0000000000..f5bc5dd9b8 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementation.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.implementation.xquery; + +import java.lang.reflect.Method; +import java.util.Map; + +import net.sf.saxon.query.XQueryExpression; + +import org.apache.tuscany.sca.assembly.Implementation; + +/** + * Class representing the XQuery implementation type + * @version $Rev$ $Date$ + */ +public interface XQueryImplementation extends Implementation { + + /** + * Location of the XQuery implementation file + * @return + */ + String getLocation(); + + void setLocation(String location); + + String getLocationURL(); + + void setLocationURL(String location); + + /** + * The XQuery expression that is loaded from the XQuery implementation file + * @return + */ + String getXqExpression(); + + void setXqExpression(String expression); + + /** + * The XQuery expression should be extended for with additional + * script, which provides the external variables needed to invoke + * a function. In this way for each function that is defined in the + * original XQuery expression additional expression is defined, which + * can invoke this function, using external variables as input. + * These expression extensions are stored in this map. It provides for + * each method of a service interface that is implemented by this component + * type corresponding expression extension + * @return + */ + Map<Method, String> getXqExpressionExtensionsMap(); + + /** + * This map is a kind of cache for function invocations. If a given + * XQuery function of this implementation has been invoked already + * its compiled expression can be reused. In this manner the performance + * can be increased + * @return + */ + Map<String, XQueryExpression> getCompiledExpressionsCache(); +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationFactory.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationFactory.java new file mode 100644 index 0000000000..bdfd390499 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationFactory.java @@ -0,0 +1,32 @@ +/* + * 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.implementation.xquery; + +import org.apache.tuscany.sca.implementation.xquery.impl.XQueryImplementationFactoryImpl; + +/** + * Provides instances of XQueryImplementation classes + * @version $Rev$ $Date$ + */ +public interface XQueryImplementationFactory { + + XQueryImplementationFactory INSTANCE = new XQueryImplementationFactoryImpl(); + + XQueryImplementation createXQueryImplementation(); +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationProvider.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationProvider.java new file mode 100644 index 0000000000..63a3a49e13 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationProvider.java @@ -0,0 +1,166 @@ +/* + * 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.implementation.xquery; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.ComponentProperty; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.databinding.javabeans.SimpleJavaDataBinding; +import org.apache.tuscany.sca.databinding.saxon.SaxonNodeDataBinding; +import org.apache.tuscany.sca.databinding.saxon.SaxonValueDataBinding; +import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; +import org.apache.tuscany.sca.interfacedef.DataType; +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.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.util.JavaXMLMapper; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * Provides the runtime configuration of XQuery-typed component. + * + * Generally the following is done: + * 1. During construction all services and references are provided + * with the Saxon data binding, which is the one required by the + * XQueryInvoker + * + * 2. During startup: + * - a proxy is created for each reference and it is kept in the referenceProxies + * map, which is used later by the invoker to configure the Saxon parser; + * - each property value is read and put into the properties map. This is + * again used by the invoker to configure the Saxon parser + * + * @version $Rev$ $Date$ + */ +public class XQueryImplementationProvider implements ImplementationProvider { + + private RuntimeComponent component; + private XQueryImplementation implementation; + private Map<String, Object> referenceProxies = new HashMap<String, Object>(); + private Map<String, Object> properties = new HashMap<String, Object>(); + private JavaPropertyValueObjectFactory javaFactory; + + public XQueryImplementationProvider(RuntimeComponent component, + XQueryImplementation implementation, + JavaPropertyValueObjectFactory factory) { + this.component = component; + this.implementation = implementation; + this.javaFactory = factory; + init(); + } + + private void init() { + List<Service> services = implementation.getServices(); + for (Service sevice : services) { + InterfaceContract interfaceContract = sevice.getInterfaceContract(); + //interfaceContract.getInterface().setDefaultDataBinding(ValueRepresentation.class.getName()); + setDataBinding(interfaceContract.getInterface(), false); + } + + List<Reference> references = implementation.getReferences(); + for (Reference reference : references) { + InterfaceContract interfaceContract = reference.getInterfaceContract(); + //interfaceContract.getInterface().setDefaultDataBinding(ValueRepresentation.class.getName()); + setDataBinding(interfaceContract.getInterface(), true); + } + } + + public Invoker createInvoker(RuntimeComponentService service, Operation operation) { + return new XQueryInvoker(service, operation, implementation, referenceProxies, properties); + } + + public boolean supportsOneWayInvocation() { + return false; + } + + public void start() { + + for (Reference reference : component.getReferences()) { + String refName = reference.getName(); + if (refName.startsWith("$self$.")) { + continue; + } + Class interfaze = ((JavaInterface)reference.getInterfaceContract().getInterface()).getJavaClass(); + Object refProxy = component.getComponentContext().getService(interfaze, refName); + referenceProxies.put(refName, refProxy); + } + + for (ComponentProperty property : component.getProperties()) { + String propName = property.getName(); + QName xmlType = property.getXSDType(); + Class clazz = JavaXMLMapper.getJavaType(xmlType); + + Object propertyValue = null; + if (clazz == null || java.lang.Object.class.equals(clazz)) { + propertyValue = property.getValue(); + } else { + ObjectFactory objfactory = javaFactory.createValueFactory(property, property.getValue(), clazz); + propertyValue = objfactory.getInstance(); + } + properties.put(propName, propertyValue); + } + } + + private void setDataBinding(Interface interfaze, boolean isReference) { + List<Operation> operations = interfaze.getOperations(); + for (Operation operation : operations) { + operation.setDataBinding(SaxonNodeDataBinding.NAME); + DataType<List<DataType>> inputType = operation.getInputType(); + if (inputType != null) { + List<DataType> logical = inputType.getLogical(); + for (DataType inArg : logical) { + if (SimpleJavaDataBinding.NAME.equals(inArg.getDataBinding())) { + if (!isReference) { + inArg.setDataBinding(SaxonValueDataBinding.NAME); + } + } else { + inArg.setDataBinding(SaxonNodeDataBinding.NAME); + } + } + } + DataType outputType = operation.getOutputType(); + if (outputType != null) { + if (SimpleJavaDataBinding.NAME.equals(outputType.getDataBinding())) { + if (!isReference) { + outputType.setDataBinding(SaxonValueDataBinding.NAME); + } + } else { + outputType.setDataBinding(SaxonNodeDataBinding.NAME); + } + } + } + } + + public void stop() { + // TODO Auto-generated method stub + + } + +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationProviderFactory.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationProviderFactory.java new file mode 100644 index 0000000000..05c4c51086 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryImplementationProviderFactory.java @@ -0,0 +1,51 @@ +/* + * 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.implementation.xquery; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ImplementationProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * A factory for XQuery implementation providers + * @version $Rev$ $Date$ + */ +public class XQueryImplementationProviderFactory implements ImplementationProviderFactory<XQueryImplementation> { + + private JavaPropertyValueObjectFactory javaFactory; + + public XQueryImplementationProviderFactory(ExtensionPointRegistry registry) { + Mediator mediator = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(Mediator.class); + this.javaFactory = new JavaPropertyValueObjectFactory(mediator); + } + + public ImplementationProvider createImplementationProvider(RuntimeComponent component, + XQueryImplementation implementation) { + return new XQueryImplementationProvider(component, implementation, javaFactory); + } + + public Class<XQueryImplementation> getModelType() { + return XQueryImplementation.class; + } + +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryInvokationException.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryInvokationException.java new file mode 100644 index 0000000000..0c628b3715 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryInvokationException.java @@ -0,0 +1,33 @@ +/* + * 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.implementation.xquery; + +/** + * Exception class used by the XQueryInvoker to report problems in + * XQuery script invocation + * @version $Rev$ $Date$ + */ +public class XQueryInvokationException extends Exception { + + private static final long serialVersionUID = -2674411654705699541L; + + public XQueryInvokationException(String msg) { + super(msg); + } +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryInvoker.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryInvoker.java new file mode 100644 index 0000000000..2a9673068b --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/XQueryInvoker.java @@ -0,0 +1,318 @@ +/* + * 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.implementation.xquery; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import net.sf.saxon.Configuration; +import net.sf.saxon.event.Builder; +import net.sf.saxon.om.DocumentInfo; +import net.sf.saxon.om.Item; +import net.sf.saxon.om.NodeInfo; +import net.sf.saxon.om.SequenceIterator; +import net.sf.saxon.query.DynamicQueryContext; +import net.sf.saxon.query.QueryResult; +import net.sf.saxon.query.StaticQueryContext; +import net.sf.saxon.query.XQueryExpression; +import net.sf.saxon.trans.XPathException; +import net.sf.saxon.value.Value; + +import org.apache.tuscany.sca.databinding.saxon.SaxonDataBindingHelper; +import org.apache.tuscany.sca.databinding.saxon.collection.ItemList; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * Performs the invocation of a requested XQuery function + * @version $Rev$ $Date$ + */ +public class XQueryInvoker implements Invoker { + + private RuntimeComponentService service; + private Operation operation; + private Method theMethod; + private XQueryImplementation implementation; + private Map<String, Object> referenceProxies; + private Map<String, Object> properties; + + /** + * Constructs a new instance of the xquery invoker. + * Also performs a search of java.lang.Method instance + * that corresponds to the invoked operation + */ + public XQueryInvoker(RuntimeComponentService service, + Operation operation, + XQueryImplementation implementation, + Map<String, Object> referenceProxies, + Map<String, Object> properties) { + this.service = service; + this.operation = operation; + this.implementation = implementation; + this.referenceProxies = referenceProxies; + this.properties = properties; + + findMatchingMethod(); + } + + /** + * This method contains the XQuery invocation logic + * The following steps are performed: + * 1. XQuery expression is produced by combining the original expression + * and the function invocation extension (See XQueryImplementation.getXqExpressionExtensionsMap() + * for details) + * 2. A check is performed if this expression has been invoked already. If yes - + * it is taken from the cache + * 3. Configuration for the execution is either created or retrieved from + * the cached expression + * 4. The input parameters of the operation to be invoked are taken from the + * payload and transformed to ones that are built with the current + * configuration. + * NOTE: This is unnecessary overhead - can the Configuration + * object be attached in some way to the invocation request? + * 5. All parameters, reference proxies and property values are mapped + * to external variables of the XQuery script + * 6. The query is executed and all the results are stored in a ItemList object + * + * NOTE: During execution of the XQuery a static variable is set with + * the current configuration. This variable is used by the NodeInfo transformers + * to produce the correct NodeInfo for all Output2Output transformations, which + * happen as result of the XQuery component invoking some reference components + * The old state of the static configuration is preserved and in this way allowing + * to nest XQuery component invocations (i.e. one XQuery component invokes another + * one) + */ + private Object doInvoke(Object payload) throws XQueryInvokationException, XPathException { + if (theMethod == null) { + throw new XQueryInvokationException("No java method for operation: " + operation.getName()); + } + String xqExpression = + implementation.getXqExpression() + implementation.getXqExpressionExtensionsMap().get(theMethod); + + Configuration config = null; + Properties props = new Properties(); + props.setProperty(OutputKeys.METHOD, "xml"); + props.setProperty(OutputKeys.INDENT, "yes"); + + XQueryExpression exp = implementation.getCompiledExpressionsCache().get(xqExpression); + if (exp == null) { + config = new Configuration(); + StaticQueryContext sqc = new StaticQueryContext(config); + exp = sqc.compileQuery(xqExpression); + implementation.getCompiledExpressionsCache().put(xqExpression, exp); + } else { + config = exp.getStaticContext().getConfiguration(); + } + + Object[] params = prepareParameters(payload, config, props); + + DynamicQueryContext dynamicContext = new DynamicQueryContext(config); + + // Setting the parameters for function invocation + String methodName = theMethod.getName(); + for (int i = 0; i < params.length; i++) { + dynamicContext.setParameter(methodName + "_" + i, params[i]); + } + + // Setting references + for (Map.Entry<String, Object> entry : referenceProxies.entrySet()) { + dynamicContext.setParameter(entry.getKey(), entry.getValue()); + } + + // Setting properties + for (Map.Entry<String, Object> entry : properties.entrySet()) { + dynamicContext.setParameter(entry.getKey(), transformProperty(entry.getValue(), config)); + } + + SequenceIterator iterator = null; + Configuration oldConfigValue = SaxonDataBindingHelper.CURR_EXECUTING_CONFIG; + SaxonDataBindingHelper.CURR_EXECUTING_CONFIG = config; + try { + iterator = exp.iterator(dynamicContext); + } finally { + SaxonDataBindingHelper.CURR_EXECUTING_CONFIG = oldConfigValue; + } + + ItemList list = new ItemList(); + Item item = iterator.next(); + + while (item != null) { + list.add(item); + item = iterator.next(); + + } + + if (list.size() == 0) { + return null; + + } else if (list.size() == 1) { + + item = list.iterator().next(); + + if (item instanceof NodeInfo) { + return item; + } else { + return Value.asValue(item); + } + + } + + return list; + + } + + public Message invoke(Message msg) { + try { + Object resp = doInvoke(msg.getBody()); + msg.setBody(resp); + } catch (XQueryInvokationException e) { + msg.setFaultBody(e.getCause()); + } catch (XPathException e) { + msg.setFaultBody(e.getCause()); + } + return msg; + } + + private void findMatchingMethod() { + Class<?> interfaze = ((JavaInterface)service.getInterfaceContract().getInterface()).getJavaClass(); + + for (Method method : interfaze.getMethods()) { + if (match(operation, method)) { + theMethod = method; + } + } + } + + private static boolean match(Operation operation, Method method) { + Class<?>[] params = method.getParameterTypes(); + DataType<List<DataType>> inputType = operation.getInputType(); + List<DataType> types = inputType.getLogical(); + boolean matched = true; + if (types.size() == params.length && method.getName().equals(operation.getName())) { + for (int i = 0; i < params.length; i++) { + Class<?> clazz = params[i]; + if (!clazz.equals(operation.getInputType().getLogical().get(i).getPhysical())) { + matched = false; + } + } + } else { + matched = false; + } + return matched; + + } + + private Object[] prepareParameters(Object payload, Configuration configuration, Properties props) { + if (payload == null) { + return new Object[0]; + } + Object[] inputArguments = null; + if (payload.getClass().isArray()) { + inputArguments = (Object[])payload; + } else { + inputArguments = new Object[1]; + inputArguments[0] = payload; + } + + Object[] parameters = new Object[inputArguments.length]; + + for (int i = 0; i < inputArguments.length; i++) { + if (inputArguments[i] instanceof NodeInfo) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + StreamResult sw = new StreamResult(baos); + try { + QueryResult.serialize((NodeInfo)inputArguments[i], sw, props, ((NodeInfo)inputArguments[i]).getConfiguration()); + baos.close(); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + StreamSource ss = new StreamSource(bais); + parameters[i] = Builder.build(ss, null, configuration); + } catch (Exception e) { + e.printStackTrace(); + parameters[i] = null; + } + } else { + parameters[i] = inputArguments[i]; + } + } + + return parameters; + } + + private Object transformProperty(Object argument, Configuration configuration) { + Object parameter = argument; + if (argument instanceof Document) { + try { + Document doc = (Document)argument; + Node valueNode = doc.getFirstChild(); + DocumentInfo docInfo = null; + if (valueNode instanceof Element && valueNode.getNodeName().equals("value")) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + StreamResult sr = new StreamResult(baos); + try { + Node element = null; + NodeList list = valueNode.getChildNodes(); + for (int i = 0; i < list.getLength(); i++) { + if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { + element = list.item(i); + break; + } + } + if (element == null) { + element = valueNode.getFirstChild(); + } + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + transformer.transform(new DOMSource(element), sr); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + docInfo = (DocumentInfo)Builder.build(new StreamSource(bais), null, configuration); + } catch (Exception e) { + e.printStackTrace(); + return parameter; + } + } else { + docInfo = (DocumentInfo)Builder.build(new DOMSource(doc), null, configuration); + } + parameter = docInfo; + } catch (XPathException e) { + e.printStackTrace(); + return parameter; + } + } + + return parameter; + } +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/impl/XQueryImplementationFactoryImpl.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/impl/XQueryImplementationFactoryImpl.java new file mode 100644 index 0000000000..5e77575c3c --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/impl/XQueryImplementationFactoryImpl.java @@ -0,0 +1,35 @@ +/* + * 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.implementation.xquery.impl; + +import org.apache.tuscany.sca.implementation.xquery.XQueryImplementation; +import org.apache.tuscany.sca.implementation.xquery.XQueryImplementationFactory; + +/** + * Actual implementation of the XQuery implementation factory + * @version $Rev$ $Date$ + */ +public class XQueryImplementationFactoryImpl implements XQueryImplementationFactory { + + public XQueryImplementation createXQueryImplementation() { + XQueryImplementation implementation = new XQueryImplementationImpl(); + return implementation; + } + +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/impl/XQueryImplementationImpl.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/impl/XQueryImplementationImpl.java new file mode 100644 index 0000000000..77c8eea511 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/impl/XQueryImplementationImpl.java @@ -0,0 +1,102 @@ +/* + * 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.implementation.xquery.impl; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import net.sf.saxon.query.XQueryExpression; + +import org.apache.tuscany.sca.assembly.impl.ImplementationImpl; +import org.apache.tuscany.sca.implementation.xquery.XQueryImplementation; + +/** + * Actual implementation of the XQuery implementation + * @version $Rev$ $Date$ + */ +public class XQueryImplementationImpl extends ImplementationImpl implements XQueryImplementation { + + private String location; + private String locationURL; + private String xqExpression; + + private Map<String, XQueryExpression> compiledExpressionsCache = new HashMap<String, XQueryExpression>(); + private Map<Method, String> xqExpressionExtensionsMap = new HashMap<Method, String>(); + + public XQueryImplementationImpl() { + setUnresolved(true); + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public String getLocationURL() { + return locationURL; + } + + public void setLocationURL(String locationURL) { + this.locationURL = locationURL; + } + + public String getXqExpression() { + return xqExpression; + } + + public void setXqExpression(String xqExpression) { + this.xqExpression = xqExpression; + } + + public Map<String, XQueryExpression> getCompiledExpressionsCache() { + return compiledExpressionsCache; + } + + public Map<Method, String> getXqExpressionExtensionsMap() { + return xqExpressionExtensionsMap; + } + + @Override + public int hashCode() { + final int PRIME = 31; + int result = super.hashCode(); + result = PRIME * result + ((location == null) ? 0 : location.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final XQueryImplementationImpl other = (XQueryImplementationImpl)obj; + if (location == null) { + if (other.location != null) + return false; + } else if (!location.equals(other.location)) + return false; + return true; + } + +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/xml/XQueryImplementationProcessor.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/xml/XQueryImplementationProcessor.java new file mode 100644 index 0000000000..12c478daea --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/xml/XQueryImplementationProcessor.java @@ -0,0 +1,159 @@ +/* + * 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.implementation.xquery.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.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.xml.Constants; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.implementation.xquery.XQueryImplementation; +import org.apache.tuscany.sca.implementation.xquery.XQueryImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +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; + +/** + * Processor for the XQuery implementation type artifact + * @version $Rev$ $Date$ + */ +public class XQueryImplementationProcessor implements StAXArtifactProcessor<XQueryImplementation> { + + private static final String LOCATION = "location"; + private static final String IMPLEMENTATION_XQUERY = "implementation.xquery"; + private static final QName IMPLEMENTATION_XQUERY_QNAME = + new QName(Constants.SCA10_TUSCANY_NS, IMPLEMENTATION_XQUERY); + + private AssemblyFactory assemblyFactory; + private JavaInterfaceFactory javaFactory; + private ContributionFactory contributionFactory; + private Monitor monitor; + + public XQueryImplementationProcessor(ModelFactoryExtensionPoint modelFactoryExtensionPoint, Monitor monitor) { + assemblyFactory = modelFactoryExtensionPoint.getFactory(AssemblyFactory.class); + javaFactory = modelFactoryExtensionPoint.getFactory(JavaInterfaceFactory.class); + contributionFactory = modelFactoryExtensionPoint.getFactory(ContributionFactory.class); + this.monitor = monitor; + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "impl-xquery-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + public QName getArtifactType() { + return IMPLEMENTATION_XQUERY_QNAME; + } + + /** + * Reads from the stream and sets the location attribute of the implementation correspondingly + */ + public XQueryImplementation read(XMLStreamReader reader) + throws ContributionReadException, XMLStreamException { + + /* Create the XQuery implementation and set the location into it */ + XQueryImplementation xqueryImplementation = null; + + /* Read the location attribute for the XQuery implementation */ + String xqueryLocation = reader.getAttributeValue(null, LOCATION); + if (xqueryLocation != null) { + xqueryImplementation = XQueryImplementationFactory.INSTANCE.createXQueryImplementation(); + xqueryImplementation.setLocation(xqueryLocation); + xqueryImplementation.setUnresolved(true); + } else { + error("LocationAttributeMissing", reader); + //throw new ContributionReadException(MSG_LOCATION_MISSING); + } + + // Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && IMPLEMENTATION_XQUERY_QNAME.equals(reader.getName())) { + break; + } + } // end while + + return xqueryImplementation; + } + + public void write(XQueryImplementation xqueryImplementation, XMLStreamWriter writer) + throws ContributionWriteException, XMLStreamException { + + writer.writeStartElement(Constants.SCA10_TUSCANY_NS, IMPLEMENTATION_XQUERY); + if (xqueryImplementation.getLocation() != null) { + writer.writeAttribute(LOCATION, xqueryImplementation.getLocation()); + } + writer.writeEndElement(); + + } + + public Class<XQueryImplementation> getModelType() { + return XQueryImplementation.class; + } + + /** + * Resolves the implementation: its services and references, by invoking the XQuery + * introspector + */ + public void resolve(XQueryImplementation xqueryImplementation, ModelResolver resolver) + throws ContributionResolveException { + + if (xqueryImplementation != null) { + Artifact artifact = contributionFactory.createArtifact(); + artifact.setURI(xqueryImplementation.getLocation()); + artifact = resolver.resolveModel(Artifact.class, artifact); + if (artifact.getLocation() != null) { + xqueryImplementation.setLocationURL(artifact.getLocation()); + + XQueryIntrospector introspector = new XQueryIntrospector(assemblyFactory, javaFactory); + + boolean success = introspector.introspect(xqueryImplementation, resolver); + if (success) { + xqueryImplementation.setUnresolved(false); + } + } else { + error("CouldNotLocateFile", resolver, xqueryImplementation.getLocation()); + //throw new ContributionResolveException("Could not locate file: " + xqueryImplementation.getLocation()); + } + } + } + +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/xml/XQueryIntrospector.java b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/xml/XQueryIntrospector.java new file mode 100644 index 0000000000..d1e74dd40d --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/java/org/apache/tuscany/sca/implementation/xquery/xml/XQueryIntrospector.java @@ -0,0 +1,328 @@ +/* + * 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.implementation.xquery.xml; + +import java.io.ByteArrayOutputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.Iterator; + +import javax.xml.namespace.QName; + +import net.sf.saxon.Configuration; +import net.sf.saxon.om.NamespaceResolver; +import net.sf.saxon.query.StaticQueryContext; +import net.sf.saxon.query.XQueryExpression; +import net.sf.saxon.trans.XPathException; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Multiplicity; +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.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.implementation.xquery.XQueryImplementation; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.util.JavaXMLMapper; + +/** + * This class introspects an XQuery file and extracts out of it + * all implemented service, references and properties + * It also creates expression extensions for each operation + * in the implemented services + * @version $Rev$ $Date$ + */ +public class XQueryIntrospector { + + private static final String SCA_SERVICE_PREFIX = "scaservice:java/"; + private static final String SCA_REFERENCE_PREFIX = "scareference:java/"; + private static final String SCA_PROPERTY_JAVA_PREFIX = "scaproperty:java/"; + private static final String SCA_PROPERTY_XML_PREFIX = "scaproperty:xml/"; + + private AssemblyFactory assemblyFactory; + private JavaInterfaceFactory javaFactory; + + public XQueryIntrospector(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) { + super(); + this.assemblyFactory = assemblyFactory; + this.javaFactory = javaFactory; + } + + public boolean introspect(XQueryImplementation xqueryImplementation, ModelResolver resolver) throws ContributionResolveException { + + String xqExpression = null; + try { + URL url = new URL(xqueryImplementation.getLocationURL()); + xqExpression = loadXQExpression(url); + } catch (FileNotFoundException e) { + throw new ContributionResolveException(e); + } catch (IOException e) { + throw new ContributionResolveException(e); + } + + if (xqExpression == null) { + return false; + } + + xqueryImplementation.setXqExpression(xqExpression); + + xqExpression += "\r\n<dummy></dummy>"; + + Configuration config = new Configuration(); + StaticQueryContext sqc = new StaticQueryContext(config); + XQueryExpression exp = null; + try { + exp = sqc.compileQuery(xqExpression); + } catch (XPathException e) { + throw new ContributionResolveException(e); + } + + if (exp == null) { + return false; + } + xqueryImplementation.getCompiledExpressionsCache().put(xqExpression, exp); + + try { + introspectServicesAndReferences(xqueryImplementation, exp, resolver); + } catch (ClassNotFoundException e) { + throw new ContributionResolveException(e); + } catch (InvalidInterfaceException e) { + throw new ContributionResolveException(e); + } + + fillExpressionExtensions(xqueryImplementation); + + return true; + } + + /** + * Loads the XQuery expression from the location that is provided with the implementation + */ + private String loadXQExpression(URL locationURL) throws FileNotFoundException, IOException { + + InputStream xqResourceStream = locationURL.openStream(); + + if (xqResourceStream == null) { + return null; + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int i = 0; + while ((i = xqResourceStream.read()) >= 0) { + baos.write(i); + } + xqResourceStream.close(); + baos.flush(); + baos.close(); + + String xqExpression = baos.toString(); + + return xqExpression; + } + + private Class<?> resolveClass(ModelResolver resolver, String className) throws ClassNotFoundException { + ClassReference classReference = new ClassReference(className); + classReference = resolver.resolveModel(ClassReference.class, classReference); + Class<?> javaClass = classReference.getJavaClass(); + if (javaClass == null) { + throw new ClassNotFoundException(className); + } else { + return javaClass; + } + } + + /** + * From the compiled XQuery expression get all namespaces and see if they + * are services, references or properties declarations + */ + private void introspectServicesAndReferences(XQueryImplementation xqueryImplementation, XQueryExpression exp, ModelResolver resolver) + throws ClassNotFoundException, InvalidInterfaceException { + NamespaceResolver namespaceResolver = exp.getStaticContext().getNamespaceResolver(); + Iterator declaredPrefixesIterator = namespaceResolver.iteratePrefixes(); + while (declaredPrefixesIterator.hasNext()) { + String prefix = (String)declaredPrefixesIterator.next(); + String uri = namespaceResolver.getURIForPrefix(prefix, false); + if (uri.startsWith(SCA_SERVICE_PREFIX)) { + String serviceName = prefix; + String className = uri.substring(SCA_SERVICE_PREFIX.length()); + Class<?> interfaze = resolveClass(resolver, className); + Service theService = createService(interfaze, serviceName); + xqueryImplementation.getServices().add(theService); + } else if (uri.startsWith(SCA_REFERENCE_PREFIX)) { + String referenceName = prefix; + String className = uri.substring(SCA_REFERENCE_PREFIX.length()); + Class<?> interfaze = resolveClass(resolver, className); + Reference theReference = createReference(interfaze, referenceName); + xqueryImplementation.getReferences().add(theReference); + } else if (uri.startsWith(SCA_PROPERTY_JAVA_PREFIX)) { + String propertyName = prefix; + String className = uri.substring(SCA_PROPERTY_JAVA_PREFIX.length()); + Class<?> clazz = resolveClass(resolver, className); + QName xmlType = JavaXMLMapper.getXMLType(clazz); + Property theProperty = createProperty(xmlType, propertyName); + xqueryImplementation.getProperties().add(theProperty); + } else if (uri.startsWith(SCA_PROPERTY_XML_PREFIX)) { + String propertyName = prefix; + String namespaceAndLocalname = uri.substring(SCA_PROPERTY_XML_PREFIX.length()); + int localNameDelimiterPostition = namespaceAndLocalname.lastIndexOf(':'); + String localName = null; + String namespace = null; + if (localNameDelimiterPostition < 0) { + localName = namespaceAndLocalname; + namespace = ""; + } else { + namespace = namespaceAndLocalname.substring(0, localNameDelimiterPostition); + localName = namespaceAndLocalname.substring(localNameDelimiterPostition + 1); + } + QName xmlType = new QName(namespace, localName); + Property theProperty = createProperty(xmlType, propertyName); + xqueryImplementation.getProperties().add(theProperty); + } + } + } + + /** + * Creates a Service for the component type based on its name and Java interface + */ + private Service createService(Class<?> interfaze, String name) throws InvalidInterfaceException { + Service service = assemblyFactory.createService(); + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + service.setInterfaceContract(interfaceContract); + + // Set the name for the service + service.setName(name); + + // Set the call interface and, if present, the callback interface + JavaInterface callInterface = null; + try { + callInterface = (JavaInterface) javaFactory.createJavaInterface(interfaze).clone(); + } catch (CloneNotSupportedException e) { + // Ignore + } + //setDataBindingForInterface(callInterface, DataObject.class.getName()); + service.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = null; + try { + callbackInterface = (JavaInterface) javaFactory.createJavaInterface(callInterface.getCallbackClass()).clone(); + } catch (CloneNotSupportedException e) { + //Ignore + } + //setDataBindingForInterface(callbackInterface, DataObject.class.getName()); + service.getInterfaceContract().setCallbackInterface(callbackInterface); + } + return service; + } // end method createService + + protected Property createProperty(QName type, String name) { + + Property property = assemblyFactory.createProperty(); + property.setName(name); + property.setXSDType(type); + + property.setMany(false); + return property; + + } + + /** + * Creates a Reference for the component type based on its name and Java interface + */ + private Reference createReference(Class<?> interfaze, String name) throws InvalidInterfaceException { + Reference reference = assemblyFactory.createReference(); + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + reference.setInterfaceContract(interfaceContract); + + // Set the name of the reference to the supplied name and the multiplicity of the reference + // to 1..1 - for XQuery implementations, this is the only multiplicity supported + reference.setName(name); + reference.setMultiplicity(Multiplicity.ONE_ONE); + + // Set the call interface and, if present, the callback interface + // Set the call interface and, if present, the callback interface + JavaInterface callInterface = null; + try { + callInterface = (JavaInterface) javaFactory.createJavaInterface(interfaze).clone(); + } catch (CloneNotSupportedException e) { + // Ignore + } + reference.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = null; + try { + callbackInterface = (JavaInterface) javaFactory.createJavaInterface(callInterface.getCallbackClass()).clone(); + } catch (CloneNotSupportedException e) { + //Ignore + } + reference.getInterfaceContract().setCallbackInterface(callbackInterface); + } + + return reference; + } + + /** + * For the methods of each implemented service corresponding expression extension + * is generated + * @param xqueryImplementation + */ + private void fillExpressionExtensions(XQueryImplementation xqueryImplementation) { + for (Service service : xqueryImplementation.getServices()) { + Class<?> interfaze = ((JavaInterface)service.getInterfaceContract().getInterface()).getJavaClass(); + + // For each of the methods + for (Method method : interfaze.getMethods()) { + String expressionExtension = createExpressionExtension(method, interfaze, service.getName()); + xqueryImplementation.getXqExpressionExtensionsMap().put(method, expressionExtension); + } + } + } + + private String createExpressionExtension(Method method, Class<?> interfaze, String serviceName) { + StringBuffer exprBuf = new StringBuffer(); + + exprBuf.append("\r\n"); + + String methodName = method.getName(); + + // For each of the declared parameters + for (int i = 0; i < method.getParameterTypes().length; i++) { + exprBuf.append("declare variable $" + methodName + "_" + i + " external;\r\n"); + } + + exprBuf.append(serviceName + ":" + methodName + "("); + + for (int i = 0; i < method.getParameterTypes().length; i++) { + exprBuf.append("$" + methodName + "_" + i); + if (i != method.getParameterTypes().length - 1) { + exprBuf.append(", "); + } + } + exprBuf.append(")"); + + return exprBuf.toString(); + } +} diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor new file mode 100644 index 0000000000..a51995e56d --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -0,0 +1,19 @@ +# 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. + +# Implementation class for the artifact processor extension +org.apache.tuscany.sca.implementation.xquery.xml.XQueryImplementationProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.0#implementation.xquery,model=org.apache.tuscany.sca.implementation.xquery.XQueryImplementation diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory new file mode 100644 index 0000000000..7ccc08d1ee --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory @@ -0,0 +1,19 @@ +# 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.
+
+# Implementation class for the implementation extension
+org.apache.tuscany.sca.implementation.xquery.XQueryImplementationProviderFactory;model=org.apache.tuscany.sca.implementation.xquery.XQueryImplementation
diff --git a/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/impl-xquery-validation-messages.properties b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/impl-xquery-validation-messages.properties new file mode 100644 index 0000000000..33c605de27 --- /dev/null +++ b/sca-java-1.x/trunk/modules/implementation-xquery/src/main/resources/impl-xquery-validation-messages.properties @@ -0,0 +1,22 @@ +# +# +# 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. +# +# +CouldNotLocateFile = Could not locate file: {0} +LocationAttributeMissing = Reading implementation.xquery - location attribute missing
\ No newline at end of file |