From 1fb6a28a73ca17dbb8c4b3059db590e2f9620943 Mon Sep 17 00:00:00 2001 From: antelder Date: Wed, 3 Aug 2011 09:21:41 +0000 Subject: Correct tag name git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1153404 13f79535-47bb-0310-9956-ffa450edef68 --- .../1.6-TUSCANY-3909/contribution-java/LICENSE | 205 +++++++++++ .../tags/1.6-TUSCANY-3909/contribution-java/NOTICE | 6 + .../1.6-TUSCANY-3909/contribution-java/pom.xml | 77 ++++ .../java/ContributionClassLoaderProvider.java | 44 +++ ...tributionClassloaderProviderExtensionPoint.java | 55 +++ .../DefaultContributionClassLoaderProvider.java | 47 +++ ...tributionClassloaderProviderExtensionPoint.java | 144 ++++++++ .../java/DefaultJavaImportExportFactory.java | 30 ++ .../tuscany/sca/contribution/java/JavaExport.java | 43 +++ .../tuscany/sca/contribution/java/JavaImport.java | 58 +++ .../contribution/java/JavaImportExportFactory.java | 42 +++ .../java/impl/ClassLoaderModelResolver.java | 165 +++++++++ .../java/impl/ClassReferenceModelResolver.java | 177 ++++++++++ .../java/impl/ContributionClassLoader.java | 390 +++++++++++++++++++++ .../contribution/java/impl/ContributionHelper.java | 96 +++++ .../sca/contribution/java/impl/JavaExportImpl.java | 59 ++++ .../java/impl/JavaExportModelResolver.java | 76 ++++ .../java/impl/JavaExportProcessor.java | 163 +++++++++ .../java/impl/JavaImportExportFactoryImpl.java | 41 +++ .../java/impl/JavaImportExportListener.java | 157 +++++++++ .../sca/contribution/java/impl/JavaImportImpl.java | 111 ++++++ .../java/impl/JavaImportModelResolver.java | 62 ++++ .../java/impl/JavaImportProcessor.java | 168 +++++++++ ...ntribution.java.ContributionClassLoaderProvider | 21 ++ ...a.ContributionClassloaderProviderExtensionPoint | 18 + ...y.sca.contribution.java.JavaImportExportFactory | 18 + ...ca.contribution.processor.StAXArtifactProcessor | 19 + ...tuscany.sca.contribution.resolver.ModelResolver | 18 + ...y.sca.contribution.service.ContributionListener | 18 + ...ontribution-java-validation-messages.properties | 23 ++ .../ClassReferenceArtifactResolverTestCase.java | 70 ++++ .../java/impl/ContributionClassLoaderTestCase.java | 262 ++++++++++++++ .../java/impl/JavaExportProcessorTestCase.java | 103 ++++++ .../java/impl/JavaImportProcessorTestCase.java | 105 ++++++ .../resources/deployables/sample-calculator.jar | Bin 0 -> 26901 bytes 35 files changed, 3091 insertions(+) create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/LICENSE create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/NOTICE create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/pom.xml create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassloaderProviderExtensionPoint.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassloaderProviderExtensionPoint.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultJavaImportExportFactory.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaExport.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImport.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportImpl.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportModelResolver.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessor.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportListener.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportImpl.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportModelResolver.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessor.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.JavaImportExportFactory create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.service.ContributionListener create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/contribution-java-validation-messages.properties create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessorTestCase.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessorTestCase.java create mode 100644 sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/resources/deployables/sample-calculator.jar (limited to 'sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java') diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/LICENSE b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/LICENSE new file mode 100644 index 0000000000..8aa906c321 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/LICENSE @@ -0,0 +1,205 @@ + + 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/tags/1.6-TUSCANY-3909/contribution-java/NOTICE b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/NOTICE new file mode 100644 index 0000000000..ad2ba40961 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/NOTICE @@ -0,0 +1,6 @@ +${pom.name} +Copyright (c) 2005 - 2010 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/tags/1.6-TUSCANY-3909/contribution-java/pom.xml b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/pom.xml new file mode 100644 index 0000000000..68cb8d5223 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/pom.xml @@ -0,0 +1,77 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-modules + 1.6 + ../pom.xml + + tuscany-contribution-java + Apache Tuscany SCA Java Import/Export Model + + + + org.apache.tuscany.sca + tuscany-contribution + 1.6 + + + + org.apache.tuscany.sca + tuscany-core-spi + 1.6 + + + + org.codehaus.woodstox + wstx-asl + 3.2.4 + runtime + + + stax + stax-api + + + + + + + + + org.apache.felix + maven-bundle-plugin + + + + ${tuscany.version} + org.apache.tuscany.sca.contribution.java + ${pom.name} + org.apache.tuscany.sca.contribution.java* + org.apache.tuscany.sca.contribution.osgi.impl + + + + + + + diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java new file mode 100644 index 0000000000..aa314dd13f --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassLoaderProvider.java @@ -0,0 +1,44 @@ +/* + * 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.contribution.java; + +import org.apache.tuscany.sca.contribution.Contribution; + +/** + * A pluggable utility to provide a classloader for a given contribution + */ +public interface ContributionClassLoaderProvider { + + /** + * Get the contribution type that this object will provide + * classloaders for. Takes values from o.a.t.s.contribution.PackageType + * + * @return + */ + String getContributionType(); + + /** + * Get the classloader for the given contribution + * @param contribution + * @param parent + * @return + */ + ClassLoader getClassLoader(Contribution contribution, ClassLoader parent); +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassloaderProviderExtensionPoint.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassloaderProviderExtensionPoint.java new file mode 100644 index 0000000000..b714090ff2 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/ContributionClassloaderProviderExtensionPoint.java @@ -0,0 +1,55 @@ +/* + * 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.contribution.java; + +/** + * An extension point for contribution classloaders. Contribution + * classloaders respect the classloading strategy for the contribution + * in question. For example, a JAR contribution loads classes from its + * root directory while an EAR contribution follows the JEE classloading + * strategy. The choice of classloader is driven by the type of + * contribution being loaded. + * + * @version $Rev$ $Date$ + */ +public interface ContributionClassloaderProviderExtensionPoint { + + /** + * Add a contribution classloader provider extension. + * + * @param provider The provider to add + */ + void addProvider(ContributionClassLoaderProvider provider); + + /** + * Remove a contribution classloader provider extension. + * + * @param provider The provider to remove + */ + void removeProvider(ContributionClassLoaderProvider provider); + + /** + * Get a contribution classloader provider for the given contribution type. + * @param contributionType the lookup key + * @return The provider + */ + ContributionClassLoaderProvider getProvider(String contributionType); + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java new file mode 100644 index 0000000000..8fd1898ad6 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassLoaderProvider.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.contribution.java; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.java.impl.ContributionClassLoader; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * The default implementation of the ContributionClassLoaderProvider + */ +public class DefaultContributionClassLoaderProvider implements ContributionClassLoaderProvider { + + public DefaultContributionClassLoaderProvider() { + super(); + } + + /** + * returns null as it is the default provider and applies when no specific + * provider has been specified + */ + public String getContributionType() { + return null; + } + + public ClassLoader getClassLoader(Contribution contribution, ClassLoader parent) { + return new ContributionClassLoader(contribution, parent); + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassloaderProviderExtensionPoint.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassloaderProviderExtensionPoint.java new file mode 100644 index 0000000000..7ffecdf312 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultContributionClassloaderProviderExtensionPoint.java @@ -0,0 +1,144 @@ +/* + * 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.contribution.java; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.PackageProcessor; +import org.apache.tuscany.sca.contribution.service.ContributionException; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; + + +/** + * Default implementation of a contribution classloader provider extension point. + * + * @version $Rev$ $Date$ + */ +public class DefaultContributionClassloaderProviderExtensionPoint implements ContributionClassloaderProviderExtensionPoint { + + private HashMap providers = new HashMap(); + private boolean loaded; + + /** + * Constructs a new DefaultModelFactoryExtensionPoint. + */ + public DefaultContributionClassloaderProviderExtensionPoint() { + } + + /** + * Add a contribution classloader provider extension. + * + * @param provider The provider to add + */ + public void addProvider(ContributionClassLoaderProvider provider){ + providers.put(provider.getContributionType(), provider); + } + + /** + * Remove a contribution classloader provider extension. + * + * @param provider The provider to remove + */ + public void removeProvider(ContributionClassLoaderProvider provider){ + providers.remove(provider.getContributionType()); + } + + /** + * Get a contribution classloader provider for the given contribution type. + * @param contributionType the lookup key + * @return The provider + */ + public ContributionClassLoaderProvider getProvider(String contributionType){ + loadProviders(); + return providers.get(contributionType); + } + + private synchronized void loadProviders() { + if (loaded) + return; + + // Get the processor service declarations + Set processorDeclarations; + try { + processorDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ContributionClassLoaderProvider.class); + } catch (IOException e) { + throw new IllegalStateException(e); + } + + for (ServiceDeclaration processorDeclaration: processorDeclarations) { + Map attributes = processorDeclaration.getAttributes(); + + // Load a URL artifact processor + String contributionType = attributes.get("type"); + + // Create a processor wrapper and register it + ContributionClassLoaderProvider provider = new LazyContributionClassLoaderProvider(contributionType, processorDeclaration); + addProvider(provider); + } + + loaded = true; + } + + /** + * A facade for package processors. + */ + private static class LazyContributionClassLoaderProvider implements ContributionClassLoaderProvider { + + private ServiceDeclaration processorDeclaration; + private String contributionType; + private ContributionClassLoaderProvider provider; + + private LazyContributionClassLoaderProvider(String contributionType, ServiceDeclaration processorDeclaration) { + this.processorDeclaration = processorDeclaration; + this.contributionType = contributionType; + } + + public String getContributionType() { + return contributionType; + } + + @SuppressWarnings("unchecked") + public ClassLoader getClassLoader(Contribution contribution, ClassLoader parent){ + if (provider == null) { + try { + Class providerClass = (Class)processorDeclaration.loadClass(); + Constructor constructor = providerClass.getConstructor(); + provider = constructor.newInstance(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + return provider.getClassLoader(contribution, parent); + } + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultJavaImportExportFactory.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultJavaImportExportFactory.java new file mode 100644 index 0000000000..886c61c00a --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/DefaultJavaImportExportFactory.java @@ -0,0 +1,30 @@ +/* + * 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.contribution.java; + +import org.apache.tuscany.sca.contribution.java.impl.JavaImportExportFactoryImpl; + +/** + * Default Java Import/Export Factory implementation + * + * @version $Rev$ $Date$ + */ +public class DefaultJavaImportExportFactory extends JavaImportExportFactoryImpl implements JavaImportExportFactory { +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaExport.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaExport.java new file mode 100644 index 0000000000..41e93ec148 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaExport.java @@ -0,0 +1,43 @@ +/* + * 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.contribution.java; + +import org.apache.tuscany.sca.contribution.Export; + +/** + * Base Java Export model interface + * + * @version $Rev$ $Date$ + */ +public interface JavaExport extends Export { + /** + * Get java package that identifies the import + * + * @return The package name + */ + String getPackage(); + + /** + * Set java package that identifies the import + * + * @param packageName The package name + */ + void setPackage(String packageName); +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImport.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImport.java new file mode 100644 index 0000000000..d3568f6283 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImport.java @@ -0,0 +1,58 @@ +/* + * 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.contribution.java; + +import org.apache.tuscany.sca.contribution.Import; + +/** + * Base Java Import model interface + * + * @version $Rev$ $Date$ + */ +public interface JavaImport extends Import { + + /** + * Get the location used to resolve the definitions for this import + * + * @return The import location + */ + String getLocation(); + + /** + * Set the location used to resolve the definitions for this import + * + * @param location The import location + */ + void setLocation(String location); + + /** + * Get java package that identifies the import + * + * @return The package name + */ + String getPackage(); + + /** + * Set java package that identifies the import + * + * @param packageName The package name + */ + void setPackage(String packageName); +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java new file mode 100644 index 0000000000..af2ca9cd57 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/JavaImportExportFactory.java @@ -0,0 +1,42 @@ +/* + * 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.contribution.java; + +/** + * Base Java Import/Export model factory + * + * @version $Rev$ $Date$ + */ +public interface JavaImportExportFactory { + + /** + * Create a java import model object + * + * @return The new JavaImport model object + */ + JavaImport createJavaImport(); + + /** + * Create a java export model object + * + * @return The new JavaExport model object + */ + JavaExport createJavaExport(); +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java new file mode 100644 index 0000000000..c44a08c67b --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java @@ -0,0 +1,165 @@ +/* + * 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.contribution.java.impl; + +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.DefaultDelegatingModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; + +/** + * A Model Resolver for ClassReferences. + * + * @version $Rev$ $Date$ + */ +public class ClassLoaderModelResolver extends URLClassLoader implements ModelResolver { + private Contribution contribution; + private Map importResolvers = new HashMap(); + + private static ClassLoader parentClassLoader() { + ClassLoader parentClassLoader = ServiceDiscovery.class.getClassLoader(); + return parentClassLoader; + } + + private static URL[] getContributionURLs(final Contribution contribution) throws IOException { + List urls = new ArrayList(); + urls.add(new URL(contribution.getLocation())); + urls.addAll(ContributionHelper.getNestedJarUrls(contribution)); + return urls.toArray(new URL[urls.size()]); + } + + public ClassLoaderModelResolver(final Contribution contribution, ModelFactoryExtensionPoint modelFactories) throws IOException { + super(getContributionURLs(contribution), parentClassLoader()); + this.contribution = contribution; + + // Index Java import resolvers by package name + Map> resolverMap = new HashMap>(); + for (Import import_: this.contribution.getImports()) { + if (import_ instanceof JavaImport) { + JavaImport javaImport = (JavaImport)import_; + List resolvers = resolverMap.get(javaImport.getPackage()); + if (resolvers == null) { + resolvers = new ArrayList(); + resolverMap.put(javaImport.getPackage(), resolvers); + } + resolvers.add(javaImport.getModelResolver()); + } + } + + // Create a delegating model resolver for each imported package + for (Map.Entry> entry: resolverMap.entrySet()) { + importResolvers.put(entry.getKey(), new DefaultDelegatingModelResolver(entry.getValue())); + } + } + + public void addModel(Object resolved) { + throw new IllegalStateException(); + } + + public Object removeModel(Object resolved) { + throw new IllegalStateException(); + } + + public T resolveModel(Class modelClass, T unresolved) { + if (!(unresolved instanceof ClassReference)) { + return unresolved; + } + + try { + + // Load the class and return a class reference for it + String className = ((ClassReference)unresolved).getClassName(); + Class clazz = Class.forName(className, true, this); + return modelClass.cast(new ClassReference(clazz)); + + } catch (ClassNotFoundException e) { + return unresolved; + } catch (NoClassDefFoundError e) { + return unresolved; + } + } + + @Override + public URL findResource(String name) { + + //TODO delegate to the Java import resolvers + + URL url = super.findResource(name); + return url; + } + + @Override + public Enumeration findResources(String name) throws IOException { + + //TODO delegate to the Java import resolvers + //Enumeration importedResources; + + Enumeration resources = super.findResources(name); + List allResources = new ArrayList(); + //for (; importedResources.hasMoreElements(); ) { + // allResources.add(importedResources.nextElement()); + //} + for (; resources.hasMoreElements(); ) { + allResources.add(resources.nextElement()); + } + return Collections.enumeration(allResources); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + + // Extract the package name + int d = name.lastIndexOf('.'); + String packageName; + if (d != -1) { + packageName = name.substring(0, d); + } else { + packageName = null; + } + + // First try to load the class using the Java import resolvers + ModelResolver importResolver = importResolvers.get(packageName); + if (importResolver != null) { + ClassReference classReference = importResolver.resolveModel(ClassReference.class, new ClassReference(name)); + if (!classReference.isUnresolved()) { + return classReference.getJavaClass(); + } + } + + // Next, try to load the class from the current contribution + Class clazz = super.findClass(name); + return clazz; + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java new file mode 100644 index 0000000000..0b1c035ff4 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceModelResolver.java @@ -0,0 +1,177 @@ +/* + * 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.contribution.java.impl; + +import java.lang.ref.WeakReference; +import java.lang.reflect.Constructor; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider; +import org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint; +import org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider; +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.UtilityExtensionPoint; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; + +/** + * A Model Resolver for ClassReferences. + * + * @version $Rev: 557916 $ $Date: 2007-07-20 01:04:40 -0700 (Fri, 20 Jul 2007) $ + */ +public class ClassReferenceModelResolver implements ModelResolver { + private final ExtensionPointRegistry registry; + private final Contribution contribution; + private WeakReference classLoader; + private Map map = new HashMap(); + + private ModelResolver osgiResolver; + + public ClassReferenceModelResolver(final Contribution contribution, ExtensionPointRegistry registry) { + this.contribution = contribution; + this.registry = registry; + if (this.contribution != null) { + // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy. + // ClassLoader cl = contribution.getClassLoader(); + ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction() { + public ClassLoader run() { + return contribution.getClassLoader(); + } + }); + + if (cl == null) { + // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy. + //ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction() { + // public ClassLoader run() { + // return Thread.currentThread().getContextClassLoader(); + // } + //}); + ClassLoader contextClassLoader = ServiceDiscovery.getInstance().getServiceDiscoverer().getClass().getClassLoader(); + ContributionClassLoaderProvider provider = null; + try { + ContributionClassloaderProviderExtensionPoint providers = + registry.getExtensionPoint(ContributionClassloaderProviderExtensionPoint.class); + + provider = providers.getProvider(contribution.getType()); + + } catch (Throwable e) { + // Ignore errors + } + if (provider == null) { + provider = new DefaultContributionClassLoaderProvider(); + } + cl = provider.getClassLoader(contribution, contextClassLoader); + contribution.setClassLoader(cl); + } + this.classLoader = new WeakReference(cl); + } else { + // This path should be used only for unit testing. + // Allow privileged access to get ClassLoader. Requires RuntimePermission in security policy. + // this.classLoader = new WeakReference(this.getClass().getClassLoader()); + ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction() { + public ClassLoader run() { + return this.getClass().getClassLoader(); + } + }); + this.classLoader = new WeakReference( cl ); + } + + try { + Class osgiResolverClass = + Class.forName("org.apache.tuscany.sca.contribution.osgi.impl.OSGiClassReferenceModelResolver"); + if (osgiResolverClass != null) { + Constructor constructor = + osgiResolverClass.getConstructor(Contribution.class, ExtensionPointRegistry.class); + this.osgiResolver = (ModelResolver)constructor.newInstance(contribution, registry); + } + } catch (Throwable e) { + // Ignore error, non-OSGi classloading is used in this case + } + } + + public void addModel(Object resolved) { + ClassReference clazz = (ClassReference)resolved; + map.put(clazz.getClassName(), clazz); + } + + public Object removeModel(Object resolved) { + return map.remove(((ClassReference)resolved).getClassName()); + } + + + + public T resolveModel(Class modelClass, T unresolved) { + if (!(unresolved instanceof ClassReference)) { + return unresolved; + } + Object resolved = map.get(((ClassReference)unresolved).getClassName()); + + if (resolved != null) { + return modelClass.cast(resolved); + } + + //Load a class on demand + Class clazz = null; + + if (osgiResolver != null) { + resolved = osgiResolver.resolveModel(modelClass, unresolved); + clazz = ((ClassReference)resolved).getJavaClass(); + } + + if (clazz == null) { + try { + // Search contribution ClassLoader (which has visibility of classes in the contribution + // as well as explicitly imported packages from other contributions) + clazz = Class.forName(((ClassReference)unresolved).getClassName(), true, classLoader.get()); + } catch (ClassNotFoundException e) { + } catch (NoClassDefFoundError e) { + } + } + + if (clazz != null) { + //if we load the class + // Store a new ClassReference wrapping the loaded class + ClassReference classReference = new ClassReference(clazz); + map.put(clazz.getName(), classReference); + + // Return the resolved ClassReference + return modelClass.cast(classReference); + } else { + return unresolved; + } + + } + + + /*************** + * Helper methods + ***************/ + + private String getPackageName(ClassReference clazz) { + int pos = clazz.getClassName().lastIndexOf("."); + return clazz.getClassName().substring(0, pos); + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java new file mode 100644 index 0000000000..b167d9af4e --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoader.java @@ -0,0 +1,390 @@ +/* + * 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.contribution.java.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.java.JavaImport; + + +public class ContributionClassLoader extends URLClassLoader { +// public class ContributionClassLoader { + + private Contribution contribution; + // private b urlClassLoader; + + /** + * Constructor for contribution ClassLoader + * + * @param contribution + * @param parent + * @throws MalformedURLException + */ + public ContributionClassLoader(Contribution contribution, final ClassLoader parent) { + super(new URL[0], parent); + // Note that privileged use of getContextClassLoader have been promoted to callers. + // super(new URL[0], parent == null?Thread.currentThread().getContextClassLoader(): null); + this.contribution = contribution; + if (contribution.getLocation() != null) { + try { + this.addURL(new URL(contribution.getLocation())); + for (URL url : ContributionHelper.getNestedJarUrls(contribution)) { + this.addURL(url); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + /* + * Return the ClassLoader corresponding to a contribution providing an export + * Create a new ClassLoader for the contribution if one does not exist + */ + private ClassLoader getExportClassLoader(Contribution exportingContribution) { + ClassLoader cl = exportingContribution.getClassLoader(); + if (!(cl instanceof ContributionClassLoader)) { + if (cl == null) { + cl = getParent(); + } + + cl = new ContributionClassLoader(exportingContribution, cl); + exportingContribution.setClassLoader(cl); + } + return cl; + } + + /* (non-Javadoc) + * @see java.net.URLClassLoader#findClass(java.lang.String) + * + * Search path for class: + * This contribution + * Imported contributions + */ + @Override + protected Class findClass(String className) throws ClassNotFoundException { + + Class clazz = null; + try { + clazz = findClassFromContribution(className); + } catch (ClassNotFoundException e) { + + for (Import import_ : this.contribution.getImports()) { + if (classNameMatchesImport(className, import_)) { + // Delegate the resolution to the imported contribution + for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) { + + ClassLoader exportClassLoader = getExportClassLoader(exportingContribution); + if (exportClassLoader instanceof ContributionClassLoader) { + + for (Export export : exportingContribution.getExports()) { + try { + if (import_.match(export)) { + clazz = ((ContributionClassLoader)exportClassLoader).findClassFromContribution(className); + break; + } + } catch (ClassNotFoundException e1) { + continue; + } + + } + if (clazz != null) break; + } + } + if (clazz != null) break; + } + } + + if (clazz == null) throw e; + } + return clazz; + } + + + /* (non-Javadoc) + * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) + * + * Search path for class: + * Parent ClassLoader + * This contribution + * Imported contributions + * + */ + @Override + protected synchronized Class loadClass(String className, boolean resolveClass) + throws ClassNotFoundException { + + Class clazz = null; + try { + + if (this.getParent() != null) + clazz = this.getParent().loadClass(className); + + } catch (ClassNotFoundException e) { + } + + if (clazz == null) + clazz = findClass(className); + + + if (resolveClass) + this.resolveClass(clazz); + return clazz; + + } + + + + /* + * (non-Javadoc) + * + * @see java.net.URLClassLoader#findResource(java.lang.String) + */ + @Override + public URL findResource(String name) { + + URL url = findResourceFromContribution(name); + + if (url == null) { + for (Import import_ : this.contribution.getImports()) { + if (resourceNameMatchesImport(name, import_)) { + // Delegate the resolution to the imported contribution + for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) { + + ClassLoader exportClassLoader = getExportClassLoader(exportingContribution); + if (exportClassLoader instanceof ContributionClassLoader) { + + for (Export export : exportingContribution.getExports()) { + if (import_.match(export)) { + url = ((ContributionClassLoader)exportClassLoader).findResourceFromContribution(name); + if (url != null) break; + } + } + if (url != null) break; + } + } + if (url != null) break; + } + } + + } + return url; + } + + + /* (non-Javadoc) + * @see java.net.URLClassLoader#findResources(java.lang.String) + */ + @Override + public Enumeration findResources(String name) throws IOException { + + return Collections.enumeration(findResourceSet(name)); + } + + + + + /* (non-Javadoc) + * @see java.lang.ClassLoader#getResource(java.lang.String) + * + * Find a resource. + * Search path for resource: + * Parent ClassLoader + * This contribution + * Imported contributions + */ + @Override + public URL getResource(String resName) { + + URL resource = null; + + if (this.getParent() != null) { + resource = this.getParent().getResource(resName); + } + if (resource == null) + resource = findResource(resName); + + return resource; + } + + + + /* (non-Javadoc) + * @see java.lang.ClassLoader#getResources(java.lang.String) + * + * Return list of resources from this contribution, resources + * imported through imported contributions and resources from parent + * ClassLoader. + */ + @Override + public Enumeration getResources(String resName) throws IOException { + + HashSet resourceSet = findResourceSet(resName); + addEnumerationToCollection(resourceSet, super.getResources(resName)); + + return Collections.enumeration(resourceSet); + } + + + /* + * Find set of resources + */ + private HashSet findResourceSet(String name) throws IOException { + + HashSet resources = new HashSet(); + + addEnumerationToCollection(resources, super.findResources(name)); + + for (Import import_ : this.contribution.getImports()) { + if (!(import_ instanceof JavaImport)) { + continue; + } + if (resourceNameMatchesImport(name, import_)) { + // Delegate the resolution to the imported contribution + for (Contribution exportingContribution : ((JavaImportModelResolver)import_.getModelResolver()).getExportContributions()) { + + ClassLoader exportClassLoader = getExportClassLoader(exportingContribution); + if (exportClassLoader instanceof ContributionClassLoader) { + + for (Export export : exportingContribution.getExports()) { + if (import_.match(export)) { + addEnumerationToCollection(resources, + ((ContributionClassLoader)exportClassLoader).findResources(name)); + } + } + } + } + } + } + + return resources; + } + + + /* + * Find class from contribution. If class has already been loaded, return loaded class. + */ + private Class findClassFromContribution(String className) throws ClassNotFoundException { + + Class clazz = findLoadedClass(className); + if (clazz == null) + clazz = super.findClass(className); + return clazz; + + } + + /* + * Find resource from contribution. + */ + private URL findResourceFromContribution(String name) { + + return super.findResource(name); + } + + /** + * Check if a class name matches an import statement. + * Class matches if the package name used in matches + * + * @param name Name of class + * @param import_ SCA contribution import + * @return true if this is a matching import + */ + private boolean classNameMatchesImport(String name, Import import_) { + + if (import_ instanceof JavaImport && name != null && name.lastIndexOf('.') > 0) { + JavaImport javaImport = (JavaImport) import_; + + String packageName = name.substring(0, name.lastIndexOf('.')); + if (javaImport.getPackage().endsWith(".*")) { + String prefix = javaImport.getPackage().substring(0, javaImport.getPackage().length() -1); + if (packageName.startsWith(prefix)) { + return true; + } + } else { + return packageName.equals(javaImport.getPackage()); + } + } + + return false; + } + + /** + * Check if a resource name matches an import statement. + * Resource matches if package/namespace match the directory of resource. + * + * @param name Name of resource + * @param import_ SCA contribution import + * @return true if this is a matching import + */ + private boolean resourceNameMatchesImport(String name, Import import_) { + + + if (name == null || name.lastIndexOf('/') <= 0) + return false; + else if (import_ instanceof JavaImport) { + JavaImport javaImport = (JavaImport) import_; + + if (javaImport.getPackage().endsWith(".*")) { + String packageName = name.substring(0, name.lastIndexOf('/')).replace('/', '.'); + String prefix = javaImport.getPackage().substring(0, javaImport.getPackage().length() -1); + if (packageName.startsWith(prefix)) { + return true; + } + } else { + // 'name' is a resource : contains "/" as separators + // Get package name from resource name + String packageName = name.substring(0, name.lastIndexOf('/')); + return packageName.equals(javaImport.getPackage().replaceAll("\\.", "/")); + } + } + return false; + } + + /* + * Add an enumeration to a Collection + */ + private void addEnumerationToCollection(Collection collection, Enumeration enumeration) { + + while (enumeration.hasMoreElements()) + collection.add(enumeration.nextElement()); + } + + + @Override + public String toString() { + return "SCA Contribution ClassLoader location: " + contribution.getLocation() + " parent ClassLoader: " + getParent(); + } + + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java new file mode 100644 index 0000000000..e913495789 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ContributionHelper.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.contribution.java.impl; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; + +public class ContributionHelper { + + public static List getNestedJarUrls(final Contribution contribution) throws IOException { + List urls = new ArrayList(); + boolean isZipContribution = contribution.getLocation().endsWith(".zip"); + URI uri = URI.create(contribution.getLocation()); + boolean isFolderContribution = !isZipContribution && uri.getScheme().equals("file") && new File(uri).isDirectory(); + if (isZipContribution || isFolderContribution) { + for (Artifact a : contribution.getArtifacts()) { + if (a.getLocation().endsWith(".jar")) { + if (isZipContribution) { + urls.add(createTempJar(a, contribution)); + } else { + urls.add(new URL(a.getLocation())); + } + } + } + } + return urls; + } + + /** + * URLClassLoader doesn't seem to work with URLs to jars within an archive so as a work around + * copy the jar to a temp file and use the url to that. + */ + private static URL createTempJar(Artifact artifact, Contribution contribution) throws IOException { + FileOutputStream fileOutputStream = null; + ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File(URI.create(contribution.getLocation())))); + try { + ZipEntry zipEntry = zipInputStream.getNextEntry(); + while (zipEntry != null) { + if (artifact.getLocation().endsWith(zipEntry.getName())) { + + String tempName = ("tmp." + artifact.getURI().substring(0, artifact.getURI().length() - 3)).replace('/', '.'); + File tempFile = File.createTempFile(tempName, ".jar"); + tempFile.deleteOnExit(); + fileOutputStream = new FileOutputStream(tempFile); + + byte[] buf = new byte[2048]; + int n; + while ((n = zipInputStream.read(buf, 0, buf.length)) > -1) { + fileOutputStream.write(buf, 0, n); + } + + fileOutputStream.close(); + zipInputStream.closeEntry(); + + return tempFile.toURI().toURL(); + + } + zipEntry = zipInputStream.getNextEntry(); + } + } finally { + zipInputStream.close(); + if (fileOutputStream != null) { + fileOutputStream.close(); + } + } + + throw new IllegalStateException(); + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportImpl.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportImpl.java new file mode 100644 index 0000000000..c0bd9465e4 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportImpl.java @@ -0,0 +1,59 @@ +/* + * 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.contribution.java.impl; + +import org.apache.tuscany.sca.assembly.impl.ExtensibleImpl; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * Implementation of a Java Import model + * + * @version $Rev$ $Date$ + */ +public class JavaExportImpl extends ExtensibleImpl implements JavaExport { + private ModelResolver modelResolver; + + /** + * Java package being exported + */ + private String packageName; + + public JavaExportImpl() { + super(); + } + + public String getPackage() { + return this.packageName; + } + + public void setPackage(String packageName) { + this.packageName = packageName; + } + + public ModelResolver getModelResolver() { + return modelResolver; + } + + public void setModelResolver(ModelResolver modelResolver) { + this.modelResolver = modelResolver; + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportModelResolver.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportModelResolver.java new file mode 100644 index 0000000000..8d5412f6d7 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportModelResolver.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.contribution.java.impl; + +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * A model resolver for Java exports. + * + * @version $Rev$ $Date$ + */ +public class JavaExportModelResolver implements ModelResolver { + + private JavaExport export; + private ModelResolver resolver; + + public JavaExportModelResolver(JavaExport export, ModelResolver resolver) { + this.export = export; + this.resolver = resolver; + } + + public void addModel(Object resolved) { + throw new IllegalStateException(); + } + + public Object removeModel(Object resolved) { + throw new IllegalStateException(); + } + + public T resolveModel(Class modelClass, T unresolved) { + if (!(unresolved instanceof ClassReference)) { + return unresolved; + } + + // Filter package name + ClassReference classReference = (ClassReference)unresolved; + String className = classReference.getClassName(); + int d = className.lastIndexOf('.'); + String packageName; + if (d != -1) { + packageName = className.substring(0, d); + } else { + packageName = ""; + } + if (export.getPackage().equals(packageName)) { + + // Package matches the exported package, delegate to the + // contribution's resolver + return resolver.resolveModel(modelClass, unresolved); + } else { + + // Package is not exported, return the unresolved object + return unresolved; + } + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessor.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessor.java new file mode 100644 index 0000000000..b79aa32dc3 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessor.java @@ -0,0 +1,163 @@ +/* + * 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.contribution.java.impl; + +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory; +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.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; + +/** + * Artifact processor for Java Export + * + * @version $Rev$ $Date$ + */ +public class JavaExportProcessor implements StAXArtifactProcessor { + private static final String SCA10_NS = "http://www.osoa.org/xmlns/sca/1.0"; + + private static final QName EXPORT_JAVA = new QName(SCA10_NS, "export.java"); + + private static final String PACKAGE = "package"; + + private final JavaImportExportFactory factory; + private final Monitor monitor; + + public JavaExportProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + super(); + this.factory = modelFactories.getFactory(JavaImportExportFactory.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(), "contribution-java-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-java-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + public QName getArtifactType() { + return EXPORT_JAVA; + } + + public Class getModelType() { + return JavaExport.class; + } + + /** + * Process + */ + public JavaExport read(XMLStreamReader reader) throws ContributionReadException { + JavaExport javaExport = this.factory.createJavaExport(); + QName element = null; + + try { + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + element = reader.getName(); + + // Read + if (EXPORT_JAVA.equals(element)) { + String packageName = reader.getAttributeValue(null, PACKAGE); + if (packageName == null) { + error("AttributePackageMissing", reader); + //throw new ContributionReadException("Attribute 'package' is missing"); + } else + javaExport.setPackage(packageName); + } + break; + case XMLStreamConstants.END_ELEMENT: + if (EXPORT_JAVA.equals(reader.getName())) { + return javaExport; + } + break; + } + + //Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error("XMLStreamException", reader, ex); + } + + return javaExport; + } + + public void write(JavaExport javaExport, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + + // Write + writer.writeStartElement(EXPORT_JAVA.getNamespaceURI(), EXPORT_JAVA.getLocalPart()); + + if (javaExport.getPackage() != null) { + writer.writeAttribute(PACKAGE, javaExport.getPackage()); + } + + writer.writeEndElement(); + } + + public void resolve(JavaExport javaExport, ModelResolver resolver) throws ContributionResolveException { + + if (javaExport.getPackage() != null) + // Initialize the export resolver + javaExport.setModelResolver(new JavaExportModelResolver(javaExport, resolver)); + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java new file mode 100644 index 0000000000..e5dc397f4e --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportFactoryImpl.java @@ -0,0 +1,41 @@ +/* + * 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.contribution.java.impl; + +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory; + +/** + * Java Import/Export Factory implementation + * + * @version $Rev$ $Date$ + */ +public class JavaImportExportFactoryImpl implements JavaImportExportFactory { + + public JavaImport createJavaImport() { + return new JavaImportImpl(); + } + + public JavaExport createJavaExport() { + return new JavaExportImpl(); + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportListener.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportListener.java new file mode 100644 index 0000000000..47cdc9767b --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportExportListener.java @@ -0,0 +1,157 @@ +/* + * 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.contribution.java.impl; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +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.Export; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.resolver.DefaultImportAllModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionListener; +import org.apache.tuscany.sca.contribution.service.ContributionRepository; + +/** + * Java Import/Export contribution listener + * The listener would process all import/export from a given contribution + * and initialize the model resolvers properly + * + * @version $Rev$ $Date$ + */ +public class JavaImportExportListener implements ContributionListener { + + private ContributionFactory contributionFactory; + + /** + * Constructs a new JavaImportExportListener + */ + public JavaImportExportListener(ModelFactoryExtensionPoint modelFactories) { + contributionFactory = modelFactories.getFactory(ContributionFactory.class); + } + + /** + * Initialize the import/export model resolvers + * Export model resolvers are same as Contribution model resolver + * Import model resolvers are matched to a specific contribution if a location URI is specified, + * otherwise it try to resolve against all the other contributions + * Also set the exporting contributions used by contribution ClassLoaders to + * match import/export for class loading. + */ + public void contributionAdded(ContributionRepository repository, Contribution contribution) { + + // If the contribution does not contain sca-contribution.xml metadata + // (for example it's an existing JAR developed before SCA existed) + // export all its Java packages + ModelResolver modelResolver = contribution.getModelResolver(); + + // Look for META-INF/sca-contribution.xml + Artifact artifact = contributionFactory.createArtifact(); + artifact.setURI(Contribution.SCA_CONTRIBUTION_META); + artifact = modelResolver.resolveModel(Artifact.class, artifact); + if (artifact.getLocation() == null) { + + // Look for META-INF/sca-contribution-generated.xml + artifact.setURI(Contribution.SCA_CONTRIBUTION_GENERATED_META); + artifact = modelResolver.resolveModel(Artifact.class, artifact); + if (artifact.getLocation() == null) { + + // No contribution metadata file was found, default to export all the + // Java packages found in the contribution + Set packages = new HashSet(); + for (Artifact a: contribution.getArtifacts()) { + String uri = a.getURI(); + if (uri.endsWith(".class")) { + uri = uri.substring(0, uri.length() - 6); + int d = uri.lastIndexOf('/'); + if (d != -1) { + packages.add(uri.substring(0, d).replace('/', '.')); + } + } + } + + // Add Java export model objects for all the packages we found + for (String pkg: packages) { + JavaExport export = new JavaExportImpl(); + export.setPackage(pkg); + contribution.getExports().add(export); + } + } + } + + // Initialize the contribution exports + for (Export export: contribution.getExports()) { + export.setModelResolver(contribution.getModelResolver()); + } + + // Initialize the contribution imports + for (Import import_: contribution.getImports()) { + boolean initialized = false; + + if(import_ instanceof JavaImport) { + JavaImport javaImport = (JavaImport) import_; + + //Find a matching contribution + if(javaImport.getLocation() != null) { + Contribution targetContribution = repository.getContribution(javaImport.getLocation()); + if (targetContribution != null) { + + // Find a matching contribution export + for (Export export: targetContribution.getExports()) { + if (export instanceof JavaExport) { + JavaExport javaExport = (JavaExport)export; + if (javaImport.getPackage().equals(javaExport.getPackage())) { + List exportingContributions = new ArrayList(); + exportingContributions.add(targetContribution); + javaImport.setModelResolver(new JavaImportModelResolver(exportingContributions, javaExport.getModelResolver())); + initialized = true; + break; + } + } + } + } + } + + //if no location was specified, try to resolve with any contribution + if (!initialized) { + //Use a resolver that will consider all contributions + import_.setModelResolver(new JavaImportModelResolver(repository.getContributions(), new DefaultImportAllModelResolver(import_, repository.getContributions()))); + } + } + } + } + + public void contributionRemoved(ContributionRepository repository, Contribution contribution) { + + } + + public void contributionUpdated(ContributionRepository repository, Contribution oldContribution, Contribution contribution) { + + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportImpl.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportImpl.java new file mode 100644 index 0000000000..f2fdb3278e --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportImpl.java @@ -0,0 +1,111 @@ +/* + * 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.contribution.java.impl; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.impl.ExtensibleImpl; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.Export; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * Implementation of a Java Import model + * + * @version $Rev$ $Date$ + */ +public class JavaImportImpl extends ExtensibleImpl implements JavaImport { + private ModelResolver modelResolver; + private List contributions; + /** + * Java package name being imported + */ + private String packageName; + /** + * Contribution URI where the artifact is imported from + */ + private String location; + + public JavaImportImpl() { + super(); + } + + public String getLocation() { + return this.location; + } + + public void setLocation(String location) { + this.location = location; + } + + public String getPackage() { + return this.packageName; + } + + public void setPackage(String packageName) { + this.packageName = packageName; + } + + public ModelResolver getModelResolver() { + return this.modelResolver; + } + + public void setModelResolver(ModelResolver modelResolver) { + this.modelResolver = modelResolver; + } + public List getExportContributions() { + return contributions; + } + + public void setExportContributions(List contributions) { + this.contributions = contributions; + } + + /** + * Match a JavaImport to a given JavaExport based on : + * location is not provided + * import and export packages match + */ + public boolean match(Export export) { + if(export instanceof JavaExport) { + JavaExport javaExport = (JavaExport)export; + String exportedPackage = javaExport.getPackage(); + if (packageName.equals(exportedPackage)) { + return true; + } else { + if (packageName.endsWith(".*")) { + String prefix = packageName.substring(0, packageName.length() - 1); + if (exportedPackage.startsWith(prefix)) { + return true; + } + } + } + } + + return false; + } + + @Override + public String toString() { + return String.valueOf(packageName); + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportModelResolver.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportModelResolver.java new file mode 100644 index 0000000000..484eddd222 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportModelResolver.java @@ -0,0 +1,62 @@ +/* + * 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.contribution.java.impl; + +import java.util.List; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * A JavaImport specific model resolver. This model resolver is temporary + * and provides the ContributionClassLoader with the list of exporting + * contributions that it currently needs. + * + * FIXME Remove this class after the ContributionClassLoader is simplified + * and cleaned up. + * + * @version $Rev$ $Date$ + */ +public class JavaImportModelResolver implements ModelResolver { + + private ModelResolver modelResolver; + private List contributions; + + public JavaImportModelResolver(List contributions, ModelResolver modelResolver) { + this.modelResolver = modelResolver; + this.contributions = contributions; + } + + public List getExportContributions() { + return contributions; + } + + public void addModel(Object resolved) { + modelResolver.addModel(resolved); + } + + public Object removeModel(Object resolved) { + return modelResolver.removeModel(resolved); + } + + public T resolveModel(Class modelClass, T unresolved) { + return modelResolver.resolveModel(modelClass, unresolved); + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessor.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessor.java new file mode 100644 index 0000000000..d18977714c --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessor.java @@ -0,0 +1,168 @@ +/* + * 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.contribution.java.impl; + +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory; +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.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; + +/** + * Artifact Processor for Java Imports + * + * @version $Rev$ $Date$ + */ +public class JavaImportProcessor implements StAXArtifactProcessor { + private static final String SCA10_NS = "http://www.osoa.org/xmlns/sca/1.0"; + + private static final QName IMPORT_JAVA = new QName(SCA10_NS, "import.java"); + + private static final String PACKAGE = "package"; + private static final String LOCATION = "location"; + + private final JavaImportExportFactory factory; + private final Monitor monitor; + + public JavaImportProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + super(); + this.factory = modelFactories.getFactory(JavaImportExportFactory.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(), "contribution-java-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "contribution-java-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + public QName getArtifactType() { + return IMPORT_JAVA; + } + + public Class getModelType() { + return JavaImport.class; + } + + /** + * Process + */ + public JavaImport read(XMLStreamReader reader) throws ContributionReadException { + JavaImport javaImport = this.factory.createJavaImport(); + QName element = null; + + try { + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + element = reader.getName(); + + // Read + if (IMPORT_JAVA.equals(element)) { + String packageName = reader.getAttributeValue(null, PACKAGE); + if (packageName == null) { + error("AttributePackageMissing", reader); + //throw new ContributionReadException("Attribute 'package' is missing"); + } else + javaImport.setPackage(packageName); + + String location = reader.getAttributeValue(null, LOCATION); + javaImport.setLocation(location); + } + break; + case XMLStreamConstants.END_ELEMENT: + if (IMPORT_JAVA.equals(reader.getName())) { + return javaImport; + } + break; + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error("XMLStreamException", reader, ex); + } + + return javaImport; + } + + public void write(JavaImport javaImport, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + + // Write + writer.writeStartElement(IMPORT_JAVA.getNamespaceURI(), IMPORT_JAVA.getLocalPart()); + + if (javaImport.getPackage() != null) { + writer.writeAttribute(PACKAGE, javaImport.getPackage()); + } + if (javaImport.getLocation() != null) { + writer.writeAttribute(LOCATION, javaImport.getLocation()); + } + + writer.writeEndElement(); + } + + + public void resolve(JavaImport model, ModelResolver resolver) throws ContributionResolveException { + + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider new file mode 100644 index 0000000000..caf247750d --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassLoaderProvider @@ -0,0 +1,21 @@ +# 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. +org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider;type=application/x-compressed +org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider;type=application/vnd.tuscany.folder +org.apache.tuscany.sca.contribution.java.DefaultContributionClassLoaderProvider;type=application/osgi.bundle + + diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint new file mode 100644 index 0000000000..e6cf964e81 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.ContributionClassloaderProviderExtensionPoint @@ -0,0 +1,18 @@ +# 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. + +org.apache.tuscany.sca.contribution.java.DefaultContributionClassloaderProviderExtensionPoint diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.JavaImportExportFactory b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.JavaImportExportFactory new file mode 100644 index 0000000000..a293330a57 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.java.JavaImportExportFactory @@ -0,0 +1,18 @@ +# 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. + +org.apache.tuscany.sca.contribution.java.impl.JavaImportExportFactoryImpl diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor new file mode 100644 index 0000000000..ac1fa9aade --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/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. + +org.apache.tuscany.sca.contribution.java.impl.JavaImportProcessor;qname=http://www.osoa.org/xmlns/sca/1.0#import.java,model=org.apache.tuscany.sca.contribution.java.JavaImport +org.apache.tuscany.sca.contribution.java.impl.JavaExportProcessor;qname=http://www.osoa.org/xmlns/sca/1.0#export.java,model=org.apache.tuscany.sca.contribution.java.JavaExport diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver new file mode 100644 index 0000000000..c69eb40636 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.resolver.ModelResolver @@ -0,0 +1,18 @@ +# 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. + +org.apache.tuscany.sca.contribution.java.impl.ClassReferenceModelResolver;model=org.apache.tuscany.sca.contribution.resolver.ClassReference diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.service.ContributionListener b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.service.ContributionListener new file mode 100644 index 0000000000..7debb3d3c6 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.service.ContributionListener @@ -0,0 +1,18 @@ +# 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. + +org.apache.tuscany.sca.contribution.java.impl.JavaImportExportListener diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/contribution-java-validation-messages.properties b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/contribution-java-validation-messages.properties new file mode 100644 index 0000000000..09fc3e9fff --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/main/resources/contribution-java-validation-messages.properties @@ -0,0 +1,23 @@ +# +# +# 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. +# +# +AttributePackageMissing = Attribute 'package' is missing +XMLStreamException = XMLStreamException occured due to : {0} + diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java new file mode 100644 index 0000000000..52c8567f3f --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ClassReferenceArtifactResolverTestCase.java @@ -0,0 +1,70 @@ + /* + * 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.contribution.java.impl; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * Test ClassReferenceArtifactResolver. + * + * @version $Rev: 560435 $ $Date: 2007-07-27 18:26:55 -0700 (Fri, 27 Jul 2007) $ + */ +public class ClassReferenceArtifactResolverTestCase extends TestCase { + private ExtensibleModelResolver resolver; + + @Override + protected void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + + ModelResolverExtensionPoint resolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); + resolvers.addResolver(ClassReference.class, ClassReferenceModelResolver.class); + ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + resolver = new ExtensibleModelResolver(null, extensionPoints); + } + + /** + * Test ClassReference resolution + * + */ + public void testResolveClass() { + ClassReference ref = new ClassReference(getClass().getName()); + ClassReference clazz = resolver.resolveModel(ClassReference.class, ref); + assertFalse(clazz.isUnresolved()); + assertTrue(clazz.getJavaClass() == getClass()); + } + + /** + * Test ClassReference resolution of inexistent class + * + */ + public void testUnresolvedClass() { + ClassReference ref = new ClassReference("NonExistentClass"); + ClassReference clazz = resolver.resolveModel(ClassReference.class, ref); + assertTrue(clazz.isUnresolved()); + assertTrue(clazz.getJavaClass() == null); + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java new file mode 100644 index 0000000000..3ae161642d --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java @@ -0,0 +1,262 @@ +/* + * 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.contribution.java.impl; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +/** + * Test ContributionClassLoader. + * + */ +public class ContributionClassLoaderTestCase { + + private ContributionFactory contributionFactory; + private JavaImportExportFactory javaImportExportFactory; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + contributionFactory = modelFactories.getFactory(ContributionFactory.class); + javaImportExportFactory = modelFactories.getFactory(JavaImportExportFactory.class); + } + + private Contribution createContribution(String fileName) throws MalformedURLException { + Contribution contrib = contributionFactory.createContribution(); + File contribDir = new File(fileName); + contrib.setLocation(contribDir.toURI().toURL().toString()); + ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction() { + public ClassLoader run() { + return Thread.currentThread().getContextClassLoader(); + } + }); + contrib.setClassLoader(new ContributionClassLoader(contrib, contextClassLoader)); + return contrib; + } + + + @Test + public void testClassLoadingFromContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + // Class present in contribution, also in parent. Class is loaded from parent + Class testClassA = contribA.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassA); + Assert.assertSame(this.getClass(), testClassA); + + // Class not present in contribution, but present in parent ClassLoader + Class testClassB = contribB.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassB); + Assert.assertSame(this.getClass(), testClassB); + + // Class present in contribution, but not in parent + Class testClassC = contribC.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassC); + + // Class not present in contribution or in parent + try { + contribA.getClassLoader().loadClass("NonExistent"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + + + } + + @Test + public void testResourceLoadingFromContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + // Resource present in contribution, and in parent + URL resA = contribA.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resA); + + // Resource not present in contribution, but present in parent ClassLoader + URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resB); + + // Resource present in contribution, but not in parent + URL resC = contribC.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNotNull(resC); + + // Load Java class as resource from parent + String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class"; + URL classResA = contribA.getClassLoader().getResource(classResName); + Assert.assertNotNull(classResA); + + // Non-existent resource + URL res = contribA.getClassLoader().getResource("deployables/NonExistent"); + Assert.assertNull(res); + + } + + private static String getPackageName(Class cls) { + String name = cls.getName(); + int index = name.lastIndexOf('.'); + return index == -1 ? "" : name.substring(0, index); + } + + @Test + public void testClassLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + ArrayList exportContribList = new ArrayList(); + exportContribList.add(contribA); + exportContribList.add(contribC); + + JavaImport import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage(getPackageName(getClass())); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage("calculator"); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + + JavaExport export = javaImportExportFactory.createJavaExport(); + export.setPackage(getPackageName(getClass())); + contribA.getExports().add(export); + export = javaImportExportFactory.createJavaExport(); + export.setPackage("calculator"); + contribC.getExports().add(export); + + // Load class from parent, class is also present in imported contribution. Class should + // be loaded from parent + Class testClassB = contribB.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassB); + Assert.assertSame(this.getClass(), testClassB); + + // Load class from parent, class is also present in parent. Class should be loaded + // from parent. + Class testClassA = contribA.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassA); + Assert.assertSame(this.getClass(), testClassA); + + // Imported class should be the same as the one loaded by the exporting contribution + Assert.assertSame(testClassA, testClassB); + + // Load class from imported contribution, class is not present in parent + Class testClassB1 = contribB.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassB1); + + // Imported class should be the same as the one loaded by the exporting contribution + Class testClassC = contribC.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassC); + Assert.assertSame(testClassC, testClassB1); + + + // Try to load class from package which is not explicitly imported - should throw ClassNotFoundException + try { + contribA.getClassLoader().loadClass("calculator.AddService"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + // Try to load non-existent class from imported package - should throw ClassNotFoundException + try { + contribB.getClassLoader().loadClass(getPackageName(getClass()) + ".NonExistentClass"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + } + + @Test + public void testResourceLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + ArrayList exportContribList = new ArrayList(); + exportContribList.add(contribA); + exportContribList.add(contribC); + + JavaImport import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage(getPackageName(getClass())); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + JavaImport import1_ = javaImportExportFactory.createJavaImport(); + import1_.setPackage("calculator"); + import1_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import1_); + + JavaExport export = javaImportExportFactory.createJavaExport(); + export.setPackage(getPackageName(getClass())); + contribA.getExports().add(export); + JavaExport export1 = javaImportExportFactory.createJavaExport(); + export1.setPackage("calculator"); + contribC.getExports().add(export1); + + + // Load resource from parent + URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resB); + + // Load Java class as resource from imported contribution with JavaImport + String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class"; + URL classResB = contribB.getClassLoader().getResource(classResName); + Assert.assertNotNull(classResB); + + // Load Java class as resource from imported contribution with JavaImport + URL classResB1 = contribB.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNotNull(classResB1); + + // Try to load resource not explicitly imported by contribution + URL classResA1 = contribA.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNull(classResA1); + + + } + +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessorTestCase.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessorTestCase.java new file mode 100644 index 0000000000..0ec5bfba39 --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessorTestCase.java @@ -0,0 +1,103 @@ +/* + * 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.contribution.java.impl; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.impl.DefaultMonitorFactoryImpl; +import org.apache.tuscany.sca.monitor.impl.DefaultMonitorImpl; + +/** + * Test JavaExportProcessorTestCase + * + * @version $Rev$ $Date$ + */ +public class JavaExportProcessorTestCase extends TestCase { + + private static final String VALID_XML = + "" + + ""; + + private static final String INVALID_XML = + "" + + ""; + + private XMLInputFactory inputFactory; + private StAXArtifactProcessor staxProcessor; + private Monitor monitor; + + @Override + protected void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + // Create a monitor + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = new DefaultMonitorFactoryImpl(); + if (monitorFactory != null) { + monitor = monitorFactory.createMonitor(); + utilities.addUtility(monitorFactory); + } + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null, null); + } + + /** + * Test loading a valid export element from a contribution metadata stream + * @throws Exception + */ + public void testLoad() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + JavaExport javaExport = (JavaExport)staxProcessor.read(reader); + assertEquals("org.apache.tuscany.sca.contribution.java", javaExport.getPackage()); + } + + /** + * Test loading an INVALID export element from a contribution metadata stream + * @throws Exception + */ + public void testLoadInvalid() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(INVALID_XML)); + /*try { + staxProcessor.read(reader); + fail("readerException should have been thrown"); + } catch (ContributionReadException e) { + assertTrue(true); + }*/ + staxProcessor.read(reader); + Problem problem = ((DefaultMonitorImpl)monitor).getLastLoggedProblem(); + assertNotNull(problem); + assertEquals("AttributePackageMissing", problem.getMessageId()); + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessorTestCase.java b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessorTestCase.java new file mode 100644 index 0000000000..9e43b744cf --- /dev/null +++ b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessorTestCase.java @@ -0,0 +1,105 @@ +/* + * 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.contribution.java.impl; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.impl.DefaultMonitorFactoryImpl; +import org.apache.tuscany.sca.monitor.impl.DefaultMonitorImpl; + +/** + * Test JavaImportProcessorTestCase + * + * @version $Rev$ $Date$ + */ +public class JavaImportProcessorTestCase extends TestCase { + + private static final String VALID_XML = + "" + + ""; + + private static final String INVALID_XML = + "" + + ""; + + private XMLInputFactory inputFactory; + private StAXArtifactProcessor staxProcessor; + private Monitor monitor; + + @Override + protected void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + // Create a monitor + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = new DefaultMonitorFactoryImpl(); + if (monitorFactory != null) { + monitor = monitorFactory.createMonitor(); + utilities.addUtility(monitorFactory); + } + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null, null); + } + + /** + * Test loading a valid import element from a contribution metadata stream + * @throws Exception + */ + public void testLoad() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + JavaImport javaImport = (JavaImport)staxProcessor.read(reader); + + assertEquals("org.apache.tuscany.sca.contribution.java", javaImport.getPackage()); + assertEquals("sca://contributions/001", javaImport.getLocation()); + } + + /** + * Test loading a INVALID import element from a contribution metadata stream + * @throws Exception + */ + public void testLoadInvalid() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(INVALID_XML)); + /*try { + staxProcessor.read(reader); + fail("readerException should have been thrown"); + } catch (ContributionReadException e) { + assertTrue(true); + }*/ + staxProcessor.read(reader); + Problem problem = ((DefaultMonitorImpl)monitor).getLastLoggedProblem(); + assertNotNull(problem); + assertEquals("AttributePackageMissing", problem.getMessageId()); + } +} diff --git a/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/resources/deployables/sample-calculator.jar b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/resources/deployables/sample-calculator.jar new file mode 100644 index 0000000000..0ca3a1b781 Binary files /dev/null and b/sca-java-1.x/tags/1.6-TUSCANY-3909/contribution-java/src/test/resources/deployables/sample-calculator.jar differ -- cgit v1.2.3