diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2011-07-18 10:21:06 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2011-07-18 10:21:06 +0000 |
commit | 62d55709374e7c299f720aa9066f8b3dcb315ac0 (patch) | |
tree | 186c4870c4a3c68f6dda1178a6bd6866e869fa25 /sca-java-2.x/branches/2.0-Beta3/modules/implementation-java | |
parent | ee58acdcb1a148d2da26140f2afd175ead15b09d (diff) |
Create a branch for beta3
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1147804 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
171 files changed, 18511 insertions, 0 deletions
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/LICENSE b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/LICENSE new file mode 100644 index 0000000000..8aa906c321 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/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-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/META-INF/MANIFEST.MF b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..4ba430760a --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/META-INF/MANIFEST.MF @@ -0,0 +1,71 @@ +Manifest-Version: 1.0
+Export-Package: org.apache.tuscany.sca.implementation.java.context;version="2.0.0";
+ uses:="org.apache.tuscany.sca.core.context,
+ org.apache.tuscany.sca.core.factory,
+ org.apache.tuscany.sca.implementation.java,
+ org.apache.tuscany.sca.core.scope,
+ org.apache.tuscany.sca.implementation.java.injection",
+ org.apache.tuscany.sca.implementation.java.injection;version="2.0.0";
+ uses:="org.apache.tuscany.sca.databinding,
+ org.apache.tuscany.sca.databinding.xml,
+ org.apache.tuscany.sca.runtime,
+ org.apache.tuscany.sca.interfacedef.util,
+ org.apache.tuscany.sca.assembly,
+ org.apache.tuscany.sca.context,
+ org.apache.tuscany.sca.databinding.impl,
+ org.apache.tuscany.sca.core.context,
+ org.apache.tuscany.sca.implementation.java.introspect,
+ org.apache.tuscany.sca.implementation.java,
+ org.apache.tuscany.sca.interfacedef,
+ org.w3c.dom,
+ javax.xml.namespace,
+ org.apache.tuscany.sca.invocation,
+ org.apache.tuscany.sca.core.factory,
+ org.apache.tuscany.sca.interfacedef.impl,
+ org.oasisopen.sca,
+ org.apache.tuscany.sca.core.invocation",
+ org.apache.tuscany.sca.implementation.java.invocation;version="2.0.0"
+Private-Package: org.apache.tuscany.sca.implementation.java.invocation
+ ;version="2.0.0",org.apache.tuscany.sca.implementation.java.module;vers
+ ion="2.0.0"
+SCA-Version: 1.1
+Bundle-Name: Apache Tuscany SCA Java Implementation Model
+Bundle-Vendor: The Apache Software Foundation
+Bundle-Version: 2.0.0
+Bundle-ManifestVersion: 2
+Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
+Bundle-Description: Apache Tuscany SCA Java Implementation Model
+Import-Package: javax.xml.namespace,
+ org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.common.xml.dom;version="2.0.0",
+ org.apache.tuscany.sca.context;version="2.0.0",
+ org.apache.tuscany.sca.core;version="2.0.0",
+ org.apache.tuscany.sca.core.context;version="2.0.0",
+ org.apache.tuscany.sca.core.factory;version="2.0.0",
+ org.apache.tuscany.sca.core.invocation;version="2.0.0",
+ org.apache.tuscany.sca.core.scope;version="2.0.0",
+ org.apache.tuscany.sca.databinding;version="2.0.0",
+ org.apache.tuscany.sca.databinding.impl;version="2.0.0",
+ org.apache.tuscany.sca.databinding.xml;version="2.0.0",
+ org.apache.tuscany.sca.implementation.java;version="2.0.0",
+ org.apache.tuscany.sca.implementation.java.context;version="2.0.0",
+ org.apache.tuscany.sca.implementation.java.injection;version="2.0.0",
+ org.apache.tuscany.sca.implementation.java.introspect;version="2.0.0",
+ org.apache.tuscany.sca.implementation.java.invocation;resolution:=optional,
+ org.apache.tuscany.sca.interfacedef;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.impl;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.java;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.java.impl;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.util;version="2.0.0",
+ org.apache.tuscany.sca.invocation;version="2.0.0",
+ org.apache.tuscany.sca.policy;version="2.0.0",
+ org.apache.tuscany.sca.policy.util;version="2.0.0",
+ org.apache.tuscany.sca.provider;version="2.0.0",
+ org.apache.tuscany.sca.runtime;version="2.0.0",
+ org.oasisopen.sca;version="2.0.0",
+ org.oasisopen.sca.annotation;version="2.0.0",
+ org.w3c.dom
+Bundle-SymbolicName: org.apache.tuscany.sca.implementation.java.runtim
+ e
+Bundle-DocURL: http://www.apache.org/
+Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/NOTICE b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/NOTICE new file mode 100644 index 0000000000..d69e595698 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/NOTICE @@ -0,0 +1,6 @@ +${pom.name} +Copyright (c) 2005 - 2011 The Apache Software Foundation + +This product includes software developed by +The Apache Software Foundation (http://www.apache.org/). + diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/pom.xml b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/pom.xml new file mode 100644 index 0000000000..6f5c9cebf0 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/pom.xml @@ -0,0 +1,57 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +--> +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-modules</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <artifactId>tuscany-implementation-java-runtime</artifactId> + <name>Apache Tuscany SCA Implementation Java Runtime</name> + + <dependencies> + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-core-spi</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-implementation-java</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-databinding</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-core</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + </dependencies> + +</project> diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/InstanceFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/InstanceFactory.java new file mode 100644 index 0000000000..e2d99852d8 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/InstanceFactory.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.implementation.java.context; + +import org.apache.tuscany.sca.core.factory.InstanceWrapper; + +/** + * Interface for a factory that returns an injected component instance. + * This is used by a Component implementation to create new instances of + * application implementation objects as determined by the component scope's + * lifecycle. + * <p/> + * The implementation of this interface may be supplied by the user, + * may be generated during deployment, or may be dynamic. + * + * @version $Rev$ $Date$ + * @param <T> Type of the instance generated by the factory. + */ +public interface InstanceFactory<T> { + /** + * Creates a new instance of the component. + * All injected values must be set but any @Init methods must not have been invoked. + * + * @return A wrapper for the created component instance. + */ + InstanceWrapper<T> newInstance(); +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/InstanceFactoryProvider.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/InstanceFactoryProvider.java new file mode 100644 index 0000000000..df21e013f1 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/InstanceFactoryProvider.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.context; + +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; + +/** + * @version $Rev$ $Date$ + */ +public interface InstanceFactoryProvider<T> { + /** + * Return the implementation class. + * + * @return the implementation class. + */ + Class<T> getImplementationClass(); + + /** + * Sets an object factory for an injection site + * + * @param element the injection site name + * @param objectFactory the object factory + */ + void setObjectFactory(JavaElementImpl element, ObjectFactory<?> objectFactory); + + /** + * Create an instance factory that can be used to create component instances. + * + * @return a new instance factory + */ + InstanceFactory<T> createFactory(); +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceFactory.java new file mode 100644 index 0000000000..f5d9860130 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceFactory.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.implementation.java.context; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.sca.core.factory.InstanceWrapper; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.implementation.java.injection.Injector; +import org.apache.tuscany.sca.implementation.java.invocation.EventInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class ReflectiveInstanceFactory<T> implements InstanceFactory<T> { + private final Constructor<T> ctr; + private final ObjectFactory<?>[] ctrArgs; + private final Injector<T>[] injectors; + private final EventInvoker<T> initInvoker; + private final EventInvoker<T> destroyInvoker; + private final Injector<T>[] callbackInjectors; + + public ReflectiveInstanceFactory(Constructor<T> ctr, + ObjectFactory<?>[] ctrArgs, + Injector<T>[] injectors, + Injector<T>[] callbackInjectors, + EventInvoker<T> initInvoker, + EventInvoker<T> destroyInvoker) { + this.ctr = ctr; + this.ctrArgs = ctrArgs; + this.injectors = injectors; + this.callbackInjectors = callbackInjectors; + this.initInvoker = initInvoker; + this.destroyInvoker = destroyInvoker; + } + + public InstanceWrapper<T> newInstance() { + T instance; + try { + if (ctrArgs != null) { + Object[] args = new Object[ctrArgs.length]; + for (int i = 0; i < args.length; i++) { + args[i] = ctrArgs[i].getInstance(); + } + instance = ctr.newInstance(args); + } else { + instance = ctr.newInstance(); + } + } catch (InstantiationException e) { + String name = ctr.getDeclaringClass().getName(); + throw new AssertionError("Class is not instantiable [" + name + "]"); + } catch (IllegalAccessException e) { + String name = ctr.getName(); + throw new AssertionError("Constructor is not accessible [" + name + "]"); + } catch ( + InvocationTargetException e) { + String name = ctr.getName(); + throw new ObjectCreationException("Exception thrown by constructor: " + name, e); + } + + if (injectors != null) { + for (Injector<T> injector : injectors) { + //FIXME Injectors should never be null + if (injector != null) + try { + injector.inject(instance); + } catch (Exception e) { + if (destroyInvoker != null) { + destroyInvoker.invokeEvent(instance); + } + throw new ObjectCreationException("Exception invoking injector - " + e.getMessage(), e); + } + } + } + + return new ReflectiveInstanceWrapper<T>(instance, initInvoker, destroyInvoker, callbackInjectors); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceWrapper.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceWrapper.java new file mode 100644 index 0000000000..a3ed16dde5 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceWrapper.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.context; + +import org.apache.tuscany.sca.core.factory.InstanceWrapper; +import org.apache.tuscany.sca.core.scope.TargetDestructionException; +import org.apache.tuscany.sca.core.scope.TargetInitializationException; +import org.apache.tuscany.sca.implementation.java.injection.Injector; +import org.apache.tuscany.sca.implementation.java.invocation.EventInvoker; + +/** + * @version $Rev$ $Date$ + */ +public class ReflectiveInstanceWrapper<T> implements InstanceWrapper<T> { + private final EventInvoker<T> initInvoker; + private final EventInvoker<T> destroyInvoker; + private final T instance; + private final Injector<T>[] callbackInjectors; + + public ReflectiveInstanceWrapper(T instance, EventInvoker<T> initInvoker, EventInvoker<T> destroyInvoker, Injector<T>[] callbackInjectors) { + this.instance = instance; + this.initInvoker = initInvoker; + this.destroyInvoker = destroyInvoker; + this.callbackInjectors = callbackInjectors; + } + + public T getInstance() { + return instance; + } + + public void start() throws TargetInitializationException { + if (initInvoker != null) { + try { + initInvoker.invokeEvent(instance); + } catch (Exception e) { + try { + stop(); + } catch (TargetDestructionException e1) { + throw new TargetInitializationException("TargetDestructionException while handling init exception", e); + } + throw new TargetInitializationException(e); + } + } + } + + public void stop() throws TargetDestructionException { + if (destroyInvoker != null) { + destroyInvoker.invokeEvent(instance); + } + } + + public Injector<T>[] getCallbackInjectors() { + return this.callbackInjectors; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/databinding/PropertyDataTypeProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/databinding/PropertyDataTypeProcessor.java new file mode 100644 index 0000000000..16de5bbefb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/databinding/PropertyDataTypeProcessor.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.databinding; + +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.util.JavaXMLMapper; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +/** + * + */ +public class PropertyDataTypeProcessor extends BaseJavaClassVisitor { + private Mediator mediator; + + /** + * @param registry + */ + public PropertyDataTypeProcessor(ExtensionPointRegistry registry) { + super(registry); + UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.mediator = utilityExtensionPoint.getUtility(Mediator.class); + } + + /** + * Introspect the property + * @param javaElement + * @return + */ + private void introspect(Property property, JavaElementImpl javaElement) { + // XMLType xmlType = new XMLType(property.getXSDElement(), property.getXSDType()); + // property.getDataType().setLogical(xmlType); + mediator.getDataBindings().introspectType(property.getDataType(), null); + } + + @Override + public <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + for (Property property : type.getProperties()) { + String name = property.getName(); + JavaElementImpl element = type.getPropertyMembers().get(name); + introspect(property, element); + DataType dt = property.getDataType(); + if (dt.getLogical() instanceof XMLType) { + XMLType xmlType = (XMLType)dt.getLogical(); + property.setXSDType(xmlType.getTypeName()); + property.setXSDElement(xmlType.getElementName()); + } else { + Class<?> baseType = JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); + property.setXSDType(JavaXMLMapper.getXMLType(baseType)); + } + } + super.visitEnd(clazz, type); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ArrayMultiplicityObjectFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ArrayMultiplicityObjectFactory.java new file mode 100644 index 0000000000..2513c63e38 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ArrayMultiplicityObjectFactory.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.lang.reflect.Array; +import java.util.List; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * Resolves targets configured in a multiplicity by delegating to object factories and returning an <code>Array</code> + * containing object instances + * + * @version $Rev$ $Date$ + */ +public class ArrayMultiplicityObjectFactory implements ObjectFactory<Object> { + + private ObjectFactory[] factories; + + private Class interfaceType; + + public ArrayMultiplicityObjectFactory(Class interfaceType, List<ObjectFactory<?>> factories) { + assert interfaceType != null : "Interface type was null"; + assert factories != null : "Object factories were null"; + this.interfaceType = interfaceType; + this.factories = factories.toArray(new ObjectFactory[factories.size()]); + } + + public Object getInstance() throws ObjectCreationException { + Object array = Array.newInstance(interfaceType, factories.length); + for (int i = 0; i < factories.length; i++) { + Array.set(array, i, factories[i].getInstance()); + } + return array; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ContextInjector.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ContextInjector.java new file mode 100644 index 0000000000..d8eef18c74 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ContextInjector.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; + +/** + * Implementations inject a pre-configured context type (interface) on an instance. + * + * @version $Rev$ $Date$ + */ +public interface ContextInjector<S, T> extends Injector<T> { + + void setContext(S context) throws ObjectCreationException; + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjector.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjector.java new file mode 100644 index 0000000000..4d97e72bd0 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjector.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.lang.reflect.Field; +import java.lang.reflect.Type; +import java.security.AccessController; +import java.security.PrivilegedAction; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * Injects a value created by an {@link org.apache.tuscany.sca.core.factory.ObjectFactory} on a given field + * + * @version $Rev$ $Date$ + */ +public class FieldInjector<T> implements Injector<T> { + + private final Field field; + + private final ObjectFactory<?> objectFactory; + + /** + * Create an injector and have it use the given <code>ObjectFactory</code> to inject a value on the instance using + * the reflected <code>Field</code> + */ + public FieldInjector(Field pField, ObjectFactory<?> objectFactory) { + field = pField; + // Allow privileged access to set accessibility. Requires ReflectPermission + // in security policy. + AccessController.doPrivileged(new PrivilegedAction<Object>() { + public Object run() { + field.setAccessible(true); // ignore Java accessibility + return null; + } + }); + + this.objectFactory = objectFactory; + } + + /** + * Inject a new value on the given instance + */ + public void inject(T instance) throws ObjectCreationException { + inject(instance, objectFactory.getInstance()); + } + + public void inject(T instance, Object value) { + try { + field.set(instance, value); + } catch (IllegalAccessException e) { + throw new ObjectCreationException("Field is not accessible [" + field + "]", e); + } + } + + public Class<?> getType() { + return field.getType(); + } + + public Type getGenericType() { + return field.getGenericType(); + } + + public void injectNull(T instance) throws ObjectCreationException { + inject(instance, null); + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/InjectionRuntimeException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/InjectionRuntimeException.java new file mode 100644 index 0000000000..8bc2dce76c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/InjectionRuntimeException.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.implementation.java.injection; + + +/** + * Root unchecked exception for the injection package + * + * @version $Rev$ $Date$ + * @tuscany.spi.extension.inheritfrom + */ +public abstract class InjectionRuntimeException extends RuntimeException { + private static final long serialVersionUID = -2264137603099898773L; + + public InjectionRuntimeException() { + super(); + } + + public InjectionRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public InjectionRuntimeException(String message) { + super(message); + } + + public InjectionRuntimeException(Throwable cause) { + super(cause); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/Injector.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/Injector.java new file mode 100644 index 0000000000..f0208c4e71 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/Injector.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.lang.reflect.Type; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; + +/** + * Implementations inject a pre-configured value on an instance + * + * @version $Rev$ $Date$ + */ +public interface Injector<T> { + + /** + * Inject a value on the given instance + */ + void inject(T instance) throws ObjectCreationException; + void injectNull(T instance) throws ObjectCreationException; + Class<?> getType(); + Type getGenericType(); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/InvalidAccessorException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/InvalidAccessorException.java new file mode 100644 index 0000000000..5aeebcca36 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/InvalidAccessorException.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.implementation.java.injection; + +/** + * @version $Rev$ $Date$ + */ +public class InvalidAccessorException extends InjectionRuntimeException { + private static final long serialVersionUID = 9196299279363310978L; + + public InvalidAccessorException() { + super(); + } + + public InvalidAccessorException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidAccessorException(String message) { + super(message); + } + + public InvalidAccessorException(Throwable cause) { + super(cause); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java new file mode 100644 index 0000000000..acacd23d8f --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java @@ -0,0 +1,371 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import org.apache.tuscany.sca.assembly.ComponentProperty; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.context.PropertyValueFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.databinding.SimpleTypeMapper; +import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; +import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.util.TypeInfo; +import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * + * @version $Rev$ $Date$ + */ +public class JavaPropertyValueObjectFactory implements PropertyValueFactory { + private Mediator mediator = null; + private SimpleTypeMapper simpleTypeMapper; + private boolean isSimpleType; + + public JavaPropertyValueObjectFactory(ExtensionPointRegistry registry) { + UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.mediator = utilityExtensionPoint.getUtility(Mediator.class); + this.simpleTypeMapper = utilityExtensionPoint.getUtility(SimpleTypeMapper.class); + } + + public JavaPropertyValueObjectFactory(Mediator mediator) { + this.mediator = mediator; + } + + public ObjectFactory createValueFactory(Property property, Object propertyValue, JavaElementImpl javaElement) { + Document doc = (Document)propertyValue; + List<Node> nodes = getValues(doc); + Class<?> javaType = JavaIntrospectionHelper.getBaseType(javaElement.getType(), javaElement.getGenericType()); + if (property.isMany()) { + if (javaElement.getType().isArray()) { + return new ArrayObjectFactoryImpl(property, nodes, javaType); + } else { + return new ListObjectFactoryImpl(property, nodes, javaType); + } + } else { + Object value = null; + if (!nodes.isEmpty()) { + value = nodes.get(0); + } + return new ObjectFactoryImpl(property, value, javaType); + + } + } + + public ObjectFactory createValueFactory(Property property, Object propertyValue, Class<?> javaType) { + Document doc = (Document)propertyValue; + List<Node> nodes = getValues(doc); + if (property.isMany()) { + return new ListObjectFactoryImpl(property, nodes, javaType); + } else { + Object value = null; + if (!nodes.isEmpty()) { + value = nodes.get(0); + } + return new ObjectFactoryImpl(property, value, javaType); + } + } + + public <B> B createPropertyValue(ComponentProperty property, Class<B> type) { + + validateTypes(property, type); + + ObjectFactory<B> factory = this.createValueFactory(property, property.getValue(), type); + return factory.getInstance(); + } + + private <B> void validateTypes(ComponentProperty property, Class<B> type) { + // JAXB seems to do some strange things with conversions, so + // we can't rely on the databinding conversion from Node->Java to catch + // incompatible types. + + DataType prop1 = property.getProperty().getDataType(); + + if ( (prop1 != null) && (type.isAssignableFrom(prop1.getPhysical())) ) { + return; + } else if ( simpleTypeMapper.getXMLType(type) != null ) { + if ( simpleTypeMapper.getXMLType(type).getQName().equals(property.getXSDType())) + return; + } else if ( isSimpleType(property) ) { + if ( type.isAssignableFrom(simpleTypeMapper.getJavaType(property.getXSDType()))) + return; + } + + throw new IllegalArgumentException("Property type " + prop1.getPhysical().getName() + " is not compatible with " + type.getName()); + + } + + abstract class ObjectFactoryImplBase implements ObjectFactory { + protected SimpleTypeMapper simpleTypeMapper = new SimpleTypeMapperImpl(); + protected Property property; + protected Object propertyValue; + protected Class<?> javaType; + protected DataType<XMLType> sourceDataType; + protected DataType<?> targetDataType; + + public ObjectFactoryImplBase(Property property, Object propertyValue, Class<?> javaType) { + this.property = property; + this.propertyValue = propertyValue; + this.javaType = javaType; + sourceDataType = + new DataTypeImpl<XMLType>(DOMDataBinding.NAME, Node.class, + new XMLType(null, this.property.getXSDType())); + + targetDataType = property.getDataType(); + if (targetDataType == null) { + TypeInfo typeInfo = null; + if (this.property.getXSDType() != null) { + if (simpleTypeMapper.isSimpleXSDType(this.property.getXSDType())) { + typeInfo = new TypeInfo(property.getXSDType(), true, null); + } else { + typeInfo = new TypeInfo(property.getXSDType(), false, null); + } + } else { + typeInfo = new TypeInfo(property.getXSDType(), false, null); + } + + XMLType xmlType = new XMLType(typeInfo); + String dataBinding = null; // (String)property.getExtensions().get(DataBinding.class.getName()); + if (dataBinding != null) { + targetDataType = new DataTypeImpl<XMLType>(dataBinding, javaType, xmlType); + } else { + targetDataType = new DataTypeImpl<XMLType>(dataBinding, javaType, xmlType); + mediator.getDataBindings().introspectType(targetDataType, null); + } + } + } + } + + class ObjectFactoryImpl extends ObjectFactoryImplBase { + private Object result = null; + + public ObjectFactoryImpl(Property property, Object propertyValue, Class<?> javaType) { + super(property, propertyValue, javaType); + } + + public Object getInstance() throws ObjectCreationException { + // TUSCANY-3791 + if (result != null){ + return result; + } + + if (isSimpleType) { + try { + result = simpleTypeMapper.toJavaObject(property.getXSDType(), (String)propertyValue, null); + } catch (NumberFormatException ex) { + throw new ObjectCreationException("Failed to create instance for property " + property.getName() + + " with value " + + propertyValue, ex); + } catch (IllegalArgumentException ex) { + throw new ObjectCreationException("Failed to create instance for property " + property.getName() + + " with value " + + propertyValue, ex); + } + } else { + result = mediator.mediate(propertyValue, sourceDataType, targetDataType, null); + } + return result; + } + } + + class ListObjectFactoryImpl extends ObjectFactoryImplBase { + private List result = null; + + public ListObjectFactoryImpl(Property property, List<?> propertyValues, Class<?> javaType) { + super(property, propertyValues, javaType); + } + + @SuppressWarnings("unchecked") + public List<?> getInstance() throws ObjectCreationException { + // TUSCANY-3791 + if (result != null){ + return result; + } + + if (isSimpleType) { + List<Object> values = new ArrayList<Object>(); + for (String aValue : (List<String>)propertyValue) { + try { + values.add(simpleTypeMapper.toJavaObject(property.getXSDType(), aValue, null)); + } catch (NumberFormatException ex) { + throw new ObjectCreationException("Failed to create instance for property " + property + .getName() + + " with value " + + aValue + + " from value list of " + + propertyValue, ex); + } catch (IllegalArgumentException ex) { + throw new ObjectCreationException("Failed to create instance for property " + property + .getName() + + " with value " + + aValue + + " from value list of " + + propertyValue, ex); + } + } + result = values; + } else { + List instances = new ArrayList(); + for (Node aValue : (List<Node>)propertyValue) { + instances.add(mediator.mediate(aValue, sourceDataType, targetDataType, null)); + } + result = instances; + } + + return result; + } + } + + class ArrayObjectFactoryImpl extends ObjectFactoryImplBase { + private Object result = null; + + public ArrayObjectFactoryImpl(Property property, List<?> propertyValues, Class<?> javaType) { + super(property, propertyValues, javaType); + } + + @SuppressWarnings("unchecked") + public Object getInstance() throws ObjectCreationException { + // TUSCANY-3791 + if (result != null){ + return result; + } + + if (isSimpleType) { + int count = 0; + Object values = Array.newInstance(javaType, ((List<Object>)propertyValue).size()); + for (String aValue : (List<String>)propertyValue) { + try { + Array.set(values, count++, simpleTypeMapper.toJavaObject(property.getXSDType(), aValue, null)); + } catch (NumberFormatException ex) { + throw new ObjectCreationException("Failed to create instance for property " + property + .getName() + + " with value " + + aValue + + " from value list of " + + propertyValue, ex); + } catch (IllegalArgumentException ex) { + throw new ObjectCreationException("Failed to create instance for property " + property + .getName() + + " with value " + + aValue + + " from value list of " + + propertyValue, ex); + } + } + result = values; + } else { + Object instances = Array.newInstance(javaType, ((List<Object>)propertyValue).size()); + int count = 0; + for (Node aValue : (List<Node>)propertyValue) { + Array.set(instances, count++, mediator.mediate(aValue, sourceDataType, targetDataType, null)); + } + result = instances; + } + + return result; + } + } + + /** + * Utility methods + */ + + /** + * + * @param property + * @return + */ + private boolean isSimpleType(Property property) { + if (property.getXSDType() != null) { + return simpleTypeMapper.isSimpleXSDType(property.getXSDType()); + } else { + if (property instanceof Document) { + Document doc = (Document)property; + Element element = doc.getDocumentElement(); + if (element.getChildNodes().getLength() == 1 && element.getChildNodes().item(0).getNodeType() == Element.TEXT_NODE) { + return true; + } + } + } + return false; + } + + /** + * Retrieve list of simple property values + * @param concatenatedValue + * @param javaType + * @return + */ + private static List<String> getSimplePropertyValues(String concatenatedValue, Class<?> javaType) { + List<String> propValues = new ArrayList<String>(); + StringTokenizer st = null; + if (javaType.getName().equals("java.lang.String")) { + st = new StringTokenizer(concatenatedValue, "\""); + } else { + st = new StringTokenizer(concatenatedValue); + } + String aToken = null; + while (st.hasMoreTokens()) { + aToken = st.nextToken(); + if (aToken.trim().length() > 0) { + propValues.add(aToken); + } + } + return propValues; + } + + /** + * Retrieve the list of complex property values + * @param document + * @return + */ + private static List<Node> getValues(Document document) { + List<Node> propValues = new ArrayList<Node>(); + if ( document == null ) + return propValues; + // The root is the property element + Element rootElement = document.getDocumentElement(); + + NodeList nodes = rootElement.getChildNodes(); + for (int count = 0; count < nodes.getLength(); ++count) { + Node node = nodes.item(count); + if (node.getNodeType() == Document.ELEMENT_NODE) { + propValues.add(DOMHelper.promote(node)); + } + } + return propValues; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java new file mode 100644 index 0000000000..a0af933c71 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ListMultiplicityObjectFactory.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * Resolves targets configured in a multiplicity by delegating to object factories and returning an <code>List</code> + * containing object instances + * + * @version $Rev$ $Date$ + */ +public class ListMultiplicityObjectFactory implements ObjectFactory<Collection<?>> { + + private Collection<ObjectFactory<?>> factories; + private Class<?> collectionType; + + public ListMultiplicityObjectFactory(List<ObjectFactory<?>> factories, Class<?> collectionType) { + assert factories != null : "Object factories were null"; + this.factories = factories; + this.collectionType = collectionType; + } + + public Collection<?> getInstance() throws ObjectCreationException { + Collection<Object> collection = null; + if (SortedSet.class.isAssignableFrom(collectionType)) { + collection = new TreeSet<Object>(); + } else if (Set.class.isAssignableFrom(collectionType)) { + collection = new HashSet<Object>(); + } else if (List.class.isAssignableFrom(collectionType)) { + collection = new ArrayList<Object>(); + } else { + collection = new ArrayList<Object>(); + } + for (ObjectFactory<?> factory : factories) { + collection.add(factory.getInstance()); + } + return collection; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjector.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjector.java new file mode 100644 index 0000000000..cd43a60ab6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjector.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Type; +import java.security.AccessController; +import java.security.PrivilegedAction; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * Injects a value created by an {@link org.apache.tuscany.sca.core.factory.ObjectFactory} using a given method + * + * @version $Rev$ $Date$ + */ +public class MethodInjector<T> implements Injector<T> { + private final Method method; + private final ObjectFactory<?> objectFactory; + + public MethodInjector(Method aMethod, ObjectFactory<?> objectFactory) { + assert aMethod != null; + assert objectFactory != null; + this.method = aMethod; + // Allow privileged access to set accessibility. Requires ReflectPermission in security + // policy. + AccessController.doPrivileged(new PrivilegedAction<Object>() { + public Object run() { + method.setAccessible(true); + return null; + } + }); + this.objectFactory = objectFactory; + } + + public void inject(T instance) throws ObjectCreationException { + inject(instance, objectFactory.getInstance()); + } + + private void inject(T instance, Object value) { + try { + method.invoke(instance, value); + } catch (IllegalAccessException e) { + throw new ObjectCreationException("Method is not accessible [" + method + "]", e); + } catch (IllegalArgumentException e) { + throw new ObjectCreationException("Exception thrown by setter: " + method.getName(), e); + } catch (InvocationTargetException e) { + throw new ObjectCreationException("Exception thrown by setter: " + method.getName(), e); + } + } + + public Class<?> getType() { + return method.getParameterTypes()[0]; + } + + public Type getGenericType() { + return method.getGenericParameterTypes()[0]; + } + + public void injectNull(T instance) throws ObjectCreationException { + inject(instance, null); + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/RequestContextObjectFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/RequestContextObjectFactory.java new file mode 100644 index 0000000000..43fb5d6170 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/RequestContextObjectFactory.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.implementation.java.injection; + +import org.apache.tuscany.sca.context.RequestContextFactory; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.oasisopen.sca.RequestContext; + +/** + * Creates instances of RequestContext for injection on component implementation instances + * + * @version $Rev$ $Date$ + */ +public class RequestContextObjectFactory implements ObjectFactory<RequestContext> { + private RequestContextFactory factory; + private RuntimeComponent component; + + public RequestContextObjectFactory(RequestContextFactory factory, RuntimeComponent component) { + this.factory = factory; + this.component = component; + } + + public RequestContext getInstance() throws ObjectCreationException { + return factory.createRequestContext(component); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceHost.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceHost.java new file mode 100644 index 0000000000..70c368a0e6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceHost.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +/** + * Interface implemented by host environments that allow for resolution of component implementation resources, e.g. + * items bound in a JNDI tree. + * + * @version $Rev$ $Date$ + */ +public interface ResourceHost { + + /** + * Resolve a resource matching the given type + * + * @param type the type of the resources + * @throws ResourceResolutionException if an error is encountered during resolution + */ + <T> T resolveResource(Class<T> type) throws ResourceResolutionException; + + /** + * Resolve a resource matching the given type and name + * + * @param type the type of the resources + * @param mappedName the mapped name of the resource + * @throws ResourceResolutionException if an error is encountered during resolution + */ + <T> T resolveResource(Class<T> type, String mappedName) throws ResourceResolutionException; + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceNotFoundException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceNotFoundException.java new file mode 100644 index 0000000000..d5c1c7b69e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceNotFoundException.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.implementation.java.injection; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; + +/** + * Denotes an exception thrown when a runtime resource is not found + * + * @version $Rev$ $Date$ + */ +public class ResourceNotFoundException extends ObjectCreationException { + private static final long serialVersionUID = 1L; + + public ResourceNotFoundException() { + super(); + } + + public ResourceNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public ResourceNotFoundException(String message) { + super(message); + } + + public ResourceNotFoundException(Throwable cause) { + super(cause); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceObjectFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceObjectFactory.java new file mode 100644 index 0000000000..8f55eb18bc --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceObjectFactory.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * Resolves a runtime resource to be injected on a field or method of a Java component type marked with {@link + * javax.annotation.Resource}. If the mapped name of the resource is an absolute URI such as + * <code>sca://localhost</code> or <code>jndi://localhost</code> the host container namespace is searched; otherwise the + * URI is assumed to be relative and the parent composite is searched. If a mapped name is not provided, i.e. resolution + * is by type, the parent composite is first searched followed by the host namespace. + * + * @version $Rev$ $Date$ + */ +public class ResourceObjectFactory<T> implements ObjectFactory<T> { + + private Class<T> type; + private String mappedName; + private ResourceHost host; + private boolean optional; + + /** + * Instantiates a factory that resolves resources by type + * + * @param type the type of the resource to inject + * @param optional true if an error should be thrown if the resource is not found + * @param host the runtime resource provider + */ + public ResourceObjectFactory(Class<T> type, boolean optional, ResourceHost host) { + this(type, null, optional, host); + } + + /** + * Instantiates a factory that resolves resources by mapped name + * + * @param type the type of the resource to inject + * @param mappedName the resource name + * @param optional true if an error should be thrown if the resource is not found + * @param host the runtime resource provider + */ + public ResourceObjectFactory(Class<T> type, String mappedName, boolean optional, ResourceHost host) { + this.type = type; + this.host = host; + this.mappedName = mappedName; + this.optional = optional; + } + + public T getInstance() throws ObjectCreationException { + try { + T resource; + if (mappedName == null) { + resource = host.resolveResource(type); + if (!optional && resource == null) { + throw new ResourceNotFoundException("Resource not found: " + type.getName()); + } + } else { + resource = host.resolveResource(type, mappedName); + if (!optional && resource == null) { + throw new ResourceNotFoundException("Resource not found: " + mappedName); + } + } + return resource; + } catch (ResourceResolutionException e) { + throw new ObjectCreationException(e); + } + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceResolutionException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceResolutionException.java new file mode 100644 index 0000000000..f94b24762a --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/ResourceResolutionException.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.implementation.java.injection; + + +/** + * @version $Rev$ $Date$ + */ +public class ResourceResolutionException extends Exception { + private static final long serialVersionUID = 13421352711315479L; + + public ResourceResolutionException() { + super(); + } + + public ResourceResolutionException(String message, Throwable cause) { + super(message, cause); + } + + public ResourceResolutionException(String message) { + super(message); + } + + public ResourceResolutionException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/EventInvocationException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/EventInvocationException.java new file mode 100644 index 0000000000..25c09231c5 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/EventInvocationException.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.implementation.java.invocation; + +import org.apache.tuscany.sca.implementation.java.injection.InjectionRuntimeException; + +/** + * Denotes an error when invoking an event on an object + * + * @version $Rev$ $Date$ + */ +public class EventInvocationException extends InjectionRuntimeException { + private static final long serialVersionUID = 1480018831708211581L; + + public EventInvocationException() { + super(); + } + + public EventInvocationException(String message, Throwable cause) { + super(message, cause); + } + + public EventInvocationException(String message) { + super(message); + } + + public EventInvocationException(Throwable cause) { + super(cause); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/EventInvoker.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/EventInvoker.java new file mode 100644 index 0000000000..e6ebb5abc3 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/EventInvoker.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +/** + * Performs an invocation on an instance + * + * @version $Rev$ $Date$ + */ +public interface EventInvoker<T> { + + /** + * Performs the invocation on a given instance + * + * @throws EventInvocationException + */ + void invokeEvent(T instance) throws EventInvocationException; +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/InvalidConversationSequenceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/InvalidConversationSequenceException.java new file mode 100644 index 0000000000..1c330ea913 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/InvalidConversationSequenceException.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + + +/** + * Denotes an unknown operation sequence in a conversation + * + * @version $Rev$ $Date$ + */ +public class InvalidConversationSequenceException extends Exception { + private static final long serialVersionUID = -5744028391493899147L; + + public InvalidConversationSequenceException() { + super(); + } + + public InvalidConversationSequenceException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidConversationSequenceException(String message) { + super(message); + } + + public InvalidConversationSequenceException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaAsyncImplementationInvoker.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaAsyncImplementationInvoker.java new file mode 100644 index 0000000000..259e3a66ad --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaAsyncImplementationInvoker.java @@ -0,0 +1,233 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.core.factory.InstanceWrapper; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.invocation.AsyncResponseException; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaOperation; +import org.apache.tuscany.sca.invocation.InterceptorAsync; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.InvokerAsyncResponse; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.oasisopen.sca.ResponseDispatch; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * Responsible for asynchronously dispatching an invocation to a Java component + * implementation instance + * + */ +public class JavaAsyncImplementationInvoker extends JavaImplementationInvoker implements InterceptorAsync { + + public JavaAsyncImplementationInvoker(Operation operation, Method method, RuntimeComponent component, + InterfaceContract interfaceContract) { + super( operation, method, component, interfaceContract); + assert method != null : "Operation method cannot be null"; + assert ((JavaOperation) operation).isAsyncServer() : "Operation must be async"; + } // end constructor + + public Message invoke(Message msg) { + Operation op = this.operation; + + Object payload = msg.getBody(); + + // Save the current thread context classloader + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + + try { + // The following call might create a new conversation, as a result, the msg.getConversationID() might + // return a new value + InstanceWrapper wrapper = scopeContainer.getWrapper(null); + + Object instance = wrapper.getInstance(); + + // Set the TCCL to the classloader used to load the implementation class + Thread.currentThread().setContextClassLoader(instance.getClass().getClassLoader()); + + // For an async server method, there is an extra input parameter, which is a DispatchResponse instance + // which is typed by the type of the response + Class<?> responseType = op.getOutputType().getPhysical(); + ResponseDispatch<?> dispatch = ResponseDispatchImpl.newInstance(responseType, msg ); + + Object ret; + Object[] payload2; + if (payload != null && !payload.getClass().isArray()) { + payload2 = new Object[2]; + payload2[0] = payload; + } else { + payload2 = new Object[ ((Object[])payload).length + 1 ]; + for( int i = 0; i < ((Object[])payload).length; i++) { + payload2[i] = ((Object[])payload)[i]; + } // end for + } + payload2[ payload2.length - 1 ] = dispatch; + + ret = method.invoke(instance, (Object[])payload2); + + throw new InvocationTargetException( new AsyncResponseException("AsyncResponse") ); + + } catch (InvocationTargetException e) { + Throwable cause = e.getTargetException(); + boolean isChecked = false; + for (DataType<?> d : operation.getFaultTypes()) { + if (d.getPhysical().isInstance(cause)) { + isChecked = true; + msg.setFaultBody(cause); + break; + } + } // end for + + if (!isChecked) { + if (cause instanceof RuntimeException) { + throw (RuntimeException)cause; + } + if (cause instanceof Error) { + throw (Error)cause; + } else { + throw new ServiceRuntimeException(cause.getMessage(), cause); + } + } // end if + + } catch (ObjectCreationException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } catch (Exception e) { + msg.setFaultBody(e); + } finally { + // set the tccl + Thread.currentThread().setContextClassLoader(tccl); + } + return msg; + } // end method invoke + + protected Invoker next; + protected InvokerAsyncResponse previous; + + public void setNext(Invoker next) { + this.next = next; + } + + public Invoker getNext() { + return next; + } + + public void invokeAsyncRequest(Message msg) throws Throwable { + processRequest(msg); + } // end method invokeAsyncRequest + + public void invokeAsyncResponse(Message msg) { + msg = processResponse(msg); + InvokerAsyncResponse thePrevious = (InvokerAsyncResponse)getPrevious(); + if (thePrevious != null ) thePrevious.invokeAsyncResponse(msg); + } // end method invokeAsyncResponse + + public void setPrevious(InvokerAsyncResponse previous) { + this.previous = previous; + } + + public InvokerAsyncResponse getPrevious() { + return previous; + } + + public Message processRequest(Message msg) { + Operation op = this.operation; + Object payload = msg.getBody(); + + // Replace TCCL with the class loader used to load the java class as per SCA Spec + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + + try { + InstanceWrapper wrapper = scopeContainer.getWrapper(null); + Object instance = wrapper.getInstance(); + + // Set the TCCL to the classloader used to load the implementation class + Thread.currentThread().setContextClassLoader(instance.getClass().getClassLoader()); + + // For an async server method, there is an extra input parameter, which is a DispatchResponse instance + // which is typed by the type of the response + Class<?> responseType = op.getOutputType().getPhysical(); + ResponseDispatch<?> dispatch = ResponseDispatchImpl.newInstance(responseType, msg ); + + Object[] payload2; + if (payload != null && !payload.getClass().isArray()) { + payload2 = new Object[2]; + payload2[0] = payload; + } else { + payload2 = new Object[ ((Object[])payload).length + 1 ]; + for( int i = 0; i < ((Object[])payload).length; i++) { + payload2[i] = ((Object[])payload)[i]; + } // end for + } + payload2[ payload2.length - 1 ] = dispatch; + + method.invoke(instance, (Object[])payload2); + + } catch (InvocationTargetException e) { + Throwable cause = e.getTargetException(); + boolean isChecked = false; + for (DataType<?> d : operation.getFaultTypes()) { + if (d.getPhysical().isInstance(cause)) { + isChecked = true; + // Ignore these errors since they should be returned asynchronously + break; + } + } // end for + + if (!isChecked) { + if (cause instanceof RuntimeException) { + throw (RuntimeException)cause; + } // end if + if (cause instanceof Error) { + throw (Error)cause; + } else { + throw new ServiceRuntimeException(cause.getMessage(), cause); + } // end if + } // end if + + } catch (Exception e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } finally { + // set the tccl + Thread.currentThread().setContextClassLoader(tccl); + } + return msg; + } // end method processRequest + + public Message postProcessRequest(Message msg) { + return msg; + } + + public Message postProcessRequest(Message msg, Throwable e) + throws Throwable { + throw e; + } + + public Message processResponse(Message msg) { + return msg; + } // end method processResponse + +} // end class JavaAsyncImplementationInvoker diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaAsyncResponseInvokerImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaAsyncResponseInvokerImpl.java new file mode 100644 index 0000000000..0bddda6965 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaAsyncResponseInvokerImpl.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.tuscany.sca.core.invocation.AsyncFaultWrapper; +import org.apache.tuscany.sca.core.invocation.AsyncResponseHandler; +import org.apache.tuscany.sca.core.invocation.JDKAsyncResponseInvoker; +import org.apache.tuscany.sca.invocation.Message; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * Class which handles the asynchronous response message from an async service back to the client Java component + * + * This class provides a registration function which permits the reference invoking code to register the Future + * which is used to return the response to the Java component code + */ +public class JavaAsyncResponseInvokerImpl implements JDKAsyncResponseInvoker { + + // Map used to link between async requests and async responses + private ConcurrentMap<String, Object> asyncMessageMap; + + public JavaAsyncResponseInvokerImpl() { + + asyncMessageMap = new ConcurrentHashMap<String, Object>(); + } // end constructor + + /** + * Deal with the asynchronous response message + * @param msg - the response message + * + * The response message must contain a RELATES_TO id, which is used to identify the Future that represents + * the operation yet to complete. The Future is then completed with the content of the message. + * The Future either calls back to the application code, or it is expected that the application is polling + * the Future for its completion. + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void invokeAsyncResponse(Message msg) { + // Obtain the Message ID for this message + String relatesID = getMessageRelatesID( msg ); + if( relatesID == null ) + throw new ServiceRuntimeException("JavaAsyncResponseInvoker - response message has no RELATES_TO id"); + + // Look up the response object & remove it from the Map + Object responseHandler = asyncMessageMap.remove(relatesID); + + if( responseHandler == null ) + throw new ServiceRuntimeException("JavaAsyncResponseInvoker - no Future matches the RELATES_TO id: " + relatesID); + + // Invoke the response handler with the content of the message + // - in the case of a Java implementation, the response handler is a Future... + AsyncResponseHandler future = (AsyncResponseHandler) responseHandler; + + Object payload = msg.getBody(); + Object response; + if( payload == null ) { + System.out.println("Returned response message was null"); + } else { + if (payload.getClass().isArray()) { + response = ((Object[])payload)[0]; + } else { + response = payload; + } // end if + if( response.getClass().equals(AsyncFaultWrapper.class)) { + future.setWrappedFault((AsyncFaultWrapper) response ); + } else if ( response instanceof Throwable ) { + future.setFault( (Throwable)response ); + } else { + future.setResponse(response); + } // end if + } // end if + + } // end method invokeAsyncResponse + + /** + * Registers an Async response, which provides an ID which identifies a given response + * and an object which can handle the response + * @param id - the ID + * @param responseHandler - the response handler object + */ + public void registerAsyncResponse( String id, Object responseHandler ) { + // Add the ID/response handler mapping into the table + if( id != null && responseHandler != null ) asyncMessageMap.put(id, responseHandler); + } // end method registerAsyncResponse + + /** + * Extracts the RELATES_TO header from the message + * @param msg - the Tuscany message + * @return - the value of the RELATES_TO header as a String + */ + private String getMessageRelatesID( Message msg ) { + return (String)msg.getHeaders().get("RELATES_TO"); + } // end method getMessageRelatesID + +} // end class JavaAsyncResponseInvoker diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaCallbackRuntimeWireProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaCallbackRuntimeWireProcessor.java new file mode 100644 index 0000000000..6cd16202ba --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaCallbackRuntimeWireProcessor.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.invocation.CallbackInterfaceInterceptor; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; +import org.apache.tuscany.sca.runtime.RuntimeWireProcessor; + +/** + * @version $Rev$ $Date$ + */ +public class JavaCallbackRuntimeWireProcessor implements RuntimeWireProcessor { + private static final Logger logger = Logger.getLogger(JavaCallbackRuntimeWireProcessor.class.getName()); + private InterfaceContractMapper interfaceContractMapper; + private JavaInterfaceFactory javaInterfaceFactory; + + public JavaCallbackRuntimeWireProcessor(ExtensionPointRegistry registry) { + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); + + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + } + + /** + * @param interfaceContractMapper + * @param javaInterfaceFactory + */ + protected JavaCallbackRuntimeWireProcessor(InterfaceContractMapper interfaceContractMapper, + JavaInterfaceFactory javaInterfaceFactory) { + super(); + this.interfaceContractMapper = interfaceContractMapper; + this.javaInterfaceFactory = javaInterfaceFactory; + } + + + private boolean supportsCallbackInterface(Interface iface, JavaImplementation impl) { + if (iface instanceof JavaInterface) { + Class<?> ifaceClass = ((JavaInterface)iface).getJavaClass(); + if (ifaceClass.isAssignableFrom(impl.getJavaClass())) { + return true; + } + } + try { + Interface implType = javaInterfaceFactory.createJavaInterface(impl.getJavaClass()); + // Ignore the remotable/conversational testing + implType.setRemotable(iface.isRemotable()); + return interfaceContractMapper.isCompatibleSubset(iface, implType); + } catch (InvalidInterfaceException e) { + logger.log(Level.WARNING, e.getMessage(), e); + return false; + } + } + + public void process(RuntimeEndpoint endpoint) { + // No operation + } + + public void process(RuntimeEndpointReference endpointReference) { + if(!(endpointReference instanceof RuntimeEndpointReference)) { + return; + } + RuntimeEndpointReference epr = (RuntimeEndpointReference) endpointReference; + Contract contract = epr.getReference(); + if (!(contract instanceof RuntimeComponentReference)) { + return; + } + RuntimeComponent component = (RuntimeComponent) epr.getComponent(); + if (component == null) { + return; + } + Implementation implementation = component.getImplementation(); + if (!(implementation instanceof JavaImplementation)) { + return; + } + JavaImplementation javaImpl = (JavaImplementation)implementation; + Endpoint callbackEndpoint = epr.getCallbackEndpoint(); + if (callbackEndpoint != null) { + Interface iface = callbackEndpoint.getService().getInterfaceContract().getInterface(); + if (!supportsCallbackInterface(iface, javaImpl)) { + // callback to this impl is not possible, so ensure a callback object is set + for (InvocationChain chain : epr.getInvocationChains()) { + chain.addInterceptor(Phase.REFERENCE, new CallbackInterfaceInterceptor()); + } + } + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextFactory.java new file mode 100644 index 0000000000..f2a0d576db --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextFactory.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.oasisopen.sca.ComponentContext; + +/** + * @version $Rev$ $Date$ + */ +public class JavaComponentContextFactory implements ObjectFactory<ComponentContext> { + private final JavaComponentContextProvider component; + + + public JavaComponentContextFactory(JavaComponentContextProvider component) { + this.component = component; + } + + + public ComponentContext getInstance() throws ObjectCreationException { + return component.getComponent().getComponentContext(); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java new file mode 100644 index 0000000000..1fa51315c3 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentContextProvider.java @@ -0,0 +1,399 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.lang.annotation.ElementType; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.ComponentProperty; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.context.ComponentContextFactory; +import org.apache.tuscany.sca.context.PropertyValueFactory; +import org.apache.tuscany.sca.context.RequestContextFactory; +import org.apache.tuscany.sca.core.factory.InstanceWrapper; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.core.invocation.CallableReferenceObjectFactory; +import org.apache.tuscany.sca.core.invocation.CallbackReferenceObjectFactory; +import org.apache.tuscany.sca.core.invocation.CallbackWireObjectFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.invocation.WireObjectFactory; +import org.apache.tuscany.sca.core.scope.ScopeContainer; +import org.apache.tuscany.sca.core.scope.TargetResolutionException; +import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.apache.tuscany.sca.implementation.java.JavaScopeImpl; +import org.apache.tuscany.sca.implementation.java.context.InstanceFactory; +import org.apache.tuscany.sca.implementation.java.injection.JavaPropertyValueObjectFactory; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaOperation; +import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; +import org.oasisopen.sca.ServiceReference; + +/** + * The runtime instantiation of Java component implementations + * + * @version $Rev$ $Date$ + */ +public class JavaComponentContextProvider { + private JavaPropertyValueObjectFactory propertyValueFactory; + private RuntimeComponent component; + private JavaInstanceFactoryProvider<?> instanceFactoryProvider; + private ProxyFactory proxyFactory; + private InstanceFactory instanceFactory; + private JavaScopeImpl scope; + + public JavaComponentContextProvider(RuntimeComponent component, + JavaInstanceFactoryProvider configuration, + DataBindingExtensionPoint dataBindingExtensionPoint, + PropertyValueFactory propertyValueObjectFactory, + ComponentContextFactory componentContextFactory, + RequestContextFactory requestContextFactory) { + super(); + this.instanceFactoryProvider = configuration; + this.proxyFactory = configuration.getProxyFactory(); + // if (componentContextFactory != null) { + // this.componentContext = componentContextFactory.createComponentContext(component, requestContextFactory); + // } else { + // this.componentContext = new ComponentContextImpl(this, requestContextFactory, this.proxyService); + // } + this.component = component; + this.propertyValueFactory = (JavaPropertyValueObjectFactory) propertyValueObjectFactory; + this.scope = ((JavaImplementation)component.getImplementation()).getJavaScope(); + } + + InstanceWrapper<?> createInstanceWrapper() throws ObjectCreationException { + if (instanceFactory == null) { + start(); + } + return instanceFactory.newInstance(); + } + + void configureProperties(List<ComponentProperty> definedProperties) { + for (ComponentProperty p : definedProperties) { + configureProperty(p); + } + } + + private void configureProperty(ComponentProperty configuredProperty) { + JavaElementImpl element = + instanceFactoryProvider.getImplementation().getPropertyMembers().get(configuredProperty.getName()); + + if (element != null && configuredProperty.getValue() != null) { + if (!(element.getAnchor() instanceof Constructor)) { + if(element.getElementType() == ElementType.FIELD) { + // Field field = (Field)element.getAnchor(); + instanceFactoryProvider.getInjectionSites().add(element); + /* + if(Modifier.isPublic(field.getModifiers())) { + instanceFactoryProvider.getInjectionSites().add(element); + } else if(field.getAnnotation(org.oasisopen.sca.annotation.Property.class) != null) { + instanceFactoryProvider.getInjectionSites().add(element); + } + */ + } else { + instanceFactoryProvider.getInjectionSites().add(element); + } + } + + //Class propertyJavaType = JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); + ObjectFactory<?> propertyObjectFactory = + createPropertyValueFactory(configuredProperty, configuredProperty.getValue(), element); + instanceFactoryProvider.setObjectFactory(element, propertyObjectFactory); + + JavaConstructorImpl<?> constructor = instanceFactoryProvider.getImplementation().getConstructor(); + for(JavaElementImpl p: constructor.getParameters()){ + if(element.getName().equals(p.getName())) { + instanceFactoryProvider.setObjectFactory(p, propertyObjectFactory); + } + } + } + } + + void start() { + List<JavaElementImpl> callbackInjectionList = null; + + // If the component implementation is stateless, we need to inject the callbacks on service invocation + // rather than doing it once at the component level. + if ( scope.equals(JavaScopeImpl.STATELESS)) { + callbackInjectionList = instanceFactoryProvider.getCallbackInjectionSites(); + } else { + callbackInjectionList = instanceFactoryProvider.getInjectionSites(); + } + + if (!instanceFactoryProvider.getImplementation().getCallbackMembers().isEmpty()) { + Map<String, List<EndpointReference>> callbackWires = new HashMap<String, List<EndpointReference>>(); + for (ComponentService service : component.getServices()) { + + RuntimeComponentReference callbackReference = (RuntimeComponentReference)service.getCallbackReference(); + if (callbackReference != null) { + List<EndpointReference> wires = callbackReference.getEndpointReferences(); + if (!wires.isEmpty()) { + RuntimeEndpointReference epr = (RuntimeEndpointReference) wires.get(0); + callbackWires.put(epr.getComponentTypeReferenceInterfaceContract().getInterface().toString(), + wires); + } + } + } + + for (Map.Entry<String, Collection<JavaElementImpl>> entry : instanceFactoryProvider.getImplementation() + .getCallbackMembers().entrySet()) { + List<EndpointReference> wires = callbackWires.get(entry.getKey()); + if (wires == null) { + // this can happen when there are no client wires to a + // component that has a callback + continue; + } + for(JavaElementImpl element : entry.getValue()) { + Class<?> businessInterface = element.getType(); + ObjectFactory<?> factory = null; + if (ServiceReference.class.isAssignableFrom(element.getType())) { + businessInterface = + JavaIntrospectionHelper.getBusinessInterface(element.getType(), element.getGenericType()); + factory = + new CallbackReferenceObjectFactory(businessInterface, proxyFactory, wires); + } else { + factory = new CallbackWireObjectFactory(businessInterface, proxyFactory, wires); + } + if (!(element.getAnchor() instanceof Constructor)) { + callbackInjectionList.add(element); + } + instanceFactoryProvider.setObjectFactory(element, factory); + } + } + } + for (Reference ref : instanceFactoryProvider.getImplementation().getReferences()) { + JavaElementImpl element = + instanceFactoryProvider.getImplementation().getReferenceMembers().get(ref.getName()); + if (element != null) { + if (!(element.getAnchor() instanceof Constructor)) { + if(element.getElementType() == ElementType.FIELD) { + Field field = (Field)element.getAnchor(); + if(Modifier.isPublic(field.getModifiers())) { + instanceFactoryProvider.getInjectionSites().add(element); + } else if(field.getAnnotation(org.oasisopen.sca.annotation.Reference.class) != null) { + instanceFactoryProvider.getInjectionSites().add(element); + } + } else { + instanceFactoryProvider.getInjectionSites().add(element); + } + } + ComponentReference componentReference = null; + List<EndpointReference> wireList = null; + for (ComponentReference reference : component.getReferences()) { + if (reference.getName().equals(ref.getName())) { + wireList = ((RuntimeComponentReference)reference).getEndpointReferences(); + componentReference = reference; + break; + } + } + if (ref.getMultiplicity() == Multiplicity.ONE_N || ref.getMultiplicity() == Multiplicity.ZERO_N) { + List<ObjectFactory<?>> factories = new ArrayList<ObjectFactory<?>>(); + Class<?> baseType = + JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); + for (int i = 0; i < wireList.size(); i++) { + ObjectFactory<?> factory = null; + if (ServiceReference.class.isAssignableFrom(baseType)) { + Type callableRefType = JavaIntrospectionHelper.getParameterType(element.getGenericType()); + // Type businessType = JavaIntrospectionHelper.getParameterType(callableRefType); + Class<?> businessInterface = + JavaIntrospectionHelper.getBusinessInterface(baseType, callableRefType); + factory = new CallableReferenceObjectFactory(businessInterface, (RuntimeEndpointReference) wireList.get(i)); + } else { + factory = createObjectFactory(baseType, wireList.get(i)); + } + factories.add(factory); + } + instanceFactoryProvider.setObjectFactories(element, factories); + JavaConstructorImpl<?> constructor = instanceFactoryProvider.getImplementation().getConstructor(); + for(JavaElementImpl p: constructor.getParameters()){ + if(element.getName().equals(p.getName())) { + instanceFactoryProvider.setObjectFactories(p, factories); + } + } + } else { + if (wireList == null && ref.getMultiplicity() == Multiplicity.ONE_ONE) { + throw new IllegalStateException("Required reference is missing: " + ref.getName()); + } + if (wireList != null && !wireList.isEmpty()) { + ObjectFactory<?> factory = null; + if (ServiceReference.class.isAssignableFrom(element.getType())) { + Class<?> businessInterface = + JavaIntrospectionHelper.getBusinessInterface(element.getType(), element + .getGenericType()); + factory = + new CallableReferenceObjectFactory(businessInterface, (RuntimeEndpointReference) wireList.get(0)); + } else { + factory = createObjectFactory(element.getType(), wireList.get(0)); + } + instanceFactoryProvider.setObjectFactory(element, factory); + JavaConstructorImpl<?> constructor = instanceFactoryProvider.getImplementation().getConstructor(); + for(JavaElementImpl p: constructor.getParameters()){ + if(element.getName().equals(p.getName())) { + instanceFactoryProvider.setObjectFactory(p, factory); + } + } + } + } + } + } + + //setUpPolicyHandlers(); + this.instanceFactory = instanceFactoryProvider.createFactory(); + + } + + void addResourceFactory(String name, ObjectFactory<?> factory) { + JavaResourceImpl resource = instanceFactoryProvider.getImplementation().getResources().get(name); + + if (resource != null && !(resource.getElement().getAnchor() instanceof Constructor)) { + instanceFactoryProvider.getInjectionSites().add(resource.getElement()); + } + + instanceFactoryProvider.setObjectFactory(resource.getElement(), factory); + } + + Object createInstance() throws ObjectCreationException { + return createInstanceWrapper().getInstance(); + } + + JavaInstanceFactoryProvider<?> getInstanceFactoryProvider() { + return instanceFactoryProvider; + } + + void stop() { + //cleanUpPolicyHandlers(); + } + + Invoker createInvoker(Operation operation, InterfaceContract interfaceContract) throws NoSuchMethodException { + Class<?> implClass = instanceFactoryProvider.getImplementationClass(); + + Method method = JavaInterfaceUtil.findMethod(implClass, operation); + if (operation instanceof JavaOperation && + ((JavaOperation) operation).isAsyncServer() ) { + return new JavaAsyncImplementationInvoker(operation, method, component, interfaceContract); + } else { + return new JavaImplementationInvoker(operation, method, component, interfaceContract); + } // end if + } // end + + private static class OptimizedObjectFactory<T> implements ObjectFactory<T> { + private ScopeContainer scopeContainer; + + public OptimizedObjectFactory(ScopeContainer scopeContainer) { + super(); + this.scopeContainer = scopeContainer; + } + + public T getInstance() throws ObjectCreationException { + try { + return (T)scopeContainer.getWrapper(null).getInstance(); + } catch (TargetResolutionException e) { + throw new ObjectCreationException(e); + } + } + + } + + private <B> ObjectFactory<B> createObjectFactory(Class<B> interfaze, EndpointReference wire) { + // FIXME: [rfeng] Disable the optimization for new as it needs more discussions + /* + boolean conversational = wire.getSource().getInterfaceContract().getInterface().isConversational(); + Binding binding = wire.getSource().getBinding(); + // Check if it's wireable binding for optimization + if (!conversational && binding instanceof OptimizableBinding) { + OptimizableBinding optimizableBinding = (OptimizableBinding)binding; + Component component = optimizableBinding.getTargetComponent(); + if (component != null) { + Implementation implementation = component.getImplementation(); + // Check if the target component is java component + if (implementation instanceof JavaImplementation) { + JavaImplementation javaImplementation = (JavaImplementation)implementation; + if (interfaze.isAssignableFrom(javaImplementation.getJavaClass())) { + ScopedRuntimeComponent scopedComponent = (ScopedRuntimeComponent)component; + ScopeContainer scopeContainer = scopedComponent.getScopeContainer(); + Scope scope = scopeContainer.getScope(); + if (scope == Scope.COMPOSITE || scope == Scope.STATELESS || scope == Scope.SYSTEM) { + boolean optimizable = true; + for (InvocationChain chain : wire.getInvocationChains()) { + if (chain.getHeadInvoker() != chain.getTailInvoker()) { + optimizable = false; + break; + } + } + if (optimizable) { + return new OptimizedObjectFactory<B>(scopeContainer); + } + } + } + } + } + } + */ + return new WireObjectFactory<B>(interfaze, (RuntimeEndpointReference) wire, proxyFactory); + } + + private ObjectFactory<?> createPropertyValueFactory(ComponentProperty property, + Object propertyValue, + JavaElementImpl javaElement) { + return propertyValueFactory.createValueFactory(property, propertyValue, javaElement); + } + + /** + * @return the component + */ + RuntimeComponent getComponent() { + return component; + } + + /*private void setUpPolicyHandlers() { + for (PolicyHandler policyHandler : policyHandlers.values()) { + policyHandler.setUp(component.getImplementation()); + } + } + + private void cleanUpPolicyHandlers() { + for (PolicyHandler policyHandler : policyHandlers.values() ) { + policyHandler.cleanUp(this); + } + }*/ + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentNameFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentNameFactory.java new file mode 100644 index 0000000000..4a5ad98e5b --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaComponentNameFactory.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * @version $Rev$ $Date$ + */ +public class JavaComponentNameFactory implements ObjectFactory<String> { + private final JavaComponentContextProvider componentContextProvider; + + + public JavaComponentNameFactory(JavaComponentContextProvider component) { + this.componentContextProvider = component; + } + + + public String getInstance() throws ObjectCreationException { + String uri = componentContextProvider.getComponent().getURI(); + return uri.substring(uri.lastIndexOf('/')+1); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java new file mode 100644 index 0000000000..0b97192cc6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationInvoker.java @@ -0,0 +1,283 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.ws.Holder; + +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.scope.Scope; +import org.apache.tuscany.sca.core.scope.ScopeContainer; +import org.apache.tuscany.sca.core.scope.ScopedRuntimeComponent; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.context.ReflectiveInstanceWrapper; +import org.apache.tuscany.sca.implementation.java.injection.Injector; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.ParameterMode; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; +import org.apache.tuscany.sca.invocation.DataExchangeSemantics; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * Responsible for synchronously dispatching an invocation to a Java component + * implementation instance + * + * @version $Rev$ $Date$ + */ +public class JavaImplementationInvoker implements Invoker, DataExchangeSemantics { + protected Operation operation; + protected Method method; + protected boolean allowsPBR; + + @SuppressWarnings("unchecked") + protected final ScopeContainer scopeContainer; + private final InterfaceContract interfaze; + + public JavaImplementationInvoker(Operation operation, Method method, RuntimeComponent component, InterfaceContract intf) { + assert method != null : "Operation method cannot be null"; + this.method = method; + this.operation = operation; + this.scopeContainer = ((ScopedRuntimeComponent)component).getScopeContainer(); + this.allowsPBR = ((JavaImplementation)component.getImplementation()).isAllowsPassByReference(method); + this.interfaze = intf; + } + + public JavaImplementationInvoker(Operation operation, RuntimeComponent component, InterfaceContract intf) { + // used if the method can't be computed statically in advance + this.operation = operation; + this.scopeContainer = ((ScopedRuntimeComponent)component).getScopeContainer(); + this.interfaze = intf; + } + + @SuppressWarnings("unchecked") + public Message invoke(Message msg) { + + Operation op = msg.getOperation(); + if (op == null) { + op = this.operation; + } + Object payload = msg.getBody(); + + Object contextId = null; + + EndpointReference from = msg.getFrom(); + + // store the current thread context classloader + // as we need to replace it with the class loader + // used to load the java class as per SCA Spec + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + + try { + // The following call might create a new conversation, as a result, the msg.getConversationID() might + // return a new value + ReflectiveInstanceWrapper wrapper = (ReflectiveInstanceWrapper) scopeContainer.getWrapper(contextId); + + // If there is a callback interface and the implementation is stateless, we need to + // inject callbacks at invocation time. For Composite scope, this has already been done. + if (( interfaze.getCallbackInterface() != null ) && (scopeContainer.getScope().equals(Scope.STATELESS))){ + injectCallbacks(wrapper, (JavaInterface)interfaze.getCallbackInterface()); + } + + Object instance = wrapper.getInstance(); + + // If the method couldn't be computed statically, or the instance being + // invoked is a user-specified callback object that doesn't implement + // the service interface from which the reflective method was obtained, + // compute the method object dynamically for this invocation. + Method imethod = method; + if (imethod == null || !imethod.getDeclaringClass().isInstance(instance)) { + try { + imethod = JavaInterfaceUtil.findMethod(instance.getClass(), operation); + } catch (NoSuchMethodException e) { + throw new IllegalArgumentException("Callback object does not provide method " + e.getMessage()); + } + } + + // Set the thread context classloader of the thread used to invoke an operation + // of a Java POJO component implementation is the class loader of the contribution + // that contains the POJO implementation class. + + Thread.currentThread().setContextClassLoader(instance.getClass().getClassLoader()); + + int argumentHolderCount = 0; + + // Holder pattern. Any payload parameters <T> which are should be in holders are placed in Holder<T>. + // Only check Holder for remotable interfaces + if (imethod != null && op.getInterface().isRemotable()) { + Object[] payloadArray = (Object[])payload; + List<Object> payloadList = new ArrayList<Object>(); + int nextIndex = 0; + for (ParameterMode mode : op.getParameterModes()) { + if (mode.equals(ParameterMode.IN)) { + payloadList.add(payloadArray[nextIndex++]); + } else if (mode.equals(ParameterMode.INOUT)) { + // Promote array params from [<T>] to [Holder<T>] + Object item = payloadArray[nextIndex++]; + Holder itemHolder = new Holder(item); + payloadList.add(itemHolder); + argumentHolderCount++; + } else { + // Create an empty Holder since we should not pass values for OUT parameters + payloadList.add(new Holder()); + argumentHolderCount++; + } + } + + // Maybe a bit odd to do but this way I don't have to worry about how the invoke if/else + // immediately following might need to be changed. + payload = payloadList.toArray(); + } + + Object ret; + if (payload != null && !payload.getClass().isArray()) { + ret = imethod.invoke(instance, payload); + } else { + ret = imethod.invoke(instance, (Object[])payload); + } + + scopeContainer.returnWrapper(wrapper, contextId); + + + if (argumentHolderCount > 0) { + + // Holder pattern. Any payload Holder<T> types are returned as part of the message body. + Object[] payloadArray = (Object[])payload; + + ArrayList<Object> holderOutputs = new ArrayList<Object>(); + ArrayList<Object> result = new ArrayList<Object>(); + if (imethod != null) { + + for (int i = 0, size = op.getParameterModes().size(); i < size; i++) { + if (ParameterMode.IN != op.getParameterModes().get(i)) { + // Demote array params from Holder<T> to <T>. + Holder<Object> item = (Holder<Object>)payloadArray[i]; + payloadArray[i] = item.value; + holderOutputs.add(payloadArray[i]); + } + } + + // + // Now we account for the fact that we may have a null because of a void return type, + // which is not part of the output DataType, and so should not be returned with the array + // of outputs, or we may have a null as value returned + // from a method with signature with return type other than void, which should be returned + // in the output array. + // + // The logic here is if we already have as many outputs in holders as we have outputs + // altogether, then we don't worry about the return value (which should be null). Might + // be simpler to just check for void, but the code in the Java introspector has a lot + // of quirks for handling parameterized types, and this seems simpler for now. + // + int holderOutputSize = holderOutputs.size(); + int numberOperationOutputs = op.getOutputType().getLogical().size(); + if (holderOutputSize == numberOperationOutputs) { + if (ret != null) { + throw new IllegalStateException("Number of holder outputs equal to number of operations outputs." + + "\nNum = " + holderOutputSize + ", but non-null return value seen: " + ret); + } + result = holderOutputs; + } else if (holderOutputSize == numberOperationOutputs - 1) { + result.add(ret); + result.addAll(1, holderOutputs); + } else { + throw new IllegalStateException("Number of holder outputs seen: " + holderOutputSize + + "\nNumber of operation outputs: " + numberOperationOutputs); + } + } + + msg.setBody(result.toArray()); + + } else { + msg.setBody(ret); + } + } catch (InvocationTargetException e) { + Throwable cause = e.getTargetException(); + boolean isChecked = false; + for (DataType<?> d : operation.getFaultTypes()) { + if (d.getPhysical().isInstance(cause)) { + isChecked = true; + msg.setFaultBody(cause); + break; + } + } + if (!isChecked) { + if (cause instanceof RuntimeException) { + throw (RuntimeException)cause; + } + if (cause instanceof Error) { + throw (Error)cause; + } else { + throw new ServiceRuntimeException(cause.getMessage(), cause); + } + } + + } catch (ObjectCreationException e) { + throw new ServiceRuntimeException(e.getMessage(), e); + } catch (Exception e) { + msg.setFaultBody(e); + } finally { + // set the tccl + Thread.currentThread().setContextClassLoader(tccl); + } + return msg; + } + + private void injectCallbacks(ReflectiveInstanceWrapper wrapper, + JavaInterface callbackInterface) { + + for (Injector injector : wrapper.getCallbackInjectors()) { + if (injector != null) { + try { + if (ServiceReference.class.isAssignableFrom(injector.getType())) { + Class<?> intf = JavaIntrospectionHelper.getBusinessInterface(injector.getType(), injector.getGenericType()); + if ( intf.isAssignableFrom(callbackInterface.getJavaClass())) { + injector.inject(wrapper.getInstance()); + } + } else if (injector.getType().isAssignableFrom(callbackInterface.getJavaClass())) { + injector.inject(wrapper.getInstance()); + } else { + injector.injectNull(wrapper.getInstance()); + } + } catch (Exception e) { + throw new ObjectCreationException("Exception invoking injector - " + e.getMessage(), e); + } + } + } + + } + + @Override + public boolean allowsPassByReference() { + return allowsPBR; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java new file mode 100644 index 0000000000..215a796cc1 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProvider.java @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.context.ComponentContextFactory; +import org.apache.tuscany.sca.context.PropertyValueFactory; +import org.apache.tuscany.sca.context.RequestContextFactory; +import org.apache.tuscany.sca.core.factory.InstanceWrapper; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.scope.Scope; +import org.apache.tuscany.sca.core.scope.ScopedImplementationProvider; +import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.apache.tuscany.sca.implementation.java.injection.RequestContextObjectFactory; +import org.apache.tuscany.sca.implementation.java.injection.ResourceHost; +import org.apache.tuscany.sca.implementation.java.injection.ResourceObjectFactory; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.InvokerAsyncRequest; +import org.apache.tuscany.sca.invocation.InvokerAsyncResponse; +import org.apache.tuscany.sca.provider.ImplementationAsyncProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.oasisopen.sca.ComponentContext; +import org.oasisopen.sca.RequestContext; + +/** + * @version $Rev$ $Date$ + */ +public class JavaImplementationProvider implements ScopedImplementationProvider, ImplementationAsyncProvider { + private JavaImplementation implementation; + private JavaComponentContextProvider componentContextProvider; + private RequestContextFactory requestContextFactory; + private Scope scope; + + public JavaImplementationProvider(RuntimeComponent component, + JavaImplementation implementation, + ProxyFactory proxyService, + DataBindingExtensionPoint dataBindingRegistry, + PropertyValueFactory propertyValueObjectFactory, + ComponentContextFactory componentContextFactory, + RequestContextFactory requestContextFactory) { + super(); + this.implementation = implementation; + this.requestContextFactory = requestContextFactory; + + try { + JavaInstanceFactoryProvider configuration = new JavaInstanceFactoryProvider(implementation); + configuration.setProxyFactory(proxyService); + componentContextProvider = + new JavaComponentContextProvider(component, + configuration, + dataBindingRegistry, + propertyValueObjectFactory, + componentContextFactory, + requestContextFactory); + + this.scope = new Scope(implementation.getJavaScope().getScope()); + componentContextProvider.configureProperties(component.getProperties()); + handleResources(implementation, proxyService); + } catch (Exception e) { + throw new IllegalStateException(e); + } + + } + + private void handleResources(JavaImplementation componentType, ProxyFactory proxyService) { + for (JavaResourceImpl resource : componentType.getResources().values()) { + String name = resource.getName(); + + ObjectFactory<?> objectFactory = + (ObjectFactory<?>)componentContextProvider.getInstanceFactoryProvider().getFactories().get(resource.getElement()); + if (objectFactory == null) { + Class<?> type = resource.getElement().getType(); + if (ComponentContext.class.equals(type)) { + objectFactory = new JavaComponentContextFactory(componentContextProvider); + } else if (RequestContext.class.equals(type)) { + objectFactory = new RequestContextObjectFactory(requestContextFactory, componentContextProvider.getComponent()); + } else if (String.class.equals(type)) { + objectFactory = new JavaComponentNameFactory(componentContextProvider); + } else { + boolean optional = resource.isOptional(); + String mappedName = resource.getMappedName(); + objectFactory = createResourceObjectFactory(type, mappedName, optional, null); + } + } + componentContextProvider.addResourceFactory(name, objectFactory); + } + } + + private <T> ResourceObjectFactory<T> createResourceObjectFactory(Class<T> type, + String mappedName, + boolean optional, + ResourceHost host) { + return new ResourceObjectFactory<T>(type, mappedName, optional, host); + } + + public Invoker createInvoker(RuntimeComponentService service, Operation operation) { + try { + return componentContextProvider.createInvoker(operation, service.getInterfaceContract()); + } catch (NoSuchMethodException e) { + // It's possible that the instance being invoked is a user-specified + // callback object that isn't an instance of the component implementation + // class. As an attempt to deal with this, look up a method object from + // the service interface. This isn't foolproof, as it's possible that + // the service interface isn't a Java interface, or that the callback + // object has the right method signature without implementing the + // callback interface. There is code in JavaImplementationInvoker + // to deal with these possibilities. + Interface iface = service.getInterfaceContract().getInterface(); + if (iface instanceof JavaInterface) { + try { + Method method = JavaInterfaceUtil.findMethod(((JavaInterface)iface).getJavaClass(), operation); + return new JavaImplementationInvoker(operation, method, componentContextProvider.getComponent(), service.getInterfaceContract()); + } catch (NoSuchMethodException e1) { + throw new IllegalArgumentException(e1); + } + } else { + return new JavaImplementationInvoker(operation, componentContextProvider.getComponent(), service.getInterfaceContract()); + } + } + } + + public boolean supportsOneWayInvocation() { + return false; + } + + public Scope getScope() { + return scope; + } + + public void start() { + componentContextProvider.start(); + } + + public void stop() { + componentContextProvider.stop(); + } + + public InstanceWrapper<?> createInstanceWrapper() { + return componentContextProvider.createInstanceWrapper(); + } + + public boolean isEagerInit() { + return implementation.isEagerInit(); + } + + public InvokerAsyncRequest createAsyncInvoker(RuntimeComponentService service, Operation operation) { + // createInvoker should automatically create a JavaAsyncImplementationInvoker - if not, then it means + // that the service is not async and the result will be an exception caused by the class cast. + return (InvokerAsyncRequest) createInvoker( service, operation ); + } // end method createAsyncInvoker + + public InvokerAsyncResponse createAsyncResponseInvoker(Operation operation) { + return new JavaAsyncResponseInvokerImpl(); + } // end method createAsyncResponseInvoker + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProviderFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProviderFactory.java new file mode 100644 index 0000000000..b59966a39e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaImplementationProviderFactory.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.context.ComponentContextFactory; +import org.apache.tuscany.sca.context.ContextFactoryExtensionPoint; +import org.apache.tuscany.sca.context.PropertyValueFactory; +import org.apache.tuscany.sca.context.RequestContextFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.core.invocation.CglibProxyFactory; +import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactoryExtensionPoint; +import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ImplementationProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * @version $Rev$ $Date$ + */ +public class JavaImplementationProviderFactory implements ImplementationProviderFactory<JavaImplementation> { + private static final Logger logger = Logger.getLogger(JavaImplementationProviderFactory.class.getName()); + private PropertyValueFactory propertyValueFactory; + private DataBindingExtensionPoint databindings; + private ProxyFactory proxyFactory; + private ComponentContextFactory componentContextFactory; + private RequestContextFactory requestContextFactory; + + public JavaImplementationProviderFactory(ExtensionPointRegistry registry) { + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + MessageFactory messageFactory = factories.getFactory(MessageFactory.class); + + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + InterfaceContractMapper interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + + ProxyFactoryExtensionPoint proxyFactories = registry.getExtensionPoint(ProxyFactoryExtensionPoint.class); + try { + proxyFactories.setClassProxyFactory(new CglibProxyFactory(messageFactory, interfaceContractMapper)); + } catch (NoClassDefFoundError e) { + logger.log(Level.WARNING, "Class-based proxy is not supported", e); + } + + databindings = registry.getExtensionPoint(DataBindingExtensionPoint.class); + propertyValueFactory = factories.getFactory(PropertyValueFactory.class); + + ContextFactoryExtensionPoint contextFactories = registry.getExtensionPoint(ContextFactoryExtensionPoint.class); + componentContextFactory = contextFactories.getFactory(ComponentContextFactory.class); + requestContextFactory = contextFactories.getFactory(RequestContextFactory.class); + + proxyFactory = ExtensibleProxyFactory.getInstance(registry); + } + + /* + public JavaImplementationProviderFactory(ProxyFactory proxyService, + DataBindingExtensionPoint dataBindingRegistry, + PropertyValueFactory propertyValueObjectFactory, + ComponentContextFactory componentContextFactory, + RequestContextFactory requestContextFactory) { + super(); + this.proxyFactory = proxyService; + this.databindings = dataBindingRegistry; + this.propertyValueFactory = propertyValueObjectFactory; + this.componentContextFactory = componentContextFactory; + this.requestContextFactory = requestContextFactory; + } + */ + + public ImplementationProvider createImplementationProvider(RuntimeComponent component, + JavaImplementation implementation) { + return new JavaImplementationProvider(component, + implementation, + proxyFactory, + databindings, + propertyValueFactory, + componentContextFactory, + requestContextFactory); + } + + public Class<JavaImplementation> getModelType() { + return JavaImplementation.class; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java new file mode 100644 index 0000000000..e89b618cfe --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/JavaInstanceFactoryProvider.java @@ -0,0 +1,204 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.lang.annotation.ElementType; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.context.InstanceFactory; +import org.apache.tuscany.sca.implementation.java.context.InstanceFactoryProvider; +import org.apache.tuscany.sca.implementation.java.context.ReflectiveInstanceFactory; +import org.apache.tuscany.sca.implementation.java.injection.ArrayMultiplicityObjectFactory; +import org.apache.tuscany.sca.implementation.java.injection.FieldInjector; +import org.apache.tuscany.sca.implementation.java.injection.Injector; +import org.apache.tuscany.sca.implementation.java.injection.InvalidAccessorException; +import org.apache.tuscany.sca.implementation.java.injection.ListMultiplicityObjectFactory; +import org.apache.tuscany.sca.implementation.java.injection.MethodInjector; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; + +/** + * Encapsulates configuration for a Java-based atomic component + * + * @version $Rev$ $Date$ + */ +public class JavaInstanceFactoryProvider<T> implements InstanceFactoryProvider<T> { + private JavaImplementation definition; + private ProxyFactory proxyService; + + private final List<JavaElementImpl> injectionSites; + private final EventInvoker<T> initInvoker; + private final EventInvoker<T> destroyInvoker; + private final Map<JavaElementImpl, Object> factories = new HashMap<JavaElementImpl, Object>(); + private final List<JavaElementImpl> callbackInjectionSites; + + public JavaInstanceFactoryProvider(JavaImplementation definition) { + this.definition = definition; + this.initInvoker = definition.getInitMethod() == null ? null : new MethodEventInvoker<T>(definition + .getInitMethod()); + this.destroyInvoker = definition.getDestroyMethod() == null ? null : new MethodEventInvoker<T>(definition + .getDestroyMethod()); + injectionSites = new ArrayList<JavaElementImpl>(); + callbackInjectionSites = new ArrayList<JavaElementImpl>(); + } + + ProxyFactory getProxyFactory() { + return proxyService; + } + + void setProxyFactory(ProxyFactory proxyService) { + this.proxyService = proxyService; + } + + /** + * @return the definition + */ + JavaImplementation getImplementation() { + return definition; + } + + @SuppressWarnings("unchecked") + public InstanceFactory<T> createFactory() { + ObjectFactory<?>[] initArgs = getConstructorArgs(); + Injector<T>[] injectors = getInjectors(false); + Injector<T>[] callbackInjectors = getInjectors(true); + return new ReflectiveInstanceFactory<T>((Constructor<T>)definition.getConstructor().getConstructor(), + initArgs, injectors, callbackInjectors, initInvoker, destroyInvoker); + } + + + + private ObjectFactory<?>[] getConstructorArgs() { + JavaConstructorImpl<?> constructor = definition.getConstructor(); + ObjectFactory<?>[] initArgs = new ObjectFactory<?>[constructor.getParameters().length]; + for (int i = 0; i < initArgs.length; i++) { + ObjectFactory<?> factory = (ObjectFactory<?>)factories.get(constructor.getParameters()[i]); + assert factory != null; + initArgs[i] = factory; + } + return initArgs; + } + + + + @SuppressWarnings("unchecked") + private Injector<T>[] getInjectors(boolean callback) { + List<JavaElementImpl> sites = null; + if ( callback ) + sites = callbackInjectionSites; + else + sites = injectionSites; + + // work around JDK1.5 issue with allocating generic arrays + Injector<T>[] injectors = new Injector[sites.size()]; + + int i = 0; + for (JavaElementImpl element : sites) { + Object obj = factories.get(element); + if (obj != null) { + if (obj instanceof ObjectFactory) { + ObjectFactory<?> factory = (ObjectFactory<?>)obj; + Member member = (Member)element.getAnchor(); + if (element.getElementType() == ElementType.FIELD) { + injectors[i++] = new FieldInjector<T>((Field)member, factory); + } else if (element.getElementType() == ElementType.PARAMETER && member instanceof Method) { + injectors[i++] = new MethodInjector<T>((Method)member, factory); + } else if (member instanceof Constructor) { + // Ignore + } else { + throw new AssertionError(String.valueOf(element)); + } + } else { + injectors[i++] = createMultiplicityInjector(element, (List<ObjectFactory<?>>)obj); + } + } + } + return injectors; + } + + private Injector<T> createMultiplicityInjector(JavaElementImpl element, List<ObjectFactory<?>> factories) { + Class<?> interfaceType = JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); + + if (element.getAnchor() instanceof Field) { + Field field = (Field)element.getAnchor(); + if (field.getType().isArray()) { + return new FieldInjector<T>(field, new ArrayMultiplicityObjectFactory(interfaceType, factories)); + } else { + return new FieldInjector<T>(field, new ListMultiplicityObjectFactory(factories, field.getType())); + } + } else if (element.getAnchor() instanceof Method) { + Method method = (Method)element.getAnchor(); + if (method.getParameterTypes()[0].isArray()) { + return new MethodInjector<T>(method, new ArrayMultiplicityObjectFactory(interfaceType, factories)); + } else { + return new MethodInjector<T>(method, new ListMultiplicityObjectFactory(factories, method.getParameterTypes()[0])); + } + } else { + throw new InvalidAccessorException("Member must be a field or method: " + element.getName()); + } + } + + @SuppressWarnings("unchecked") + public Class<T> getImplementationClass() { + return (Class<T>)definition.getJavaClass(); + } + + public void setObjectFactory(JavaElementImpl element, ObjectFactory<?> objectFactory) { + factories.put(element, objectFactory); + } + + public void setObjectFactories(JavaElementImpl element, List<ObjectFactory<?>> objectFactory) { + factories.put(element, objectFactory); + } + + /** + * @return the injectionSites + */ + List<JavaElementImpl> getInjectionSites() { + return injectionSites; + } + + /** + * @return the callbackInjectionSites + */ + public List<JavaElementImpl> getCallbackInjectionSites() { + return callbackInjectionSites; + } + + /** + * @return the factories + */ + Map<JavaElementImpl, Object> getFactories() { + return factories; + } + + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/MethodEventInvoker.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/MethodEventInvoker.java new file mode 100644 index 0000000000..515a7c8a43 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/MethodEventInvoker.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + + +/** + * Performs an wire on a method of a given instance + * + * @version $Rev$ $Date$ + */ +public class MethodEventInvoker<T> implements EventInvoker<T> { + private final Method method; + + /** + * Instantiates an invoker for the given method + */ + public MethodEventInvoker(Method method) { + assert method != null; + this.method = method; + } + + public void invokeEvent(T instance) throws EventInvocationException { + try { + method.invoke(instance, (Object[]) null); + } catch (IllegalArgumentException e) { + String name = method.getName(); + throw new EventInvocationException("Exception thrown by event method [" + name + "]", e.getCause()); + } catch (IllegalAccessException e) { + String name = method.getName(); + throw new EventInvocationException("Method is not accessible [" + name + "]"); + } catch (InvocationTargetException e) { + String name = method.getName(); + throw new EventInvocationException("Exception thrown by event method [" + name + "]", e.getCause()); + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/NoConversationalContractException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/NoConversationalContractException.java new file mode 100644 index 0000000000..dbc5c6b640 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/NoConversationalContractException.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.invocation; + + +/** + * Raised when a component has conversational scope but no conversational contract + * + * @version $Rev$ $Date$ + */ +public class NoConversationalContractException extends Exception { + private static final long serialVersionUID = -1157790036638157539L; + + public NoConversationalContractException(String message) { + super(message); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/ResponseDispatchImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/ResponseDispatchImpl.java new file mode 100644 index 0000000000..343682d8b1 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/invocation/ResponseDispatchImpl.java @@ -0,0 +1,251 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.invocation; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.context.CompositeContext; +import org.apache.tuscany.sca.context.ThreadMessageContext; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.core.invocation.AsyncFaultWrapper; +import org.apache.tuscany.sca.core.invocation.AsyncResponseHandler; +import org.apache.tuscany.sca.core.invocation.AsyncResponseInvoker; +import org.apache.tuscany.sca.core.invocation.CallbackReferenceObjectFactory; +import org.apache.tuscany.sca.core.invocation.Constants; +import org.apache.tuscany.sca.core.invocation.ExtensibleProxyFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; +import org.oasisopen.sca.ResponseDispatch; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * Implementation of the ResponseDispatch interface of the OASIS SCA Java API + * + * This is used for invocations of asynchronous services, where it is passed as a parameter on async service operations + * and it provides the path by which the service implementation returns the response to the request, or a Fault + * + * Note that this class is serializable and can be serialized, stored and deserialized by the service implementation + * + * @param <T> - type of the response message + */ +public class ResponseDispatchImpl<T> implements ResponseDispatch<T>, Serializable { + + /** + * Generated serialVersionUID value + */ + private static final long serialVersionUID = 300158355992568592L; + + // A latch used to ensure that the sendResponse() and sendFault() operations are used at most once + // The latch is initialized with the value "false" + private AtomicBoolean latch = new AtomicBoolean(); + + private final Lock lock = new ReentrantLock(); + private final Condition completed = lock.newCondition(); + + // The result + private transient volatile T response = null; + private transient volatile Throwable fault = null; + + private transient ExtensionPointRegistry registry; + private MessageFactory msgFactory; + + // Service Reference used for the callback + private volatile ServiceReference<AsyncResponseHandler<?>> callbackRef; + private AsyncResponseInvoker<?> respInvoker; + private String messageID; + + /** + * No-arg constructor for serialization purposes + */ + public ResponseDispatchImpl() { + super(); + } // end constructor + + public ResponseDispatchImpl( Message msg ) { + super(); + + respInvoker = (AsyncResponseInvoker<?>)msg.getHeaders().get(Constants.ASYNC_RESPONSE_INVOKER); + //if( respInvoker == null ) throw new ServiceRuntimeException("Async Implementation invoked with no response invoker"); + + if( respInvoker == null ) { + callbackRef = getAsyncCallbackRef( msg ); + } // end if + + messageID = (String) msg.getHeaders().get(Constants.MESSAGE_ID); + + } // end constructor + + public static <T> ResponseDispatchImpl<T> newInstance( Class<T> type, Message msg ) { + return new ResponseDispatchImpl<T>( msg ); + } + + /** + * Provide Context data for this ResponseDispatch that the service implementation can use + */ + public Map<String, Object> getContext() { + return null; + } + + /** + * Send a Fault. Must only be invoked once for this ResponseDispatch object + * @param e - the Fault to send + * @throws IllegalStateException if either the sendResponse method or the sendFault method have been called previously + */ + public void sendFault(Throwable e) { + if( sendOK() ) { + lock.lock(); + try { + fault = e; + completed.signalAll(); + } finally { + lock.unlock(); + } // end try + } else { + throw new IllegalStateException("sendResponse() or sendFault() has been called previously"); + } // end if + + // Use response invoker if present + if( respInvoker != null ) { + //respInvoker.invokeAsyncResponse(new AsyncFaultWrapper(e)); + respInvoker.invokeAsyncResponse(e, null); + return; + } // end if + + // Now dispatch the response to the callback... + AsyncResponseHandler<T> handler = (AsyncResponseHandler<T>) callbackRef.getService(); + setResponseHeaders(); + handler.setWrappedFault(new AsyncFaultWrapper(e)); + } // end method sendFault + + /** + * Send the response message. Must only be invoked once for this ResponseDispatch object + * @throws IllegalStateException if either the sendResponse method or the sendFault method have been called previously + * @param res - the response message, which is of type T + */ + @SuppressWarnings("unchecked") + public void sendResponse(T res) { + if( sendOK() ) { + lock.lock(); + try { + response = res; + completed.signalAll(); + } finally { + lock.unlock(); + } // end try + } else { + throw new IllegalStateException("sendResponse() or sendFault() has been called previously"); + } // end if + + // Now dispatch the response to the callback, if present... + if( callbackRef != null ) { + AsyncResponseHandler<T> handler = (AsyncResponseHandler<T>) callbackRef.getService(); + setResponseHeaders(); + handler.setResponse(res); + } // end if + + // Use response invoker if present + if( respInvoker != null ) { + respInvoker.invokeAsyncResponse(res, null); + return; + } // end if + } // end method sendResponse + + public T get(long timeout, TimeUnit unit) throws Throwable { + lock.lock(); + try { + // wait for result to be available + if( response == null && fault == null ) completed.await( timeout, unit); + if( response != null ) return response; + if( fault != null ) throw fault; + } finally { + lock.unlock(); + } // end try + + return null; + } // end method get + + /** + * Indicates that sending a response is OK - this is a transactional + * query in that it also updates the state of this ResponseDispatch, so + * that it will return true once and once only + * @return - true if it is OK to send the response, false otherwise + */ + private boolean sendOK() { + return latch.compareAndSet(false, true); + } + + /** + * Creates a service reference for the async callback, based on information contained in the supplied message + * @param msg - the incoming message + * @return - a CallBackServiceReference + */ + @SuppressWarnings("unchecked") + private ServiceReference<AsyncResponseHandler<?>> getAsyncCallbackRef( Message msg ) { + RuntimeEndpointReference callbackEPR = (RuntimeEndpointReference) msg.getHeaders().get(Constants.ASYNC_CALLBACK); + if( callbackEPR == null ) return null; + + CompositeContext compositeContext = callbackEPR.getCompositeContext(); + registry = compositeContext.getExtensionPointRegistry(); + ProxyFactory proxyFactory = ExtensibleProxyFactory.getInstance(registry); + msgFactory = getMessageFactory(); + List<EndpointReference> eprList = new ArrayList<EndpointReference>(); + eprList.add(callbackEPR); + ObjectFactory<?> factory = new CallbackReferenceObjectFactory(AsyncResponseHandler.class, proxyFactory, eprList); + + return (ServiceReference<AsyncResponseHandler<?>>) factory.getInstance(); + + } // end method getAsyncCallbackEPR + + /** + * Sets the values of various headers in the response message + */ + private void setResponseHeaders() { + // Is there an existing message context? + Message msgContext = ThreadMessageContext.getMessageContext(); + if( msgContext == null ) { + // Create a message context + msgContext = msgFactory.createMessage(); + } // end if + + // Add in the header for the RelatesTo Message ID + msgContext.getHeaders().put(Constants.RELATES_TO, messageID); + + ThreadMessageContext.setMessageContext(msgContext); + } // end method setResponseHeaders + + private MessageFactory getMessageFactory() { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + return modelFactories.getFactory(MessageFactory.class); + } // end method getMessageFactory +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.context.PropertyValueFactory b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.context.PropertyValueFactory new file mode 100644 index 0000000000..9d5f943f46 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.context.PropertyValueFactory @@ -0,0 +1,17 @@ +# 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.implementation.java.injection.JavaPropertyValueObjectFactory
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.definitions.xml.Definitions b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.definitions.xml.Definitions new file mode 100644 index 0000000000..2b67aa59ca --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.definitions.xml.Definitions @@ -0,0 +1,17 @@ +# 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/implementation/java/definitions/definitions.xml
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor new file mode 100644 index 0000000000..e649b68466 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation
+# 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
+# "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.
+#
+# NOTE: The ranking attribute is important for the JavaClassVistors
+# Some visitors need to be called after the others
+org.apache.tuscany.sca.implementation.java.databinding.PropertyDataTypeProcessor;ranking=750
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory new file mode 100644 index 0000000000..1c2626706b --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.ImplementationProviderFactory @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Implementation class for the implementation extension
+org.apache.tuscany.sca.implementation.java.invocation.JavaImplementationProviderFactory;model=org.apache.tuscany.sca.implementation.java.JavaImplementation
+
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.RuntimeWireProcessor b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.RuntimeWireProcessor new file mode 100644 index 0000000000..07008049e8 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.RuntimeWireProcessor @@ -0,0 +1,17 @@ +# 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.implementation.java.invocation.JavaCallbackRuntimeWireProcessor
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/org/apache/tuscany/sca/implementation/java/definitions/definitions.xml b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/org/apache/tuscany/sca/implementation/java/definitions/definitions.xml new file mode 100644 index 0000000000..e790317458 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/main/resources/org/apache/tuscany/sca/implementation/java/definitions/definitions.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +--> + +<!-- $Rev$ $Date$ --> + +<sca:definitions xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912" + targetNamespace="http://docs.oasis-open.org/ns/opencsa/sca/200912" + xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912" + xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"> + + <sca:implementationType type="sca:implementation.java" mayProvide="" alwaysProvides=""/> + + </sca:definitions> diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/context/MultiplicityTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/context/MultiplicityTestCase.java new file mode 100644 index 0000000000..629ba8b321 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/context/MultiplicityTestCase.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.context; + +import org.junit.Test; + + + +/** + * Tests wires that are configured with a multiplicity + * + * @version $Rev$ $Date$ + */ +public class MultiplicityTestCase { + + @Test + public void testMultiplicity() throws Exception { + // TODO implement + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceWrapperTestCase.java.fixme b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceWrapperTestCase.java.fixme new file mode 100644 index 0000000000..628a308c86 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/context/ReflectiveInstanceWrapperTestCase.java.fixme @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.context; + +import static org.easymock.EasyMock.createMock; +import junit.framework.TestCase; + +import org.apache.tuscany.sca.core.scope.TargetDestructionException; +import org.apache.tuscany.sca.core.scope.TargetInitializationException; +import org.apache.tuscany.sca.implementation.java.invocation.EventInvoker; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ReflectiveInstanceWrapperTestCase extends TestCase { + private ReflectiveInstanceWrapper<Object> wrapper; + private Object instance; + private EventInvoker<Object> initInvoker; + private EventInvoker<Object> destroyInvoker; + + public void testWithNoCallbacks() { + wrapper = new ReflectiveInstanceWrapper<Object>(instance, null, null); + try { + wrapper.start(); + } catch (TargetInitializationException e) { + fail(); + } + try { + wrapper.stop(); + } catch (TargetDestructionException e) { + fail(); + } + } + + public void testWithStartCallback() { + initInvoker.invokeEvent(instance); + EasyMock.replay(initInvoker); + wrapper = new ReflectiveInstanceWrapper<Object>(instance, initInvoker, null); + try { + wrapper.start(); + } catch (TargetInitializationException e) { + fail(); + } + EasyMock.verify(initInvoker); + } + + public void testWithStopCallback() { + destroyInvoker.invokeEvent(instance); + EasyMock.replay(destroyInvoker); + wrapper = new ReflectiveInstanceWrapper<Object>(instance, null, destroyInvoker); + try { + wrapper.stop(); + } catch (TargetDestructionException e) { + fail(); + } + EasyMock.verify(destroyInvoker); + } + + @Override + @SuppressWarnings("unchecked") + protected void setUp() throws Exception { + super.setUp(); + instance = new Object(); + initInvoker = createMock(EventInvoker.class); + destroyInvoker = createMock(EventInvoker.class); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/CallbackWireObjectFactoryTestCase.java.fixme b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/CallbackWireObjectFactoryTestCase.java.fixme new file mode 100644 index 0000000000..29dcfa8169 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/CallbackWireObjectFactoryTestCase.java.fixme @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import static org.easymock.EasyMock.createMock; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.core.invocation.CallbackWireObjectFactory; +import org.apache.tuscany.sca.core.invocation.ProxyFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CallbackWireObjectFactoryTestCase extends TestCase { + + @SuppressWarnings({"unchecked"}) + public void testCreateInstance() throws Exception { + ProxyFactory service = createMock(ProxyFactory.class); + Foo foo = new Foo() { + }; + EasyMock.expect(service.createCallbackProxy(EasyMock.eq(Foo.class), EasyMock.isA(List.class))).andReturn(foo); + EasyMock.replay(service); + List<RuntimeWire> wires = new ArrayList<RuntimeWire>(); + CallbackWireObjectFactory factory = new CallbackWireObjectFactory(Foo.class, service, wires); + assertEquals(foo, factory.getInstance()); + EasyMock.verify(service); + } + + private interface Foo { + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjectorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjectorTestCase.java new file mode 100644 index 0000000000..098789f495 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjectorTestCase.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Field; + +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class FieldInjectorTestCase { + + protected Field protectedField; + + @Test + public void testIllegalAccess() throws Exception { + FieldInjector<Foo> injector = new FieldInjector<Foo>(protectedField, new SingletonObjectFactory<String>("foo")); + Foo foo = new Foo(); + injector.inject(foo); + assertEquals("foo", foo.hidden); + } + + + @Before + public void setUp() throws Exception { + protectedField = Foo.class.getDeclaredField("hidden"); + } + + private class Foo { + private String hidden; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactoryTestCase.java.fixme b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactoryTestCase.java.fixme new file mode 100644 index 0000000000..366f21d9f0 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactoryTestCase.java.fixme @@ -0,0 +1,449 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; +import org.apache.tuscany.sca.databinding.DefaultDataBindingExtensionPoint; +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * This test case will test the JavaPropertyValueObjectFactory. + * + * @version $Rev$ $Date$ + */ +public class JavaPropertyValueObjectFactoryTestCase { + + /** + * The factory we should use for testing. + */ + private static JavaPropertyValueObjectFactory factory; + + /** + * The assembly factory used to create Properties. + */ + private static AssemblyFactory assemblyFactory; + + /** + * Test Setup. + */ + @BeforeClass + public static void setup() { + // Create the factory + Mediator mediator = EasyMock.createNiceMock(Mediator.class); + DataBindingExtensionPoint dpep = new DefaultDataBindingExtensionPoint(); + EasyMock.expect(mediator.getDataBindings()).andReturn(dpep).anyTimes(); + EasyMock.replay(mediator); + factory = new JavaPropertyValueObjectFactory(mediator); + + // Create the AssemblyFactory we should use + assemblyFactory = new DefaultAssemblyFactory(); + } + + /** + * A test that will attempt to inject positive, negative and zero into an + * int property. + */ + @Test + public void testIntegerInjectionValid() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.expectedType = Integer.TYPE; + + for (int i = -5; i <= 5; i++) { + params.propertyValue = Integer.toString(i); + params.expectedValueFromFactory = i; + doInjection(params); + } + } + + /** + * A test that will attempt to inject positive, negative and zero into an + * int property using a JavaElement. + */ + @Test + public void testIntegerInjectionValidWithJavaElement() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.expectedJavaElement = new JavaElementImpl(int.class); + + for (int i = -5; i <= 5; i++) { + params.propertyValue = Integer.toString(i); + params.expectedValueFromFactory = i; + doInjection(params); + } + } + + /** + * A test that will attempt to inject multiple int values into an + * int property. + */ + @Test + public void testIntegerArrayInjectionValid() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.isMany = true; + params.expectedType = int.class; + params.propertyValue = "1 2 3 4 5"; + params.expectedValueFromFactory = Arrays.asList(1, 2, 3, 4, 5); + doInjection(params); + } + + /** + * A test that will attempt to inject multiple int values into an + * int property using a JavaElement. + */ + @Test + public void testIntegerArrayInjectionValidWithJavaElement() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.isMany = true; + params.expectedJavaElement = new JavaElementImpl(int[].class); + params.propertyValue = "1 2 3 4 5"; + int[] expected = { 1, 2, 3, 4, 5 }; + params.expectedValueFromFactory = expected; + doInjection(params); + } + + /** + * A test that will attempt to inject a non-number into an + * int property. + */ + @Test + public void testIntegerInjectionBadNumberInvalid() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.expectedType = Integer.TYPE; + params.propertyValue = "a"; + params.exceptionExpected = true; + doInjection(params); + } + + /** + * A test that will attempt to inject a non-number into an + * int property using a JavaElement. + */ + @Test + public void testIntegerInjectionBadNumberInvalidWithJavaElement() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.expectedJavaElement = new JavaElementImpl(Integer.TYPE); + params.propertyValue = "a"; + params.exceptionExpected = true; + doInjection(params); + } + + /** + * A test that will attempt to inject multiple int values into an + * int property where one of the property values is not a number. + * The injection should throw ObjectCreationException + */ + @Test + public void testIntegerArrayInjectionBadNumberInvalid() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.isMany = true; + params.expectedType = int.class; + params.propertyValue = "1 2 aa 4 5"; + params.exceptionExpected = true; + doInjection(params); + } + + /** + * A test that will attempt to inject multiple int values into an + * int property using a JavaElement where one of the property + * values is not a number. + * The injection should throw ObjectCreationException + */ + @Test + public void testIntegerArrayInjectionBadNumberInvalidWithJavaElement() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.isMany = true; + params.expectedJavaElement = new JavaElementImpl(int[].class); + params.propertyValue = "1 2 aa 4 5"; + params.exceptionExpected = true; + doInjection(params); + } + + /** + * A test that will attempt to inject an empty string into an int property. + * The injection should throw ObjectCreationException + */ + @Test + public void testIntegerInjectionEmptyStringInvalid() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.expectedType = Integer.TYPE; + params.propertyValue = ""; + params.exceptionExpected = true; + doInjection(params); + } + + /** + * A test that will attempt to inject an empty string into an int property + * using a JavaElement. + * The injection should throw ObjectCreationException + */ + @Test + public void testIntegerInjectionEmptyStringInvalidWithJavaElement() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "intField"; + params.xsdType = SimpleTypeMapperImpl.XSD_INT; + params.expectedJavaElement = new JavaElementImpl(Integer.TYPE); + params.propertyValue = ""; + params.exceptionExpected = true; + doInjection(params); + } + + /** + * A test that will attempt to inject a String into a String + * property. + */ + @Test + public void testStringInjectionValid() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "StringField"; + params.xsdType = SimpleTypeMapperImpl.XSD_STRING; + params.expectedType = String.class; + + params.propertyValue = "Some Test String"; + params.expectedValueFromFactory = "Some Test String"; + doInjection(params); + } + + /** + * A test that will attempt to inject a String into a String + * property using a JavaElement. + */ + @Test + public void testStringInjectionValidWithJavaElement() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "StringField"; + params.xsdType = SimpleTypeMapperImpl.XSD_STRING; + params.expectedJavaElement = new JavaElementImpl(String.class); + + params.propertyValue = "Some Test String"; + params.expectedValueFromFactory = "Some Test String"; + doInjection(params); + } + + /** + * This class defines all the parameters for the Property Injection test. + */ + private class InjectionTestParams { + // Input parameters for the test + public boolean isMany = false; + public String propertyName; + public String propertyValue; + public QName xsdType; + + // Expected result for test + public Object expectedValueFromFactory; + public Class<?> expectedType; + public JavaElementImpl expectedJavaElement; + public boolean exceptionExpected = false; + } + + /** + * A test that will attempt to inject multiple String values into an + * String property. + */ + @Test + public void testStringArrayInjectionValid() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "StringField"; + params.xsdType = SimpleTypeMapperImpl.XSD_STRING; + params.isMany = true; + params.expectedType = String.class; + params.propertyValue = "\"String1\" \"String2\" \"String3\" \"String4\" \"String5\""; + params.expectedValueFromFactory = Arrays.asList( + "String1", "String2", "String3", "String4", "String5"); + doInjection(params); + } + + /** + * A test that will attempt to inject multiple String values into an + * String property using a JavaElement. + */ + @Test + public void testStringArrayInjectionValidWithJavaElement() { + InjectionTestParams params = new InjectionTestParams(); + params.propertyName = "StringField"; + params.xsdType = SimpleTypeMapperImpl.XSD_STRING; + params.isMany = true; + params.expectedJavaElement = new JavaElementImpl(String.class); + params.propertyValue = "\"String1\" \"String2\" \"String3\" \"String4\" \"String5\""; + params.expectedValueFromFactory = Arrays.asList( + "String1", "String2", "String3", "String4", "String5"); + doInjection(params); + } + + /** + * Utility method for testing creating properties with the + * JavaPropertyValueObjectFactory. + * + * @param testParams The parameters for the test + */ + private void doInjection(final InjectionTestParams testParams) { + // Create the property + Property prop = assemblyFactory.createProperty(); + prop.setMany(testParams.isMany); + prop.setName(testParams.propertyName); + prop.setXSDType(testParams.xsdType); + + // Mock up the XML that will contain the Property details + Document doc = EasyMock.createNiceMock(Document.class); + Element rootElement = EasyMock.createMock(Element.class); + EasyMock.expect(doc.getDocumentElement()).andReturn(rootElement); + NodeList nodeList = EasyMock.createMock(NodeList.class); + EasyMock.expect(rootElement.getChildNodes()).andReturn(nodeList).anyTimes(); + EasyMock.expect(nodeList.getLength()).andReturn(1); + Node node = EasyMock.createMock(Node.class); + EasyMock.expect(nodeList.item(0)).andReturn(node); + EasyMock.expect(node.getTextContent()).andReturn(testParams.propertyValue); + EasyMock.replay(doc, rootElement, nodeList, node); + + // Create a factory either using the Class or JavaElementImpl constructor + ObjectFactory<?> objectFactory; + if (testParams.expectedJavaElement != null) { + objectFactory = factory.createValueFactory(prop, doc, testParams.expectedJavaElement); + } else { + objectFactory = factory.createValueFactory(prop, doc, testParams.expectedType); + } + Assert.assertNotNull(objectFactory); + + // Lets test the factory + try { + // Create a new instance with the factory + Object value = objectFactory.getInstance(); + + // Did we expect an exception to be thrown? + if (testParams.exceptionExpected) { + Assert.fail("Test should have thrown ObjectCreationException"); + } + + // Make sure the result is of the correct type + if (testParams.expectedValueFromFactory instanceof Collection<?>) { + // Make sure the Collections contain the same type + Assert.assertTrue(value instanceof Collection<?>); + Iterator<?> iter1 = ((Collection<?>) testParams.expectedValueFromFactory).iterator(); + Iterator<?> iter2 = ((Collection<?>) value).iterator(); + Assert.assertEquals(iter1.next().getClass(), iter2.next().getClass()); + } else { + Assert.assertEquals(testParams.expectedValueFromFactory.getClass(), value.getClass()); + } + + // Validate the result + Assert.assertNotNull(value); + if (testParams.expectedValueFromFactory.getClass().isArray()) { + Assert.assertTrue(compareArrays(testParams.expectedValueFromFactory, value)); + } else { + Assert.assertEquals(testParams.expectedValueFromFactory, value); + } + } catch (ObjectCreationException ex) { + // Is this an expected exception? + if (testParams.exceptionExpected) { + // Make sure the exception error message contains the property name + Assert.assertTrue(ex.toString().indexOf(testParams.propertyName) != -1); + + // Make sure the exception error message contains the property value + if (testParams.propertyValue != null) { + if (testParams.isMany) { + // FIXME: No simple way to do this for multi-value properties + } else { + Assert.assertTrue(ex.toString().indexOf(testParams.propertyValue) != -1); + } + } + } else { + // Test failure. We were not expecting an exception + ex.printStackTrace(); + Assert.fail("Unexpected exception " + ex); + } + } + } + + /** + * Compares two Objects that are actually arrays to make sure that they are + * equal. + * + * @param array1 The first array + * @param array2 The second array + * @return True if they are equal. False if they are not + */ + private boolean compareArrays(final Object array1, final Object array2) { + // Check for primitive array types + if (array1 instanceof boolean[]) { + return Arrays.equals((boolean[]) array1, (boolean[]) array2); + } + if (array1 instanceof byte[]) { + return Arrays.equals((byte[]) array1, (byte[]) array2); + } + if (array1 instanceof char[]) { + return Arrays.equals((char[]) array1, (char[]) array2); + } + if (array1 instanceof double[]) { + return Arrays.equals((double[]) array1, (double[]) array2); + } + if (array1 instanceof float[]) { + return Arrays.equals((float[]) array1, (float[]) array2); + } + if (array1 instanceof int[]) { + return Arrays.equals((int[]) array1, (int[]) array2); + } + if (array1 instanceof long[]) { + return Arrays.equals((long[]) array1, (long[]) array2); + } + if (array1 instanceof short[]) { + return Arrays.equals((short[]) array1, (short[]) array2); + } + + // Not a primitive so must be an Object[] + return Arrays.equals((Object[]) array1, (Object[]) array2); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/MethodEventInvokerTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/MethodEventInvokerTestCase.java new file mode 100644 index 0000000000..e9cbce4390 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/MethodEventInvokerTestCase.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.implementation.java.invocation.EventInvocationException; +import org.apache.tuscany.sca.implementation.java.invocation.MethodEventInvoker; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class MethodEventInvokerTestCase { + private Method privateMethod; + private Method exceptionMethod; + + @Test + public void testIllegalAccess() throws Exception { + MethodEventInvoker<MethodEventInvokerTestCase.Foo> injector = new MethodEventInvoker<Foo>(privateMethod); + try { + injector.invokeEvent(new Foo()); + fail(); + } catch (EventInvocationException e) { + // expected + } + } + + @Test + public void testException() throws Exception { + MethodEventInvoker<MethodEventInvokerTestCase.Foo> injector = new MethodEventInvoker<Foo>(exceptionMethod); + try { + injector.invokeEvent(new Foo()); + fail(); + } catch (RuntimeException e) { + // expected + } + } + + @Before + public void setUp() throws Exception { + privateMethod = MethodEventInvokerTestCase.Foo.class.getDeclaredMethod("hidden"); + exceptionMethod = MethodEventInvokerTestCase.Foo.class.getDeclaredMethod("exception"); + + } + + public class Foo { + + public void foo() { + } + + private void hidden() { + } + + public void exception() { + throw new RuntimeException(); + } + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjectorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjectorTestCase.java new file mode 100644 index 0000000000..19184d5204 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjectorTestCase.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class MethodInjectorTestCase { + private Method fooMethod; + private Method privateMethod; + private Method exceptionMethod; + + @Test + public void testIllegalArgument() throws Exception { + ObjectFactory<Object> factory = new SingletonObjectFactory<Object>(new Object()); + MethodInjector<Foo> injector = new MethodInjector<Foo>(fooMethod, factory); + try { + injector.inject(new Foo()); + fail(); + } catch (ObjectCreationException e) { + // expected + } + } + + @Test + public void testException() throws Exception { + ObjectFactory<Object> factory = new SingletonObjectFactory<Object>("foo"); + MethodInjector<Foo> injector = new MethodInjector<Foo>(exceptionMethod, factory); + try { + injector.inject(new Foo()); + fail(); + } catch (RuntimeException e) { + // expected + } + } + + @Before + public void setUp() throws Exception { + fooMethod = Foo.class.getMethod("foo", String.class); + privateMethod = Foo.class.getDeclaredMethod("hidden", String.class); + exceptionMethod = Foo.class.getDeclaredMethod("exception", String.class); + + } + + private class Foo { + + public void foo(String bar) { + } + + private void hidden(String bar) { + } + + public void exception(String bar) { + throw new RuntimeException(); + } + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/ResourceObjectFactoryTestCase.java.fixme b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/ResourceObjectFactoryTestCase.java.fixme new file mode 100644 index 0000000000..ed7a0e7d03 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/ResourceObjectFactoryTestCase.java.fixme @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import junit.framework.TestCase; + +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ResourceObjectFactoryTestCase extends TestCase { + + public void testResolveFromHostByType() throws Exception { + ResourceHost host = EasyMock.createMock(ResourceHost.class); + EasyMock.expect(host.resolveResource(EasyMock.eq(String.class))).andReturn("foo"); + EasyMock.replay(host); + ResourceObjectFactory<String> factory = new ResourceObjectFactory<String>(String.class, false, host); + assertEquals("foo", factory.getInstance()); + EasyMock.verify(host); + } + + public void testResolveFromHostByName() throws Exception { + ResourceHost host = EasyMock.createMock(ResourceHost.class); + EasyMock.expect(host.resolveResource(EasyMock.eq(String.class), + EasyMock.eq("sca://localhost/bar"))).andReturn("foo"); + EasyMock.replay(host); + ResourceObjectFactory<String> factory = + new ResourceObjectFactory<String>(String.class, "sca://localhost/bar", false, host); + assertEquals("foo", factory.getInstance()); + EasyMock.verify(host); + } + + + public void testResolveFromParentThenResolveFromHostNotFound() throws Exception { + ResourceHost host = EasyMock.createMock(ResourceHost.class); + EasyMock.expect(host.resolveResource(EasyMock.eq(String.class))).andReturn(null); + EasyMock.replay(host); + ResourceObjectFactory<String> factory = new ResourceObjectFactory<String>(String.class, true, host); + assertNull(factory.getInstance()); + EasyMock.verify(host); + } + + public void testResolveByTypeNotFound() throws Exception { +// ResourceHost host = EasyMock.createMock(ResourceHost.class); +// EasyMock.expect(host.resolveResource(EasyMock.eq(String.class))).andReturn(null); +// EasyMock.replay(host); +// +// RuntimeWire wire = EasyMock.createMock(RuntimeWire.class); +// EasyMock.expect(wire.getTargetInstance()).andReturn(null); +// EasyMock.replay(wire); +// +// ResourceObjectFactory<String> factory = new ResourceObjectFactory<String>(String.class, false, host); +// try { +// factory.getInstance(); +// fail(); +// } catch (ResourceNotFoundException e) { +// //expected +// } +// EasyMock.verify(host); + } + + public void testResolveByTypeNotFoundOptional() throws Exception { + ResourceHost host = EasyMock.createMock(ResourceHost.class); + EasyMock.expect(host.resolveResource(EasyMock.eq(String.class))).andReturn(null); + EasyMock.replay(host); + ResourceObjectFactory<String> factory = new ResourceObjectFactory<String>(String.class, true, host); + assertNull(factory.getInstance()); + EasyMock.verify(host); + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/SingletonObjectFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/SingletonObjectFactory.java new file mode 100644 index 0000000000..c1650c3a83 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/SingletonObjectFactory.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * Implementation of ObjectFactory that returns a single instance, typically an immutable type. + * + * @version $Rev$ $Date$ + */ +public class SingletonObjectFactory<T> implements ObjectFactory<T> { + private final T instance; + + public SingletonObjectFactory(T instance) { + this.instance = instance; + } + + public T getInstance() { + return instance; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/SingletonObjectFactoryTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/SingletonObjectFactoryTestCase.java new file mode 100644 index 0000000000..0bb222bad4 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/SingletonObjectFactoryTestCase.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class SingletonObjectFactoryTestCase { + + @Test + public void testSingleton() throws Exception { + Object o = new Object(); + SingletonObjectFactory<Object> factory = new SingletonObjectFactory<Object>(o); + assertEquals(o, factory.getInstance()); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/TestObjectFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/TestObjectFactory.java new file mode 100644 index 0000000000..de371e09bb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/TestObjectFactory.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +import org.apache.tuscany.sca.core.factory.ObjectCreationException; +import org.apache.tuscany.sca.core.factory.ObjectFactory; + +/** + * Creates new instances of a Java class + * + * @version $Rev$ $Date$ + * @see org.apache.tuscany.sca.implementation.java.injection.Injector + */ +public class TestObjectFactory<T> implements ObjectFactory<T> { + + private final Constructor<T> ctr; + private ObjectFactory[] initializerFactories; + + /** + * Creates the object factory + * + * @param ctr the constructor to use when instantiating a new object + */ + public TestObjectFactory(Constructor<T> ctr) { + assert ctr != null; + this.ctr = ctr; + initializerFactories = new ObjectFactory[ctr.getParameterTypes().length]; + } + + /** + * Creates the object factory + * + * @param ctr the constructor to use when instantiating a new object + * @param factories an ordered list of <code>ObjectFactory</code>s to use for returning constructor parameters + */ + public TestObjectFactory(Constructor<T> ctr, List<ObjectFactory> factories) { + assert ctr != null; + int params = ctr.getParameterTypes().length; + assert params == factories.size(); + this.ctr = ctr; + initializerFactories = new ObjectFactory[params]; + int i = 0; + for (ObjectFactory factory : factories) { + initializerFactories[i] = factory; + i++; + } + } + + /** + * Returns the ordered array of <code>ObjectFactory</code>s use in creating constructor parameters + */ + public ObjectFactory[] getInitializerFactories() { + return initializerFactories; + } + + /** + * Sets an <code>ObjectFactory</code>s to use in creating constructor parameter + * + * @param pos the constructor parameter position + * @param factory the object factory + */ + public void setInitializerFactory(int pos, ObjectFactory factory) { + assert pos < initializerFactories.length; + initializerFactories[pos] = factory; + } + + /** + * Creates a new instance of an object + */ + public T getInstance() throws ObjectCreationException { + int size = initializerFactories.length; + Object[] initargs = new Object[size]; + // create the constructor arg array + for (int i = 0; i < size; i++) { + ObjectFactory<?> objectFactory = initializerFactories[i]; + if (objectFactory == null) { + // this can happen if a reference is optional + initargs[i] = null; + } else { + initargs[i] = objectFactory.getInstance(); + } + } + try { + ctr.setAccessible(true); + return ctr.newInstance(initargs); + } catch (IllegalArgumentException e) { + String name = ctr.getName(); + throw new ObjectCreationException("Exception thrown by constructor: " + name, e); + } catch (InstantiationException e) { + String name = ctr.getDeclaringClass().getName(); + throw new AssertionError("Class is not instantiable [" + name + "]"); + } catch (IllegalAccessException e) { + String name = ctr.getName(); + throw new AssertionError("Constructor is not accessible [" + name + "]"); + } catch (InvocationTargetException e) { + String name = ctr.getName(); + throw new ObjectCreationException("Exception thrown by constructor: " + name, e); + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/TestObjectFactoryTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/TestObjectFactoryTestCase.java new file mode 100644 index 0000000000..ea9d15b3f6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/injection/TestObjectFactoryTestCase.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.injection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.core.factory.ObjectFactory; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class TestObjectFactoryTestCase { + + private Constructor<Foo> ctor; + + @Test + public void testConstructorInjection() throws Exception { + List<ObjectFactory> initializers = new ArrayList<ObjectFactory>(); + initializers.add(new SingletonObjectFactory<String>("foo")); + TestObjectFactory<Foo> factory = new TestObjectFactory<Foo>(ctor, initializers); + Foo foo = factory.getInstance(); + assertEquals("foo", foo.foo); + } + + /** + * Verifies null parameters can be passed to a constructor. This is valid when a reference is optional during + * constructor injection + */ + @Test + public void testConstructorInjectionOptionalParam() throws Exception { + List<ObjectFactory> initializers = new ArrayList<ObjectFactory>(); + initializers.add(null); + TestObjectFactory<Foo> factory = new TestObjectFactory<Foo>(ctor, initializers); + Foo foo = factory.getInstance(); + assertNull(foo.foo); + } + + @Test + public void testConstructorInitializerInjection() throws Exception { + TestObjectFactory<Foo> factory = new TestObjectFactory<Foo>(ctor); + factory.setInitializerFactory(0, new SingletonObjectFactory<String>("foo")); + Foo foo = factory.getInstance(); + assertEquals("foo", foo.foo); + } + + @Before + public void setUp() throws Exception { + ctor = Foo.class.getConstructor(String.class); + } + + private static class Foo { + + private String foo; + + public Foo(String foo) { + this.foo = foo; + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Bean1.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Bean1.java new file mode 100644 index 0000000000..e993cdd382 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Bean1.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.util; + + +/** + * + * @version $Rev$ $Date$ + */ +public class Bean1 extends SuperBean { + + public static final int ALL_BEAN1_FIELDS = 6 + ALL_SUPER_FIELDS; + public static final int ALL_BEAN1_PUBLIC_PROTECTED_FIELDS = 5 + ALL_SUPER_PUBLIC_PROTECTED_FIELDS; + public static final int ALL_BEAN1_METHODS = 4 + ALL_SUPER_METHODS - 1; + public String field3; + protected String field2; + private String field1; + + public void setMethod1(String param) { + } + + public void setMethod1(int param) { + } + + @Override + public void override(String param) throws Exception { + } + + + public void noOverride(String param) throws Exception { + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Bean2.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Bean2.java new file mode 100644 index 0000000000..f5128f6a6c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Bean2.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.util; + +import java.util.List; + +/** + * + * @version $Rev$ $Date$ + */ +public class Bean2 { + + private List methodList; + private List fieldList; + + public List getMethodList() { + return methodList; + } + + public void setMethodList(List list) { + methodList = list; + } + + public List getfieldList() { + return fieldList; + } + + public void setfieldList(List list) { + throw new RuntimeException("setter inadvertantly called"); + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Entry.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Entry.java new file mode 100644 index 0000000000..a5e12d181c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/Entry.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.util; + +/** + * Implementations are used in wiring tests + * + * @version $Rev$ $Date$ + */ +public interface Entry { + + String getString(); + + void setString(String val); +} + diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/JavaIntrospectionHelperTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/JavaIntrospectionHelperTestCase.java new file mode 100644 index 0000000000..9624865dcc --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/JavaIntrospectionHelperTestCase.java @@ -0,0 +1,189 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.junit.Test; + +/** + * + * @version $Rev$ $Date$ + */ +public class JavaIntrospectionHelperTestCase { + + private List testNoGenericsList; + private List<String> testList; + private Map<String, Bean1> testMap; + private Entry[] testArray; + private String[] testStringArray; + + @Test + public void testBean1AllPublicProtectedFields() throws Exception { + Set<Field> beanFields = JavaIntrospectionHelper.getAllPublicAndProtectedFields(Bean1.class, true); + assertEquals(4, beanFields.size()); //Bean1.ALL_BEAN1_PUBLIC_PROTECTED_FIELDS + } + + @Test + public void testGetSuperAllMethods() throws Exception { + Set<Method> superBeanMethods = JavaIntrospectionHelper.getAllUniquePublicProtectedMethods(SuperBean.class, true); + assertEquals(SuperBean.ALL_SUPER_METHODS, superBeanMethods.size()); + } + + @Test + public void testGetBean1AllMethods() throws Exception { + Set<Method> beanMethods = JavaIntrospectionHelper.getAllUniquePublicProtectedMethods(Bean1.class, true); + assertEquals(Bean1.ALL_BEAN1_METHODS, beanMethods.size()); + } + + @Test + public void testOverrideMethod() throws Exception { + Set<Method> beanFields = JavaIntrospectionHelper.getAllUniquePublicProtectedMethods(Bean1.class, true); + boolean invoked = false; + for (Method method : beanFields) { + if (method.getName().equals("override")) { + method.invoke(new Bean1(), "foo"); + invoked = true; + } + } + if (!invoked) { + throw new Exception("Override never invoked"); + } + } + + @Test + public void testNoOverrideMethod() throws Exception { + Set<Method> beanFields = JavaIntrospectionHelper.getAllUniquePublicProtectedMethods(Bean1.class, true); + boolean found = false; + for (Method method : beanFields) { + if (method.getName().equals("noOverride") && method.getParameterTypes().length == 0) { + found = true; + } + } + if (!found) { + throw new Exception("No override not found"); + } + } + + @Test + public void testDefaultConstructor() throws Exception { + Constructor<Bean2> ctr = JavaIntrospectionHelper.getDefaultConstructor(Bean2.class); + assertEquals(ctr, Bean2.class.getConstructor()); + assertTrue(Bean2.class == ctr.newInstance((Object[]) null).getClass()); + } + + + @Test + public void testGetAllInterfaces() { + Set<Class<?>> interfaces = JavaIntrospectionHelper.getAllInterfaces(Z.class); + assertEquals(2, interfaces.size()); + assertTrue(interfaces.contains(W.class)); + assertTrue(interfaces.contains(W2.class)); + } + + + @Test + public void testGetAllInterfacesObject() { + Set<Class<?>> interfaces = JavaIntrospectionHelper.getAllInterfaces(Object.class); + assertEquals(0, interfaces.size()); + } + + @Test + public void testGetAllInterfacesNoInterfaces() { + Set<Class<?>> interfaces = JavaIntrospectionHelper.getAllInterfaces(NoInterface.class); + assertEquals(0, interfaces.size()); + } + + /** + * Tests generics introspection capabilities + */ + @Test + public void testGenerics() throws Exception { + + List classes = JavaIntrospectionHelper.getGenerics(getClass().getDeclaredField("testList").getGenericType()); + assertEquals(1, classes.size()); + assertEquals(String.class, classes.get(0)); + + classes = + JavaIntrospectionHelper.getGenerics(getClass().getDeclaredField("testNoGenericsList").getGenericType()); + assertEquals(0, classes.size()); + + classes = JavaIntrospectionHelper.getGenerics(getClass().getDeclaredField("testMap").getGenericType()); + assertEquals(2, classes.size()); + assertEquals(String.class, classes.get(0)); + assertEquals(Bean1.class, classes.get(1)); + + classes = JavaIntrospectionHelper + .getGenerics(getClass().getDeclaredMethod("fooMethod", Map.class).getGenericParameterTypes()[0]); + assertEquals(2, classes.size()); + assertEquals(String.class, classes.get(0)); + assertEquals(Bean1.class, classes.get(1)); + + classes = JavaIntrospectionHelper + .getGenerics(getClass().getDeclaredMethod("fooMethod", List.class).getGenericParameterTypes()[0]); + assertEquals(1, classes.size()); + assertEquals(String.class, classes.get(0)); + + } + + private void fooMethod(List<String> foo) { + + } + + private void fooMethod(Map<String, Bean1> foo) { + + } + + public void setTestArray(Entry[] array) { + } + + private interface W { + + } + + private interface W2 { + + } + + private class X implements W { + + } + + private class Y extends X implements W, W2 { + + } + + private class Z extends Y { + + } + + private class NoInterface { + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/SuperBean.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/SuperBean.java new file mode 100644 index 0000000000..408cedb8be --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/sca/implementation/java/util/SuperBean.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.util; + +/** + * @version $Rev$ $Date$ + */ +public class SuperBean { + + public static final int ALL_SUPER_FIELDS = 6; + public static final int ALL_SUPER_PUBLIC_PROTECTED_FIELDS = 5; + public static final int ALL_SUPER_METHODS = 4; + public String superField2; + + protected String superField3; + + private String superField1; + + public void setSuperMethod1(String param) { + } + + public void setSuperMethod1(int param) { + } + + public void override(String param) throws Exception { + throw new Exception("Override not handled"); + } + + public void noOverride() throws Exception { + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/LICENSE b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/LICENSE new file mode 100644 index 0000000000..8aa906c321 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-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-2.x/branches/2.0-Beta3/modules/implementation-java/META-INF/MANIFEST.MF b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..2b741f7161 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/META-INF/MANIFEST.MF @@ -0,0 +1,51 @@ +Manifest-Version: 1.0
+Export-Package: org.apache.tuscany.sca.implementation.java;version="2.0.0";
+ uses:="org.apache.tuscany.sca.assembly,
+ org.apache.tuscany.sca.implementation.java.introspect,
+ org.apache.tuscany.sca.core,
+ org.apache.tuscany.sca.policy,
+ org.apache.tuscany.sca.interfacedef.java",
+ org.apache.tuscany.sca.implementation.java.introspect;version="2.0.0";uses:="org.apache.tuscany.sca.assembly,org.apache.tuscany.sca.implementation.java,org.oasisopen.sca",
+ org.apache.tuscany.sca.implementation.java.introspect.impl;version="2.0.0"
+Private-Package: org.apache.tuscany.sca.implementation.java.impl;versi
+ on="2.0.0",org.apache.tuscany.sca.implementation.java.introspect.impl;v
+ ersion="2.0.0"
+SCA-Version: 1.1
+Bundle-Name: Apache Tuscany SCA Java Implementation Model
+Bundle-Vendor: The Apache Software Foundation
+Bundle-Version: 2.0.0
+Bundle-ManifestVersion: 2
+Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
+Bundle-Description: Apache Tuscany SCA Java Implementation Model
+Import-Package: javax.jws,
+ javax.jws.soap,
+ javax.xml.namespace,
+ javax.xml.stream,
+ javax.xml.ws,
+ org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.assembly.builder;version="2.0.0";resolution:=optional,
+ org.apache.tuscany.sca.assembly.impl;version="2.0.0",
+ org.apache.tuscany.sca.assembly.xml;version="2.0.0",
+ org.apache.tuscany.sca.binding.ws;version="2.0.0",
+ org.apache.tuscany.sca.binding.ws.xml,
+ org.apache.tuscany.sca.contribution.processor;version="2.0.0",
+ org.apache.tuscany.sca.contribution.resolver;version="2.0.0",
+ org.apache.tuscany.sca.core;version="2.0.0",
+ org.apache.tuscany.sca.definitions;version="2.0.0";resolution:=optional,
+ org.apache.tuscany.sca.extensibility;version="2.0.0",
+ org.apache.tuscany.sca.implementation.java;version="2.0.0",
+ org.apache.tuscany.sca.implementation.java.introspect;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.impl;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.java;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.java.impl;version="2.0.0";resolution:=optional,
+ org.apache.tuscany.sca.interfacedef.util;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.wsdl;version="2.0.0",
+ org.apache.tuscany.sca.monitor;version="2.0.0",
+ org.apache.tuscany.sca.policy;version="2.0.0",
+ org.apache.tuscany.sca.policy.util;version="2.0.0",
+ org.oasisopen.sca;version="2.0.0",
+ org.oasisopen.sca.annotation;version="2.0.0"
+Bundle-SymbolicName: org.apache.tuscany.sca.implementation.java
+Bundle-DocURL: http://www.apache.org/
+Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/NOTICE b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/NOTICE new file mode 100644 index 0000000000..d69e595698 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/NOTICE @@ -0,0 +1,6 @@ +${pom.name} +Copyright (c) 2005 - 2011 The Apache Software Foundation + +This product includes software developed by +The Apache Software Foundation (http://www.apache.org/). + diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/pom.xml b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/pom.xml new file mode 100644 index 0000000000..4968c5ed15 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/pom.xml @@ -0,0 +1,109 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +--> +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-modules</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <artifactId>tuscany-implementation-java</artifactId> + <name>Apache Tuscany SCA Implementation Java Model</name> + + <dependencies> + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-assembly</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-interface-java</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-interface-java-jaxws</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-interface-wsdl</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-sca-api</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-assembly-xml</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-extensibility</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-contribution</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-binding-ws</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + </dependency> + + <!--dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-builder</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-binding-sca-runtime</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-binding-ws</artifactId> + <version>2.0-Beta3-SNAPSHOT</version> + <scope>test</scope> + </dependency--> + + </dependencies> + +</project> diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/BaseJavaImplementation.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/BaseJavaImplementation.java new file mode 100644 index 0000000000..a3b0343d51 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/BaseJavaImplementation.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Extensible; +import org.apache.tuscany.sca.assembly.Implementation; + +/** + * Represents a Java implementation. + * + * @version $Rev$ $Date$ + */ +public interface BaseJavaImplementation extends Implementation, Extensible { + + /** + * Returns the name of the Java implementation class. + * + * @return the name of the Java implementation class + */ + String getName(); + + /** + * Sets the name of the Java implementation class. + * + * @param className the name of the Java implementation class + */ + void setName(String className); + + /** + * Returns the Java implementation class. + * + * @return the Java implementation class + */ + Class<?> getJavaClass(); + + /** + * Sets the Java implementation class. + * + * @param javaClass the Java implementation class + */ + void setJavaClass(Class<?> javaClass); + + /** + * Customize the implementation type so that components are implemented using Java based framework such as + * implementation.spring or implementation.jaxrs can leverage the introspection + * @param type + */ + void setType(QName type); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/DefaultJavaImplementationFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/DefaultJavaImplementationFactory.java new file mode 100644 index 0000000000..f98a617925 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/DefaultJavaImplementationFactory.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java; + +/** + * A factory for the Java model. + * + * @version $Rev$ $Date$ + */ +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.impl.JavaImplementationFactoryImpl; + +public class DefaultJavaImplementationFactory extends JavaImplementationFactoryImpl implements JavaImplementationFactory { + + public DefaultJavaImplementationFactory(ExtensionPointRegistry registry) { + super(registry); + } + + // For UNIT test only + public DefaultJavaImplementationFactory() { + super(new DefaultExtensionPointRegistry()); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/IntrospectionException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/IntrospectionException.java new file mode 100644 index 0000000000..2198a632c0 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/IntrospectionException.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java; + +import java.lang.reflect.Member; + +/** + * Denotes a problem processing annotations on a POJO implementation + * + * @version $Rev$ $Date$ + */ +public class IntrospectionException extends Exception { + private static final long serialVersionUID = -361025119035104470L; + private Member member; + + public IntrospectionException() { + } + + public IntrospectionException(String message) { + super(message); + } + + public IntrospectionException(String message, Member member) { + super(message); + this.member = member; + } + + public IntrospectionException(String message, Throwable cause) { + super(message, cause); + } + + public IntrospectionException(Throwable cause) { + super(cause); + } + + public Member getMember() { + return member; + } + + public void setMember(Member member) { + this.member = member; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaConstructorImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaConstructorImpl.java new file mode 100644 index 0000000000..df76d3ea4e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaConstructorImpl.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java; + +import java.lang.reflect.Constructor; + + +/** + * Hold injection information for the constructor used to instantiate a + * component implementation instance + * + * @version $Rev$ $Date$ + */ +public class JavaConstructorImpl<T> { + private Constructor<T> constructor; + private JavaParameterImpl[] parameters; + + public JavaConstructorImpl(Constructor<T> constructor) { + this.constructor = constructor; + int size = constructor.getParameterTypes().length; + parameters = new JavaParameterImpl[size]; + for (int i = 0; i < size; i++) { + parameters[i] = new JavaParameterImpl(constructor, i); + } + } + + /** + * @return the constructor + */ + public Constructor<T> getConstructor() { + return constructor; + } + + /** + * @return the parameters + */ + public JavaParameterImpl[] getParameters() { + return parameters; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaElementImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaElementImpl.java new file mode 100644 index 0000000000..e748a5f6fa --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaElementImpl.java @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java; + +import java.lang.annotation.Annotation; +import java.lang.annotation.ElementType; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Type; + +/** + * This class represents a java element such as a Package, Class, Constructor, + * Field, Method or Parameter. + * + * @version $Rev$ $Date$ + */ +public class JavaElementImpl { + private AnnotatedElement anchor; + private ElementType elementType; + private Class<?> type; + private Type genericType; + private int index = -1; + private String name; + private Class<? extends Annotation> classifer; + + public JavaElementImpl(Class<?> cls) { + this.anchor = cls; + this.elementType = ElementType.TYPE; + this.type = cls; + this.genericType = cls; + this.name = cls.getName(); + } + + public JavaElementImpl(Field field) { + this.anchor = field; + this.elementType = ElementType.FIELD; + this.type = field.getType(); + this.genericType = field.getGenericType(); + this.name = field.getName(); + } + + public JavaElementImpl(Constructor<?> constructor, int index) { + this.anchor = constructor; + this.elementType = ElementType.PARAMETER; + this.type = constructor.getParameterTypes()[index]; + this.genericType = constructor.getGenericParameterTypes()[index]; + this.index = index; + this.name = ""; + } + + public JavaElementImpl(Method method, int index) { + this.anchor = method; + this.elementType = ElementType.PARAMETER; + this.type = method.getParameterTypes()[index]; + this.genericType = method.getGenericParameterTypes()[index]; + this.index = index; + this.name = ""; + } + + /** + * For testing purpose + * + * @param name + * @param type + * @param classifer TODO + * @param elementType + */ + public JavaElementImpl(String name, Class<?> type, Class<? extends Annotation> classifer) { + super(); + this.type = type; + this.name = name; + this.classifer = classifer; + } + + /** + * @return the anchor + */ + public AnnotatedElement getAnchor() { + return anchor; + } + + /** + * @return the elementType + */ + public ElementType getElementType() { + return elementType; + } + + /** + * @return the genericType + */ + public Type getGenericType() { + return genericType; + } + + /** + * @return the index + */ + public int getIndex() { + return index; + } + + /** + * @return the type + */ + public Class<?> getType() { + return type; + } + + /** + * @return the annotations + */ + public Annotation[] getAnnotations() { + if (elementType == ElementType.PARAMETER) { + if (anchor instanceof Method) { + // We only care about the method-level annotations + return ((Method)anchor).getAnnotations(); + } + if (anchor instanceof Constructor) { + return ((Constructor<?>)anchor).getParameterAnnotations()[index]; + } + } + return anchor.getAnnotations(); + } + + public <T extends Annotation> T getAnnotation(Class<T> annotationType) { + for (Annotation a : getAnnotations()) { + if (a.annotationType() == annotationType) { + return annotationType.cast(a); + } + } + return null; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the classifier + */ + public Class<? extends Annotation> getClassifer() { + return classifer; + } + + /** + * @param classifer the classifier to set + */ + public void setClassifer(Class<? extends Annotation> classifer) { + this.classifer = classifer; + } + + + @Override + public String toString() { + return anchor.toString() + (elementType == ElementType.PARAMETER ? "[" + index + "]" : ""); + } + + @Override + public int hashCode() { + final int PRIME = 31; + int result = 1; + result = PRIME * result + ((anchor == null) ? 0 : anchor.hashCode()); + result = PRIME * result + index; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final JavaElementImpl other = (JavaElementImpl)obj; + if (anchor == null) { + if (other.anchor != null) { + return false; + } + } else if (!anchor.equals(other.anchor)) { + return false; + } + if (index != other.index) { + return false; + } + return true; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaImplementation.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaImplementation.java new file mode 100644 index 0000000000..60649eb5d6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaImplementation.java @@ -0,0 +1,164 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +/** + * Represents a Java implementation. + * + * @version $Rev$ $Date$ + */ +public interface JavaImplementation extends BaseJavaImplementation { + QName TYPE = new QName(SCA11_NS, "implementation.java"); + /** + * Returns the constructor used to instantiate implementation instances. + * + * @return the constructor used to instantiate implementation instances + */ + JavaConstructorImpl<?> getConstructor(); + + /** + * Sets the constructor used to instantiate implementation instances + * + * @param definition the constructor used to instantiate implementation instances + */ + void setConstructor(JavaConstructorImpl<?> definition); + + /** + * Returns the component initializer method. + * + * @return the component initializer method + */ + Method getInitMethod(); + + /** + * Sets the component initializer method. + * + * @param initMethod the component initializer method + */ + void setInitMethod(Method initMethod); + + /** + * Returns the component destructor method. + * + * @return the component destructor method + */ + Method getDestroyMethod(); + + /** + * Sets the component destructor method. + * + * @param destroyMethod the component destructor method + */ + void setDestroyMethod(Method destroyMethod); + + /** + * Returns the resources injected into this implementation. + * + * @return + */ + Map<String, JavaResourceImpl> getResources(); + + /** + * Returns the Java member used to inject a conversation ID. + * + * @return + */ + List<Member> getConversationIDMembers(); + + /** + * Sets the Java member used to inject a conversation ID. + * + * @param conversationIDMember + */ + void addConversationIDMember(Member conversationIDMember); + + /** + * Returns true if AllowsPassReference is set. + * + * @return true if AllowsPassByReference is set + */ + boolean isAllowsPassByReference(); + + /** + * @param allowsPassByReference the allowsPassByReference to set + */ + void setAllowsPassByReference(boolean allowsPassByReference); + + /** + * @return the allowsPassByReferenceMethods + */ + List<Method> getAllowsPassByReferenceMethods(); + + /** + * @param method + * @return + */ + boolean isAllowsPassByReference(Method method); + + /** + * @return the constructors + */ + Map<Constructor, JavaConstructorImpl> getConstructors(); + + /** + * @return the eagerInit + */ + boolean isEagerInit(); + + /** + * @param eagerInit the eagerInit to set + */ + void setEagerInit(boolean eagerInit); + + /** + * @return the callbacks + */ + Map<String, Collection<JavaElementImpl>> getCallbackMembers(); + + /** + * @return the properties + */ + Map<String, JavaElementImpl> getPropertyMembers(); + + /** + * @return the references + */ + Map<String, JavaElementImpl> getReferenceMembers(); + + /** + * @return the scope + */ + JavaScopeImpl getJavaScope(); + + /** + * @param scope the scope to set + */ + void setJavaScope(JavaScopeImpl scope); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaImplementationFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaImplementationFactory.java new file mode 100644 index 0000000000..8228c80372 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaImplementationFactory.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java; + +import java.util.List; + +import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; + + +/** + * Factory for the Java model + * + * @version $Rev$ $Date$ + */ +public interface JavaImplementationFactory { + + /** + * Creates a new Java implementation model. + * + * @return + */ + JavaImplementation createJavaImplementation(); + + /** + * Creates a new Java implementation model from an implementation class. + * + * @param implementationClass The implementation class to introspect. + * @return + */ + JavaImplementation createJavaImplementation(Class<?> implementationClass) throws IntrospectionException; + + /** + * Creates the contents of a Java implementation model from an implementation class. + * + * @param implementationClass The implementation class to introspect. + * @return + */ + void createJavaImplementation(JavaImplementation javaImplementation, Class<?> implementationClass) throws IntrospectionException; + + /** + * Registers the given visitor. + * + * @param visitor + */ + void addClassVisitor(JavaClassVisitor visitor); + + /** + * Deregisters the given visitor. + */ + void removeClassVisitor(JavaClassVisitor visitor); + + /** + * Returns the list of visitors. + * + * @return + */ + List<JavaClassVisitor> getClassVisitors(); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaParameterImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaParameterImpl.java new file mode 100644 index 0000000000..80911a9552 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaParameterImpl.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java; + +import java.lang.reflect.Constructor; + + +/** + * @version $Rev$ $Date$ + */ +public class JavaParameterImpl extends JavaElementImpl { + /** + * @param constructor + * @param index + */ + public JavaParameterImpl(Constructor<?> constructor, int index) { + super(constructor, index); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaResourceImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaResourceImpl.java new file mode 100644 index 0000000000..19a9f38549 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaResourceImpl.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java; + + + +/** + * A resource dependency declared by a Java component implementation + * + * @version $Rev$ $Date$ + */ +public class JavaResourceImpl { + private JavaElementImpl element; + private String mappedName; + private boolean optional; + + public JavaResourceImpl(JavaElementImpl element) { + this.element = element; + } + + /** + * The name of the resource + * + * @return the name of the resource + */ + public String getName() { + return element.getName(); + } + + /** + * Returns the URI of the resource + * + * @return the URI of the resource + */ + public String getMappedName() { + return mappedName; + } + + /** + * Sets the resource URI + */ + public void setMappedName(String mappedName) { + this.mappedName = mappedName; + } + + /** + * If true, the resource is optional + * + * @return true if the resource is optional + */ + public boolean isOptional() { + return optional; + } + + /** + * Sets whether the resource is optional + */ + public void setOptional(boolean optional) { + this.optional = optional; + } + + /** + * @return the element + */ + public JavaElementImpl getElement() { + return element; + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaScopeImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaScopeImpl.java new file mode 100644 index 0000000000..c977736630 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/JavaScopeImpl.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.implementation.java; + +/** + * The default implementation scopes supported by assemblies. + * + * @version $Rev$ $Date$ + */ +public class JavaScopeImpl { + public static final JavaScopeImpl STATELESS = new JavaScopeImpl("STATELESS"); + public static final JavaScopeImpl COMPOSITE = new JavaScopeImpl("COMPOSITE"); + public static final JavaScopeImpl INVALID = new JavaScopeImpl("INVALID"); + + private String scope; + + public JavaScopeImpl(String scope) { + this.scope = scope.toUpperCase().intern(); + } + + public String getScope() { + return scope; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final JavaScopeImpl scope1 = (JavaScopeImpl) o; + return !(scope != null ? scope != scope1.scope.intern() : scope1.scope != null); + } + + @Override + public int hashCode() { + return scope != null ? scope.hashCode() : 0; + } + + @Override + public String toString() { + return scope; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/BaseJavaImplementationImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/BaseJavaImplementationImpl.java new file mode 100644 index 0000000000..a5dd89f75a --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/BaseJavaImplementationImpl.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.impl; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.impl.ImplementationImpl; +import org.apache.tuscany.sca.implementation.java.BaseJavaImplementation; + +/** + * Represents a Java implementation. + * + * @version $Rev$ $Date$ + */ +abstract class BaseJavaImplementationImpl extends ImplementationImpl implements BaseJavaImplementation { + + private String className; + private Class<?> javaClass; + + protected BaseJavaImplementationImpl(QName type) { + super(type); + } + + public String getName() { + if (isUnresolved()) { + return className; + } else if (javaClass != null) { + return javaClass.getName(); + } else { + return null; + } + } + + public void setName(String className) { + if (!isUnresolved()) { + throw new IllegalStateException(); + } + this.className = className; + } + + public Class<?> getJavaClass() { + return javaClass; + } + + public void setJavaClass(Class<?> javaClass) { + this.javaClass = javaClass; + if (this.className == null) { + this.className = javaClass.getName(); + } + } + + @Override + public String toString() { + return getType() + " (class=" + getName() + ")"; + } + +/* TUSCANY-3876 - disable implementation model sharing so that + * we can get implementation policy modelled on an + * impementation by implementation basis rather than + * storing it on the component + @Override + public int hashCode() { + return String.valueOf(getName()).hashCode(); + } +*/ + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj instanceof BaseJavaImplementation) { + if (getName() != null) { + return getName().equals(((BaseJavaImplementation)obj).getName()); + } else { + return ((BaseJavaImplementation)obj).getName() == null; + } + } else { + return false; + } + } + + @Override + public void setType(QName type) { + this.type = type; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java new file mode 100644 index 0000000000..491408098e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaClassIntrospectorImpl.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.impl; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Set; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; + +/** + * An extensible Java class introspector implementation. + * + * @version $Rev$ $Date$ + */ +public class JavaClassIntrospectorImpl { + + private List<JavaClassVisitor> visitors; + + public JavaClassIntrospectorImpl(List<JavaClassVisitor> visitors) { + this.visitors = visitors; + } + + /** + * JSR-250 PFD recommends the following guidelines for how annotations + * interact with inheritance in order to keep the resulting complexity in + * control: + * <ol> + * <li>Class-level annotations only affect the class they annotate and + * their members, that is, its methods and fields. They never affect a + * member declared by a superclass, even if it is not hidden or overridden + * by the class in question. + * <li>In addition to affecting the annotated class, class-level + * annotations may act as a shorthand for member-level annotations. If a + * member carries a specific member-level annotation, any annotations of the + * same type implied by a class-level annotation are ignored. In other + * words, explicit member-level annotations have priority over member-level + * annotations implied by a class-level annotation. + * <li>The interfaces implemented by a class never contribute annotations + * to the class itself or any of its members. + * <li>Members inherited from a superclass and which are not hidden or + * overridden maintain the annotations they had in the class that declared + * them, including member-level annotations implied by class-level ones. + * <li>Member-level annotations on a hidden or overridden member are always + * ignored. + * </ol> + */ + public void introspectClass(JavaImplementation type, Class<?> clazz) + throws IntrospectionException { + for (JavaClassVisitor visitor : visitors) { + visitor.visitClass(clazz, type); + for (Constructor<?> constructor : clazz.getConstructors()) { + visitor.visitConstructor(constructor, type); + // Assuming the visitClass or visitConstructor will populate the + // type.getConstructors + JavaConstructorImpl<?> definition = type.getConstructors().get(constructor); + if (definition != null) { + for (JavaParameterImpl p : definition.getParameters()) { + visitor.visitConstructorParameter(p, type); + } + } + } + + Set<Field> fields = JavaIntrospectionHelper.getInjectableFields(clazz, true); + for (Field field : fields) { + visitor.visitField(field, type); + } + + Set<Method> methods = JavaIntrospectionHelper.getAllUniquePublicProtectedMethods(clazz, true); + for (Method method : methods) { + visitor.visitMethod(method, type); + } + + // Check if any private methods have illegal annotations that should be raised as errors + Set<Method> privateMethods = JavaIntrospectionHelper.getPrivateMethods(clazz); + for (Method method : privateMethods) { + visitor.visitMethod(method, type); + } + + Class<?> superClass = clazz.getSuperclass(); + if (superClass != null) { + visitSuperClass(superClass, type, visitor); + } + + visitor.visitEnd(clazz, type); + + } + + } + + private void visitSuperClass(Class<?> clazz, JavaImplementation type, JavaClassVisitor visitor) + throws IntrospectionException { + if (!Object.class.equals(clazz)) { + visitor.visitSuperClass(clazz, type); + clazz = clazz.getSuperclass(); + if (clazz != null) { + visitSuperClass(clazz, type, visitor); + } + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaImplementationFactoryImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaImplementationFactoryImpl.java new file mode 100644 index 0000000000..a579a6719c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaImplementationFactoryImpl.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.impl; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; + + +/** + * A factory for the Java model. + * + * @version $Rev$ $Date$ + */ +public abstract class JavaImplementationFactoryImpl implements JavaImplementationFactory { + + private List<JavaClassVisitor> visitors = new ArrayList<JavaClassVisitor>(); + private JavaClassIntrospectorImpl introspector; + private boolean loaded; + protected ExtensionPointRegistry registry; + + public JavaImplementationFactoryImpl(ExtensionPointRegistry registry) { + this.registry = registry; + } + + public JavaImplementation createJavaImplementation() { + JavaImplementation javaImplementation = new JavaImplementationImpl(); + return javaImplementation; + } + + public JavaImplementation createJavaImplementation(Class<?> implementationClass) throws IntrospectionException { + JavaImplementation javaImplementation = createJavaImplementation(); + getIntrospector().introspectClass(javaImplementation, implementationClass); + return javaImplementation; + } + + public void createJavaImplementation(JavaImplementation javaImplementation, Class<?> implementationClass) throws IntrospectionException { + getIntrospector().introspectClass(javaImplementation, implementationClass); + } + + public void addClassVisitor(JavaClassVisitor visitor) { + for (JavaClassVisitor tmpVisitor : visitors){ + if (tmpVisitor.getClass() == visitor.getClass()){ + // trying to add a duplicate visitor so + // ignore it + return; + } + } + visitors.add(visitor); + } + + public void removeClassVisitor(JavaClassVisitor visitor) { + visitors.remove(visitor); + } + + public List<JavaClassVisitor> getClassVisitors() { + loadVisitors(); + return visitors; + } + + /** + * Load visitors declared under META-INF/services + */ + @SuppressWarnings("unchecked") + private synchronized void loadVisitors() { + if (loaded) + return; + + // Get the databinding service declarations + Collection<ServiceDeclaration> visitorDeclarations; + try { + visitorDeclarations = registry.getServiceDiscovery().getServiceDeclarations(JavaClassVisitor.class, true); + } catch (IOException e) { + throw new IllegalStateException(e); + } + + // Load data bindings + for (ServiceDeclaration visitorDeclaration: visitorDeclarations) { + JavaClassVisitor visitor = null; + try { + Class<JavaClassVisitor> visitorClass = (Class<JavaClassVisitor>)visitorDeclaration.loadClass(); + + try { + Constructor<JavaClassVisitor> constructor = visitorClass.getConstructor(ExtensionPointRegistry.class); + visitor = constructor.newInstance(registry); + } catch (NoSuchMethodException e) { + visitor = visitorClass.newInstance(); + } + + + } catch (Exception e) { + IllegalStateException ie = new IllegalStateException(e); + throw ie; + } + + addClassVisitor(visitor); + } + + loaded = true; + } + + private synchronized JavaClassIntrospectorImpl getIntrospector() { + if (introspector != null) { + return introspector; + } + introspector = new JavaClassIntrospectorImpl(getClassVisitors()); + return introspector; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaImplementationImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaImplementationImpl.java new file mode 100644 index 0000000000..5f7885ed5d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/impl/JavaImplementationImpl.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.implementation.java.impl; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.apache.tuscany.sca.implementation.java.JavaScopeImpl; + +/** + * A component type specialization for POJO implementations + * + * @version $Rev$ $Date$ + */ +public class JavaImplementationImpl extends BaseJavaImplementationImpl implements JavaImplementation { + private JavaConstructorImpl<?> constructorDefinition; + private Map<Constructor, JavaConstructorImpl> constructors = new HashMap<Constructor, JavaConstructorImpl>(); + private Method initMethod; + private Method destroyMethod; + private final Map<String, JavaResourceImpl> resources = new HashMap<String, JavaResourceImpl>(); + private final Map<String, JavaElementImpl> propertyMembers = new HashMap<String, JavaElementImpl>(); + private final Map<String, JavaElementImpl> referenceMembers = new HashMap<String, JavaElementImpl>(); + private final Map<String, Collection<JavaElementImpl>> callbackMembers = new HashMap<String, Collection<JavaElementImpl>>(); + private List<Member> conversationIDMember = new ArrayList<Member>(); + private boolean eagerInit; + private boolean allowsPassByReference; + private List<Method> allowsPassByReferenceMethods = new ArrayList<Method>(); + private JavaScopeImpl scope = JavaScopeImpl.STATELESS; + + protected JavaImplementationImpl() { + super(TYPE); + } + + public JavaConstructorImpl<?> getConstructor() { + return constructorDefinition; + } + + public void setConstructor(JavaConstructorImpl<?> definition) { + this.constructorDefinition = definition; + } + + public Method getInitMethod() { + return initMethod; + } + + public void setInitMethod(Method initMethod) { + this.initMethod = initMethod; + } + + public Method getDestroyMethod() { + return destroyMethod; + } + + public void setDestroyMethod(Method destroyMethod) { + this.destroyMethod = destroyMethod; + } + + public Map<String, JavaResourceImpl> getResources() { + return resources; + } + + public List<Member> getConversationIDMembers() { + return this.conversationIDMember; + } + + public void addConversationIDMember(Member conversationIDMember) { + this.conversationIDMember.add(conversationIDMember); + } + + public boolean isAllowsPassByReference() { + return allowsPassByReference; + } + + public void setAllowsPassByReference(boolean allowsPassByReference) { + this.allowsPassByReference = allowsPassByReference; + } + + public List<Method> getAllowsPassByReferenceMethods() { + return allowsPassByReferenceMethods; + } + + public boolean isAllowsPassByReference(Method method) { + return allowsPassByReference || allowsPassByReferenceMethods.contains(method); + } + + public Map<Constructor, JavaConstructorImpl> getConstructors() { + return constructors; + } + + public boolean isEagerInit() { + return eagerInit; + } + + public void setEagerInit(boolean eagerInit) { + this.eagerInit = eagerInit; + } + + public Map<String, Collection<JavaElementImpl>> getCallbackMembers() { + return callbackMembers; + } + + public Map<String, JavaElementImpl> getPropertyMembers() { + return propertyMembers; + } + + public Map<String, JavaElementImpl> getReferenceMembers() { + return referenceMembers; + } + + public JavaScopeImpl getJavaScope() { + return scope; + } + + public void setJavaScope(JavaScopeImpl scope) { + this.scope = scope; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/BaseJavaClassVisitor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/BaseJavaClassVisitor.java new file mode 100644 index 0000000000..26ca54df88 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/BaseJavaClassVisitor.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +/** + * A convenience class for annotation processors which alleviates the need to + * implement unused callbacks + * + * @version $Rev$ $Date$ + */ +public abstract class BaseJavaClassVisitor implements JavaClassVisitor { + protected AssemblyFactory assemblyFactory; + protected JavaInterfaceFactory javaInterfaceFactory; + + protected BaseJavaClassVisitor(AssemblyFactory factory) { + this.assemblyFactory = factory; + } + + protected BaseJavaClassVisitor(ExtensionPointRegistry registry) { + super(); + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = factories.getFactory(AssemblyFactory.class); + this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class); + } + + public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + } + + public <T> void visitSuperClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + } + + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + } + + public <T> void visitConstructor(Constructor<T> constructor, JavaImplementation type) throws IntrospectionException { + } + + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + } + + public <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + } + + public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) throws IntrospectionException { + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/JavaClassVisitor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/JavaClassVisitor.java new file mode 100644 index 0000000000..4b0f5bea55 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/JavaClassVisitor.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; + +/** + * Implementations process class-level metadata, typically parsing annotations + * and updating the corresponding <code>ComponentType</code>. A processor + * may, for example, create a Property which is responsible for injecting a + * complex type on a component implementation instance when it is instantiated. + * <p/> Processors will receive callbacks as the implementation class is walked + * while evaluating an assembly. It is the responsibility of the parser to + * determine whether to perform an action during the callback. + * + * @version $Rev$ $Date$ + */ +public interface JavaClassVisitor { + + /** + * A callback received when the component implementation class is first + * loaded + * + * @param clazz the component implementation class + * @param type the incomplete component type associated with the + * implementation class + * @throws IntrospectionException if an error is encountered while processing + * metadata + */ + <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException; + + /** + * A callback received as the component implementation class hierarchy is + * evaluated + * + * @param clazz the superclass in the component implmentation's class + * hierarchy + * @param type the incomplete component type associated with the + * implementation class + * @throws IntrospectionException if an error is encountered while processing + * metadata + */ + <T> void visitSuperClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException; + + /** + * A callback received as the component implementation's public and + * protected methods are evaluated + * + * @param method the current public or protected method being evaluated + * @param type the incomplete component type associated with the + * implementation class + * @throws IntrospectionException if an error is encountered while processing + * metadata + */ + void visitMethod(Method method, JavaImplementation type) throws IntrospectionException; + + /** + * A callback received as the component implementation's constructor used + * for instantiation by the runtime is evaluated. If an implementation + * contains more than one constructor, the constructor passed to the + * callback will be chosen according to the algorithm described in the SCA + * Java Client and Implementation Model Specification. + * + * @param constructor the constructor used for instantiating component + * implementation instances + * @param type the incomplete component type associated with the + * implementation class + * @throws IntrospectionException if an error is encountered while processing + * metadata + */ + <T> void visitConstructor(Constructor<T> constructor, JavaImplementation type) throws IntrospectionException; + + /** + * @param parameter + * @param type + * @throws IntrospectionException + */ + void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) throws IntrospectionException; + + /** + * A callback received as the component implementation's public and + * protected fields are evaluated + * + * @param field the current public or protected field being evaluated + * @param type the incomplete component type associated with the + * implementation class + * @throws IntrospectionException if an error is encountered while processing + * metadata + */ + void visitField(Field field, JavaImplementation type) throws IntrospectionException; + + /** + * The final callback received when all other callbacks during evaluation of + * the component implementation have been issued + * + * @param clazz the component implementation class + * @param type the incomplete component type associated with the + * implementation class + * @throws IntrospectionException if an error is encountered while processing + * metadata + */ + <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException; + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/JavaIntrospectionHelper.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/JavaIntrospectionHelper.java new file mode 100644 index 0000000000..a04ad3721a --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/JavaIntrospectionHelper.java @@ -0,0 +1,648 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect; + +import java.beans.Introspector; +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.lang.reflect.WildcardType; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.oasisopen.sca.ServiceReference; + +/** + * Implements various reflection-related operations + * + * @version $Rev$ $Date$ + */ +public final class JavaIntrospectionHelper { + private static final Logger logger = Logger.getLogger(JavaIntrospectionHelper.class.getName()); + private static final Class<?>[] EMPTY_CLASS_ARRY = new Class[0]; + + /** + * Hide the constructor + */ + private JavaIntrospectionHelper() { + } + + /** + * Returns a collection of public, and protected fields declared by a class + * or one of its supertypes + */ + public static Set<Field> getAllPublicAndProtectedFields(Class<?> clazz, boolean validating) { + return getAllPublicAndProtectedFields(clazz, new HashSet<Field>(), validating); + } + + private static void checkInvalidAnnotations(AnnotatedElement element) { + for (Annotation a : element.getAnnotations()) { + if (a.annotationType().getName().startsWith("org.oasisopen.sca.annotation.")) { + logger.warning("Invalid annotation " + a + " is found on " + element); + } + } + } + + /** + * Recursively evaluates the type hierarchy to return all fields that are + * public or protected + */ + private static Set<Field> getAllPublicAndProtectedFields(Class<?> clazz, Set<Field> fields, boolean validating) { + if (clazz == null || clazz.isArray() || Object.class.equals(clazz)) { + return fields; + } + fields = getAllPublicAndProtectedFields(clazz.getSuperclass(), fields, validating); + + Field[] declaredFields = null; + + try { + declaredFields = clazz.getDeclaredFields(); + } catch(Throwable t) { + //TUSCANY-3667 - clazz.getDeclaredFields might fail in GAE environment (log and ignore) + logger.log(Level.WARNING, "Error retrieving declared fields from class : " + t.getMessage()); + } + + if( declaredFields != null ) { + for (final Field field : declaredFields) { + int modifiers = field.getModifiers(); + // The field should be non-final and non-static + if ((Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) && !Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers)) { + // Allow privileged access to set accessibility. Requires ReflectPermission + // in security policy. + AccessController.doPrivileged(new PrivilegedAction<Object>() { + public Object run() { + field.setAccessible(true); // ignore Java accessibility + return null; + } + }); + fields.add(field); + } /*else { + if (validating) { + checkInvalidAnnotations(field); + } + }*/ + } + } + return fields; + } + + /** + * Returns a collection of injectable fields declared by a class + * or one of its supertypes + * + * For now we will include final or static fields so that validation problems can be reported + */ + public static Set<Field> getInjectableFields(Class<?> clazz, boolean validating) { + return getInjectableFields(clazz, new HashSet<Field>(), validating); + } + + /** + * Recursively evaluates the type hierarchy to return all fields + */ + private static Set<Field> getInjectableFields(Class<?> clazz, Set<Field> fields, boolean validating) { + if (clazz == null || clazz.isArray() || Object.class.equals(clazz)) { + return fields; + } + + fields = getInjectableFields(clazz.getSuperclass(), fields, validating); + + Field[] declaredFields = null; + + try { + declaredFields = clazz.getDeclaredFields(); + } catch(Throwable t) { + //TUSCANY-3667 - clazz.getDeclaredFields might fail in GAE environment (log and ignore) + logger.log(Level.WARNING, "Error retrieving declared fields from class : " + t.getMessage()); + } + + if( declaredFields != null ) { + for (final Field field : declaredFields) { + int modifiers = field.getModifiers(); + // The field should be non-final and non-static + if (!Modifier.isStatic(modifiers) + // && !Modifier.isFinal(modifiers) + ) { + + // Allow privileged access to set accessibility. Requires ReflectPermission + // in security policy. + AccessController.doPrivileged(new PrivilegedAction<Object>() { + public Object run() { + field.setAccessible(true); // ignore Java accessibility + return null; + } + }); + fields.add(field); + } else { + if (validating) { + checkInvalidAnnotations(field); + } + } + } + } + + + return fields; + } + + /** + * Returns a collection of public and protected methods declared by a class + * or one of its supertypes. Note that overridden methods will not be + * returned in the collection (i.e. only the method override will be). <p/> + * This method can potentially be expensive as reflection information is not + * cached. It is assumed that this method will be used during a + * configuration phase. + */ + public static Set<Method> getAllUniquePublicProtectedMethods(Class<?> clazz, boolean validating) { + return getAllUniqueMethods(clazz, new HashSet<Method>(), validating); + } + + /** + * Recursively evaluates the type hierarchy to return all unique methods + */ + private static Set<Method> getAllUniqueMethods(Class<?> pClass, Set<Method> methods, boolean validating) { + if (pClass == null || pClass.isArray() || Object.class.equals(pClass)) { + return methods; + } + // we first evaluate methods of the subclass and then move to the parent + Method[] declaredMethods = pClass.getDeclaredMethods(); + for (final Method declaredMethod : declaredMethods) { + int modifiers = declaredMethod.getModifiers(); + if ((!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers)) || Modifier.isStatic(modifiers)) { + if (validating) { + checkInvalidAnnotations(declaredMethod); + } + continue; + } + if (methods.size() == 0) { + methods.add(declaredMethod); + } else { + List<Method> temp = new ArrayList<Method>(); + boolean matched = false; + for (Method method : methods) { + // only add if not already in the set from a superclass (i.e. + // the method is not overridden) + if (exactMethodMatch(declaredMethod, method)) { + matched = true; + break; + } + } + if (!matched) { + // Allow privileged access to set accessibility. Requires ReflectPermission + // in security policy. + AccessController.doPrivileged(new PrivilegedAction<Object>() { + public Object run() { + declaredMethod.setAccessible(true); + return null; + } + }); + temp.add(declaredMethod); + } + methods.addAll(temp); + temp.clear(); + } + } + // evaluate class hierarchy - this is done last to track inherited + // methods + methods = getAllUniqueMethods(pClass.getSuperclass(), methods, validating); + return methods; + } + + /** + * Finds the closest matching field with the given name, that is, a field of + * the exact specified type or, alternately, of a supertype. + * + * @param name the name of the field + * @param type the field type + * @param fields the collection of fields to search + * @return the matching field or null if not found + */ + public static Field findClosestMatchingField(String name, Class type, Set<Field> fields) { + Field candidate = null; + for (Field field : fields) { + if (field.getName().equals(name)) { + if (field.getType().equals(type)) { + return field; // exact match + } else if (field.getType().isAssignableFrom(type) || (field.getType().isPrimitive() && primitiveAssignable(field + .getType(), + type))) { + // We could have the situation where a field parameter is a + // primitive and the demarshalled value is + // an object counterpart (e.g. Integer and int) + // @spec issue + // either an interface or super class, so keep a reference + // until + // we know there are no closer types + candidate = field; + } + } + } + if (candidate != null) { + return candidate; + } else { + return null; + } + } + + /** + * Finds the closest matching method with the given name, that is, a method + * taking the exact parameter types or, alternately, parameter supertypes. + * + * @param name the name of the method + * @param types the method parameter types + * @param methods the collection of methods to search + * @return the matching method or null if not found + */ + public static Method findClosestMatchingMethod(String name, Class[] types, Set<Method> methods) { + if (types == null) { + types = EMPTY_CLASS_ARRY; + } + Method candidate = null; + for (Method method : methods) { + if (method.getName().equals(name) && method.getParameterTypes().length == types.length) { + Class<?>[] params = method.getParameterTypes(); + boolean disqualify = false; + boolean exactMatch = true; + for (int i = 0; i < params.length; i++) { + if (!params[i].equals(types[i]) && !params[i].isAssignableFrom(types[i])) { + // no match + disqualify = true; + exactMatch = false; + break; + } else if (!params[i].equals(types[i]) && params[i].isAssignableFrom(types[i])) { + // not exact match + exactMatch = false; + } + } + if (disqualify) { + continue; + } else if (exactMatch) { + return method; + } else { + candidate = method; + } + } + } + if (candidate != null) { + return candidate; + } else { + return null; + } + } + + /** + * Determines if two methods "match" - that is, they have the same method + * names and exact parameter types (one is not a supertype of the other) + */ + public static boolean exactMethodMatch(Method method1, Method method2) { + if (!method1.getName().equals(method2.getName())) { + return false; + } + Class<?>[] types1 = method1.getParameterTypes(); + Class<?>[] types2 = method2.getParameterTypes(); + if (types1.length != types2.length) { + return false; + } + boolean matched = true; + for (int i = 0; i < types1.length; i++) { + if (types1[i] != types2[i]) { + matched = false; + break; + } + } + return matched; + } + + public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws NoSuchMethodException { + return clazz.getConstructor((Class[])null); + } + + /** + * Returns the simple name of a class - i.e. the class name devoid of its + * package qualifier + * + * @param implClass the implementation class + */ + public static String getBaseName(Class<?> implClass) { + return implClass.getSimpleName(); + } + + public static boolean isImmutable(Class<?> clazz) { + return String.class == clazz || clazz.isPrimitive() + || Number.class.isAssignableFrom(clazz) + || Boolean.class.isAssignableFrom(clazz) + || Character.class.isAssignableFrom(clazz) + || Byte.class.isAssignableFrom(clazz); + } + + /** + * Takes a property name and converts it to a getter method name according + * to JavaBean conventions. For example, property + * <code>foo<code> is returned as <code>getFoo</code> + */ + public static String toGetter(String name) { + return "get" + name.toUpperCase().substring(0, 1) + name.substring(1); + } + + /** + * Takes a setter or getter method name and converts it to a property name + * according to JavaBean conventions. For example, <code>setFoo(var)</code> + * is returned as property <code>foo<code> + */ + public static String toPropertyName(String name) { + if (!name.startsWith("set")) { + return name; + } + return Introspector.decapitalize(name.substring(3)); + } + + public static Class<?> getErasure(Type type) { + if (type instanceof Class) { + return (Class<?>)type; + } else if (type instanceof GenericArrayType) { + // FIXME: How to deal with the []? + GenericArrayType arrayType = (GenericArrayType)type; + return getErasure(arrayType.getGenericComponentType()); + } else if (type instanceof ParameterizedType) { + ParameterizedType pType = (ParameterizedType)type; + return getErasure(pType.getRawType()); + } else if (type instanceof WildcardType) { + WildcardType wType = (WildcardType)type; + Type[] types = wType.getUpperBounds(); + return getErasure(types[0]); + } else if (type instanceof TypeVariable) { + TypeVariable var = (TypeVariable)type; + Type[] types = var.getBounds(); + return getErasure(types[0]); + } + return null; + } + + public static Class<?> getBaseType(Class<?> cls, Type genericType) { + if (cls.isArray()) { + return cls.getComponentType(); + } else if (Collection.class.isAssignableFrom(cls)) { + if (genericType instanceof ParameterizedType) { + // Collection<BaseType> + ParameterizedType parameterizedType = (ParameterizedType)genericType; + Type baseType = parameterizedType.getActualTypeArguments()[0]; + if (baseType instanceof GenericArrayType) { + // Base is array + return cls; + } else { + return getErasure(baseType); + } + } else { + return cls; + } + } else { + return cls; + } + } + + public static Type getParameterType(Type type) { + if (type instanceof ParameterizedType) { + // Collection<BaseType> + ParameterizedType parameterizedType = (ParameterizedType)type; + Type baseType = parameterizedType.getActualTypeArguments()[0]; + return baseType; + } else { + return Object.class; + } + } + + public static Class<?> getBusinessInterface(Class<?> cls, Type callableReferenceType) { + if (ServiceReference.class.isAssignableFrom(cls) && callableReferenceType instanceof ParameterizedType) { + // Collection<BaseType> + ParameterizedType parameterizedType = (ParameterizedType)callableReferenceType; + Type baseType = parameterizedType.getActualTypeArguments()[0]; + if (baseType instanceof GenericArrayType) { + // Base is array + return cls; + } else { + return getErasure(baseType); + } + } + return Object.class; + } + + /** + * Takes a property name and converts it to a setter method name according + * to JavaBean conventions. For example, the property + * <code>foo<code> is returned as <code>setFoo(var)</code> + */ + public static String toSetter(String name) { + return "set" + name.toUpperCase().substring(0, 1) + name.substring(1); + } + + /** + * Compares a two types, assuming one is a primitive, to determine if the + * other is its object counterpart + */ + private static boolean primitiveAssignable(Class<?> memberType, Class<?> param) { + if (memberType == Integer.class) { + return param == Integer.TYPE; + } else if (memberType == Double.class) { + return param == Double.TYPE; + } else if (memberType == Float.class) { + return param == Float.TYPE; + } else if (memberType == Short.class) { + return param == Short.TYPE; + } else if (memberType == Character.class) { + return param == Character.TYPE; + } else if (memberType == Boolean.class) { + return param == Boolean.TYPE; + } else if (memberType == Byte.class) { + return param == Byte.TYPE; + } else if (param == Integer.class) { + return memberType == Integer.TYPE; + } else if (param == Double.class) { + return memberType == Double.TYPE; + } else if (param == Float.class) { + return memberType == Float.TYPE; + } else if (param == Short.class) { + return memberType == Short.TYPE; + } else if (param == Character.class) { + return memberType == Character.TYPE; + } else if (param == Boolean.class) { + return memberType == Boolean.TYPE; + } else if (param == Byte.class) { + return memberType == Byte.TYPE; + } else { + return false; + } + } + + /** + * Returns the generic types represented in the given type. Usage as + * follows: <code> + * JavaIntrospectionHelper.getGenerics(field.getGenericType()); + * <p/> + * JavaIntrospectionHelper.getGenerics(m.getGenericParameterTypes()[0];); </code> + * + * @return the generic types in order of declaration or an empty array if + * the type is not genericized + */ + public static List<? extends Type> getGenerics(Type genericType) { + List<Type> classes = new ArrayList<Type>(); + if (genericType instanceof ParameterizedType) { + ParameterizedType ptype = (ParameterizedType)genericType; + // get the type arguments + Type[] targs = ptype.getActualTypeArguments(); + for (Type targ : targs) { + classes.add(targ); + } + } + return classes; + } + + /** + * Returns the generic type specified by the class at the given position as + * in: <p/> <code> public class Foo<Bar,Baz>{ //.. } + * <p/> + * JavaIntrospectionHelper.introspectGeneric(Foo.class,1); <code> + * <p/> + * will return Baz. + */ + public static Class introspectGeneric(Class<?> clazz, int pos) { + assert clazz != null : "No class specified"; + Type type = clazz.getGenericSuperclass(); + if (type instanceof ParameterizedType) { + Type[] args = ((ParameterizedType)type).getActualTypeArguments(); + if (args.length <= pos) { + throw new IllegalArgumentException("Invalid index value for generic class " + clazz.getName()); + } + return (Class)((ParameterizedType)type).getActualTypeArguments()[pos]; + } else { + Type[] interfaces = clazz.getGenericInterfaces(); + for (Type itype : interfaces) { + if (!(itype instanceof ParameterizedType)) { + continue; + } + ParameterizedType interfaceType = (ParameterizedType)itype; + return (Class)interfaceType.getActualTypeArguments()[0]; + } + } + return null; + } + + /** + * Returns the set of interfaces implemented by the given class and its + * ancestors or a blank set if none + */ + public static Set<Class<?>> getAllInterfaces(Class<?> clazz) { + Set<Class<?>> implemented = new HashSet<Class<?>>(); + getAllInterfaces(clazz, implemented); + return implemented; + } + + private static void getAllInterfaces(Class<?> clazz, Set<Class<?>> implemented) { + Class<?>[] interfaces = clazz.getInterfaces(); + for (Class<?> interfaze : interfaces) { + implemented.add(interfaze); + } + Class<?> superClass = clazz.getSuperclass(); + // Object has no superclass so check for null + if (superClass != null && !superClass.equals(Object.class)) { + getAllInterfaces(superClass, implemented); + } + } + + public static boolean isSetter(Method method) { + return (void.class == method.getReturnType() && method.getParameterTypes().length == 1 && method.getName() + .startsWith("set")); + } + + public static boolean isGetter(Method method) { + return (void.class != method.getReturnType() && method.getParameterTypes().length == 0 && method.getName() + .startsWith("get")); + } + + private final static Map<Class<?>, String> signatures = new HashMap<Class<?>, String>(); + static { + signatures.put(boolean.class, "Z"); + signatures.put(byte.class, "B"); + signatures.put(char.class, "C"); + signatures.put(short.class, "S"); + signatures.put(int.class, "I"); + signatures.put(long.class, "J"); + signatures.put(float.class, "F"); + signatures.put(double.class, "D"); + }; + + public static String getSignature(Class<?> cls) { + if (cls.isPrimitive()) { + return signatures.get(cls); + } + if (cls.isArray()) { + return "[" + getSignature(cls.getComponentType()); + } + return "L" + cls.getName().replace('.', '/') + ";"; + } + + public static Class<?> getArrayType(Class<?> componentType, int dims) throws ClassNotFoundException { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < dims; i++) { + buf.append('['); + } + buf.append(getSignature(componentType)); + return Class.forName(buf.toString(), false, componentType.getClassLoader()); + } + + public static Set<Method> getPrivateMethods(Class<?> clazz) { + Set<Method> methods = new HashSet<Method>(); + Method[] declaredMethods = clazz.getDeclaredMethods(); + for (final Method declaredMethod : declaredMethods) { + int modifiers = declaredMethod.getModifiers(); + if(Modifier.isPrivate(modifiers)) { + methods.add(declaredMethod); + } + } + + return methods; + } + + public static Set<Field> getPrivateFields(Class<?> clazz) { + Set<Field> fields = new HashSet<Field>(); + Field[] declaredFields = clazz.getDeclaredFields(); + for (final Field declaredField : declaredFields) { + int modifiers = declaredField.getModifiers(); + if(Modifier.isPrivate(modifiers)) { + fields.add(declaredField); + } + } + + return fields; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java new file mode 100644 index 0000000000..ff70fb89eb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java @@ -0,0 +1,257 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.annotation.Annotation; +import java.lang.annotation.ElementType; +import java.lang.reflect.Field; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +/** + * Base class for ImplementationProcessors that handle annotations that add + * Properties. + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractPropertyProcessor<A extends Annotation> extends BaseJavaClassVisitor { + private final Class<A> annotationClass; + + protected AbstractPropertyProcessor(ExtensionPointRegistry registry, Class<A> annotationClass) { + super(registry); + this.annotationClass = annotationClass; + } + + private static boolean removeProperty(JavaElementImpl prop, JavaImplementation type) { + if(prop==null) { + return false; + } + List<Property> props = type.getProperties(); + for(int i=0;i<props.size();i++) { + if(props.get(i).getName().equals(prop.getName())) { + props.remove(i); + return true; + } + } + return false; + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + A annotation = method.getAnnotation(annotationClass); + if (annotation != null) { + + if (!JavaIntrospectionHelper.isSetter(method)) { + throw new IllegalPropertyException("Annotated method is not a setter: " + method, method); + } + + if(Modifier.isStatic(method.getModifiers())) { + throw new IllegalPropertyException("Static method " + method.getName() +" in class " + method.getDeclaringClass().getName() + " can not be annotated as a Property"); + } + + String name = getName(annotation); + if (name == null || "".equals(name)) { + name = method.getName(); + if (name.startsWith("set")) { + name = JavaIntrospectionHelper.toPropertyName(method.getName()); + } + } + + Map<String, JavaElementImpl> properties = type.getPropertyMembers(); + JavaElementImpl prop = properties.get(name); + // Setter override field + if (prop != null && prop.getElementType() != ElementType.FIELD) { + throw new DuplicatePropertyException(name); + } + + removeProperty(prop, type); + + JavaElementImpl element = new JavaElementImpl(method, 0); + Property property = createProperty(name, element); + + // add databinding available as annotations, as extensions + + initProperty(property, annotation); + type.getProperties().add(property); + properties.put(name, element); + } + + // enforce the constraint that an ordinary method's argument can not be a Property + Annotation paramsAnnotations[][] = method.getParameterAnnotations(); + for (int i = 0; i < paramsAnnotations.length; i++) { + Annotation argAnnotations[] = paramsAnnotations[i]; + for (int j = 0; j < argAnnotations.length; j++) { + if(argAnnotations[j].annotationType() == org.oasisopen.sca.annotation.Property.class) { + throw new IllegalPropertyException("[JCA90001] Argument " + (i+1) + " of method " + method.getName() + " in class " + method.getDeclaringClass() + " can not be a Property"); + } + } + } + } + + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + + A annotation = field.getAnnotation(annotationClass); + if (annotation == null) { + return; + } + + if(Modifier.isStatic(field.getModifiers())) { + throw new IllegalPropertyException("Static field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as a Property"); + } + + if(Modifier.isFinal(field.getModifiers())) { + throw new IllegalPropertyException("[JCA90011] Final field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as a Property"); + } + + String name = getName(annotation); + if (name == null) { + name = ""; + } + if ("".equals(name) || name.equals(field.getType().getName())) { + name = field.getName(); + } + + Map<String, JavaElementImpl> properties = type.getPropertyMembers(); + JavaElementImpl prop = properties.get(name); + // Setter override field + if (prop != null && prop.getElementType() == ElementType.FIELD) { + throw new DuplicatePropertyException(name); + } + + if (prop == null) { + JavaElementImpl element = new JavaElementImpl(field); + Property property = createProperty(name, element); + initProperty(property, annotation); + type.getProperties().add(property); + properties.put(name, element); + } + } + + @Override + public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) + throws IntrospectionException { + + Map<String, JavaElementImpl> properties = type.getPropertyMembers(); + A annotation = parameter.getAnnotation(annotationClass); + if (annotation != null) { + String name = getName(annotation); + if (name == null) { + name = parameter.getType().getName(); + } + if (!"".equals(name) && !"".equals(parameter.getName()) && !name.equals(parameter.getName())) { + throw new InvalidConstructorException("Mismatched property name: " + parameter); + } + if ("".equals(name) && "".equals(parameter.getName())) { + throw new InvalidPropertyException("[JCA90013] Missing property name: " + parameter); + } + if ("".equals(name)) { + name = parameter.getName(); + } + + if (!getRequired(annotation)) { + throw new InvalidPropertyException("[JCA90014] Constructor property must not have required=false: " + type.getName()); + } + + JavaElementImpl prop = properties.get(name); + // Setter override field + if (prop != null && prop.getElementType() != ElementType.FIELD) { + throw new DuplicatePropertyException(name); + } + removeProperty(prop, type); + + parameter.setName(name); + parameter.setClassifer(annotationClass); + Property property = createProperty(name, parameter); + initProperty(property, annotation); + type.getProperties().add(property); + properties.put(name, parameter); + } + } + + protected Property createProperty(String name, JavaElementImpl element) throws IntrospectionException { + + Type type = element.getGenericType(); + Class<?> javaType = element.getType(); + + return createProperty(assemblyFactory, name, javaType, type); + + } + + public static Property createProperty(AssemblyFactory assemblyFactory, String name, Class<?> javaClass, Type genericType) { + Property property = assemblyFactory.createProperty(); + property.setName(name); + + if (javaClass.isArray() || Collection.class.isAssignableFrom(javaClass)) { + property.setMany(true); + if (javaClass.isArray()) { + Class<?> propType = javaClass.getComponentType(); + Type genericPropType = propType; + if (genericType instanceof GenericArrayType) { + genericPropType = ((GenericArrayType)genericType).getGenericComponentType(); + } + DataType dt = new DataTypeImpl(null, propType, genericPropType, XMLType.UNKNOWN); + property.setDataType(dt); + } else { + if (genericType instanceof ParameterizedType) { + // Collection<BaseType> property; + Type genericPropType = ((ParameterizedType)genericType).getActualTypeArguments()[0]; + Class<?> propType = JavaIntrospectionHelper.getErasure(genericPropType); + DataType dt = new DataTypeImpl(null, propType, genericPropType, XMLType.UNKNOWN); + property.setDataType(dt); + } else { + // Collection property; + DataType dt = new DataTypeImpl(null, Object.class, Object.class, XMLType.UNKNOWN); + property.setDataType(dt); + } + } + } else { + DataType dt = new DataTypeImpl(null, javaClass, genericType, XMLType.UNKNOWN); + property.setDataType(dt); + } + return property; + } + + protected abstract String getName(A annotation); + protected abstract boolean getRequired(A annotation); + + protected abstract void initProperty(Property property, A annotation) throws IntrospectionException; + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessor.java new file mode 100644 index 0000000000..4aa29d3b91 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessor.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.oasisopen.sca.annotation.AllowsPassByReference; + +/** + * Processes {@link AllowsPassByReference} on an implementation + * + * @version $Rev$ $Date$ + */ +public class AllowsPassByReferenceProcessor extends BaseJavaClassVisitor { + + public AllowsPassByReferenceProcessor(AssemblyFactory factory) { + super(factory); + } + + public AllowsPassByReferenceProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + type.setAllowsPassByReference(clazz.isAnnotationPresent(AllowsPassByReference.class)); + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + boolean pbr = method.isAnnotationPresent(AllowsPassByReference.class); + if (pbr) { + type.getAllowsPassByReferenceMethods().add(method); + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AmbiguousConstructorException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AmbiguousConstructorException.java new file mode 100644 index 0000000000..c6e6b6e4d6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AmbiguousConstructorException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when constructor parameters cannot be unambiguously resolved to a property or reference + * + * @version $Rev$ $Date$ + */ +public class AmbiguousConstructorException extends IntrospectionException { + private static final long serialVersionUID = 3662860753837091880L; + + public AmbiguousConstructorException(String message) { + super(message); + } + + public AmbiguousConstructorException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ComponentNameProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ComponentNameProcessor.java new file mode 100644 index 0000000000..1813a177b4 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ComponentNameProcessor.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.oasisopen.sca.annotation.ComponentName; + +/** + * Processes {@link @ComponentName} annotations on a component implementation and adds + * a {@link JavaMappedProperty} to the component type which will be used to + * inject the appropriate component name. + * + * @version $Rev$ $Date$ + */ +public class ComponentNameProcessor extends BaseJavaClassVisitor { + + public ComponentNameProcessor(AssemblyFactory factory) { + super(factory); + } + + public ComponentNameProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + if (method.getAnnotation(ComponentName.class) == null) { + return; + } + if (method.getParameterTypes().length != 1) { + throw new IllegalContextException("ComponentName setter must have one parameter", method); + } + Class<?> paramType = method.getParameterTypes()[0]; + String name = JavaIntrospectionHelper.toPropertyName(method.getName()); + if (String.class.equals(paramType)) { + JavaElementImpl element = new JavaElementImpl(method, 0); + element.setName(name); + element.setClassifer(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); + JavaResourceImpl resource = new JavaResourceImpl(element); + type.getResources().put(resource.getName(), resource); + } else { + throw new IllegalContextException(paramType.getName()); + } + } + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + if (field.getAnnotation(ComponentName.class) == null) { + return; + } + Class<?> paramType = field.getType(); + if (String.class.equals(paramType)) { + JavaElementImpl element = new JavaElementImpl(field); + element.setClassifer(Resource.class); + JavaResourceImpl resource = new JavaResourceImpl(element); + type.getResources().put(resource.getName(), resource); + } else { + throw new IllegalContextException(paramType.getName()); + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessor.java new file mode 100644 index 0000000000..52a3fbd89a --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessor.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; + +/** + * Handles processing of a constructor decorated with + * {@link org.oasisopen.sca.annotation.Constructor} + * + * @version $Rev$ $Date$ + */ +@SuppressWarnings("unchecked") +public class ConstructorProcessor extends BaseJavaClassVisitor { + + public ConstructorProcessor(AssemblyFactory factory) { + super(factory); + } + + public ConstructorProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + Constructor[] ctors = clazz.getConstructors(); + boolean found = false; + for (Constructor constructor : ctors) { + JavaConstructorImpl<?> definition = new JavaConstructorImpl(constructor); + type.getConstructors().put(constructor, definition); + if (constructor.getAnnotation(org.oasisopen.sca.annotation.Constructor.class) != null) { + if (found) { + throw new DuplicateConstructorException("[JCI50002] Multiple constructors marked with @Constructor", constructor); + } + found = true; + type.setConstructor(definition); + } + } + } + + @Override + public <T> void visitConstructor(Constructor<T> constructor, JavaImplementation type) + throws IntrospectionException { + org.oasisopen.sca.annotation.Constructor annotation = constructor + .getAnnotation(org.oasisopen.sca.annotation.Constructor.class); + if (annotation == null) { + return; + } + JavaConstructorImpl<?> definition = type.getConstructor(); + if (definition == null) { + definition = new JavaConstructorImpl(constructor); + type.setConstructor(definition); + } + JavaParameterImpl[] parameters = definition.getParameters(); + String[] value = annotation.value(); + boolean isDefault = value.length == 0 || (value.length == 1 && "".equals(value[0])); + if (!isDefault && value.length != parameters.length) { + throw new InvalidConstructorException("Invalid Number of names in @Constructor"); + } + + for (JavaParameterImpl p : parameters) { + if (!hasAnnotation(p)) { + throw new InvalidConstructorException("JCA90003 constructor parameters for class " + type.getName() + " must have @Property or @Reference annotation"); + } + } + + for (int i = 0; i < parameters.length; i++) { + parameters[i].setName(i < value.length ? value[i] : ""); + } + type.setConstructor(definition); + } + + private boolean hasAnnotation(JavaParameterImpl p) { + if (p.getAnnotations() != null && p.getAnnotations().length > 0) { + return true; + } +// TODO: need to verify JCA90003 as it seems like any annotation should be ok not just SCA ref or prop +// if (p.getAnnotation(Reference.class) != null) { +// return true; +// } +// if (p.getAnnotation(Property.class) != null) { +// return true; +// } + return false; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessor.java new file mode 100644 index 0000000000..651ecdbd7e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessor.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.oasisopen.sca.ComponentContext; +import org.oasisopen.sca.RequestContext; +import org.oasisopen.sca.annotation.Context; + +/** + * Processes {@link @Context} annotations on a component implementation and adds + * a {@link JavaMappedProperty} to the component type which will be used to + * inject the appropriate context + * + * @version $Rev$ $Date$ + */ +public class ContextProcessor extends BaseJavaClassVisitor { + + public ContextProcessor(AssemblyFactory factory) { + super(factory); + } + + public ContextProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + if (method.getAnnotation(Context.class) == null) { + return; + } + if (method.getParameterTypes().length != 1) { + throw new IllegalContextException("Context setter must have one parameter", method); + } + Class<?> paramType = method.getParameterTypes()[0]; + String name = JavaIntrospectionHelper.toPropertyName(method.getName()); + if (ComponentContext.class.equals(paramType) || RequestContext.class.equals(paramType)) { + JavaElementImpl element = new JavaElementImpl(method, 0); + element.setName(name); + element.setClassifer(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); + JavaResourceImpl resource = new JavaResourceImpl(element); + type.getResources().put(resource.getName(), resource); + } else { + throw new UnknownContextTypeException(paramType.getName()); + } + } + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + if (field.getAnnotation(Context.class) == null) { + return; + } + Class<?> paramType = field.getType(); + if (ComponentContext.class.equals(paramType) || RequestContext.class.equals(paramType)) { + JavaElementImpl element = new JavaElementImpl(field); + element.setClassifer(Resource.class); + JavaResourceImpl resource = new JavaResourceImpl(element); + type.getResources().put(resource.getName(), resource); + } else { + throw new UnknownContextTypeException(paramType.getName()); + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessor.java new file mode 100644 index 0000000000..a4b760760c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessor.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.oasisopen.sca.annotation.Destroy; + +/** + * Processes the {@link @Destroy} annotation on a component implementation and + * updates the component type with the decorated destructor method + * + * @version $Rev$ $Date$ + */ +public class DestroyProcessor extends BaseJavaClassVisitor { + + public DestroyProcessor(AssemblyFactory factory) { + super(factory); + } + + public DestroyProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + Destroy annotation = method.getAnnotation(Destroy.class); + if (annotation == null) { + return; + } + if (method.getParameterTypes().length != 0) { + throw new IllegalDestructorException("[JCA90004] Destructor must not have arguments", method); + } + if(!method.getReturnType().equals(void.class)) { + throw new IllegalDestructorException("Destructor must return void.", method); + } + if (type.getDestroyMethod() != null) { + throw new DuplicateDestructorException("More than one destructor found on implementation"); + } + type.setDestroyMethod(method); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateConstructorException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateConstructorException.java new file mode 100644 index 0000000000..d6494e3845 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateConstructorException.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.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when more than one component implementation constructor is annotated with {@link + * org.oasisopen.sca.annotation.Constructor} + * + * @version $Rev$ $Date$ + */ +public class DuplicateConstructorException extends IntrospectionException { + private static final long serialVersionUID = -5926763756570552986L; + + public DuplicateConstructorException(String message) { + super(message); + } + + public DuplicateConstructorException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateDestructorException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateDestructorException.java new file mode 100644 index 0000000000..8bfae89162 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateDestructorException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when an implementation is annotated multiple times with {@link org.oasisopen.sca.annotation.Destroy} + * + * @version $Rev$ $Date$ + */ +public class DuplicateDestructorException extends IntrospectionException { + private static final long serialVersionUID = -7474912510114895203L; + + public DuplicateDestructorException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateInitException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateInitException.java new file mode 100644 index 0000000000..38f6478531 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateInitException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when an implementation is annotated multiple times with {@link @org.oasisopen.sca.annotation.Init} + * + * @version $Rev$ $Date$ + */ +public class DuplicateInitException extends IntrospectionException { + private static final long serialVersionUID = -6282935288115512057L; + + public DuplicateInitException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicatePropertyException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicatePropertyException.java new file mode 100644 index 0000000000..b9e1923e73 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicatePropertyException.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when an implementation has more than one property injection site with the same name + * + * @version $Rev$ $Date$ + */ +public class DuplicatePropertyException extends IntrospectionException { + private static final long serialVersionUID = 5536415875694904037L; + + public DuplicatePropertyException(String message) { + super(message); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateReferenceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateReferenceException.java new file mode 100644 index 0000000000..300ad6817e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateReferenceException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when an implementation has more than one reference injection site with the same name + * + * @version $Rev$ $Date$ + */ +public class DuplicateReferenceException extends IntrospectionException { + private static final long serialVersionUID = 907910648213477158L; + + public DuplicateReferenceException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateResourceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateResourceException.java new file mode 100644 index 0000000000..2d177b3f76 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateResourceException.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when an implementation has more than one resource injection site with the same name + * + * @version $Rev$ $Date$ + */ +public class DuplicateResourceException extends IntrospectionException { + + private static final long serialVersionUID = 1619276459330463299L; + + public DuplicateResourceException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessor.java new file mode 100644 index 0000000000..5c0e2c1397 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessor.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.oasisopen.sca.annotation.EagerInit; + +/** + * Handles processing of {@link org.oasisopen.sca.annotation.EagerInit} + * + * @version $Rev$ $Date$ + */ +public class EagerInitProcessor extends BaseJavaClassVisitor { + + public EagerInitProcessor(AssemblyFactory factory) { + super(factory); + } + + public EagerInitProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public <T> void visitClass(Class<T> clazz, + JavaImplementation type) throws IntrospectionException { + super.visitClass(clazz, type); + EagerInit annotation = clazz.getAnnotation(EagerInit.class); + if (annotation == null) { + Class<?> superClass = clazz.getSuperclass(); + while (superClass != null && !Object.class.equals(superClass)) { + annotation = superClass.getAnnotation(EagerInit.class); + if (annotation != null) { + break; + } + superClass = superClass.getSuperclass(); + } + if (annotation == null) { + return; + } + } + type.setEagerInit(true); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessor.java new file mode 100644 index 0000000000..19d3992781 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessor.java @@ -0,0 +1,758 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getAllInterfaces; +import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getAllPublicAndProtectedFields; +import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getAllUniquePublicProtectedMethods; +import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getBaseType; +import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.toPropertyName; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.jws.WebService; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.ComponentName; +import org.oasisopen.sca.annotation.Context; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Remotable; + +/** + * Heuristically evaluates an un-annotated Java implementation type to determine + * services, references, and properties according to the algorithm described in + * the SCA Java Client and Implementation Model Specification <p/> TODO + * Implement: <p/> When no service interface is annotated, need to calculate a + * single service comprising all public methods that are not reference or + * property injection sites. If that service can be exactly mapped to an + * interface implemented by the class then the service interface will be defined + * in terms of that interface. + * + * @version $Rev$ $Date$ + */ +public class HeuristicPojoProcessor extends BaseJavaClassVisitor { + + public HeuristicPojoProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) { + super(assemblyFactory); + this.javaInterfaceFactory = javaFactory; + } + + public HeuristicPojoProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + List<org.apache.tuscany.sca.assembly.Service> services = type.getServices(); + if (services.isEmpty()) { + // heuristically determine the service + /** + * The following is quoted from Java Specification 1.2.1.3. Introspecting services offered by a Java implementation + * In the cases described below, the services offered by a Java implementation class may be determined + * through introspection, eliding the need to specify them using @Service. The following algorithm is used + * to determine how services are introspected from an implementation class: + * + * If the interfaces of the SCA services are not specified with the @Service annotation on the + * implementation class, it is assumed that all implemented interfaces that have been annotated + * as @Remotable are the service interfaces provided by the component. If none of the implemented + * interfaces is remotable, then by default the implementation offers a single service whose type + * is the implementation class. + */ + Set<Class<?>> interfaces = getAllInterfaces(clazz); + for (Class<?> i : interfaces) { + if (i.isAnnotationPresent(Remotable.class) || i.isAnnotationPresent(WebService.class)) { + addService(type, i); + } + } + if (services.isEmpty()) { + // class is the interface + addService(type, clazz); + } + } + if (!(type.getReferenceMembers().isEmpty() && type.getPropertyMembers().isEmpty())) { + // references and properties have been explicitly defined + // if (type.getServices().isEmpty()) { + // calculateServiceInterface(clazz, type, methods); + // if (type.getServices().isEmpty()) { + // throw new ServiceTypeNotFoundException(clazz.getName()); + // } + // } + evaluateConstructor(type, clazz); + return; + } + Set<Method> methods = getAllUniquePublicProtectedMethods(clazz, false); + + calcPropRefs(methods, services, type, clazz); + evaluateConstructor(type, clazz); + } + + // Check if the implementation is implementation.java + private boolean isImplementationJava(JavaImplementation type) { + return JavaImplementation.TYPE.equals(type.getType()); + } + + private void addService(JavaImplementation type, Class<?> clazz) throws IntrospectionException { + try { + org.apache.tuscany.sca.assembly.Service service = createService(clazz); + type.getServices().add(service); + } catch (InvalidInterfaceException e) { + throw new IntrospectionException(e); + } + } + + private static boolean isAnnotatedWithSCA(AnnotatedElement element) { + for (Annotation a : element.getAnnotations()) { + // JCI_8023 + // policy annotations can be added to reference fields that + // don't have @Reference annotations so we need to allow + // for the fields to be detected as references + if (isSCAPolicyAnnotation(a)){ + continue; + } else if (isSCAAnnotation(a)) { + return true; + } + } + return false; + } + + private static boolean isSCAAnnotation(Annotation a) { + return a.annotationType().getName().startsWith("org.oasisopen.sca.annotation."); + } + + private static boolean isSCAPolicyAnnotation(Annotation a) { + if (a.annotationType().getName().startsWith("org.oasisopen.sca.annotation.PolicySets") ){ + return true; + } else if (a.annotationType().getName().startsWith("org.oasisopen.sca.annotation.Intent") ){ + return true; + } else { + return false; + } + } + + private <T> void calcPropRefs(Set<Method> methods, + List<org.apache.tuscany.sca.assembly.Service> services, + JavaImplementation type, + Class<T> clazz) throws IntrospectionException { + // heuristically determine the properties references + // make a first pass through all public methods with one param + Set<String> setters = new HashSet<String>(); + Set<String> others = new HashSet<String>(); + for (Method method : methods) { + if (!isPublicSetter(method)) { + continue; + } + if (isAnnotatedWithSCA(method)) { + // Add the property name as others + others.add(toPropertyName(method.getName())); + continue; + } + if (!isInServiceInterface(method, services)) { + // Not part of the service interface + String name = toPropertyName(method.getName()); + setters.add(name); + // avoid duplicate property or ref names + if (!type.getPropertyMembers().containsKey(name) && !type.getReferenceMembers().containsKey(name)) { + Class<?> param = method.getParameterTypes()[0]; + Type genericType = method.getGenericParameterTypes()[0]; + if (isReferenceType(param, genericType)) { + type.getReferences().add(createReference(name, param, genericType)); + type.getReferenceMembers().put(name, new JavaElementImpl(method, 0)); + } else { + type.getProperties().add(createProperty(name, param, genericType)); + type.getPropertyMembers().put(name, new JavaElementImpl(method, 0)); + } + } + } + } + // second pass for protected methods with one param + for (Method method : methods) { + if (!isProtectedSetter(method)) { + continue; + } + if (isAnnotatedWithSCA(method)) { + // Add the property name as others + others.add(toPropertyName(method.getName())); + continue; + } + Class<?> param = method.getParameterTypes()[0]; + Type paramType = method.getGenericParameterTypes()[0]; + + String name = toPropertyName(method.getName()); + setters.add(name); + // avoid duplicate property or ref names + if (isReferenceType(param, method.getGenericParameterTypes()[0])) { + if (!type.getReferenceMembers().containsKey(name)) { + type.getReferences().add(createReference(name, param, paramType)); + type.getReferenceMembers().put(name, new JavaElementImpl(method, 0)); + } + } else { + if (!type.getPropertyMembers().containsKey(name)) { + type.getProperties().add(createProperty(name, param, paramType)); + type.getPropertyMembers().put(name, new JavaElementImpl(method, 0)); + } + } + } + + // Public or protected fields unless there is a public or protected + // setter method + // for the same name + Set<Field> fields = getAllPublicAndProtectedFields(clazz, false); + for (Field field : fields) { + if (isAnnotatedWithSCA(field)) { + continue; + } + if (setters.contains(field.getName()) || others.contains(field.getName())) { + continue; + } + String name = field.getName(); + Class<?> paramType = field.getType(); + Type genericType = field.getGenericType(); + if (isReferenceType(paramType, field.getGenericType())) { + if (!type.getReferenceMembers().containsKey(name)) { + type.getReferences().add(createReference(name, paramType, genericType)); + type.getReferenceMembers().put(name, new JavaElementImpl(field)); + } + } else { + if (!type.getPropertyMembers().containsKey(name)) { + type.getProperties().add(createProperty(name, paramType, genericType)); + type.getPropertyMembers().put(name, new JavaElementImpl(field)); + } + } + } + } + + /** + * Determines the constructor to use based on the component type's + * references and properties + * + * @param type the component type + * @param clazz the implementation class corresponding to the component type + * @throws NoConstructorException if no suitable constructor is found + * @throws AmbiguousConstructorException if the parameters of a constructor + * cannot be unambiguously mapped to references and properties + */ + @SuppressWarnings("unchecked") + private <T> void evaluateConstructor(JavaImplementation type, Class<T> clazz) throws IntrospectionException { + // determine constructor if one is not annotated + JavaConstructorImpl<?> definition = type.getConstructor(); + Constructor constructor; + boolean explict = false; + if (definition != null && definition.getConstructor() + .isAnnotationPresent(org.oasisopen.sca.annotation.Constructor.class)) { + // the constructor was already defined explicitly + return; + } else if (definition != null) { + explict = true; + constructor = definition.getConstructor(); + } else { + if (!isImplementationJava(type)) { + // FIXME: [rfeng] Don't process the constructors for non implementation.java types + return; + } + // no definition, heuristically determine constructor + Constructor[] constructors = clazz.getConstructors(); + if (constructors.length == 0) { + throw new NoConstructorException("[JCI50001] No public constructor for class :" + type.getName()); + } else if (constructors.length == 1) { + // Only one constructor, take it + constructor = constructors[0]; + } else { + Constructor<T> selected = null; + for (Constructor<T> ctor : constructors) { + if (allArgsAnnotated(ctor)) { + selected = ctor; + for (Constructor<T> ctor2 : constructors) { + if (selected != ctor2 && allArgsAnnotated(ctor2)) { + throw new InvalidConstructorException("[JCI50005] Multiple annotated constructors for class :" + type.getName()); + } + } + } + } + if (selected == null) { + for (Constructor<T> ctor : constructors) { + if (ctor.getParameterTypes().length == 0) { + selected = ctor; + break; + } + } + } + if (selected == null) { + throw new NoConstructorException(); + } + constructor = selected; + definition = type.getConstructors().get(selected); + type.setConstructor(definition); + } + definition = type.getConstructors().get(constructor); + type.setConstructor(definition); + } + JavaParameterImpl[] parameters = definition.getParameters(); + if (parameters.length == 0) { + return; + } + Map<String, JavaElementImpl> props = type.getPropertyMembers(); + Map<String, JavaElementImpl> refs = type.getReferenceMembers(); + Annotation[][] annotations = constructor.getParameterAnnotations(); + if (!explict) { + // the constructor wasn't defined by an annotation, so check to see + // if any of the params have an annotation + // which we can impute as explicitly defining the constructor, e.g. + // @Property, @Reference, or @Autowire + explict = injectionAnnotationsPresent(annotations); + } + if (explict) { + for (int i = 0; i < parameters.length; i++) { + if (isAnnotated(parameters[i])) { + continue; + } else if (!findReferenceOrProperty(parameters[i], props, refs)) { + throw new AmbiguousConstructorException(parameters[i].toString()); + } + } + } else { + if (!areUnique(parameters)) { + throw new AmbiguousConstructorException("Cannot resolve non-unique parameter types, use @Constructor"); + } + if (!calcPropRefUniqueness(props.values(), refs.values())) { + throw new AmbiguousConstructorException("Cannot resolve non-unique parameter types, use @Constructor"); + } + if (!(props.isEmpty() && refs.isEmpty())) { + calcParamNames(parameters, props, refs); + } else { + heuristicParamNames(type, parameters); + + } + } + } + + private boolean allArgsAnnotated(Constructor<?> ctor) { + if (ctor.getParameterTypes().length < 1) { + return false; + } + for (Annotation[] as : ctor.getParameterAnnotations()) { + if (as.length < 1) { + return false; + } + } + return true; + } + + private void calcParamNames(JavaParameterImpl[] parameters, + Map<String, JavaElementImpl> props, + Map<String, JavaElementImpl> refs) throws AmbiguousConstructorException { + // the constructor param types must unambiguously match defined + // reference or property types + for (JavaParameterImpl param : parameters) { + if (!findReferenceOrProperty(param, props, refs)) { + throw new AmbiguousConstructorException(param.getName()); + } + } + } + + private void heuristicParamNames(JavaImplementation type, JavaParameterImpl[] parameters) + throws IntrospectionException { + // heuristically determine refs and props from the parameter types + for (JavaParameterImpl p : parameters) { + String name = p.getType().getSimpleName().toLowerCase(); + if (isReferenceType(p.getType(), p.getGenericType())) { + type.getReferences().add(createReference(name, p.getType(), p.getGenericType())); + p.setClassifer(Reference.class); + type.getReferenceMembers().put(name, p); + } else { + type.getProperties().add(createProperty(name, p.getType(), p.getGenericType())); + p.setClassifer(Property.class); + type.getPropertyMembers().put(name, p); + } + p.setName(name); + } + } + + + + /** + * Returns true if the union of the given collections of properties and + * references have unique Java types + */ + private boolean calcPropRefUniqueness(Collection<JavaElementImpl> props, Collection<JavaElementImpl> refs) { + + Class<?>[] classes = new Class[props.size() + refs.size()]; + int i = 0; + for (JavaElementImpl property : props) { + classes[i] = property.getType(); + i++; + } + for (JavaElementImpl reference : refs) { + classes[i] = reference.getType(); + i++; + } + return areUnique(classes); + } + + /** + * Unambiguously finds the reference or property associated with the given + * type + * + * @return the name of the reference or property if found, null if not + * @throws AmbiguousConstructorException if the constructor parameter cannot + * be resolved to a property or reference + */ + private boolean findReferenceOrProperty(JavaParameterImpl parameter, + Map<String, JavaElementImpl> props, + Map<String, JavaElementImpl> refs) throws AmbiguousConstructorException { + + boolean found = false; + if (!"".equals(parameter.getName())) { + // Match by name + JavaElementImpl prop = props.get(parameter.getName()); + if (prop != null && prop.getType() == parameter.getType()) { + parameter.setClassifer(Property.class); + return true; + } + JavaElementImpl ref = refs.get(parameter.getName()); + if (ref != null && ref.getType() == parameter.getType()) { + parameter.setClassifer(Reference.class); + return true; + } + } + for (JavaElementImpl property : props.values()) { + if (property.getType() == parameter.getType()) { + if (found) { + throw new AmbiguousConstructorException("Ambiguous property or reference for constructor type", + (Member)parameter.getAnchor()); + } + parameter.setClassifer(Property.class); + parameter.setName(property.getName()); + found = true; + // do not break since ambiguities must be checked, i.e. more + // than one prop or ref of the same type + } + } + for (JavaElementImpl reference : refs.values()) { + if (reference.getType() == parameter.getType()) { + if (found) { + throw new AmbiguousConstructorException("Ambiguous property or reference for constructor type", + (Member)parameter.getAnchor()); + } + parameter.setClassifer(Reference.class); + parameter.setName(reference.getName()); + found = true; + // do not break since ambiguities must be checked, i.e. more + // than one prop or ref of the same type + } + } + return found; + } + + /** + * Creates a mapped property. + * + * @param name the property name + * @param paramType the property type + */ + private org.apache.tuscany.sca.assembly.Property createProperty(String name, Class<?> javaClass, Type genericType) { + return AbstractPropertyProcessor.createProperty(assemblyFactory, name, javaClass, genericType); + } + + private org.apache.tuscany.sca.assembly.Reference createReference(String name, Class<?> paramType, + Type genericType ) + throws IntrospectionException { + org.apache.tuscany.sca.assembly.Reference reference = assemblyFactory.createReference(); + reference.setName(name); + JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + reference.setInterfaceContract(interfaceContract); + // Compute the interface type dealing with array/Collection types and with Generic types + Class<?> baseType = getBaseType(paramType, genericType); + if (ServiceReference.class.isAssignableFrom(baseType)) { + if (Collection.class.isAssignableFrom(paramType)) { + genericType = JavaIntrospectionHelper.getParameterType(genericType); + } + baseType = JavaIntrospectionHelper.getBusinessInterface(baseType, genericType); + } // end if + try { + JavaInterface callInterface = javaInterfaceFactory.createJavaInterface(baseType); + + reference.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaInterfaceFactory.createJavaInterface(callInterface.getCallbackClass()); + reference.getInterfaceContract().setCallbackInterface(callbackInterface); + } + // Multiplicity of an implicit reference is 1..1 for a simple interface type + // and 1..n for an array of interface or Collection of interface type + // as defined in the OASIS SCA Java POJO specification, section 8.1 : TUSCANY-3636 + if( isCollectionType( paramType ) || isArrayType( paramType )) { + reference.setMultiplicity(Multiplicity.ONE_N); + } else { + reference.setMultiplicity(Multiplicity.ONE_ONE); + } // end if + } catch (InvalidInterfaceException e1) { + throw new IntrospectionException(e1); + } // end try + + // FIXME: This part seems to have already been taken care above!! + try { + processCallback(paramType, reference); + } catch (InvalidServiceTypeException e) { + throw new IntrospectionException(e); + } + return reference; + } + + /** + * Reports if a parameter type is a form of java.util.Collection + * @param paramType + * @return true if paramType is a form of java.util.Collection, false otherwise + */ + private boolean isCollectionType( Class<?> paramType ) { + if( Collection.class.isAssignableFrom(paramType) ) return true; + return false; + } + + /** + * Reports if a parameter type is an array + * @param paramType + * @return true if paramType is an array + */ + private boolean isArrayType( Class<?> paramType ) { + if( paramType.isArray() ) return true; + return false; + } + + private org.apache.tuscany.sca.assembly.Service createService(Class<?> interfaze) throws InvalidInterfaceException { + org.apache.tuscany.sca.assembly.Service service = assemblyFactory.createService(); + service.setName(interfaze.getSimpleName()); + + JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + service.setInterfaceContract(interfaceContract); + + JavaInterface callInterface = javaInterfaceFactory.createJavaInterface(interfaze); + service.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaInterfaceFactory.createJavaInterface(callInterface.getCallbackClass()); + service.getInterfaceContract().setCallbackInterface(callbackInterface); + } + + Interface javaInterface = service.getInterfaceContract().getInterface(); + javaInterface.setRemotable(interfaze.getAnnotation(Remotable.class) != null); + service.getInterfaceContract().setInterface(javaInterface); + return service; + } + + private void processCallback(Class<?> interfaze, Contract contract) throws InvalidServiceTypeException { + Callback callback = interfaze.getAnnotation(Callback.class); + if (callback != null && !Void.class.equals(callback.value())) { + Class<?> callbackClass = callback.value(); + JavaInterface javaInterface; + try { + javaInterface = javaInterfaceFactory.createJavaInterface(callbackClass); + contract.getInterfaceContract().setCallbackInterface(javaInterface); + } catch (InvalidInterfaceException e) { + throw new InvalidServiceTypeException("Invalid callback interface "+callbackClass, interfaze); + } + } else if (callback != null && Void.class.equals(callback.value())) { + throw new InvalidServiceTypeException("No callback interface specified on annotation", interfaze); + } + } + + + /** + * Utility methods + */ + + + /** + * Verify if the method is a public setter + * @param method + * @return + */ + private static boolean isPublicSetter(Method method) { + return method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) + && method.getName().startsWith("set") + && method.getReturnType() == void.class; + } + + /** + * Verify if the method is a protected setter + * @param method + * @return + */ + private static boolean isProtectedSetter(Method method) { + return method.getParameterTypes().length == 1 && Modifier.isProtected(method.getModifiers()) + && method.getName().startsWith("set") + && method.getReturnType() == void.class; + } + + /** + * @param collection + * @return + */ + private static boolean areUnique(Class<?>[] collection) { + Set<Class<?>> set = new HashSet<Class<?>>(Arrays.asList(collection)); + return set.size() == collection.length; + } + + /** + * Returns true if a given type is reference according to the SCA + * specification rules for determining reference types The following rules + * are used to determine whether an unannotated field or setter method is a + * property or reference: + * <ol> + * <li>If its type is simple, then it is a property. + * <li>If its type is complex, then if the type is an interface marked by + * + * @Remotable, then it is a reference; otherwise, it is a property. + * <li>Otherwise, if the type associated with the member is an + * array or a java.util.Collection, the basetype is the element + * type of the array or the parameterized type of the + * Collection; otherwise the basetype is the member type. If the + * basetype is an interface with an + * @Remotable or + * @Service annotation then the member is defined as a reference. Otherwise, + * it is defined as a property. + * </ol> + * <p> + * The name of the reference or of the property is derived from the + * name found on the setter method or on the field. + */ + // FIXME: [rfeng] What if it's a collection of references? + private static boolean isReferenceType(Class<?> cls, Type genericType) { + Class<?> baseType = JavaIntrospectionHelper.getBaseType(cls, genericType); + return baseType.isInterface() && baseType.isAnnotationPresent(Remotable.class); + } + + /** + * Returns true if the given operation is defined in the collection of + * service interfaces + * @param operation + * @param services + * @return + */ + private static boolean isInServiceInterface(Method operation, List<org.apache.tuscany.sca.assembly.Service> services) { + for (org.apache.tuscany.sca.assembly.Service service : services) { + Interface interface1 = service.getInterfaceContract().getInterface(); + if (interface1 instanceof JavaInterface) { + Class<?> clazz = ((JavaInterface)interface1).getJavaClass(); + if (isMethodMatched(clazz, operation)) { + return true; + } + } + } + return false; + } + + /** + * Test if the class declares a method which matches the signature of the + * given method + * + * @param clazz + * @param method + * @return + */ + private static boolean isMethodMatched(Class<?> clazz, Method method) { + if (method.getDeclaringClass() == clazz) { + return true; + } + Method[] methods = clazz.getMethods(); + for (Method m : methods) { + if (JavaIntrospectionHelper.exactMethodMatch(method, m)) { + return true; + } + } + return false; + } + + /** + * Verify if there is any SCA annotation on the parameter + * @param parameter + * @return + */ + private static boolean isAnnotated(JavaParameterImpl parameter) { + for (Annotation annotation : parameter.getAnnotations()) { + if (isSCAAnnotation(annotation)) { + return true; + } + } + return false; + } + + /** + * Verify if the parameters are unique + * @param parameters + * @return + */ + private static boolean areUnique(JavaParameterImpl[] parameters) { + Set<Class<?>> set = new HashSet<Class<?>>(parameters.length); + for (JavaParameterImpl p : parameters) { + if (!set.add(p.getType())) { + return false; + } + } + return true; + } + + /** + * Verify if the annotations are SCA annotation + * @param annots + * @return + */ + private static boolean injectionAnnotationsPresent(Annotation[][] annots) { + for (Annotation[] annotations : annots) { + for (Annotation annotation : annotations) { + Class<? extends Annotation> annotType = annotation.annotationType(); + if (annotType == Property.class || annotType == Reference.class + || annotType == Resource.class + || annotType == ComponentName.class + || annotType == Context.class + || annotType == Callback.class) { + return true; + } + } + } + return false; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalCallbackReferenceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalCallbackReferenceException.java new file mode 100644 index 0000000000..badd41fbb9 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalCallbackReferenceException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal use of {@link org.oasisopen.sca.annotation.Callback} on a reference + * + * @version $Rev$ $Date$ + */ +public class IllegalCallbackReferenceException extends IntrospectionException { + private static final long serialVersionUID = -8932525723147700591L; + + public IllegalCallbackReferenceException(String message) { + super(message); + } + + public IllegalCallbackReferenceException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalContextException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalContextException.java new file mode 100644 index 0000000000..fa1e67a336 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalContextException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal signature for a method decorated with {@link org.oasisopen.sca.annotation.Context} + * + * @version $Rev$ $Date$ + */ +public class IllegalContextException extends IntrospectionException { + private static final long serialVersionUID = -6946383136750117008L; + + public IllegalContextException(String message) { + super(message); + } + + public IllegalContextException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalDestructorException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalDestructorException.java new file mode 100644 index 0000000000..0c4c3d5646 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalDestructorException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal signature for a method decorated with {@link org.oasisopen.sca.annotation.Destroy} + * + * @version $Rev$ $Date$ + */ +public class IllegalDestructorException extends IntrospectionException { + private static final long serialVersionUID = 365719353107446326L; + + public IllegalDestructorException(String message) { + super(message); + } + + public IllegalDestructorException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalInitException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalInitException.java new file mode 100644 index 0000000000..2956b60b65 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalInitException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal signature for a method decorated with {@link @org.oasisopen.sca.annotation.Init} + * + * @version $Rev$ $Date$ + */ +public class IllegalInitException extends IntrospectionException { + private static final long serialVersionUID = -3690763271986854701L; + + public IllegalInitException(String message) { + super(message); + } + + public IllegalInitException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalPropertyException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalPropertyException.java new file mode 100644 index 0000000000..54f7dfad1e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalPropertyException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal property definition in a component type + * + * @version $Rev$ $Date$ + */ +public class IllegalPropertyException extends IntrospectionException { + private static final long serialVersionUID = -2836849110706758494L; + + public IllegalPropertyException(String message) { + super(message); + } + + public IllegalPropertyException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalReferenceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalReferenceException.java new file mode 100644 index 0000000000..25196c56e5 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalReferenceException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal reference definition in a component type + * + * @version $Rev$ $Date$ + */ +public class IllegalReferenceException extends IntrospectionException { + private static final long serialVersionUID = 4612984122225271395L; + + public IllegalReferenceException(String message) { + super(message); + } + + public IllegalReferenceException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalResourceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalResourceException.java new file mode 100644 index 0000000000..ccfbaf0043 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalResourceException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal resource definition in a component type + * + * @version $Rev$ $Date$ + */ +public class IllegalResourceException extends IntrospectionException { + private static final long serialVersionUID = -1100936539412435579L; + + public IllegalResourceException(String message) { + super(message); + } + + public IllegalResourceException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalServiceDefinitionException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalServiceDefinitionException.java new file mode 100644 index 0000000000..e0f90779a1 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalServiceDefinitionException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal use of the {@link @org.oasisopen.sca.annotation.Service} annotation + * + * @version $Rev$ $Date$ + */ +public class IllegalServiceDefinitionException extends IntrospectionException { + private static final long serialVersionUID = -7151534258405092548L; + + public IllegalServiceDefinitionException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessor.java new file mode 100644 index 0000000000..a5227fc4f5 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessor.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.oasisopen.sca.annotation.Init; + +/** + * Processes the {@link @Init} annotation on a component implementation and + * updates the component type with the decorated initializer method + * + * @version $Rev$ $Date$ + */ +public class InitProcessor extends BaseJavaClassVisitor { + + public InitProcessor(AssemblyFactory factory) { + super(factory); + } + + public InitProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + Init annotation = method.getAnnotation(Init.class); + if (annotation == null) { + return; + } + if (method.getParameterTypes().length != 0) { + throw new IllegalInitException("[JCA90008] Initializer must not have argments", method); + } + if(!method.getReturnType().equals(void.class)) { + throw new IllegalInitException("Initializer must return void.", method); + } + if (type.getInitMethod() != null) { + throw new DuplicateInitException("More than one initializer found on implementaton"); + } + if (!Modifier.isPublic(method.getModifiers())) { + throw new IllegalInitException("Initializer must be a public method. Invalid annotation @Init found on "+method); + } + type.setInitMethod(method); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConstructorException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConstructorException.java new file mode 100644 index 0000000000..c75dfdaf46 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConstructorException.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an invalid constructor definition, e.g. when the number of injection names specified in {@link + * org.oasisopen.sca.annotation.Constructor} do not match the number of actual constructor parameters + * + * @version $Rev$ $Date$ + */ +public class InvalidConstructorException extends IntrospectionException { + private static final long serialVersionUID = 1411492435210741512L; + + public InvalidConstructorException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConversationalImplementationException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConversationalImplementationException.java new file mode 100644 index 0000000000..079495e4db --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidConversationalImplementationException.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Raised when an implementation specifies improper conversational metadata + * + * @version $Rev$ $Date$ + */ +public class InvalidConversationalImplementationException extends IntrospectionException { + private static final long serialVersionUID = -5487291552769408149L; + + public InvalidConversationalImplementationException(String message) { + super(message); + } + + public InvalidConversationalImplementationException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidPropertyException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidPropertyException.java new file mode 100644 index 0000000000..429b88aa1f --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidPropertyException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an invalid usage of {@link org.oasisopen.sca.annotation.Property} + * + * @version $Rev$ $Date$ + */ +public class InvalidPropertyException extends IntrospectionException { + private static final long serialVersionUID = -2682862652069727948L; + + public InvalidPropertyException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidReferenceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidReferenceException.java new file mode 100644 index 0000000000..fbf3fc9ed3 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidReferenceException.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.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an invalid usage of {@link org.oasisopen.sca.annotation.Reference} + * + * @version $Rev$ $Date$ + */ +public class InvalidReferenceException extends IntrospectionException { + private static final long serialVersionUID = -3285246635989254165L; + + public InvalidReferenceException(String message) { + super(message); + } + + public InvalidReferenceException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidReferenceException(Throwable cause) { + super(cause); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidResourceException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidResourceException.java new file mode 100644 index 0000000000..debb5cbacc --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidResourceException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an invalid usage of {@link @org.apache.tuscany.api.annotation.Resource} + * + * @version $Rev$ $Date$ + */ +public class InvalidResourceException extends IntrospectionException { + private static final long serialVersionUID = 511728001735534934L; + + public InvalidResourceException(String message) { + super(message); + } + + public InvalidResourceException(String message, Member member) { + super(message, member); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidServiceTypeException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidServiceTypeException.java new file mode 100644 index 0000000000..230c8c6fdd --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InvalidServiceTypeException.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when a service type specified by an {@link org.oasisopen.sca.annotation.Service} annotation is invalid, e.g. it is + * not an interface + * + * @version $Rev$ $Date$ + */ +public class InvalidServiceTypeException extends IntrospectionException { + private static final long serialVersionUID = -1076466639416644386L; + private Class<?> serviceType; + + public InvalidServiceTypeException(String message) { + super(message); + } + + public InvalidServiceTypeException(String message, Class<?> clazz) { + super(message); + this.serviceType = clazz; + } + + /** + * @return the serviceType + */ + public Class<?> getServiceType() { + return serviceType; + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java new file mode 100644 index 0000000000..50bb5f86d3 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java @@ -0,0 +1,337 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.implementation.java.introspect.impl;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
+import javax.jws.WebParam;
+import javax.jws.WebResult;
+import javax.jws.WebService;
+import javax.jws.soap.SOAPBinding;
+import javax.xml.ws.BindingType;
+import javax.xml.ws.ServiceMode;
+import javax.xml.ws.WebServiceProvider;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Service;
+import org.apache.tuscany.sca.assembly.xml.Constants;
+import org.apache.tuscany.sca.binding.ws.WebServiceBinding;
+import org.apache.tuscany.sca.binding.ws.WebServiceBindingFactory;
+import org.apache.tuscany.sca.binding.ws.xml.WebServiceConstants;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+import org.apache.tuscany.sca.implementation.java.IntrospectionException;
+import org.apache.tuscany.sca.implementation.java.JavaImplementation;
+import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor;
+import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
+import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory;
+import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface;
+import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterfaceContract;
+import org.apache.tuscany.sca.policy.ExtensionType;
+import org.apache.tuscany.sca.policy.Intent;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.apache.tuscany.sca.policy.PolicySubject;
+
+/**
+ * Process JAXWS annotations and updates the component type accordingly
+ *
+ */
+public class JAXWSProcessor extends BaseJavaClassVisitor {
+
+ private PolicyFactory policyFactory;
+ private WSDLFactory wsdlFactory;
+ private WebServiceBindingFactory wsBindingFactory;
+
+ public JAXWSProcessor(ExtensionPointRegistry registry) {
+ super(registry);
+ FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class);
+ this.wsdlFactory = factories.getFactory(WSDLFactory.class);
+ this.assemblyFactory = factories.getFactory(AssemblyFactory.class);
+ this.policyFactory = factories.getFactory(PolicyFactory.class);
+ this.javaInterfaceFactory = factories.getFactory(JavaInterfaceFactory.class);
+ this.wsBindingFactory = factories.getFactory(WebServiceBindingFactory.class);
+ }
+
+ @Override
+ public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException {
+
+ boolean hasJaxwsAnnotation = false;
+
+ // Process @ServiceMode annotation - JCA 11013
+ if ( clazz.getAnnotation(ServiceMode.class) != null ) {
+ addSOAPIntent(type);
+ hasJaxwsAnnotation = true;
+ }
+
+ // Process @WebService annotation - POJO_8029, POJO_8030
+ WebService webServiceAnnotation = clazz.getAnnotation(WebService.class);
+ org.oasisopen.sca.annotation.Service serviceAnnotation = clazz.getAnnotation(org.oasisopen.sca.annotation.Service.class);
+
+ if (webServiceAnnotation != null &&
+ serviceAnnotation == null) {
+ String serviceName = clazz.getSimpleName();
+ serviceName = getValue(webServiceAnnotation.name(), serviceName);
+
+ String serviceInterfaceClassName = webServiceAnnotation.endpointInterface();
+
+ String wsdlLocation = webServiceAnnotation.wsdlLocation();
+
+ try {
+ createService(type, clazz, serviceName, serviceInterfaceClassName, wsdlLocation, false);
+ } catch (InvalidInterfaceException e) {
+ throw new IntrospectionException(e);
+ }
+ hasJaxwsAnnotation = true;
+ }
+
+ // Process @WebServiceProvider annotation - JCA_11015, POJO_8034
+ WebServiceProvider webServiceProviderAnnotation = clazz.getAnnotation(WebServiceProvider.class);
+ if (webServiceProviderAnnotation != null) {
+ // if the implmentation already has a service set, use it's name
+ // and the new service, which uses the implementation as an interface,
+ // will be replaced
+ String serviceName = clazz.getSimpleName();
+
+ if (type.getServices().size() > 0){
+ serviceName = ((Service)type.getServices().get(0)).getName();
+ }
+
+ // the annotation may specify a service name
+ serviceName = getValue(webServiceProviderAnnotation.serviceName(), serviceName);
+
+ String wsdlLocation = webServiceProviderAnnotation.wsdlLocation();
+
+ // Make sure that there is a service with an interface
+ // based on the implementation class and have it replace
+ // any service with the same name
+ try {
+ createService(type, clazz, serviceName, null, wsdlLocation, true);
+ } catch (InvalidInterfaceException e) {
+ throw new IntrospectionException(e);
+ }
+
+ // Make sure all service references are remotable
+ for ( Service service : type.getServices() ) {
+ service.getInterfaceContract().getInterface().setRemotable(true);
+ }
+
+ hasJaxwsAnnotation = true;
+ }
+
+ // Process @WebParam and @WebResult annotations - POJO_8031, POJO_8032
+ Class<?> interfaze = clazz;
+ Method[] implMethods = interfaze.getDeclaredMethods();
+ for ( Service service : type.getServices() ) {
+ JavaInterface javaInterface = (JavaInterface)service.getInterfaceContract().getInterface();
+ interfaze = javaInterface.getJavaClass();
+
+ if (interfaze == null){
+ // this interface has come from an @WebService enpointInterface annotation
+ // so hasn't been resolved. Use the implementation class as the interface
+ interfaze = clazz;
+ }
+
+ boolean hasHeaderParam = false;
+ for (Method method : interfaze.getDeclaredMethods()){
+ // find the impl method for this service method
+ for (int i = 0; i < implMethods.length; i++){
+ Method implMethod = implMethods[i];
+ if (implMethod.getName().equals(method.getName())){
+ for (int j = 0; j < implMethod.getParameterTypes().length; j++) {
+ WebParam webParamAnnotation = getParameterAnnotation(implMethod, j, WebParam.class);
+ if (webParamAnnotation != null &&
+ webParamAnnotation.header()) {
+ hasHeaderParam = true;
+ break;
+ }
+ }
+
+ WebResult webResultAnnotation = implMethod.getAnnotation(WebResult.class);
+ if (webResultAnnotation != null &&
+ webResultAnnotation.header()){
+ hasHeaderParam = true;
+ break;
+ }
+ }
+ }
+ }
+
+ if (hasHeaderParam){
+ // Add a SOAP intent to the service
+ addSOAPIntent(service);
+ hasJaxwsAnnotation = true;
+ }
+ }
+
+ // Process @SOAPBinding annotation - POJO_8033
+ if ( clazz.getAnnotation(SOAPBinding.class) != null ) {
+ // If the implementation is annotated with @SOAPBinding,
+ // give all services a SOAP intent
+ for ( Service service : type.getServices() ) {
+ addSOAPIntent(service);
+ }
+ hasJaxwsAnnotation = true;
+ }
+
+
+ // Process @BindingType annotation - POJO_8037
+ BindingType bindingType = clazz.getAnnotation(BindingType.class);
+
+ if ( bindingType != null ) {
+ String bindingTypeValue = bindingType.value();
+ for ( Service service : type.getServices() ) {
+ addBindingTypeIntent(service, bindingTypeValue);
+ }
+ hasJaxwsAnnotation = true;
+ }
+
+ if (hasJaxwsAnnotation == true){
+ // Note that services are based on JAXWS annotations in case
+ // we need to know later. Add a ws binding and a JAXWS annotation
+ // implies a WS binding
+ for ( Service service : type.getServices() ) {
+ service.setJAXWSService(true);
+ createWSBinding(type, service);
+ }
+ }
+
+
+
+ }
+
+ private void addBindingTypeIntent(PolicySubject subject,
+ String bindingTypeValue) {
+
+ Intent soapIntent = policyFactory.createIntent();
+ if ( javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING.equals(bindingTypeValue)) {
+ soapIntent.setName(Constants.SOAP11_INTENT);
+ } else if ( javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING.equals(bindingTypeValue)) {
+ soapIntent.setName(Constants.SOAP12_INTENT);
+ } else {
+ soapIntent.setName(Constants.SOAP11_INTENT);
+ }
+
+ subject.getRequiredIntents().add(soapIntent);
+
+ }
+
+ /**
+ * Utility methods
+ */
+
+ private static String getValue(String value, String defaultValue) {
+ return "".equals(value) ? defaultValue : value;
+ }
+
+ private Service createService(JavaImplementation type, Class<?> clazz, String serviceName, String javaInterfaceName, String wsdlFileName, boolean replace) throws InvalidInterfaceException, IntrospectionException {
+ Service service = assemblyFactory.createService();
+
+ if (serviceName != null) {
+ service.setName(serviceName);
+ } else if (javaInterfaceName != null){
+ service.setName(javaInterfaceName.substring(javaInterfaceName.lastIndexOf('.')));
+ }
+
+ // create the physical Java interface contract
+ JavaInterfaceContract javaInterfaceContract = javaInterfaceFactory.createJavaInterfaceContract();;
+ service.setInterfaceContract(javaInterfaceContract);
+
+ if (javaInterfaceName != null &&
+ javaInterfaceName.length() > 0){
+ JavaInterface callInterface = javaInterfaceFactory.createJavaInterface();
+ callInterface.setName(javaInterfaceName);
+ callInterface.setRemotable(true);
+ callInterface.setUnresolved(true);
+ javaInterfaceContract.setInterface(callInterface);
+ } else {
+ // we use the bean class as the service interface
+ JavaInterface callInterface = javaInterfaceFactory.createJavaInterface(clazz);
+ callInterface.setRemotable(true);
+ callInterface.setUnresolved(false); // this will already be false but this makes it easy to follow the logic
+ javaInterfaceContract.setInterface(callInterface);
+ }
+
+ // create the logical WSDL interface if it's specified in
+ // the @WebService annotation
+ if (wsdlFileName != null &&
+ wsdlFileName.length() > 0){
+ WSDLInterface callInterface = wsdlFactory.createWSDLInterface();
+ callInterface.setUnresolved(true);
+ callInterface.setRemotable(true);
+
+ WSDLInterfaceContract wsdlInterfaceContract = wsdlFactory.createWSDLInterfaceContract();
+ wsdlInterfaceContract.setInterface(callInterface);
+ wsdlInterfaceContract.setLocation(wsdlFileName);
+ javaInterfaceContract.setNormailizedWSDLContract(wsdlInterfaceContract);
+ }
+
+ // add the service model into the implementation type
+ Service serviceAlreadyPresent = null;
+ for (Service typeService : type.getServices()){
+ if (typeService.getName().equals(service.getName())){
+ serviceAlreadyPresent = typeService;
+ break;
+ }
+ }
+
+ if (replace == true){
+ type.getServices().remove(serviceAlreadyPresent);
+ type.getServices().add(service);
+ } else {
+ if (serviceAlreadyPresent == null){
+ type.getServices().add(service);
+ }
+ }
+
+ return service;
+ }
+
+ private <T extends Annotation> T getParameterAnnotation(Method method, int index, Class<T> annotationType) {
+ Annotation[] annotations = method.getParameterAnnotations()[index];
+ for (Annotation annotation : annotations) {
+ if (annotation.annotationType() == annotationType) {
+ return annotationType.cast(annotation);
+ }
+ }
+ return null;
+ }
+
+ private void addSOAPIntent(PolicySubject policySubject){
+ Intent soapIntent = policyFactory.createIntent();
+ soapIntent.setName(Constants.SOAP_INTENT);
+ policySubject.getRequiredIntents().add(soapIntent);
+ }
+
+ private void createWSBinding(JavaImplementation javaImplementation, Service service){
+ if(service.getBindings().size() == 0){
+ WebServiceBinding wsBinding = wsBindingFactory.createWebServiceBinding();
+ wsBinding.setName(service.getName());
+ ExtensionType bindingType = policyFactory.createBindingType();
+ bindingType.setType(WebServiceConstants.BINDING_WS_QNAME);
+ bindingType.setUnresolved(true);
+ ((PolicySubject)wsBinding).setExtensionType(bindingType);
+ service.getBindings().add(wsBinding);
+ }
+ }
+
+}
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/NoConstructorException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/NoConstructorException.java new file mode 100644 index 0000000000..453cfa35fe --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/NoConstructorException.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when a suitable constructor for a component implementation cannot be found + * + * @version $Rev$ $Date$ + */ +public class NoConstructorException extends IntrospectionException { + private static final long serialVersionUID = 3086706387280694424L; + + public NoConstructorException() { + } + + public NoConstructorException(String message) { + super(message); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java new file mode 100644 index 0000000000..15d31a5ad9 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessor.java @@ -0,0 +1,303 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.oasisopen.sca.ServiceRuntimeException; +import org.oasisopen.sca.annotation.PolicySets; +import org.oasisopen.sca.annotation.Qualifier; +import org.oasisopen.sca.annotation.Requires; + +/** + * Processes an {@link org.oasisopen.sca.annotation.Requires} annotation + * + * @version $Rev$ $Date$ + */ +public class PolicyProcessor extends BaseJavaClassVisitor { + + private PolicyFactory policyFactory; + + public PolicyProcessor(AssemblyFactory assemblyFactory, + PolicyFactory policyFactory, + JavaInterfaceFactory javaInterfaceFactory) { + super(assemblyFactory); + this.policyFactory = policyFactory; + this.javaInterfaceFactory = javaInterfaceFactory; + } + + public PolicyProcessor(ExtensionPointRegistry registry) { + super(registry); + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.policyFactory = factories.getFactory(PolicyFactory.class); + } + + @Override + public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + + // Read intents on the Java implementation class + readPolicySetAndIntents((PolicySubject)type, clazz); + + /* + // FIXME: [rfeng] We might want to refactor this out + // Find the business methods in the implementation class for all services + Set<Method> methods = new HashSet<Method>(); + for (Service service : type.getServices()) { + for (Operation op : service.getInterfaceContract().getInterface().getOperations()) { + Method method; + try { + method = JavaInterfaceUtil.findMethod(clazz, op); + } catch (NoSuchMethodException e1) { + throw new IntrospectionException(e1); + } + if (method != null) { + methods.add(method); + } + } + } + for (Method method : methods) { + JavaOperation op = javaInterfaceFactory.createJavaOperation(method); + type.getOperations().add(op); + } + + // Read the operation-level policy settings for the implementation + for (Operation op : type.getOperations()) { + JavaOperation operation = (JavaOperation)op; + PolicySubject subject = op; + Method method = operation.getJavaMethod(); + if (subject != null) { + readPolicySetAndIntents(subject, method); + } + } + */ + + // Start to process annotations on the reference members + Map<String, Reference> referenceMap = new HashMap<String, Reference>(); + for (Reference ref : type.getReferences()) { + referenceMap.put(ref.getName(), ref); + } + Map<String, JavaElementImpl> members = type.getReferenceMembers(); + for (Map.Entry<String, JavaElementImpl> e : members.entrySet()) { + Reference reference = referenceMap.get(e.getKey()); + readPolicySetAndIntents(reference, e.getValue().getAnchor()); + } + + } + + private void readPolicySetAndIntents(PolicySubject subject, AnnotatedElement element) { + readIntents(element.getAnnotation(Requires.class), subject.getRequiredIntents()); + readSpecificIntents(element.getAnnotations(), subject.getRequiredIntents()); + readPolicySets(element.getAnnotation(PolicySets.class), subject.getPolicySets()); + } + + private void readPolicySetAndIntents(PolicySubject subject, JavaElementImpl element) { + readIntents(element.getAnnotation(Requires.class), subject.getRequiredIntents()); + readSpecificIntents(element.getAnnotations(), subject.getRequiredIntents()); + readPolicySets(element.getAnnotation(PolicySets.class), subject.getPolicySets()); + } + + private void readSpecificIntents(Annotation[] annotations, List<Intent> requiredIntents) { + for (Annotation a : annotations) { + org.oasisopen.sca.annotation.Intent intentAnnotation = + a.annotationType().getAnnotation(org.oasisopen.sca.annotation.Intent.class); + if (intentAnnotation == null) { + continue; + } + QName qname = null; + String value = intentAnnotation.value(); + if (!value.equals("")) { + qname = getQName(value); + } else { + qname = new QName(intentAnnotation.targetNamespace(), intentAnnotation.localPart()); + } + Set<String> qualifiers = new HashSet<String>(); + for (Method m : a.annotationType().getMethods()) { + Qualifier qualifier = m.getAnnotation(Qualifier.class); + if (qualifier != null && m.getReturnType() == String[].class) { + try { + qualifiers.addAll(Arrays.asList((String[])m.invoke(a))); + } catch (Throwable e) { + e.printStackTrace(); + } + } + } + qualifiers.remove(""); + if (qualifiers.isEmpty()) { + Intent intent = policyFactory.createIntent(); + intent.setUnresolved(true); + intent.setName(qname); + requiredIntents.add(intent); + } else { + for (String q : qualifiers) { + Intent intent = policyFactory.createIntent(); + intent.setUnresolved(true); + qname = new QName(qname.getNamespaceURI(), qname.getLocalPart() + "." + q); + intent.setName(qname); + requiredIntents.add(intent); + } + } + } + } + + /** + * Read intent annotations on the given interface or class + * @param intentAnnotation + * @param requiredIntents + */ + private void readIntents(Requires intentAnnotation, List<Intent> requiredIntents) { + //Requires intentAnnotation = method.getAnnotation(Requires.class); + if (intentAnnotation != null) { + String[] intentNames = intentAnnotation.value(); + if (intentNames.length != 0) { + //Operation operation = assemblyFactory.createOperation(); + //operation.setName(method.getName()); + //operation.setUnresolved(true); + for (String intentName : intentNames) { + + // Add each intent to the list, associated with the + // operation corresponding to the annotated method + Intent intent = policyFactory.createIntent(); + intent.setName(getQName(intentName)); + //intent.getOperations().add(operation); + requiredIntents.add(intent); + } + } + } + } + + /** + * Read policy set annotations on a given interface or class + * @param policySetAnnotation + * @param policySets + */ + private void readPolicySets(PolicySets policySetAnnotation, List<PolicySet> policySets) { + if (policySetAnnotation != null) { + String[] policySetNames = policySetAnnotation.value(); + if (policySetNames.length != 0) { + //Operation operation = assemblyFactory.createOperation(); + //operation.setName(method.getName()); + //operation.setUnresolved(true); + for (String policySetName : policySetNames) { + // Add each intent to the list, associated with the + // operation corresponding to the annotated method + PolicySet policySet = policyFactory.createPolicySet(); + policySet.setName(getQName(policySetName)); + //intent.getOperations().add(operation); + policySets.add(policySet); + } + } + } + } + + /** + * Utility methods + */ + + /** + * + * @param intentName + * @return + */ + private static QName getQName(String intentName) { + QName qname; + if (intentName.startsWith("{")) { + int i = intentName.indexOf('}'); + if (i != -1) { + qname = new QName(intentName.substring(1, i), intentName.substring(i + 1)); + } else { + qname = new QName("", intentName); + } + } else { + qname = new QName("", intentName); + } + return qname; + } + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + // Check if a field that is not an SCA reference has any policySet/intent annotations + JavaElementImpl element = new JavaElementImpl(field); + if (!type.getReferenceMembers().values().contains(element)) { + PolicySubject subject = assemblyFactory.createComponent(); + readPolicySetAndIntents(subject, field); + if (subject.getPolicySets().isEmpty() && subject.getRequiredIntents().isEmpty()) { + return; + } + throw new ServiceRuntimeException( + "[JCA70002,JCA70005] Field that is not an SCA reference cannot have policySet/intent annotations: " + field); + } + } + + @Override + public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) { + if (!type.getReferenceMembers().values().contains(parameter)) { + PolicySubject subject = assemblyFactory.createComponent(); + readPolicySetAndIntents(subject, parameter); + if (subject.getPolicySets().isEmpty() && subject.getRequiredIntents().isEmpty()) { + return; + } + throw new ServiceRuntimeException( + "[JCA70002,JCA70005] Constructor parameter that is not an SCA reference cannot have policySet/intent annotations: " + parameter); + } + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + Set<AnnotatedElement> annotatedElements = new HashSet<AnnotatedElement>(); + for (JavaElementImpl element : type.getReferenceMembers().values()) { + annotatedElements.add(element.getAnchor()); + } + // Check if a field that is not an SCA reference has any policySet/intent annotations + if (!annotatedElements.contains(method)) { + PolicySubject subject = assemblyFactory.createComponent(); + readPolicySetAndIntents(subject, method); + if (subject.getPolicySets().isEmpty() && subject.getRequiredIntents().isEmpty()) { + return; + } + throw new ServiceRuntimeException( + "[JCA70002,JCA70005] Method that is not an SCA reference cannot have policySet/intent annotations: " + method); + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessor.java new file mode 100644 index 0000000000..ed68be20b4 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessor.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.oasisopen.sca.annotation.Property; + +/** + * Processes an {@link @Property} annotation, updating the component type with + * corresponding {@link JavaMappedProperty} + * + * @version $Rev$ $Date$ + */ +public class PropertyProcessor extends AbstractPropertyProcessor<Property> { + + public PropertyProcessor(ExtensionPointRegistry registry) { + super(registry, Property.class); + } + + @Override + protected String getName(Property annotation) { + return annotation.name(); + } + + @Override + protected void initProperty(org.apache.tuscany.sca.assembly.Property property, Property annotation) { + property.setMustSupply(annotation.required()); + } + + @Override + protected boolean getRequired(Property annotation) { + return annotation.required(); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java new file mode 100644 index 0000000000..225099758d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java @@ -0,0 +1,283 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getBaseType; + +import java.lang.annotation.Annotation; +import java.lang.annotation.ElementType; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.annotation.AllowsPassByReference; +import org.oasisopen.sca.annotation.Reference; + +/** + * Processes an {@link @Reference} annotation, updating the component type with + * corresponding {@link + * org.apache.tuscany.spi.implementation.java.JavaMappedReference} + * + * @version $Rev$ $Date$ + */ +public class ReferenceProcessor extends BaseJavaClassVisitor { + + public ReferenceProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) { + super(assemblyFactory); + this.javaInterfaceFactory = javaFactory; + } + + public ReferenceProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + Reference annotation = method.getAnnotation(Reference.class); + if (annotation != null) { + + if (!JavaIntrospectionHelper.isSetter(method)) { + throw new IllegalReferenceException("Annotated method is not a setter: " + method, method); + } + + if(Modifier.isStatic(method.getModifiers())) { + throw new IllegalPropertyException("Static method " + method.getName() +" in class " + method.getDeclaringClass().getName() + " can not be annotated as a Reference"); + } + + String name = annotation.name(); + if ("".equals(name)) { + name = JavaIntrospectionHelper.toPropertyName(method.getName()); + } + JavaElementImpl ref = type.getReferenceMembers().get(name); + // Setter override field + if (ref != null && ref.getElementType() != ElementType.FIELD) { + throw new DuplicateReferenceException(name); + } + removeReference(ref, type); + + JavaElementImpl element = new JavaElementImpl(method, 0); + org.apache.tuscany.sca.assembly.Reference reference = createReference(type, element, name); + type.getReferences().add(reference); + type.getReferenceMembers().put(name, element); + } + + // enforce the constraint that an ordinary method's argument can not be a reference + Annotation paramsAnnotations[][] = method.getParameterAnnotations(); + for (int i = 0; i < paramsAnnotations.length; i++) { + Annotation argAnnotations[] = paramsAnnotations[i]; + for (int j = 0; j < argAnnotations.length; j++) { + if(argAnnotations[j].annotationType() == Reference.class) { + throw new IllegalReferenceException("Argument " + (i+1) + " of method " + method.getName() + " in class " + method.getDeclaringClass() + " can not be a Reference"); + } + } + } + } + + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + Reference annotation = field.getAnnotation(Reference.class); + if (annotation == null) { + return; + } + + if(Modifier.isStatic(field.getModifiers())) { + throw new IllegalReferenceException("Static field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as Reference"); + } + String name = annotation.name(); + if ("".equals(name)) { + name = field.getName(); + } + JavaElementImpl ref = type.getReferenceMembers().get(name); + if (ref != null && ref.getElementType() == ElementType.FIELD) { + throw new DuplicateReferenceException(name); + } + + // Setter method override field + if (ref == null) { + JavaElementImpl element = new JavaElementImpl(field); + org.apache.tuscany.sca.assembly.Reference reference = createReference(type, element, name); + type.getReferences().add(reference); + type.getReferenceMembers().put(name, element); + } + } + + @Override + public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) + throws IntrospectionException { + Reference refAnnotation = parameter.getAnnotation(Reference.class); + if (refAnnotation == null) { + return; + } + + if (!refAnnotation.required()) { + throw new InvalidReferenceException("[JCA90016] Constructor has @Reference with required=false: " + type.getName()); + } + + if (refAnnotation.name() == null || refAnnotation.name().length() < 1) { + throw new InvalidReferenceException("[JCA90018] @Reference in a Constructor must have a name attribute" + type.getName()); + } + + String paramName = parameter.getName(); + String name = getReferenceName(paramName, parameter.getIndex(), refAnnotation.name()); + JavaElementImpl ref = type.getReferenceMembers().get(name); + + // Setter override field + if (ref != null && ref.getElementType() != ElementType.FIELD) { + throw new DuplicateReferenceException(name); + } + + removeReference(ref, type); + org.apache.tuscany.sca.assembly.Reference reference = createReference(type, parameter, name); + type.getReferences().add(reference); + type.getReferenceMembers().put(name, parameter); + parameter.setClassifer(Reference.class); + parameter.setName(name); + } + + /** + * Create a SCA reference for a java Element + * @param implementation TODO + * @param element + * @param name + * @return + * @throws IntrospectionException + */ + private org.apache.tuscany.sca.assembly.Reference createReference(JavaImplementation implementation, JavaElementImpl element, String name) + throws IntrospectionException { + org.apache.tuscany.sca.assembly.Reference reference = assemblyFactory.createReference(); + JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + reference.setInterfaceContract(interfaceContract); + + AllowsPassByReference pbr = element.getAnnotation(AllowsPassByReference.class); + if (pbr != null) { + reference.setAllowsPassByReference(true); + } else { + reference.setAllowsPassByReference(implementation.isAllowsPassByReference()); + } + + // reference.setMember((Member)element.getAnchor()); + boolean required = true; + Reference ref = element.getAnnotation(Reference.class); + if (ref != null) { + required = ref.required(); + } + // reference.setRequired(required); + reference.setName(name); + Class<?> rawType = element.getType(); + if (rawType.isArray() || Collection.class.isAssignableFrom(rawType)) { + if (required) { + reference.setMultiplicity(Multiplicity.ONE_N); + } else { + reference.setMultiplicity(Multiplicity.ZERO_N); + } + } else { + if (required) { + reference.setMultiplicity(Multiplicity.ONE_ONE); + } else { + reference.setMultiplicity(Multiplicity.ZERO_ONE); + } + } + Type genericType = element.getGenericType(); + Class<?> baseType = getBaseType(rawType, genericType); + if (ServiceReference.class.isAssignableFrom(baseType)) { + if (Collection.class.isAssignableFrom(rawType)) { + genericType = JavaIntrospectionHelper.getParameterType(genericType); + } + baseType = JavaIntrospectionHelper.getBusinessInterface(baseType, genericType); + } + try { + JavaInterface callInterface = javaInterfaceFactory.createJavaInterface(baseType); + reference.getInterfaceContract().setInterface(callInterface); + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaInterfaceFactory.createJavaInterface(callInterface.getCallbackClass()); + reference.getInterfaceContract().setCallbackInterface(callbackInterface); + } + } catch (InvalidInterfaceException e) { + throw new IntrospectionException(e); + } + return reference; + } + + + /** + * Utility methods + */ + + /** + * + * @param paramName + * @param pos + * @param name + * @return + * @throws InvalidConstructorException + */ + private static String getReferenceName(String paramName, int pos, String name) throws InvalidConstructorException { + if ("".equals(name)) { + name = paramName; + } + if ("".equals(name)) { + return "_ref" + pos; + } + if (!"".equals(paramName) && !name.equals(paramName)) { + throw new InvalidConstructorException("Mismatching names specified for reference parameter " + pos); + } else { + return name; + } + } + + /** + * + * @param ref + * @param type + * @return + */ + private static boolean removeReference(JavaElementImpl ref, JavaImplementation type) { + if (ref == null) { + return false; + } + List<org.apache.tuscany.sca.assembly.Reference> refs = type.getReferences(); + for (int i = 0; i < refs.size(); i++) { + if (refs.get(i).getName().equals(ref.getName())) { + refs.remove(i); + return true; + } + } + return false; + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/Resource.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/Resource.java new file mode 100644 index 0000000000..3d43e39ce8 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/Resource.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation used to indicate a resource should be provided to an implementation by the runtime. + * + * @version $Rev$ $Date$ + */ +@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Resource { + + /** + * Denotes the name of the resource declared by the implementation. + */ + String name() default ""; + + /** + * Denotes if the resource is optional + */ + boolean optional() default false; + + /** + * Denotes the default name of the resource provided by the runtime environment. + */ + String mappedName() default ""; +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessor.java new file mode 100644 index 0000000000..3abe832cb9 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessor.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; + +/** + * Processes an {@link @Resource} annotation, updating the component type with + * corresponding {@link org.apache.tuscany.spi.implementation.java.JavaResourceImpl} + * + * @version $Rev$ $Date$ + */ +public class ResourceProcessor extends BaseJavaClassVisitor { + + public ResourceProcessor(AssemblyFactory factory) { + super(factory); + } + + public ResourceProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + org.apache.tuscany.sca.implementation.java.introspect.impl.Resource annotation = method + .getAnnotation(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); + if (annotation == null) { + return; + } + if (method.getParameterTypes().length != 1) { + throw new IllegalResourceException("Resource setter must have one parameter", method); + } + String name = annotation.name(); + if (name.length() < 1) { + name = JavaIntrospectionHelper.toPropertyName(method.getName()); + } + if (type.getResources().get(name) != null) { + throw new DuplicateResourceException(name); + } + + String mappedName = annotation.mappedName(); + JavaResourceImpl resource = createResource(name, new JavaElementImpl(method, 0)); + resource.setOptional(annotation.optional()); + if (mappedName.length() > 0) { + resource.setMappedName(mappedName); + } + type.getResources().put(resource.getName(), resource); + } + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + + org.apache.tuscany.sca.implementation.java.introspect.impl.Resource annotation = field + .getAnnotation(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); + if (annotation == null) { + return; + } + String name = annotation.name(); + if (name.length() < 1) { + name = field.getName(); + } + if (type.getResources().get(name) != null) { + throw new DuplicateResourceException(name); + } + + String mappedName = annotation.mappedName(); + + JavaResourceImpl resource = createResource(name, new JavaElementImpl(field)); + resource.setOptional(annotation.optional()); + if (mappedName.length() > 0) { + resource.setMappedName(mappedName); + } + type.getResources().put(resource.getName(), resource); + } + + @Override + public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) + throws IntrospectionException { + org.apache.tuscany.sca.implementation.java.introspect.impl.Resource resourceAnnotation = parameter + .getAnnotation(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); + if (resourceAnnotation != null) { + String name = resourceAnnotation.name(); + if ("".equals(name)) { + name = parameter.getName(); + } + if ("".equals(name)) { + throw new InvalidResourceException("Missing resource name", (Member)parameter.getAnchor()); + } + + if (!"".equals(parameter.getName()) && !name.equals(parameter.getName())) { + throw new InvalidConstructorException("Mismatched resource name: " + parameter); + } + + if (type.getResources().get(name) != null) { + throw new DuplicateResourceException(name); + } + + String mappedName = resourceAnnotation.mappedName(); + + JavaResourceImpl resource = createResource(name, parameter); + resource.setOptional(resourceAnnotation.optional()); + if (mappedName.length() > 0) { + resource.setMappedName(mappedName); + } + type.getResources().put(resource.getName(), resource); + } + } + + + /** + * Utility methods + */ + + /** + * + * @param name + * @param element + * @return + */ + private static JavaResourceImpl createResource(String name, JavaElementImpl element) { + element.setClassifer(org.apache.tuscany.sca.implementation.java.introspect.impl.Resource.class); + element.setName(name); + return new JavaResourceImpl(element); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessor.java new file mode 100644 index 0000000000..dfc122c715 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessor.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaScopeImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; + +/** + * Processes the {@link JavaScopeImpl} annotation and updates the component type with the corresponding implmentation scope + * + * @version $Rev$ $Date$ + */ +public class ScopeProcessor extends BaseJavaClassVisitor { + + public ScopeProcessor(AssemblyFactory factory) { + super(factory); + } + + public ScopeProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public <T> void visitClass(Class<T> clazz, + JavaImplementation type) + throws IntrospectionException { + org.oasisopen.sca.annotation.Scope annotation = clazz.getAnnotation(org.oasisopen.sca.annotation.Scope.class); + if (annotation == null) { + type.setJavaScope(JavaScopeImpl.STATELESS); + return; + } + + String name = annotation.value(); + JavaScopeImpl scope; + if ("COMPOSITE".equals(name)) { + scope = JavaScopeImpl.COMPOSITE; + } else if ("STATELESS".equals(name)) { + scope = JavaScopeImpl.STATELESS; + } else { + scope = JavaScopeImpl.INVALID; + } + type.setJavaScope(scope); + + if (type.getJavaScope().equals(JavaScopeImpl.INVALID)) { + throw new IntrospectionException("Invalid scope :" + name + " for " + type.getName()); + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessor.java new file mode 100644 index 0000000000..6ad2140304 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessor.java @@ -0,0 +1,268 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getAllInterfaces; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import javax.jws.WebService; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaScopeImpl; +import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Remotable; + +/** + * Processes an {@link org.oasisopen.sca.annotation.Service} annotation and updates + * the component type with corresponding {@link Service}s. Also processes + * related {@link org.oasisopen.sca.annotation.Callback} annotations. + * + * This Visitor MUST follow the ScopeProcessor in the sequence of visitors, since processing of + * the Callback annotations depends on knowing the Scope of the implementation + * + * @version $Rev$ $Date$ + */ +public class ServiceProcessor extends BaseJavaClassVisitor { + + public ServiceProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) { + super(assemblyFactory); + this.javaInterfaceFactory = javaFactory; + } + + public ServiceProcessor(ExtensionPointRegistry registry) { + super(registry); + } + + @Override + public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + org.oasisopen.sca.annotation.Service annotation = clazz.getAnnotation(org.oasisopen.sca.annotation.Service.class); + if (annotation == null) { + // scan interfaces for remotable + Set<Class<?>> interfaces = getAllInterfaces(clazz); + for (Class<?> interfaze : interfaces) { + if (interfaze.isAnnotationPresent(Remotable.class) + || interfaze.isAnnotationPresent(WebService.class) + || interfaze.isAnnotationPresent(Callback.class) + ) { + Service service; + try { + service = createService(clazz, interfaze, null); + } catch (InvalidInterfaceException e) { + throw new IntrospectionException(e); + } + type.getServices().add(service); + } + } + return; + } + + if (annotation.value().length == 0) { + throw new IntrospectionException("[JCA90059] The array of interfaces or classes specified by the value attribute of the @Service annotation MUST contain at least one element"); + } + Class<?>[] interfaces = annotation.value(); + if (annotation.names().length > 0) { + if (annotation.names().length != interfaces.length) { + throw new IntrospectionException("[JCA90050] The number of Strings in the names attribute array of the @Service annotation MUST match the number of elements in the value attribute array"); + } + Set<String> names = new HashSet<String>(); + names.addAll(Arrays.asList(annotation.names())); + if (names.size() != annotation.names().length) { + throw new IntrospectionException("[JCA90060] The value of each element in the @Service names array MUST be unique amongst all the other element values in the array"); + } + } + + //validate no scope on servce interface + for (Class<?> iface : interfaces) { + if (iface.getAnnotation(org.oasisopen.sca.annotation.Scope.class) != null) { + throw new IntrospectionException("[JCA90041] @Scope annotation not allowed on service interface " + iface + .getName()); + } + } + + //validate service methods implemented + Method[] ms = clazz.getMethods(); + for (Class<?> iface : interfaces) { + for (Method m : iface.getMethods()) { + if (!hasMethod(m, ms)) { + throw new IntrospectionException("[JCA90042,JCI20002] Implementation missing service method " + m.getName() + " service interface " + iface.getName()); + } + } + } + + for (int i=0; i < interfaces.length; i++) { + try { + String name = (annotation.names().length > 0) ? annotation.names()[i] : null; + Service service = createService(clazz, interfaces[i], name); + type.getServices().add(service); + } catch (InvalidInterfaceException e) { + throw new IntrospectionException(e); + } + } + + } + + protected boolean hasMethod(Method m1, Method[] ms) { + for (Method m2 : ms) { + if (JavaIntrospectionHelper.exactMethodMatch(m1, m2)) { + return true; + } + } + return false; + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + + Callback annotation = method.getAnnotation(Callback.class); + if (annotation == null) { + return; + } + if( type.getJavaScope() == JavaScopeImpl.COMPOSITE ) { + throw new IllegalCallbackReferenceException("[JCA90057] @Callback on field or method cannot be used for a class with @Scope(COMPOSITE): " + type.getName() + "." + method.getName()); + } + + if (!(annotation.value() == null || annotation.value() == Void.class)) { + throw new IllegalCallbackReferenceException("[JCA90046] @Callback on field of method must not have any parameters: " + type.getName() + "." + method.getName()); + } + + if(Modifier.isPrivate(method.getModifiers())) { + throw new IllegalCallbackReferenceException("Illegal annotation @Callback found on "+method, method); + } + if (method.getParameterTypes().length != 1) { + throw new IllegalCallbackReferenceException("Setter must have one parameter", method); + } + JavaElementImpl element = new JavaElementImpl(method, 0); + createCallback(type, element); + } + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + + Callback annotation = field.getAnnotation(Callback.class); + if (annotation == null) { + return; + } + if( type.getJavaScope() == JavaScopeImpl.COMPOSITE ) { + throw new IllegalCallbackReferenceException("[JCA90057] @Callback on field or method cannot be used for a class with @Scope(COMPOSITE): " + type.getName() + "." + field.getName()); + } + if (!(annotation.value() == null || annotation.value() == Void.class)) { + throw new IllegalCallbackReferenceException("[JCA90046] @Callback on field of method must not have any parameters: " + type.getName() + "." + field.getName()); + } + if(Modifier.isPrivate(field.getModifiers())) { + throw new IllegalCallbackReferenceException("Illegal annotation @Callback found on "+field, field); + } + JavaElementImpl element = new JavaElementImpl(field); + createCallback(type, element); + } + + public Service createService(Class<?> clazz, Class<?> interfaze, String name) throws InvalidInterfaceException { + Service service = assemblyFactory.createService(); + JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract(); + service.setInterfaceContract(interfaceContract); + + JavaInterface callInterface = javaInterfaceFactory.createJavaInterface(interfaze); + + if (name == null) { + String serviceName = interfaze.getSimpleName(); + // If the interface has @WebService annotation then take the + // service name from the @name attribute if present + if (interfaze.isAnnotationPresent(WebService.class)){ + if (callInterface.getQName() != null){ + serviceName = callInterface.getQName().getLocalPart(); + } + } + + service.setName(serviceName); + } else { + service.setName(name); + } + + + boolean remotable = clazz.getAnnotation(Remotable.class) != null; + if (remotable){ + callInterface.setRemotable(true); + } + service.getInterfaceContract().setInterface(callInterface); + + if (callInterface.getCallbackClass() != null) { + JavaInterface callbackInterface = javaInterfaceFactory.createJavaInterface(callInterface.getCallbackClass()); + if (remotable){ + callbackInterface.setRemotable(true); + } + service.getInterfaceContract().setCallbackInterface(callbackInterface); + } + return service; + } + + /** + * Utility methods + */ + + + /** + * @param type + * @param element + * @throws IllegalCallbackReferenceException + */ + private static void createCallback(JavaImplementation type, JavaElementImpl element) + throws IllegalCallbackReferenceException { + Service callbackService = null; + Class<?> callbackClass = element.getType(); + Type genericType = element.getGenericType(); + Class<?> baseType = callbackClass; + if(ServiceReference.class.isAssignableFrom(baseType)) { + // @Callback protected CallableReference<MyCallback> callback; + // The base type will be MyCallback + baseType = JavaIntrospectionHelper.getBusinessInterface(baseType, genericType); + } + for (Service service : type.getServices()) { + JavaInterface javaInterface = (JavaInterface)service.getInterfaceContract().getCallbackInterface(); + if (javaInterface != null && baseType == javaInterface.getJavaClass()) { + callbackService = service; + } + } + if (callbackService == null) { + throw new IllegalCallbackReferenceException("Callback type does not match a service callback interface: " + type.getName() ); + } + if(type.getCallbackMembers().get(baseType.getName()) == null) { + type.getCallbackMembers().put(baseType.getName(), new ArrayList<JavaElementImpl>()); + } + type.getCallbackMembers().get(baseType.getName()).add(element); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceTypeNotFoundException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceTypeNotFoundException.java new file mode 100644 index 0000000000..e1ca3ea565 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceTypeNotFoundException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when a service interface cannot be determined based on a heuristic evaluation of an implementation + * + * @version $Rev$ $Date$ + */ +public class ServiceTypeNotFoundException extends IntrospectionException { + private static final long serialVersionUID = -5124437274726947007L; + + public ServiceTypeNotFoundException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/UnknownContextTypeException.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/UnknownContextTypeException.java new file mode 100644 index 0000000000..bfaa759cd2 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/UnknownContextTypeException.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +/** + * Thrown when a method or field marked with {@link org.oasisopen.sca.annotation.Context} takes an unknown type + * + * @version $Rev$ $Date$ + */ +public class UnknownContextTypeException extends IllegalContextException { + private static final long serialVersionUID = 8125863714365422419L; + + public UnknownContextTypeException(String message) { + super(message); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/package.html b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/package.html new file mode 100644 index 0000000000..53614a627b --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/package.html @@ -0,0 +1,29 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<!-- + * 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. +--> +<html> +<head> +</head> +<body> +Base Package for the implementation.java extension. + +Whilst this package and its subpackages are not currently deemed to represent extension developers SPI, this extension has a special relationship with implementation.spring. Implementation.spring can be viewed as an extension of implementation.java. + +</body> +</html> diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationConstants.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationConstants.java new file mode 100644 index 0000000000..c23f5e4d39 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationConstants.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.xml; + +import javax.xml.namespace.QName; + +/** + * Constants for the Java Implementation. + * + * @version $Rev$ $Date$ + */ +public interface JavaImplementationConstants { + String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912"; + String IMPLEMENTATION_JAVA = "implementation.java"; + QName IMPLEMENTATION_JAVA_QNAME = new QName(SCA11_NS, "implementation.java"); + String CLASS = "class"; + String OPERATION = "operation"; + QName OPERATION_QNAME = new QName(SCA11_NS, OPERATION); +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java new file mode 100644 index 0000000000..ebf905ec44 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java @@ -0,0 +1,395 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static org.apache.tuscany.sca.implementation.java.xml.JavaImplementationConstants.CLASS; +import static org.apache.tuscany.sca.implementation.java.xml.JavaImplementationConstants.IMPLEMENTATION_JAVA; +import static org.apache.tuscany.sca.implementation.java.xml.JavaImplementationConstants.IMPLEMENTATION_JAVA_QNAME; +import static org.apache.tuscany.sca.implementation.java.xml.JavaImplementationConstants.SCA11_NS; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.xml.PolicySubjectProcessor; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.apache.tuscany.sca.interfacedef.Compatibility; +import org.apache.tuscany.sca.interfacedef.IncompatibleInterfaceContractException; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterfaceContract; +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.policy.ExtensionType; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * + * @version $Rev$ $Date$ + */ +public class JavaImplementationProcessor implements StAXArtifactProcessor<JavaImplementation> { + + private JavaImplementationFactory javaFactory; + private AssemblyFactory assemblyFactory; + private PolicyFactory policyFactory; + private PolicySubjectProcessor policyProcessor; + private StAXArtifactProcessor<Object> extensionProcessor; + private transient InterfaceContractMapper interfaceContractMapper; + + public JavaImplementationProcessor(ExtensionPointRegistry registry, StAXArtifactProcessor<?> staxProcessor) { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + this.policyFactory = modelFactories.getFactory(PolicyFactory.class); + this.javaFactory = modelFactories.getFactory(JavaImplementationFactory.class); + this.policyProcessor = new PolicySubjectProcessor(policyFactory); + this.extensionProcessor = (StAXArtifactProcessor<Object>)staxProcessor; + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + "impl-javaxml-validation-messages", + Severity.ERROR, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + "impl-javaxml-validation-messages", + Severity.ERROR, + model, + message, + ex); + monitor.problem(problem); + } + } + + public JavaImplementation read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + + // Read an <implementation.java> + JavaImplementation javaImplementation = javaFactory.createJavaImplementation(); + + ExtensionType implType = policyFactory.createImplementationType(); + implType.setType(getArtifactType()); + implType.setUnresolved(true); + javaImplementation.setExtensionType(implType); + + javaImplementation.setUnresolved(true); + javaImplementation.setName(reader.getAttributeValue(null, CLASS)); + + // Read policies + policyProcessor.readPolicies(javaImplementation, reader); + + // read operation elements if exists or skip unto end element + int event; + while (reader.hasNext()) { + event = reader.next(); + if (event == END_ELEMENT && IMPLEMENTATION_JAVA_QNAME.equals(reader.getName())) { + break; + } + } + return javaImplementation; + } + + public void write(JavaImplementation javaImplementation, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + + // Write an <implementation.java> + writer.writeStartElement(SCA11_NS, IMPLEMENTATION_JAVA); + policyProcessor.writePolicyAttributes(javaImplementation, writer); + + if (javaImplementation.getName() != null) { + writer.writeAttribute(CLASS, javaImplementation.getName()); + } + + writer.writeEndElement(); + } + + public void resolve(JavaImplementation javaImplementation, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + Monitor monitor = context.getMonitor(); + try { + ClassReference classReference = new ClassReference(javaImplementation.getName()); + classReference = resolver.resolveModel(ClassReference.class, classReference, context); + Class<?> javaClass = classReference.getJavaClass(); + if (javaClass == null) { + error(monitor, "ClassNotFoundException", resolver, javaImplementation.getName()); + //throw new ContributionResolveException(new ClassNotFoundException(javaImplementation.getName())); + return; + } + + javaImplementation.setJavaClass(javaClass); + + try { + javaFactory.createJavaImplementation(javaImplementation, javaImplementation.getJavaClass()); + } catch (IntrospectionException e) { + ContributionResolveException ce = new ContributionResolveException(e); + error(monitor, "ContributionResolveException", javaFactory, ce); + //throw ce; + return; + } + + checkNoStaticAnnotations(monitor, javaImplementation); + + postJAXWSProcessorResolve(resolver, javaImplementation, context); + + javaImplementation.setUnresolved(false); + + mergeComponentType(resolver, javaImplementation, context); + + // FIXME the introspector should always create at least one service + if (javaImplementation.getServices().isEmpty()) { + javaImplementation.getServices().add(assemblyFactory.createService()); + } + } catch (Throwable e) { + throw new ContributionResolveException("Resolving Java implementation: " + javaImplementation.getName() + + ", " + + e.getMessage(), e); + } // end try + } // end method + + private void checkNoStaticAnnotations(Monitor monitor, JavaImplementation javaImplementation) { + if (javaImplementation.getJavaClass() != null) { + Class<?> clazz = javaImplementation.getJavaClass(); + for (Method m : clazz.getMethods()) { + if (Modifier.isStatic(m.getModifiers())) { + for (Annotation a : m.getAnnotations()) { + if (a.annotationType().getName().startsWith("org.oasisopen.sca.annotation")) { + error(monitor, "IllegalSCAAnnotation", javaFactory, javaImplementation.getName(), m.getName()); + } + } + } + } + for (Field f : clazz.getFields()) { + if (Modifier.isStatic(f.getModifiers())) { + for (Annotation a : f.getAnnotations()) { + if (a.annotationType().getName().startsWith("org.oasisopen.sca.annotation")) { + error(monitor, "IllegalSCAAnnotation", javaFactory, javaImplementation.getName(), f.getName()); + } + } + } + } + } + } + + private JavaElementImpl getMemeber(JavaImplementation impl, String name, Class<?> type) { + String setter = JavaIntrospectionHelper.toSetter(name); + try { + Method method = impl.getJavaClass().getDeclaredMethod(setter, type); + int mod = method.getModifiers(); + if ((Modifier.isPublic(mod) || Modifier.isProtected(mod)) && (!Modifier.isStatic(mod))) { + return new JavaElementImpl(method, 0); + } + } catch (NoSuchMethodException e) { + Field field; + try { + field = impl.getJavaClass().getDeclaredField(name); + int mod = field.getModifiers(); + if ((Modifier.isPublic(mod) || Modifier.isProtected(mod)) && (!Modifier.isStatic(mod))) { + return new JavaElementImpl(field); + } + } catch (NoSuchFieldException e1) { + // Ignore + } + } + return null; + } + + /** + * Merge the componentType from introspection and external file + * @param resolver + * @param impl + */ + private void mergeComponentType(ModelResolver resolver, JavaImplementation impl, ProcessorContext context) { + // FIXME: Need to clarify how to merge + ComponentType componentType = getComponentType(resolver, impl, context); + if (componentType != null && !componentType.isUnresolved()) { + Map<String, Reference> refMap = new HashMap<String, Reference>(); + for (Reference ref : impl.getReferences()) { + refMap.put(ref.getName(), ref); + } + for (Reference reference : componentType.getReferences()) { + refMap.put(reference.getName(), reference); + } + impl.getReferences().clear(); + impl.getReferences().addAll(refMap.values()); + + // Try to match references by type + Map<String, JavaElementImpl> refMembers = impl.getReferenceMembers(); + for (Reference ref : impl.getReferences()) { + if (ref.getInterfaceContract() != null) { + Interface i = ref.getInterfaceContract().getInterface(); + if (i instanceof JavaInterface) { + Class<?> type = ((JavaInterface)i).getJavaClass(); + if (!refMembers.containsKey(ref.getName())) { + JavaElementImpl e = getMemeber(impl, ref.getName(), type); + if (e != null) { + refMembers.put(ref.getName(), e); + } + } + } + } + } + + Map<String, Service> serviceMap = new HashMap<String, Service>(); + for (Service svc : impl.getServices()) { + serviceMap.put(svc.getName(), svc); + } + for (Service service : componentType.getServices()) { + serviceMap.put(service.getName(), service); + } + impl.getServices().clear(); + impl.getServices().addAll(serviceMap.values()); + + Map<String, Property> propMap = new HashMap<String, Property>(); + for (Property prop : impl.getProperties()) { + propMap.put(prop.getName(), prop); + } + for (Property property : componentType.getProperties()) { + propMap.put(property.getName(), property); + } + impl.getProperties().clear(); + impl.getProperties().addAll(propMap.values()); + + } + } + + private void postJAXWSProcessorResolve(ModelResolver resolver, JavaImplementation impl, ProcessorContext context) + throws ContributionResolveException, IncompatibleInterfaceContractException { + for(Service service : impl.getServices()){ + JavaInterfaceContract javaInterfaceContract = (JavaInterfaceContract)service.getInterfaceContract(); + + JavaInterface javaInterface = (JavaInterface)javaInterfaceContract.getInterface(); + if (javaInterface.isUnresolved()){ + extensionProcessor.resolve(javaInterfaceContract, resolver, context); + } + + WSDLInterfaceContract wsdlInterfaceContract = (WSDLInterfaceContract)javaInterfaceContract.getNormalizedWSDLContract(); + if(wsdlInterfaceContract != null){ + // The user has explicitly associated a WSDL with the Java implementation + // using a @WebService(wsdlLocation="...") annotation + WSDLInterface wsdlInterface = (WSDLInterface)wsdlInterfaceContract.getInterface(); + if (wsdlInterface.isUnresolved()){ + //WSDLDefinition resolved = resolver.resolveModel(WSDLDefinition.class, wsdlInterface.getWsdlDefinition(), context); + extensionProcessor.resolve(wsdlInterfaceContract, resolver, context); + + // check that the Java and WSDL contracts are compatible + interfaceContractMapper.checkCompatibility(wsdlInterfaceContract, + javaInterfaceContract, + Compatibility.SUBSET, + false, + false); + + // retrieve the resolved WSDL interface + wsdlInterface = (WSDLInterface)wsdlInterfaceContract.getInterface(); + + // copy policy from the WSDL interface to the Java interface + javaInterface.getPolicySets().addAll(wsdlInterface.getPolicySets()); + javaInterface.getRequiredIntents().addAll(wsdlInterface.getRequiredIntents()); + + // copy policy from the WSDL interface to the component type service + service.getPolicySets().addAll(wsdlInterface.getPolicySets()); + service.getRequiredIntents().addAll(wsdlInterface.getRequiredIntents()); + + // TODO - is there anything else to be copied from the user specified WSDL? + } + } + } + } + + private ComponentType getComponentType(ModelResolver resolver, JavaImplementation impl, ProcessorContext context) { + String className = impl.getJavaClass().getName(); + String componentTypeURI = className.replace('.', '/') + ".componentType"; + ComponentType componentType = assemblyFactory.createComponentType(); + componentType.setUnresolved(true); + componentType.setURI(componentTypeURI); + componentType = resolver.resolveModel(ComponentType.class, componentType, context); + if (!componentType.isUnresolved()) { + return componentType; + } + return null; + } + + public QName getArtifactType() { + return IMPLEMENTATION_JAVA_QNAME; + } + + public Class<JavaImplementation> getModelType() { + return JavaImplementation.class; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor new file mode 100644 index 0000000000..2b09ee1d4d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Implementation class for the artifact processor extension +org.apache.tuscany.sca.implementation.java.xml.JavaImplementationProcessor;qname=http://docs.oasis-open.org/ns/opencsa/sca/200912#implementation.java,model=org.apache.tuscany.sca.implementation.java.JavaImplementation +
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.JavaImplementationFactory b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.JavaImplementationFactory new file mode 100644 index 0000000000..5c1a797cf5 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.JavaImplementationFactory @@ -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.implementation.java.DefaultJavaImplementationFactory
+
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor new file mode 100644 index 0000000000..d0723f466c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor @@ -0,0 +1,34 @@ +# Licensed to the Apache Software Foundation
+# 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
+# "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.
+#
+# NOTE: The ranking attribute is important for the JavaClassVistors
+# Some visitors need to be called after the others
+org.apache.tuscany.sca.implementation.java.introspect.impl.ConstructorProcessor;ranking=2000
+org.apache.tuscany.sca.implementation.java.introspect.impl.AllowsPassByReferenceProcessor;ranking=1900
+org.apache.tuscany.sca.implementation.java.introspect.impl.ComponentNameProcessor;ranking=1800
+org.apache.tuscany.sca.implementation.java.introspect.impl.ContextProcessor;ranking=1700
+org.apache.tuscany.sca.implementation.java.introspect.impl.DestroyProcessor;ranking=1600
+org.apache.tuscany.sca.implementation.java.introspect.impl.EagerInitProcessor;ranking=1500
+org.apache.tuscany.sca.implementation.java.introspect.impl.InitProcessor;ranking=1400
+org.apache.tuscany.sca.implementation.java.introspect.impl.PropertyProcessor;ranking=1300
+org.apache.tuscany.sca.implementation.java.introspect.impl.ReferenceProcessor;ranking=1200
+org.apache.tuscany.sca.implementation.java.introspect.impl.ResourceProcessor;ranking=1100
+org.apache.tuscany.sca.implementation.java.introspect.impl.ScopeProcessor;ranking=1000
+org.apache.tuscany.sca.implementation.java.introspect.impl.ServiceProcessor;ranking=900
+org.apache.tuscany.sca.implementation.java.introspect.impl.JAXWSProcessor;ranking=850
+org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor;ranking=800
+org.apache.tuscany.sca.implementation.java.introspect.impl.PolicyProcessor;ranking=700
\ No newline at end of file diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/impl-javaxml-validation-messages.properties b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/impl-javaxml-validation-messages.properties new file mode 100644 index 0000000000..4976bc0e9f --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/main/resources/impl-javaxml-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. +# +# +ClassNotFoundException = Class Not Found Exception: {0} +ContributionResolveException = Contribution Resolve Exception occured due to: +IllegalSCAAnnotation = [JCA9002] SCA annotations are not permitted on static members: {0}.{1} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/AddService.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/AddService.java new file mode 100644 index 0000000000..2532f7f409 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/AddService.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * Interface for the Add Service. + * + * @version $Rev$ $Date$ + */ +@Remotable +public interface AddService { + + double add(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/AddServiceImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/AddServiceImpl.java new file mode 100644 index 0000000000..64298b6275 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/AddServiceImpl.java @@ -0,0 +1,37 @@ +/* + * 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 calculator; + +import org.oasisopen.sca.annotation.EagerInit; +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Add service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +@EagerInit +public class AddServiceImpl implements AddService { + + public double add(double n1, double n2) { + return n1 + n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/CalculatorService.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/CalculatorService.java new file mode 100644 index 0000000000..e0cf23dc17 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/CalculatorService.java @@ -0,0 +1,36 @@ +/* + * 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 calculator; + +/** + * The Calculator service interface. + * + * @version $Rev$ $Date$ + */ +public interface CalculatorService { + + double add(double n1, double n2); + + double subtract(double n1, double n2); + + double multiply(double n1, double n2); + + double divide(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/CalculatorServiceImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/CalculatorServiceImpl.java new file mode 100644 index 0000000000..9f64dc8bca --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/CalculatorServiceImpl.java @@ -0,0 +1,73 @@ +/* + * 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 calculator; + +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Calculator service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class CalculatorServiceImpl implements CalculatorService { + + private AddService addService; + private SubtractService subtractService; + private MultiplyService multiplyService; + private DivideService divideService; + + @Reference + public void setAddService(AddService addService) { + this.addService = addService; + } + + @Reference + public void setSubtractService(SubtractService subtractService) { + this.subtractService = subtractService; + } + + @Reference + public void setDivideService(DivideService divideService) { + this.divideService = divideService; + } + + @Reference + public void setMultiplyService(MultiplyService multiplyService) { + this.multiplyService = multiplyService; + } + + public double add(double n1, double n2) { + return addService.add(n1, n2); + } + + public double subtract(double n1, double n2) { + return subtractService.subtract(n1, n2); + } + + public double multiply(double n1, double n2) { + return multiplyService.multiply(n1, n2); + } + + public double divide(double n1, double n2) { + return divideService.divide(n1, n2); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/DivideService.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/DivideService.java new file mode 100644 index 0000000000..9599c86292 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/DivideService.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 calculator; + +/** + * Interface for the Divide Service. + * + * @version $Rev$ $Date$ + */ +public interface DivideService { + + double divide(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/DivideServiceImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/DivideServiceImpl.java new file mode 100644 index 0000000000..f5b8ff5be0 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/DivideServiceImpl.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Divide service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class DivideServiceImpl implements DivideService { + + public double divide(double n1, double n2) { + return n1 / n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/MultiplyService.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/MultiplyService.java new file mode 100644 index 0000000000..7a3d06c15e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/MultiplyService.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 calculator; + +/** + * Interface for the Multiply Service. + * + * @version $Rev$ $Date$ + */ +public interface MultiplyService { + + double multiply(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/MultiplyServiceImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/MultiplyServiceImpl.java new file mode 100644 index 0000000000..d146d23153 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/MultiplyServiceImpl.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the Multiply service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class MultiplyServiceImpl implements MultiplyService { + + public double multiply(double n1, double n2) { + return n1 * n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/SubtractService.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/SubtractService.java new file mode 100644 index 0000000000..21af91f392 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/SubtractService.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 calculator; + +/** + * Interface for the Subtract Service. + * + * @version $Rev$ $Date$ + */ +public interface SubtractService { + + double subtract(double n1, double n2); + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/SubtractServiceImpl.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/SubtractServiceImpl.java new file mode 100644 index 0000000000..ffe09c3dde --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/calculator/SubtractServiceImpl.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package calculator; + +import org.oasisopen.sca.annotation.Scope; + +/** + * An implementation of the subtract service. + * + * @version $Rev$ $Date$ + */ +@Scope("COMPOSITE") +public class SubtractServiceImpl implements SubtractService { + + public double subtract(double n1, double n2) { + return n1 - n2; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java new file mode 100644 index 0000000000..b77aea4c78 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractProcessorTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + + +/** + * Base class to simulate the processor sequences + * + * @version $Rev$ $Date$ + */ +public abstract class AbstractProcessorTest { + protected AssemblyFactory factory; + protected JavaInterfaceFactory javaFactory; + protected ConstructorProcessor constructorProcessor; + protected ReferenceProcessor referenceProcessor; + protected PropertyProcessor propertyProcessor; + private ResourceProcessor resourceProcessor; + + + protected AbstractProcessorTest() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + factory = new DefaultAssemblyFactory(registry); + javaFactory = new DefaultJavaInterfaceFactory(registry); + referenceProcessor = new ReferenceProcessor(factory, javaFactory); + propertyProcessor = new PropertyProcessor(registry); + resourceProcessor = new ResourceProcessor(factory); + constructorProcessor = new ConstructorProcessor(factory); + referenceProcessor = new ReferenceProcessor(factory, javaFactory); + propertyProcessor = new PropertyProcessor(registry); + } + + protected <T> void visitConstructor(Constructor<T> constructor, + JavaImplementation type) throws IntrospectionException { + constructorProcessor.visitConstructor(constructor, type); + JavaConstructorImpl<?> definition = type.getConstructor(); + if (definition == null) { + definition = new JavaConstructorImpl<T>(constructor); + type.getConstructors().put(constructor, definition); + } + + JavaParameterImpl[] parameters = definition.getParameters(); + for (int i = 0; i < parameters.length; i++) { + referenceProcessor.visitConstructorParameter(parameters[i], type); + propertyProcessor.visitConstructorParameter(parameters[i], type); + resourceProcessor.visitConstructorParameter(parameters[i], type); + // monitorProcessor.visitConstructorParameter(parameters[i], type); + } + } + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessorTestCase.java new file mode 100644 index 0000000000..ce79ae2045 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AllowsPassByReferenceProcessorTestCase.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.AllowsPassByReference; + +/** + * @version $Rev$ $Date$ + */ +public class AllowsPassByReferenceProcessorTestCase { + + JavaImplementation type; + AllowsPassByReferenceProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testClassAnnotation() throws Exception { + processor.visitClass(Foo.class, type); + assertEquals(true, type.isAllowsPassByReference()); + + processor.visitClass(Bar.class, type); + assertEquals(false, type.isAllowsPassByReference()); + + Method m1 = Bar.class.getMethod("m1", new Class[] {}); + processor.visitMethod(m1, type); + assertTrue(type.isAllowsPassByReference(m1)); + } + + @Before + public void setUp() throws Exception { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + processor = new AllowsPassByReferenceProcessor(new DefaultAssemblyFactory()); + } + + @AllowsPassByReference + private class Foo { + } + + // no annotation + private class Bar { + @AllowsPassByReference + public void m1() { + + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessorTestCase.java new file mode 100644 index 0000000000..512e34a0fa --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorProcessorTestCase.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getReference; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorProcessorTestCase { + private ConstructorProcessor processor = new ConstructorProcessor(new DefaultAssemblyFactory()); + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testDuplicateConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitClass(BadFoo.class, type); + fail(); + } catch (DuplicateConstructorException e) { + // expected + } + } + + @Test + public void testConstructorAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor1 = Foo.class.getConstructor(String.class); + processor.visitConstructor(ctor1, type); + assertEquals("foo", type.getConstructor().getParameters()[0].getName()); + } + + @Test + public void testNoAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<NoAnnotation> ctor1 = NoAnnotation.class.getConstructor(); + processor.visitConstructor(ctor1, type); + assertNull(type.getConstructor()); + } + + @Test + public void testBadAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadAnnotation> ctor1 = BadAnnotation.class.getConstructor(String.class, Foo.class); + try { + processor.visitConstructor(ctor1, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testMixedParameters() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Mixed> ctor1 = Mixed.class.getConstructor(String.class, String.class, String.class); + processor.visitConstructor(ctor1, type); + + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(registry); + JavaInterfaceFactory javaFactory = new DefaultJavaInterfaceFactory(registry); + ReferenceProcessor referenceProcessor = new ReferenceProcessor(assemblyFactory, javaFactory); + PropertyProcessor propertyProcessor = new PropertyProcessor(registry); + JavaParameterImpl[] parameters = type.getConstructor().getParameters(); + for (int i = 0; i < parameters.length; i++) { + referenceProcessor.visitConstructorParameter(parameters[i], type); + propertyProcessor.visitConstructorParameter(parameters[i], type); + } + + assertEquals("_ref0", parameters[0].getName()); + assertEquals("foo", parameters[1].getName()); + assertEquals("bar", parameters[2].getName()); + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor("foo") + public BadFoo(String foo) { + + } + + @org.oasisopen.sca.annotation.Constructor( {"foo", "bar"}) + public BadFoo(String foo, String bar) { + + } + } + + private static class Foo { + @org.oasisopen.sca.annotation.Constructor("foo") + public Foo(@Property String foo) { + + } + } + + private static class NoAnnotation { + public NoAnnotation() { + } + } + + private static class BadAnnotation { + @org.oasisopen.sca.annotation.Constructor("foo") + public BadAnnotation(String foo, Foo ref) { + } + } + + public static final class Mixed { + @org.oasisopen.sca.annotation.Constructor + public Mixed(@Reference(name="_ref0") + String param1, @Property(name = "foo") + String param2, @Reference(name = "bar") + String param3) { + } + } + + public static final class Multiple { + @org.oasisopen.sca.annotation.Constructor + public Multiple(@Reference(name="_ref0") + Collection<String> param1, @Property(name = "foo") + String[] param2, @Reference(name = "bar", required = true) + List<String> param3, @Property(name = "abc") + Set<String> param4, @Reference(name = "xyz") + String[] param5) { + } + } + + @Test + public void testMultiplicity() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Multiple> ctor1 = Multiple.class.getConstructor(Collection.class, + String[].class, + List.class, + Set.class, + String[].class); + processor.visitConstructor(ctor1, type); + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(); + JavaInterfaceFactory javaFactory = new DefaultJavaInterfaceFactory(registry); + ReferenceProcessor referenceProcessor = new ReferenceProcessor(assemblyFactory, javaFactory); + PropertyProcessor propertyProcessor = new PropertyProcessor(registry); + JavaParameterImpl[] parameters = type.getConstructor().getParameters(); + for (int i = 0; i < parameters.length; i++) { + referenceProcessor.visitConstructorParameter(parameters[i], type); + propertyProcessor.visitConstructorParameter(parameters[i], type); + } + + org.apache.tuscany.sca.assembly.Reference ref0 = getReference(type, "_ref0"); + assertNotNull(ref0); + assertEquals(Multiplicity.ONE_N, ref0.getMultiplicity()); + org.apache.tuscany.sca.assembly.Reference ref1 = getReference(type, "bar"); + assertNotNull(ref1); + assertEquals(Multiplicity.ONE_N, ref1.getMultiplicity()); + org.apache.tuscany.sca.assembly.Reference ref2 = getReference(type, "xyz"); + assertNotNull(ref2); + assertEquals(Multiplicity.ONE_N, ref2.getMultiplicity()); + org.apache.tuscany.sca.assembly.Property prop1 = getProperty(type, "foo"); + assertNotNull(prop1); + assertTrue(prop1.isMany()); + org.apache.tuscany.sca.assembly.Property prop2 = getProperty(type, "abc"); + assertNotNull(prop2); + assertTrue(prop2.isMany()); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java new file mode 100644 index 0000000000..83e26dff84 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.List; + +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorPropertyTestCase extends AbstractProcessorTest { + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testProperty() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + visitConstructor(ctor, type); + org.apache.tuscany.sca.assembly.Property property = getProperty(type, "myProp"); + assertTrue(property.isMustSupply()); + assertEquals("myProp", property.getName()); + } + + @Test + public void testTwoPropertiesSameType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + visitConstructor(ctor, type); + assertNotNull(getProperty(type, "myProp1")); + assertNotNull(getProperty(type, "myProp2")); + } + + @Test + public void testDuplicateProperty() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testNoName() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidPropertyException e) { + // expected + } + } + + @Test + public void testNamesOnConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + visitConstructor(ctor, type); + assertNotNull(getProperty(type, "myProp")); + } + + @Test + public void testInvalidNumberOfNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testNoMatchingNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(List.class, List.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + +// public void testMultiplicityRequired() throws Exception { + // TODO multiplicity +// } + + + + private static class Foo { + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Property(name = "myProp", required = true)String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor("myProp") + public Foo(@Property Integer prop) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Property(name = "myProp1")String prop1, @Property(name = "myProp2")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Property List prop) { + + } + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor() + public BadFoo(@Property(name = "myProp")String prop1, @Property(name = "myProp")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public BadFoo(@Property String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor("myProp") + public BadFoo(@Property Integer prop, @Property Integer prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor({"myRef", "myRef2"}) + public BadFoo(@Property List ref, @Property(name = "myOtherRef")List ref2) { + + } + + } + + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java new file mode 100644 index 0000000000..eeeee851e4 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getReference; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.List; + +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.junit.Ignore; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +//import com.sun.xml.internal.ws.model.JavaMethodImpl; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorReferenceTestCase extends AbstractProcessorTest { + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testReference() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + visitConstructor(ctor, type); + org.apache.tuscany.sca.assembly.Reference reference = getReference(type, "myRef"); + assertEquals(Multiplicity.ONE_ONE, reference.getMultiplicity()); + assertEquals("myRef", reference.getName()); + } + + @Test + public void testTwoReferencesSameType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + visitConstructor(ctor, type); + assertNotNull(getReference(type, "myRef1")); + assertNotNull(getReference(type, "myRef2")); + } + + @Test + public void testDuplicateProperty() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (DuplicateReferenceException e) { + // expected + } + } + + @Test + public void testNoName() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<NoNameFoo> ctor = NoNameFoo.class.getConstructor(String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidReferenceException e) { + //expected + } + } + + @Test + public void testNamesOnConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + visitConstructor(ctor, type); + assertNotNull(getReference(type, "myRef2")); + } + + @Test + public void testInvalidNumberOfNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testNoMatchingNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(List.class, List.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidReferenceException e) { + // expected + } + } + + +// public void testMultiplicityRequired() throws Exception { + // TODO multiplicity +// } + + private static class Foo { + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Reference(name = "myRef", required = true)String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Reference(name = "myRef1")String prop1, @Reference(name = "myRef2")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor("myRef2") + public Foo(@Reference(name = "myRef2") Integer prop) { + + } + + @org.oasisopen.sca.annotation.Constructor() + public Foo(@Reference(name = "myRef3") List prop) { + + } + } + + private static class NoNameFoo { + + @org.oasisopen.sca.annotation.Constructor + public NoNameFoo(@Reference String prop) { + + } + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Reference(name = "myRef")String prop1, @Reference(name = "myRef")String prop2) { + + } + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Reference String prop) { + + } + + @org.oasisopen.sca.annotation.Constructor("myRef") + public BadFoo(@Reference Integer ref, @Reference Integer ref2) { + + } + + @org.oasisopen.sca.annotation.Constructor({"myRef", "myRef2"}) + public BadFoo(@Reference List ref, @Reference(name = "myOtherRef")List ref2) { + + } + + } + + + + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorResourceTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorResourceTestCase.java new file mode 100644 index 0000000000..e8d26f6103 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorResourceTestCase.java @@ -0,0 +1,164 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; +import java.util.List; + +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class ConstructorResourceTestCase extends AbstractProcessorTest { + + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testResource() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + visitConstructor(ctor, type); + org.apache.tuscany.sca.implementation.java.JavaResourceImpl resource = type.getResources().get("myResource"); + assertFalse(resource.isOptional()); + } + + @Test + public void testTwoResourcesSameType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + visitConstructor(ctor, type); + assertNotNull(type.getResources().get("myResource1")); + assertNotNull(type.getResources().get("myResource2")); + } + + @Test + public void testDuplicateResource() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<BadFoo> ctor = BadFoo.class.getConstructor(String.class, String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (DuplicateResourceException e) { + // expected + } + } + + @Test + public void testNoName() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ConstructorResourceTestCase.BadFoo> ctor = + ConstructorResourceTestCase.BadFoo.class.getConstructor(String.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidResourceException e) { + // expected + } + } + + @Test + public void testNamesOnConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(Integer.class); + visitConstructor(ctor, type); + assertNotNull(type.getResources().get("myResource")); + } + + @Test + public void testInvalidNumberOfNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ConstructorResourceTestCase.BadFoo> ctor = + ConstructorResourceTestCase.BadFoo.class.getConstructor(Integer.class, Integer.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + @Test + public void testNoMatchingNames() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ConstructorResourceTestCase.BadFoo> ctor = + ConstructorResourceTestCase.BadFoo.class.getConstructor(List.class, List.class); + try { + visitConstructor(ctor, type); + fail(); + } catch (InvalidConstructorException e) { + // expected + } + } + + private static class Foo { + + @org.oasisopen.sca.annotation.Constructor + public Foo(@Resource(name = "myResource") String resource) { + + } + + @org.oasisopen.sca.annotation.Constructor("myResource") + public Foo(@Resource Integer resource) { + + } + + @org.oasisopen.sca.annotation.Constructor + public Foo(@Resource(name = "myResource1") String res1, @Resource(name = "myResource2") String res2) { + + } + + @org.oasisopen.sca.annotation.Constructor + public Foo(@Resource List res) { + + } + } + + private static class BadFoo { + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Resource(name = "myResource") String res1, @Resource(name = "myResource") String res2) { + + } + + @org.oasisopen.sca.annotation.Constructor + public BadFoo(@Resource String res) { + + } + + @org.oasisopen.sca.annotation.Constructor("myProp") + public BadFoo(@Resource Integer res, @Resource Integer res2) { + + } + + @org.oasisopen.sca.annotation.Constructor({"myRes", "myRes2"}) + public BadFoo(@Resource List res, @Resource(name = "myOtherRes") List res2) { + + } + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessorTestCase.java new file mode 100644 index 0000000000..124c83e9e2 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ContextProcessorTestCase.java @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.ComponentContext; +import org.oasisopen.sca.RequestContext; +import org.oasisopen.sca.annotation.ComponentName; +import org.oasisopen.sca.annotation.Context; + +/** + * @version $Rev$ $Date$ + */ +public class ContextProcessorTestCase { + private ContextProcessor processor; + private ComponentNameProcessor nameProcessor; + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testComponentContextMethod() throws Exception { + Method method = Foo.class.getMethod("setContext", ComponentContext.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitMethod(method, type); + assertNotNull(type.getResources().get("context")); + } + + @Test + public void testComponentContextField() throws Exception { + Field field = Foo.class.getDeclaredField("context"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitField(field, type); + assertNotNull(type.getResources().get("context")); + } + + @Test + public void testRequestContextMethod() throws Exception { + Method method = Foo.class.getMethod("setRequestContext", RequestContext.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitMethod(method, type); + assertNotNull(type.getResources().get("requestContext")); + } + + @Test + public void testRequestContextField() throws Exception { + Field field = Foo.class.getDeclaredField("requestContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitField(field, type); + assertNotNull(type.getResources().get("requestContext")); + } + + @Test + public void testComponentNameMethod() throws Exception { + Method method = Foo.class.getMethod("setName", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + nameProcessor.visitMethod(method, type); + assertNotNull(type.getResources().get("name")); + } + + @Test + public void testComponentNameField() throws Exception { + Field field = Foo.class.getDeclaredField("name"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + nameProcessor.visitField(field, type); + assertNotNull(type.getResources().get("name")); + } + + @Test + public void testInvalidParamType() throws Exception { + Method method = Foo.class.getMethod("setContext", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitMethod(method, type); + fail(); + } catch (UnknownContextTypeException e) { + // expected + } + } + + @Test + public void testInvalidParamTypeField() throws Exception { + Field field = Foo.class.getDeclaredField("badContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitField(field, type); + fail(); + } catch (UnknownContextTypeException e) { + // expected + } + } + + + @Test + public void testInvalidParamNum() throws Exception { + Method method = Foo.class.getMethod("setContext", ComponentContext.class, String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalContextException e) { + // expected + } + } + + @Test + public void testInvalidNoParams() throws Exception { + Method method = Foo.class.getMethod("setContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalContextException e) { + // expected + } + } + + @Test + public void testNoContext() throws Exception { + Method method = Foo.class.getMethod("noContext", ComponentContext.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitMethod(method, type); + assertEquals(0, type.getResources().size()); + } + + @Test + public void testNoContextField() throws Exception { + Field field = Foo.class.getDeclaredField("noContext"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitField(field, type); + assertEquals(0, type.getResources().size()); + } + + @Before + public void setUp() throws Exception { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + processor = new ContextProcessor(new DefaultAssemblyFactory()); + nameProcessor = new ComponentNameProcessor(new DefaultAssemblyFactory()); + } + + private class Foo { + @Context + protected ComponentContext context; + + @ComponentName + protected String name; + + @Context + protected Object badContext; + + protected ComponentContext noContext; + + @Context + protected RequestContext requestContext; + + @Context + public void setContext(ComponentContext context) { + + } + + @ComponentName + public void setName(String name) { + + } + + @Context + public void setContext(String context) { + + } + + @Context + public void setContext(ComponentContext context, String string) { + + } + + @Context + public void setContext() { + + } + + public void noContext(ComponentContext context) { + + } + + @Context + public void setRequestContext(RequestContext requestContext) { + this.requestContext = requestContext; + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessorTestCase.java new file mode 100644 index 0000000000..b524803062 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DestroyProcessorTestCase.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Destroy; + +/** + * @version $Rev$ $Date$ + */ +public class DestroyProcessorTestCase { + + private AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(); + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Foo.class.getMethod("destroy"); + processor.visitMethod(method, type); + assertNotNull(type.getDestroyMethod()); + } + + @Test + public void testBadDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getMethod("badDestroy", String.class); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalDestructorException e) { + // expected + } + } + + @Test + public void testTwoDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getMethod("destroy"); + Method method2 = Bar.class.getMethod("destroy2"); + processor.visitMethod(method, type); + try { + processor.visitMethod(method2, type); + fail(); + } catch (DuplicateDestructorException e) { + // expected + } + } + + @Test + public void testProtectedDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getDeclaredMethod("protectedDestroy"); + try { + processor.visitMethod(method, type); + } catch (IllegalDestructorException e) { + fail(); + } + } + + @Test + public void testPrivateDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getDeclaredMethod("privateDestroy"); + try { + processor.visitMethod(method, type); + } catch (IllegalDestructorException e) { + fail(); + } + } + + @Test + public void testDefaultVisibilityDestroy() throws Exception { + DestroyProcessor processor = new DestroyProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = Bar.class.getDeclaredMethod("defaultVisibilityDestroy"); + try { + processor.visitMethod(method, type); + } catch (IllegalDestructorException e) { + fail(); + } + } + + private class Foo { + + @Destroy + public void destroy() { + } + } + + + private class Bar { + + @Destroy + public void destroy() { + } + + @Destroy + public void destroy2() { + } + + @Destroy + public void badDestroy(String foo) { + } + + @Destroy + protected void protectedDestroy(){ + } + + @Destroy + private void privateDestroy(){ + } + + @Destroy + void defaultVisibilityDestroy(){ + } + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessorTestCase.java new file mode 100644 index 0000000000..04d87f60fe --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/EagerInitProcessorTestCase.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.EagerInit; + +/** + * @version $Rev$ $Date$ + */ +public class EagerInitProcessorTestCase { + + private AssemblyFactory assemblyFactory = new DefaultAssemblyFactory(); + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + @Test + public void testNoLevel() throws IntrospectionException { + EagerInitProcessor processor = new EagerInitProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(Level.class, type); + } + + @Test + public void testSubclass() throws IntrospectionException { + EagerInitProcessor processor = new EagerInitProcessor(assemblyFactory); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(SubClass.class, type); + } + + @EagerInit + private class Level { + } + + private class SubClass extends Level { + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicAndPropertyTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicAndPropertyTestCase.java new file mode 100644 index 0000000000..39a2980b2b --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicAndPropertyTestCase.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; + +/** + * @version $Rev$ $Date$ + */ +public class HeuristicAndPropertyTestCase { + + private PropertyProcessor propertyProcessor; + private HeuristicPojoProcessor heuristicProcessor; + private JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + + /** + * Verifies the property and heuristic processors don't collide + */ + @SuppressWarnings("unchecked") + @Test + public void testPropertyProcessorWithHeuristicProcessor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor ctor = Foo.class.getConstructor(String.class); + type.setConstructor(new JavaConstructorImpl(ctor)); + propertyProcessor.visitConstructorParameter(type.getConstructor().getParameters()[0], type); + heuristicProcessor.visitEnd(Foo.class, type); + assertEquals(1, type.getProperties().size()); + assertEquals("foo", type.getProperties().get(0).getName()); + } + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + propertyProcessor = new PropertyProcessor(registry); + heuristicProcessor = new HeuristicPojoProcessor(registry); + } + + public static class Foo { + public Foo(@Property(name = "foo") + String prop) { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicConstructorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicConstructorTestCase.java new file mode 100644 index 0000000000..394857576d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicConstructorTestCase.java @@ -0,0 +1,330 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Remotable; + +/** + * @version $Rev$ $Date$ + */ +public class HeuristicConstructorTestCase extends AbstractProcessorTest { + + private AssemblyFactory factory; + private JavaInterfaceFactory javaFactory; + private HeuristicPojoProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + public HeuristicConstructorTestCase() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + factory = new DefaultAssemblyFactory(registry); + javaFactory = new DefaultJavaInterfaceFactory(registry); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + processor = new HeuristicPojoProcessor(factory, javaFactory); + } + + private <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + for (Constructor<?> constructor : clazz.getConstructors()) { + visitConstructor(constructor, type); + } + processor.visitEnd(clazz, type); + } + + /** + * Verifies a single constructor is chosen with a parameter as the type + */ + @Test + public void testSingleConstructorWithParam() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Property prop = factory.createProperty(); + prop.setName("foo"); + type.getProperties().add(prop); + // Hack to add a property member + JavaElementImpl element = new JavaElementImpl("foo", String.class, null); + type.getPropertyMembers().put("foo", element); + visitEnd(Foo1.class, type); + assertNotNull(type.getConstructor().getConstructor()); + assertEquals("foo", type.getConstructor().getParameters()[0].getName()); + } + + /** + * Verifies a single constructor is chosen with a reference as the type + */ + @Test + public void testSingleConstructorWithRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Reference ref = factory.createReference(); + ref.setName("foo"); + type.getReferences().add(ref); + type.getReferenceMembers().put("foo", new JavaElementImpl("foo", String.class, null)); + visitEnd(Foo1.class, type); + assertNotNull(type.getConstructor().getConstructor()); + assertEquals("foo", type.getConstructor().getParameters()[0].getName()); + } + + /** + * Verifies a single constructor is chosen with a property and a reference + * as the type + */ + @Test + public void testSingleConstructorWithPropRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + + org.apache.tuscany.sca.assembly.Property prop = factory.createProperty(); + prop.setName("foo"); + type.getProperties().add(prop); + // Hack to add a property member + JavaElementImpl element = new JavaElementImpl("foo", String.class, null); + type.getPropertyMembers().put("foo", element); + + org.apache.tuscany.sca.assembly.Reference ref = ModelHelper.createReference(factory, javaFactory, "ref", Foo1.class); + type.getReferences().add(ref); + type.getReferenceMembers().put("ref", new JavaElementImpl("ref", Foo1.class, null)); + visitEnd(Foo2.class, type); + assertNotNull(type.getConstructor().getConstructor()); + assertEquals(2, type.getConstructor().getParameters().length); + } + + @Test + public void testSingleConstructorResolvableParam() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo5.class, type); + assertEquals(String.class, type.getPropertyMembers().get("string").getType()); + } + + @Test + public void testSingleConstructorResolvableRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo6.class, type); + assertTrue(ModelHelper.matches(ModelHelper.getReference(type, "ref"), Ref.class)); + } + + @Test + public void testSingleConstructorAmbiguousRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Reference ref = ModelHelper.createReference(factory, javaFactory, "ref", Foo1.class); + type.getReferences().add(ref); + type.getReferenceMembers().put("ref", new JavaElementImpl("ref", Foo1.class, null)); + org.apache.tuscany.sca.assembly.Reference ref2 = ModelHelper.createReference(factory, javaFactory, "ref2", Foo1.class); + type.getReferences().add(ref2); + type.getReferenceMembers().put("ref2", new JavaElementImpl("ref2", Foo1.class, null)); + try { + visitEnd(Foo4.class, type); + fail(); + } catch (AmbiguousConstructorException e) { + // expected + } + } + + @Test + public void testConstructorPropertyAnnotatedParamsOnly() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo7.class, type); + assertNotNull(getProperty(type, "myProp")); + } + + @Test + public void testConstructorReferenceAnnotatedParamsOnly() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo8.class, type); + assertNotNull(ModelHelper.getReference(type, "myRef")); + } + + @Test + public void testDefaultConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo3.class, type); + assertNotNull(type.getConstructor().getConstructor()); + } + + @Test + public void testSameTypesButAnnotated() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo12.class, type); + assertEquals(2, type.getProperties().size()); + assertNotNull(getProperty(type, "prop1")); + assertNotNull(getProperty(type, "prop2")); + } + + /** + * Verifies processing executes with additional extension annotations + */ + @Test + public void testRandomAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo11.class, type); + assertEquals(1, type.getProperties().size()); + assertNotNull(getProperty(type, "prop1")); + } + + @Test + public void testPrivateConstructor() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + visitEnd(Foo14.class, type); + fail(); + } catch (NoConstructorException e) { + // expected + } + } + + @Test + public void testMultipleConstructors() throws Exception { + // throw new UnsupportedOperationException("Finish heuristic multiple + // constructors - Foo10"); + } + + public static class Foo1 { + public Foo1(String val) { + } + } + + public static class Foo2 { + public Foo2(String val, Foo1 ref) { + } + } + + public static class Foo3 { + } + + public static class Foo4 { + public Foo4(Foo1 ref) { + } + } + + public static class Prop { + + } + + @Remotable + public static interface Ref { + + } + + public static class Foo5 { + public Foo5(String val) { + } + } + + public static class Foo6 { + public Foo6(Ref ref) { + } + } + + public static class Foo7 { + public Foo7(@Property(name = "myProp") + String prop) { + } + } + + public static class Foo8 { + public Foo8(@Reference(name = "myRef") + String ref) { + } + } + + public static class Foo9 { + public Foo9(@Reference(name = "myRef") + String ref) { + } + } + + public static class Foo10 { + + public Foo10() { + } + + public Foo10(String prop) { + } + + public Foo10(@Property(name = "prop1") + String prop1, @Property(name = "prop2") + String prop2) { + + } + } + + public static class Foo11 { + + public Foo11(@Property(name = "prop1") + String prop, @Baz + String baz) { + } + } + + public static class Foo12 { + + public Foo12(@Property(name = "prop1") + String prop, @Property(name = "prop2") + String baz) { + } + } + + public @interface Baz { + + } + + public static class Foo13 { + public Foo13(@Reference + String foo) { + } + } + + public static final class Foo14 { + private Foo14() { + } + } + + public static final class Foo15 { + public Foo15(@Reference + String param1, @Reference + String param2) { + } + } + + public static final class Foo16 { + public Foo16(@Reference + String param1, @Property(name = "foo") + String param2, @Reference(name = "bar") + String param3) { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessorTestCase.java new file mode 100644 index 0000000000..a850f86995 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeuristicPojoProcessorTestCase.java @@ -0,0 +1,581 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Constructor; +import java.util.Collection; +import java.util.List; + +import javax.jws.WebService; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Remotable; +import org.oasisopen.sca.annotation.Service; + +/** + * Verifies component type information is properly introspected from an unadorned + * POJO according to the SCA Java Client and Implementation Model Specification + * + * @version $Rev$ $Date$ + */ +public class HeuristicPojoProcessorTestCase extends AbstractProcessorTest { + + private org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + public HeuristicPojoProcessorTestCase() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new HeuristicPojoProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + private <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + for (Constructor<?> constructor : clazz.getConstructors()) { + visitConstructor(constructor, type); + } + processor.visitEnd(clazz, type); + } + + /** + * Verifies a single service interface is computed when only one interface + * is implemented + */ + @Test + public void testSingleInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<SingleInterfaceImpl> ctor = SingleInterfaceImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<SingleInterfaceImpl>(ctor)); + processor.visitEnd(SingleInterfaceImpl.class, type); + assertEquals(1, type.getServices().size()); + assertTrue(ModelHelper.matches(ModelHelper.getService(type, PropertyInterface.class.getSimpleName()), + PropertyInterface.class)); + assertTrue(type.getProperties().isEmpty()); + assertTrue(type.getReferences().isEmpty()); + } + + /** + * Verifies property and reference setters are computed + */ + @Test + public void testPropertyReference() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<SingleInterfaceWithPropertyReferenceImpl> ctor = SingleInterfaceWithPropertyReferenceImpl.class + .getConstructor(); + type.setConstructor(new JavaConstructorImpl<SingleInterfaceWithPropertyReferenceImpl>(ctor)); + processor.visitEnd(SingleInterfaceWithPropertyReferenceImpl.class, type); + assertEquals(1, type.getServices().size()); + assertTrue(ModelHelper + .matches(ModelHelper.getService(type, Interface1.class.getSimpleName()), Interface1.class)); + assertEquals(1, type.getProperties().size()); + org.apache.tuscany.sca.assembly.Property prop = ModelHelper.getProperty(type, "property"); + assertNotNull(prop); + assertEquals(ComplexProperty.class, type.getPropertyMembers().get("property").getType()); + assertEquals(1, type.getReferences().size()); + assertTrue(ModelHelper.matches(ModelHelper.getReference(type, "reference"), Ref.class)); + } + + /** + * Verifies that a property setter is not introspected if an analogous + * operation is in the service interface + */ + @Test + public void testPropertySetterInInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<SingleInterfaceImpl> ctor = SingleInterfaceImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<SingleInterfaceImpl>(ctor)); + processor.visitEnd(SingleInterfaceImpl.class, type); + assertEquals(0, type.getProperties().size()); + } + + /** + * Verifies that a reference setter is not introspected if an analogous + * operation is in the service interface + */ + @Test + public void testReferenceSetterInInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<RefInterfaceImpl> ctor = RefInterfaceImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<RefInterfaceImpl>(ctor)); + processor.visitEnd(RefInterfaceImpl.class, type); + assertEquals(0, type.getReferences().size()); + } + + /** + * Verifies collection generic types or array types are introspected as + * references according to specification rules + */ + @Test + public void testReferenceCollectionType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ReferenceCollectionImpl> ctor = ReferenceCollectionImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<ReferenceCollectionImpl>(ctor)); + processor.visitEnd(ReferenceCollectionImpl.class, type); + assertEquals(1, type.getProperties().size()); + assertEquals(3, type.getReferences().size()); + } + + /** + * Verifies collection generic types or array types are introspected as + * properties according to specification rules + */ + @Test + public void testPropertyCollectionType() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<PropertyCollectionImpl> ctor = PropertyCollectionImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<PropertyCollectionImpl>(ctor)); + processor.visitEnd(PropertyCollectionImpl.class, type); + assertEquals(0, type.getReferences().size()); + assertEquals(4, type.getProperties().size()); + } + + /** + * Verifies references are calculated when the type marked with is + * + * @Remotable + */ + @Test + public void testRemotableRef() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<RemotableRefImpl> ctor = RemotableRefImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<RemotableRefImpl>(ctor)); + processor.visitEnd(RemotableRefImpl.class, type); + assertEquals(2, type.getReferences().size()); + assertEquals(0, type.getProperties().size()); + } + + @Test + public void testParentInterface() throws IntrospectionException, NoSuchMethodException { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Child> ctor = Child.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<Child>(ctor)); + processor.visitEnd(Child.class, type); + assertNotNull(ModelHelper.getService(type, Interface1.class.getSimpleName())); + } + + /** + * Verifies a service interface is calculated when only props and refs are + * given + */ + @Test + public void testExcludedPropertyAndReference() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + org.apache.tuscany.sca.assembly.Reference ref = factory.createReference(); + ref.setName("reference"); + type.getReferences().add(ref); + type.getReferenceMembers().put("reference", new JavaElementImpl("reference", Ref.class, null)); + org.apache.tuscany.sca.assembly.Reference ref2 = factory.createReference(); + ref2.setName("reference2"); + type.getReferences().add(ref2); + type.getReferenceMembers().put("reference2", new JavaElementImpl("reference2", Ref.class, null)); + org.apache.tuscany.sca.assembly.Property prop1 = factory.createProperty(); + prop1.setName("string1"); + type.getProperties().add(prop1); + type.getPropertyMembers().put("string1", new JavaElementImpl("string1", String.class, null)); + org.apache.tuscany.sca.assembly.Property prop2 = factory.createProperty(); + prop2.setName("string2"); + type.getProperties().add(prop2); + type.getPropertyMembers().put("string2", new JavaElementImpl("string2", String.class, null)); + visitEnd(MockService.class, type); + assertEquals(1, type.getServices().size()); + } + + @Test + public void testProtectedRemotableRefField() throws IntrospectionException, NoSuchMethodException { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ProtectedRemotableRefFieldImpl> ctor = ProtectedRemotableRefFieldImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<ProtectedRemotableRefFieldImpl>(ctor)); + processor.visitEnd(ProtectedRemotableRefFieldImpl.class, type); + assertNotNull(ModelHelper.getReference(type, "otherRef")); + } + + @Test + public void testProtectedRemotableRefMethod() throws IntrospectionException, NoSuchMethodException { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<ProtectedRemotableRefMethodImpl> ctor = ProtectedRemotableRefMethodImpl.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<ProtectedRemotableRefMethodImpl>(ctor)); + processor.visitEnd(ProtectedRemotableRefMethodImpl.class, type); + assertNotNull(ModelHelper.getReference(type, "otherRef")); + } + + @Test + public void testSetDataTypes() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<PropertyIntTypeOnConstructor> ctor = PropertyIntTypeOnConstructor.class.getConstructor(); + type.setConstructor(new JavaConstructorImpl<PropertyIntTypeOnConstructor>(ctor)); + processor.visitEnd(PropertyIntTypeOnConstructor.class, type); + org.apache.tuscany.sca.assembly.Property foo = ModelHelper.getProperty(type, "foo"); + assertEquals(int.class, type.getPropertyMembers().get("foo").getType()); + // assertEquals(new QName(JavaXMLMapper.URI_2001_SCHEMA_XSD, "int"), foo.getXSDType()); + } + + /** + * Errata for Java Component Implementation Specification v1.0 corrects the algorithm for determining + * references of an unannotated POJO (section 1.2.7). This test makes sure that the earlier implementation + * is corrected as per the errata. A notable difference is that the interfaces annotated with @Service + * no longer result in references. + */ + @Test + public void testUpdatedRule() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(SomeServiceImpl.class, type); + assertEquals(12, type.getReferenceMembers().size()); + assertTrue(type.getReferenceMembers().containsKey("rri1")); + assertTrue(type.getReferenceMembers().containsKey("rri2")); + assertTrue(type.getReferenceMembers().containsKey("rri3")); + assertTrue(type.getReferenceMembers().containsKey("rri4")); + + assertTrue(type.getReferenceMembers().containsKey("rria1")); + assertTrue(type.getReferenceMembers().containsKey("rria2")); + assertTrue(type.getReferenceMembers().containsKey("rria3")); + assertTrue(type.getReferenceMembers().containsKey("rria4")); + + assertTrue(type.getReferenceMembers().containsKey("rric1")); + assertTrue(type.getReferenceMembers().containsKey("rric2")); + assertTrue(type.getReferenceMembers().containsKey("rric3")); + assertTrue(type.getReferenceMembers().containsKey("rric4")); + + assertEquals(16, type.getPropertyMembers().size()); + assertTrue(type.getPropertyMembers().containsKey("pnri1")); + assertTrue(type.getPropertyMembers().containsKey("pnri2")); + assertTrue(type.getPropertyMembers().containsKey("pnri3")); + assertTrue(type.getPropertyMembers().containsKey("pnri4")); + + assertTrue(type.getPropertyMembers().containsKey("pnria1")); + assertTrue(type.getPropertyMembers().containsKey("pnria2")); + assertTrue(type.getPropertyMembers().containsKey("pnria3")); + assertTrue(type.getPropertyMembers().containsKey("pnria4")); + + assertTrue(type.getPropertyMembers().containsKey("pnric1")); + assertTrue(type.getPropertyMembers().containsKey("pnric2")); + assertTrue(type.getPropertyMembers().containsKey("pnric3")); + assertTrue(type.getPropertyMembers().containsKey("pnric4")); + + assertTrue(type.getPropertyMembers().containsKey("gen1")); + assertTrue(type.getPropertyMembers().containsKey("gen2")); + assertTrue(type.getPropertyMembers().containsKey("gen3")); + assertTrue(type.getPropertyMembers().containsKey("gen4")); + } + + /** + * Interfaces with "@WebService" annotation implemented by the class should result + * in a Service in the same manner as an "@Remotable" annotation would. + */ + @Test + public void testInterfaceWithWebServiceAnnotation() throws Exception{ + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(SomeWebServiceImpl.class, type); + assertEquals(1, type.getServices().size()); + assertEquals("SomeWebService", type.getServices().get(0).getName()); + } + + @Remotable + private interface ReferenceRemotableInterface { + void operation1(String param1); + } + + private interface PropertyNonRemotableInterface { + void operation1(String param1); + } + + @Remotable + private interface SomeService { + void serviceOperation1(); + } + + private static class SomeServiceImpl implements SomeService { + + public SomeServiceImpl() { + } + + // References - interface with @Remotable + public void setRri1(ReferenceRemotableInterface rri) { + } + protected void setRri2(ReferenceRemotableInterface rri) { + } + public ReferenceRemotableInterface rri3; + protected ReferenceRemotableInterface rri4; + + // References - array of interface with @Remotable + public void setRria1(ReferenceRemotableInterface[] rri) { + } + protected void setRria2(ReferenceRemotableInterface[] rri) { + } + public ReferenceRemotableInterface[] rria3; + protected ReferenceRemotableInterface[] rria4; + + // References - parametrized Collection of interface with @Remotable + public void setRric1(Collection<ReferenceRemotableInterface> rri) { + } + protected void setRric2(Collection<ReferenceRemotableInterface> rri) { + } + public Collection<ReferenceRemotableInterface> rric3; + protected Collection<ReferenceRemotableInterface> rric4; + + // Properties - interface with @Service and without @Remotable + public void setPnri1(PropertyNonRemotableInterface arg) { + } + protected void setPnri2(PropertyNonRemotableInterface arg) { + } + public PropertyNonRemotableInterface pnri3; + protected PropertyNonRemotableInterface pnri4; + + // Properties - array of interface with @Service and without @Remotable + public void setPnria1(PropertyNonRemotableInterface[] arg) { + } + protected void setPnria2(PropertyNonRemotableInterface[] arg) { + } + public PropertyNonRemotableInterface[] pnria3; + protected PropertyNonRemotableInterface[] pnria4; + + // Properties - parametrized Collection of interface with @Service and without @Remotable + public void setPnric1(Collection<PropertyNonRemotableInterface> arg) { + } + protected void setPnric2(Collection<PropertyNonRemotableInterface> arg) { + } + public Collection<PropertyNonRemotableInterface> pnric3; + protected Collection<PropertyNonRemotableInterface> pnric4; + + // Properties - Non-parametrized Collection + public void setGen1(Collection arg) { + } + protected void setGen2(Collection arg) { + } + public Collection gen3; + protected Collection gen4; + + public void serviceOperation1() { + } + } + + private static class PropertyIntTypeOnConstructor { + protected int foo; + + public PropertyIntTypeOnConstructor() { + } + + public int getFoo() { + return foo; + } + } + + @Remotable + private interface PropertyInterface { + void setString1(String val); + } + + @Remotable + private interface Interface1 { + } + + private static class Parent implements Interface1 { + + } + + private static class Child extends Parent { + public Child() { + } + + } + + private static class SingleInterfaceImpl implements PropertyInterface { + public SingleInterfaceImpl() { + } + + public void setString1(String val) { + } + + } + + private interface HeuristicServiceInterface { + void fooOperation(String ref); + + void setInvalid1(); // No parameter + + void setInvalid2(String str, int i); // More than one parameter + + String setInvalid3(String str); // return should be void + } + + public static class MockService implements PropertyInterface, RefInterface, HeuristicServiceInterface { + + @Property + public void setString1(String val) { + } + + @Property + public void setString2(String val) { + } + + @Reference + public void setReference(Ref ref) { + } + + @Reference + public void setReference2(Ref ref) { + } + + public void fooOperation(String ref) { + + } + + public void setInvalid1() { + } + + public void setInvalid2(String str, int i) { + } + + public String setInvalid3(String str) { + return null; + } + + } + + @Remotable + private interface Ref { + } + + private class ComplexProperty { + } + + private interface RefInterface { + void setReference(Ref ref); + } + + private static class RefInterfaceImpl implements RefInterface { + public RefInterfaceImpl() { + } + + public void setReference(Ref ref) { + } + } + + private static class SingleInterfaceWithPropertyReferenceImpl implements Interface1 { + public SingleInterfaceWithPropertyReferenceImpl() { + } + + public void setReference(Ref ref) { + } + + public void setProperty(ComplexProperty prop) { + } + } + + private static class ReferenceCollectionImpl implements Interface1 { + public ReferenceCollectionImpl() { + } + + public void setCollectionReference(Collection<Ref> ref) { + } + + public void setNonGenericCollectionReference(Collection ref) { + // [rfeng] By the SCA specification, this should be classified as property + } + + public void setListReference(List<Ref> ref) { + } + + public void setArrayReference(Ref[] ref) { + } + } + + private static class PropertyCollectionImpl implements Interface1 { + public PropertyCollectionImpl() { + } + + public void setCollectionProperty(Collection<ComplexProperty> prop) { + } + + public void setCollectionProperty2(Collection<String> prop) { + } + + public void setArrayProperty(ComplexProperty[] prop) { + } + + public void setArrayProperty2(String[] prop) { + } + } + + @Remotable + private interface RemotableRef { + } + + private static class RemotableRefImpl implements Interface1 { + protected RemotableRef otherRef; + + public RemotableRefImpl() { + } + + public void setRef(RemotableRef ref) { + + } + } + + private static class ProtectedRemotableRefFieldImpl implements Interface1 { + protected RemotableRef otherRef; + + public ProtectedRemotableRefFieldImpl() { + } + + public ProtectedRemotableRefFieldImpl(RemotableRef otherRef) { + this.otherRef = otherRef; + } + + } + + private static class ProtectedRemotableRefMethodImpl implements Interface1 { + public ProtectedRemotableRefMethodImpl() { + } + + protected void setOtherRef(RemotableRef otherRef) { + } + + } + + @WebService + private interface SomeWebService { + void serviceOperation1(); + } + + @Service(SomeWebService.class) + private static class SomeWebServiceImpl implements SomeWebService { + public SomeWebServiceImpl() { + + } + + public void serviceOperation1() { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeutisticExtensibleConstructorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeutisticExtensibleConstructorTestCase.java new file mode 100644 index 0000000000..07ab6f28c8 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/HeutisticExtensibleConstructorTestCase.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.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.junit.Test; + +/** + * Verifies constructors that have extensible annotation types, i.e. that have + * parameters marked by annotations which are themselves processed by some other + * implementation processor + * + * @version $Rev$ $Date$ + */ +public class HeutisticExtensibleConstructorTestCase extends AbstractProcessorTest { + + private org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + public HeutisticExtensibleConstructorTestCase() { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new HeuristicPojoProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + private <T> void visitEnd(Class<T> clazz, JavaImplementation type) throws IntrospectionException { + for (Constructor<?> constructor : clazz.getConstructors()) { + visitConstructor(constructor, type); + } + processor.visitEnd(clazz, type); + } + + /** + * Verifies heuristic processing can be called prior to an extension + * annotation processors being called. + */ + @Test + public void testBarAnnotationProcessedFirst() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo> ctor = Foo.class.getConstructor(String.class, String.class); + JavaConstructorImpl<Foo> definition = new JavaConstructorImpl<Foo>(ctor); + type.setConstructor(definition); + Property property = factory.createProperty(); + property.setName("myBar"); + definition.getParameters()[0].setName("myBar"); + type.getProperties().add(property); + visitEnd(Foo.class, type); + assertEquals(2, type.getProperties().size()); + } + + /** + * Verifies heuristic processing can be called before an extension + * annotation processors is called. <p/> For example, given: + * + * <pre> + * Foo(@Bar String prop, @org.oasisopen.sca.annotation.Property(name = "foo") String prop2) + * </pre> + * + * <p/> Heuristic evaluation of + * @Property can occur prior to another implementation processor evaluating + * @Bar + * @throws Exception + */ + @Test + public void testBarAnnotationProcessedLast() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + visitEnd(Foo.class, type); + + // now simulate process the bar impl + JavaConstructorImpl<?> definition = type.getConstructor(); + definition.getParameters()[0].setName("myBar"); + Property property = factory.createProperty(); + property.setName("myBar"); + type.getProperties().add(property); + + assertEquals(2, type.getProperties().size()); + assertEquals("foo", definition.getParameters()[1].getName()); + } + + /** + * Verifies heuristic processing can be called before an extension + * annotation processors is called with the extension parameter in a middle + * position. Specifically, verifies that the heuristic processor updates + * injection names and preserves their ordering. + */ + @Test + public void testBarAnnotationProcessedFirstInMiddle() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Constructor<Foo2> ctor = Foo2.class.getConstructor(String.class, String.class, String.class); + JavaConstructorImpl<Foo2> definition = new JavaConstructorImpl<Foo2>(ctor); + type.setConstructor(definition); + // insert placeholder for first param, which would be done by a + // processor + definition.getParameters()[0].setName(""); + Property property = factory.createProperty(); + // Hack to add a property member + JavaElementImpl element = new JavaElementImpl("myBar", String.class, null); + type.getPropertyMembers().put("myBar", element); + property.setName("myBar"); + definition.getParameters()[1].setName("myBar"); + type.getProperties().add(property); + visitEnd(Foo2.class, type); + assertEquals("baz", definition.getParameters()[0].getName()); + assertEquals(2, type.getProperties().size()); + assertEquals(1, type.getReferences().size()); + } + + public @interface Bar { + + } + + public static class Foo { + public Foo(@Bar + String prop, @org.oasisopen.sca.annotation.Property(name = "foo") + String prop2) { + } + } + + public static class Foo2 { + public Foo2(@org.oasisopen.sca.annotation.Reference(name = "baz") + String prop1, @Bar + String prop2, @org.oasisopen.sca.annotation.Property(name = "foo") + String prop3) { + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessorTestCase.java new file mode 100644 index 0000000000..5df7078aa6 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/InitProcessorTestCase.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.junit.Test; +import org.oasisopen.sca.annotation.Init; + +/** + * @version $Rev$ $Date$ + */ +public class InitProcessorTestCase { + + private JavaImplementationFactory javaImplementationFactory; + + public InitProcessorTestCase() { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @Test + public void testInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Foo.class.getMethod("init"); + processor.visitMethod(method, type); + assertNotNull(type.getInitMethod()); + } + + @Test + public void testBadInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getMethod("badInit", String.class); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + @Test + public void testTwoInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getMethod("init"); + Method method2 = InitProcessorTestCase.Bar.class.getMethod("init2"); + processor.visitMethod(method, type); + try { + processor.visitMethod(method2, type); + fail(); + } catch (DuplicateInitException e) { + // expected + } + } + + @Test + public void testProtectedInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getDeclaredMethod("protectedInit"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + @Test + public void testPrivateInit() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getDeclaredMethod("privateInit"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + @Test + public void testBadInit2() throws Exception { + InitProcessor processor = new InitProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + Method method = InitProcessorTestCase.Bar.class.getDeclaredMethod("badInit2"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalInitException e) { + // expected + } + } + + private class Foo { + @Init + public void init() { + } + } + + + private class Bar { + @Init + public void init() { + } + + @Init + public void init2() { + } + + @Init + public void badInit(String foo) { + } + + @Init + public String badInit2() { + return null; + } + + @Init + protected void protectedInit() { + } + + @Init + private void privateInit() { + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java new file mode 100644 index 0000000000..2e6ef50996 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java @@ -0,0 +1,127 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.implementation.java.introspect.impl;
+
+import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getService;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import javax.jws.WebService;
+
+import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
+import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory;
+import org.apache.tuscany.sca.implementation.java.IntrospectionException;
+import org.apache.tuscany.sca.implementation.java.JavaImplementation;
+import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory;
+import org.apache.tuscany.sca.interfacedef.InvalidCallbackException;
+import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory;
+import org.apache.tuscany.sca.policy.DefaultPolicyFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.oasisopen.sca.ServiceReference;
+import org.oasisopen.sca.annotation.Callback;
+import org.oasisopen.sca.annotation.Service;
+
+/**
+ * @version $Rev: 826368 $ $Date: 2009-10-18 08:22:23 +0100 (Sun, 18 Oct 2009) $
+ */
+public class JAXWSProcessorTestCase {
+ private JAXWSProcessor processor;
+ private JavaImplementationFactory javaImplementationFactory;
+
+ @Before
+ public void setUp() throws Exception {
+ ExtensionPointRegistry registry = new DefaultExtensionPointRegistry();
+ processor = new JAXWSProcessor(registry);
+ javaImplementationFactory = new DefaultJavaImplementationFactory();
+ }
+
+ @Test
+ public void testWebServiceNoName() throws Exception {
+ JavaImplementation type = javaImplementationFactory.createJavaImplementation();
+ processor.visitClass(Foo0Impl.class, type);
+ org.apache.tuscany.sca.assembly.Service service = getService(type, "Foo0Impl");
+ assertNotNull(service);
+ }
+
+ @Test
+ public void testWebServiceName() throws Exception {
+ JavaImplementation type = javaImplementationFactory.createJavaImplementation();
+ processor.visitClass(Foo1Impl.class, type);
+ org.apache.tuscany.sca.assembly.Service service = getService(type, "Foo1");
+ assertNotNull(service);
+ }
+
+ @Test
+ public void testWebServiceEP() throws Exception {
+ JavaImplementation type = javaImplementationFactory.createJavaImplementation();
+ processor.visitClass(Foo2Impl.class, type);
+ org.apache.tuscany.sca.assembly.Service service = getService(type, "Foo2");
+ assertNotNull(service);
+ }
+
+ @Test
+ public void testWebServiceWSDL() throws Exception {
+ JavaImplementation type = javaImplementationFactory.createJavaImplementation();
+ processor.visitClass(Foo3Impl.class, type);
+ org.apache.tuscany.sca.assembly.Service service = getService(type, "Foo3");
+ assertNotNull(service);
+ }
+
+ @WebService()
+ private static class Foo0Impl{
+ public String doSomething(String aParam){
+ return null;
+ }
+ }
+
+ @WebService(name="Foo1")
+ private static class Foo1Impl{
+ public String doSomething(String aParam){
+ return null;
+ }
+ }
+
+ private interface Foo2 {
+ public String doSomething(String aParam);
+ }
+
+ @WebService(name="Foo2", endpointInterface="org.apache.tuscany.sca.implementation.java.introspect.impl.JAXWSProcessorTestCase.Foo2")
+ private static class Foo2Impl{
+ public String doSomething(String aParam){
+ return null;
+ }
+ }
+
+ @WebService(name="Foo3", wsdlLocation="foo3.wsdl")
+ private static class Foo3Impl{
+ public String doSomething(String aParam){
+ return null;
+ }
+ }
+
+
+}
diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ModelHelper.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ModelHelper.java new file mode 100644 index 0000000000..aad6b96cf9 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ModelHelper.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +/** + * @version $Rev$ $Date$ + */ +public class ModelHelper { + + public static Property getProperty(JavaImplementation type, String name) { + for (Property prop : type.getProperties()) { + if (prop.getName().equals(name)) { + return prop; + } + } + return null; + } + + public static Reference getReference(JavaImplementation type, String name) { + for (Reference ref : type.getReferences()) { + if (ref.getName().equals(name)) { + return ref; + } + } + return null; + } + + public static Service getService(JavaImplementation type, String name) { + for (Service svc : type.getServices()) { + if (svc.getName().equals(name)) { + return svc; + } + } + return null; + } + + public static boolean matches(Contract contract, Class<?> type) { + Interface interface1 = contract.getInterfaceContract().getInterface(); + if (interface1 instanceof JavaInterface) { + return type == ((JavaInterface)interface1).getJavaClass(); + } else { + return false; + } + } + + public static ComponentService createService(AssemblyFactory factory, + JavaInterfaceFactory javaFactory, Class<?> type) { + org.apache.tuscany.sca.assembly.ComponentService ref = factory.createComponentService(); + ref.setName(type.getSimpleName()); + JavaInterface i = javaFactory.createJavaInterface(); + i.setJavaClass(type); + JavaInterfaceContract ic = javaFactory.createJavaInterfaceContract(); + ic.setInterface(i); + ref.setInterfaceContract(ic); + return ref; + } + + public static Reference createReference(AssemblyFactory factory, + JavaInterfaceFactory javaFactory, String name, Class<?> type) { + org.apache.tuscany.sca.assembly.Reference ref = factory.createReference(); + ref.setName(name); + JavaInterface i = javaFactory.createJavaInterface(); + i.setJavaClass(type); + JavaInterfaceContract ic = javaFactory.createJavaInterfaceContract(); + ic.setInterface(i); + ref.setInterfaceContract(ic); + return ref; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessorTestCase.java new file mode 100644 index 0000000000..e3d747fc28 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PolicyProcessorTestCase.java @@ -0,0 +1,428 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.impl.PolicyJavaInterfaceVisitor; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Requires; +import org.oasisopen.sca.annotation.Service; + +/** + * @version $Rev$ $Date$ + */ +public class PolicyProcessorTestCase { + private ServiceProcessor serviceProcessor; + private PolicyProcessor policyProcessor; + private PolicyJavaInterfaceVisitor visitor; + private JavaImplementation type; + + // This actually is a test for PolicyJavaInterfaceProcessor. It will get + // invoked via the call to ImplementationProcessorServiceImpl.createService in + // ServiceProcessor. Of course ServiceProcessor class has to be working. + @Test + public void testSingleInterfaceWithIntentsOnInterfaceAtInterfaceLevel() throws Exception { + serviceProcessor.visitClass(Service1.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service1.class, type); + verifyIntents(Service1.class, type); + } + + @Test + public void testMultipleInterfacesWithIntentsOnInterfaceAtInterfaceLevel() throws Exception { + serviceProcessor.visitClass(Service2.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + visitor.visitInterface((JavaInterface)type.getServices().get(1).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service2.class, type); + verifyIntents(Service2.class, type); + } + + @Test + public void testSingleInterfaceWithIntentsOnImplAtClassLevel() throws Exception { + serviceProcessor.visitClass(Service3.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service3.class, type); + verifyIntents(Service3.class, type); + } + + @Test + public void testMultipleInterfacesWithIntentsOnImplAtClassLevel() throws Exception { + serviceProcessor.visitClass(Service4.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service4.class, type); + verifyIntents(Service4.class, type); + } + + public void stestSingleInterfaceWithIntentsOnInterfaceAtMethodLevel() throws Exception { + serviceProcessor.visitClass(Service5.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service5.class, type); + verifyIntents(Service5.class, type); + } + + @Test + public void testSingleInterfaceWithIntentsOnServiceAndInterfaceAtImplAndInertfaceAndMethodLevel() throws Exception { + serviceProcessor.visitClass(Service6.class, type); + visitor.visitInterface((JavaInterface)type.getServices().get(0).getInterfaceContract().getInterface()); + policyProcessor.visitClass(Service6.class, type); + for (Method method : Service6.class.getDeclaredMethods()) { + policyProcessor.visitMethod(method, type); + } + verifyIntents(Service6.class, type); + } + + private void verifyIntents(Class<?> serviceImplClass, JavaImplementation type) { + if ( !(type instanceof PolicySubject) ) { + fail("No Intents on the service "); + } + Requires serviceImplIntentAnnotation = (Requires)serviceImplClass.getAnnotation(Requires.class); + if (serviceImplIntentAnnotation != null) { + String[] serviceImplIntents = serviceImplIntentAnnotation.value(); + List<Intent> requiredIntents = ((PolicySubject)type).getRequiredIntents(); + if (serviceImplIntents.length > 0) { + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on the service "); + } + Map<String, Intent> intentMap = new HashMap<String, Intent>(); + for (Intent intent : requiredIntents) { + intentMap.put(intent.getName().getLocalPart(), intent); + } + for (String intent : serviceImplIntents) { + assertTrue("ComponentType for Service class " + serviceImplClass.getName() + + " did not contain Service Implementation intent " + + intent, intentMap.containsKey(intent)); + } + } + } + + // This should match what was specified on @Service for a Service Implementation + // If we use these to get the Service names and we get a null Service + // name then it would seem that wrong values were put on the @Service annotation + // or the wrong interfaces were specified on the implements list of the class + // statement? + Map<String, org.apache.tuscany.sca.assembly.Service> serviceMap = new HashMap<String, org.apache.tuscany.sca.assembly.Service>(); + for (org.apache.tuscany.sca.assembly.Service service: type.getServices()) { + serviceMap.put(service.getName(), service); + } + for (Class<?> interfaceClass : serviceImplClass.getInterfaces()) { + Requires interfaceIntentAnnotation = (Requires)interfaceClass.getAnnotation(Requires.class); + org.apache.tuscany.sca.assembly.Service service = serviceMap.get(interfaceClass.getSimpleName()); + if (service == null) { + fail("No service defined for interface " + interfaceClass.getSimpleName() + + " on Service Implementation " + + serviceImplClass.getName()); + } + + if (interfaceIntentAnnotation != null) { + String[] interfaceIntents = interfaceIntentAnnotation.value(); + List<Intent> requiredIntents = service.getInterfaceContract().getInterface().getRequiredIntents(); + if (interfaceIntents.length > 0) { + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on the service " + service.getName()); + } + Map<String, Intent> intentMap = new HashMap<String, Intent>(); + for (Intent intent : requiredIntents) { + intentMap.put(intent.getName().getLocalPart(), intent); + } + for (String intent : interfaceIntents) { + assertTrue("Interface " + service.getName() + + " did not contain Service Interface intent " + + intent, intentMap.containsKey(intent)); + } + } + } + + /* + for (Method method : interfaceClass.getDeclaredMethods()) { + Requires methodIntentAnnotation = method.getAnnotation(Requires.class); + + // Verify that each of the Intents on each of the Service + // Interface Methods exist on their associated operation. + if (methodIntentAnnotation != null) { + String[] methodIntents = methodIntentAnnotation.value(); + if (methodIntents.length > 0) { + List<Intent> requiredIntents = null; + for ( ConfiguredOperation confOp : service.getConfiguredOperations() ) { + if ( confOp.getName().equals(method.getName()) && + confOp.getContractName().equals(service.getName()) ) { + requiredIntents = confOp.getRequiredIntents(); + } + } + + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on operation " + method.getName()); + } + for (String intent : methodIntents) { + boolean found = false; + for (Intent requiredIntent: requiredIntents) { + if (requiredIntent.getName().getLocalPart().equals(intent)) { + found = true; + break; + } + } + assertTrue("Operation " + method.getName() + + " did not contain Service Interface method intent " + + intent, found); + } + } + } + } + + for (Method method : serviceImplClass.getDeclaredMethods()) { + Requires methodIntentAnnotation = method.getAnnotation(Requires.class); + + // Verify that each of the Intents on each of the Service + // Implementation Methods exist on their associated + // operation. + if (methodIntentAnnotation != null) { + String[] methodIntents = methodIntentAnnotation.value(); + if (methodIntents.length > 0) { + List<Intent> requiredIntents = null; + for ( ConfiguredOperation confOp : ((OperationsConfigurator)type).getConfiguredOperations() ) { + if ( confOp.getName().equals(method.getName()) ) { + requiredIntents = confOp.getRequiredIntents(); + } + } + + if (requiredIntents == null || requiredIntents.size() == 0) { + fail("No Intents on operation " + method.getName()); + } + + for (String intent : methodIntents) { + boolean found = false; + for (Intent requiredIntent: requiredIntents) { + if (requiredIntent.getName().getLocalPart().equals(intent)) { + found = true; + break; + } + } + assertTrue("Operation " + method.getName() + + " did not contain Implementation method intent " + + intent, found); + } + } + } + } + */ + } + } + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + serviceProcessor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + policyProcessor = new PolicyProcessor(registry); + visitor = new PolicyJavaInterfaceVisitor(registry); + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + } + + // @Remotable + @Requires( {"transaction.global"}) + private interface Interface1 { + int method1(); + + int method2(); + + int method3(); + + int method4(); + } + + @Service(Interface1.class) + private class Service1 implements Interface1 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + } + + // @Remotable + @Requires( {"transaction.local"}) + private interface Interface2 { + int method5(); + + int method6(); + } + + @Service({Interface1.class, Interface2.class}) + private class Service2 implements Interface1, Interface2 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + + public int method5() { + return 0; + } + + public int method6() { + return 0; + } + } + + // @Remotable + private interface Interface3 { + int method1(); + + int method2(); + + int method3(); + + int method4(); + } + + @Service(Interface3.class) + @Requires( {"transaction.global"}) + private class Service3 implements Interface3 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + } + + // @Remotable + private interface Interface4 { + int method5(); + + int method6(); + } + + @Service({Interface3.class, Interface4.class}) + @Requires( {"transaction.local"}) + private class Service4 implements Interface3, Interface4 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + + public int method3() { + return 0; + } + + public int method4() { + return 0; + } + + public int method5() { + return 0; + } + + public int method6() { + return 0; + } + } + + private interface Interface5 { + @Requires( {"transaction.global"}) + int method1(); + + @Requires( {"transaction.local"}) + int method2(); + } + + @Service(Interface5.class) + private class Service5 implements Interface5 { + public int method1() { + return 0; + } + + public int method2() { + return 0; + } + } + + @Requires( {"transaction.global.Interface6"}) + private interface Interface6 { + @Requires( {"transaction.global.Interface6.method1"}) + int method1(); + + @Requires( {"transaction.local.Interface6.method2"}) + int method2(); + } + + @Service(Interface6.class) + @Requires( {"transaction.global.Service6"}) + private class Service6 implements Interface6 { + // @Requires( {"transaction.global.Service6.method1"}) + public int method1() { + return 0; + } + + // @Requires( {"transaction.global.Service6.method1"}) + public int method2() { + return 0; + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java new file mode 100644 index 0000000000..559659cdeb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java @@ -0,0 +1,304 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; + +/** + * @version $Rev$ $Date$ + */ +public class PropertyProcessorTestCase { + + JavaImplementation type; + PropertyProcessor processor; + + @Before + public void setUp() throws Exception { + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + processor = new PropertyProcessor(new DefaultExtensionPointRegistry()); + } + + @Test + public void testMethodAnnotation() throws Exception { + processor.visitMethod(Foo.class.getMethod("setFoo", String.class), type); + assertNotNull(getProperty(type, "foo")); + } + + @Test + public void testMethodRequired() throws Exception { + processor.visitMethod(Foo.class.getMethod("setFooRequired", String.class), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "fooRequired"); + assertNotNull(prop); + assertTrue(prop.isMustSupply()); + } + + @Test + public void testMethodName() throws Exception { + processor.visitMethod(Foo.class.getMethod("setBarMethod", String.class), type); + assertNotNull(getProperty(type, "bar")); + } + + @Test + public void testFieldAnnotation() throws Exception { + processor.visitField(Foo.class.getDeclaredField("baz"), type); + assertNotNull(getProperty(type, "baz")); + } + + @Test + public void testFieldRequired() throws Exception { + processor.visitField(Foo.class.getDeclaredField("bazRequired"), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "bazRequired"); + assertNotNull(prop); + assertTrue(prop.isMustSupply()); + } + + @Test + public void testFieldName() throws Exception { + processor.visitField(Foo.class.getDeclaredField("bazField"), type); + assertNotNull(getProperty(type, "theBaz")); + } + + @Test + public void testDuplicateFields() throws Exception { + processor.visitField(Bar.class.getDeclaredField("dup"), type); + try { + processor.visitField(Bar.class.getDeclaredField("baz"), type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testDuplicateMethods() throws Exception { + processor.visitMethod(Bar.class.getMethod("setDupMethod", String.class), type); + try { + processor.visitMethod(Bar.class.getMethod("setDupSomeMethod", String.class), type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testInvalidProperty() throws Exception { + try { + processor.visitMethod(Bar.class.getMethod("badMethod"), type); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + @Test + public void testMultiplicityCollection() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs1"), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs1"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testMultiplicityArray() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs2"), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs2"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testMultiplicityArrayMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs3", String[].class), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs3"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testMultiplicityCollectionMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs4", Collection.class), type); + org.apache.tuscany.sca.assembly.Property prop = getProperty(type, "refs4"); + assertNotNull(prop); + assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName()))); + assertTrue(prop.isMany()); + } + + @Test + public void testRejectStaticFieldProperty() throws Exception { + try { + processor.visitField(BadStaticProps.class.getDeclaredField("stint"), type); + fail("Processor should not accept a static field with Property annotation"); + } + catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + } + } + + @Test + public void testRejectStaticMethodProperty() throws Exception { + try { + processor.visitMethod(BadStaticProps.class.getDeclaredMethod("setStint",int.class), type); + fail("Processor should not accept a static method with Property annotation"); + } + catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + + } + + @Test + public void testClassWithBadMethodArgProperty() throws Exception { + Method meth = BadMethodProps.class.getMethod("BadMethod", String.class); + + try { + processor.visitMethod(meth, type); + + fail("Method with @Property annotated args should be rejected"); + } catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception received"); + e.printStackTrace(); + } + + } + + /** + * Private classes utilized in the tests + */ + + private class Foo { + + @Property + protected String baz; + + @Property(required = true) + protected String bazRequired; + + @Property(name = "theBaz") + protected String bazField; + + @Property + public void setFoo(String string) { + } + + @Property(required = true) + public void setFooRequired(String string) { + } + + @Property(name = "bar") + public void setBarMethod(String string) { + } + + } + + private class Bar { + + @Property + protected String dup; + + @Property(name = "dup") + protected String baz; + + @Property + public void setDupMethod(String s) { + } + + @Property(name = "dupMethod") + public void setDupSomeMethod(String s) { + } + + @Property + public void badMethod() { + } + } + + private class Multiple { + @Property + protected List<String> refs1; + + @Property + protected String[] refs2; + + @Property + public void setRefs3(String[] refs) { + } + + @Property + public void setRefs4(Collection<String> refs) { + } + } + + private static class BadMethodProps { + + @org.oasisopen.sca.annotation.Constructor() + public BadMethodProps(@Property(name = "myProp", required = true)String prop) { + + } + + /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/ + public void BadMethod(@Property(name = "badMethodArgProp")String methArg) { + + } + } + + private static class BadStaticProps { + + @Property(name="badstaticfield")static int stint; + + @Property(name="badstaticfield")static void setStint(int theStint) { + stint = theStint; + } + } + + private Class<?> getBaseType(JavaElementImpl element) { + return JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType()); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java new file mode 100644 index 0000000000..6a7987a376 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java @@ -0,0 +1,310 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getReference; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; + +/** + * @version $Rev$ $Date$ + */ +public class ReferenceProcessorTestCase { + + private JavaImplementation type; + private ReferenceProcessor processor; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + processor = new ReferenceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + } + + @Test + public void testMethodAnnotation() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Foo.class.getMethod("setFoo", Ref.class), type); + org.apache.tuscany.sca.assembly.Reference reference = getReference(type, "foo"); + assertNotNull(reference); + assertEquals(Ref.class, ((JavaInterface)reference.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testMethodRequired() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Foo.class.getMethod("setFooRequired", Ref.class), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "fooRequired"); + assertNotNull(ref); + assertEquals(Multiplicity.ONE_ONE, ref.getMultiplicity()); + } + + @Test + public void testMethodName() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Foo.class.getMethod("setBarMethod", Ref.class), type); + assertNotNull(getReference(type, "bar")); + } + + @Test + public void testFieldAnnotation() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Foo.class.getDeclaredField("baz"), type); + org.apache.tuscany.sca.assembly.Reference reference = getReference(type, "baz"); + assertNotNull(reference); + assertEquals(Ref.class, ((JavaInterface)reference.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testFieldRequired() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Foo.class.getDeclaredField("bazRequired"), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "bazRequired"); + assertNotNull(ref); + assertEquals(Multiplicity.ONE_ONE, ref.getMultiplicity()); + } + + @Test + public void testFieldName() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Foo.class.getDeclaredField("bazField"), type); + assertNotNull(getReference(type, "theBaz")); + } + + @Test + public void testDuplicateFields() throws Exception { + processor.visitField(ReferenceProcessorTestCase.Bar.class.getDeclaredField("dup"), type); + try { + processor.visitField(ReferenceProcessorTestCase.Bar.class.getDeclaredField("baz"), type); + fail(); + } catch (DuplicateReferenceException e) { + // expected + } + } + + @Test + public void testDuplicateMethods() throws Exception { + processor.visitMethod(ReferenceProcessorTestCase.Bar.class.getMethod("setDupMethod", Ref.class), type); + try { + processor.visitMethod(ReferenceProcessorTestCase.Bar.class.getMethod("setDupSomeMethod", Ref.class), type); + fail(); + } catch (DuplicateReferenceException e) { + // expected + } + } + + @Test + public void testInvalidProperty() throws Exception { + try { + processor.visitMethod(ReferenceProcessorTestCase.Bar.class.getMethod("badMethod"), type); + fail(); + } catch (IllegalReferenceException e) { + // expected + } + } + + @Test + public void testClassWithBadMethodArgReference() throws Exception { + Method meth = BadMethAnn.class.getMethod("BadMethod", String.class); + + try { + processor.visitMethod(meth, type); + + fail("reference annotation on ordinary method arg should be rejected"); + } catch (IllegalReferenceException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + } + + @Test + public void testMultiplicity1ToN() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs1"), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs1"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ONE_N, ref.getMultiplicity()); + } + + @Test + public void testMultiplicityTo0ToN() throws Exception { + processor.visitField(Multiple.class.getDeclaredField("refs2"), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs2"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ZERO_N, ref.getMultiplicity()); + } + + @Test + public void testMultiplicity1ToNMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs3", Ref[].class), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs3"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ONE_N, ref.getMultiplicity()); + } + + @Test + public void testMultiplicity0ToNMethod() throws Exception { + processor.visitMethod(Multiple.class.getMethod("setRefs4", Collection.class), type); + org.apache.tuscany.sca.assembly.Reference ref = getReference(type, "refs4"); + assertNotNull(ref); + assertSame(Ref.class, ((JavaInterface)ref.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Multiplicity.ZERO_N, ref.getMultiplicity()); + } + + @Test + public void testRejectStaticFieldReference() throws Exception { + try { + processor.visitField(BadStaticRefs.class.getDeclaredField("stint"), type); + fail("Processor should not accept a static field with Property annotation"); + } + catch (IllegalReferenceException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + } + + @Test + public void testRejectStaticMethodReference() throws Exception { + try { + processor.visitMethod(BadStaticRefs.class.getDeclaredMethod("setStint",int.class), type); + fail("Processor should not accept a static method with Property annotation"); + } + catch (IllegalPropertyException e) { + // expected + } + catch (Exception e) { + fail("Wrong exception detected"); + e.printStackTrace(); + } + + } + + /** + * Private classes utilized in the tests + */ + + private interface Ref { + } + + private class Foo { + @Reference + protected Ref baz; + + @Reference(required = true) + protected Ref bazRequired; + + @Reference(name = "theBaz") + protected Ref bazField; + + @Reference + public void setFoo(Ref ref) { + } + + @Reference(required = true) + public void setFooRequired(Ref ref) { + } + + @Reference(name = "bar") + public void setBarMethod(Ref ref) { + } + } + + private class Bar { + @Reference + protected Ref dup; + + @Reference(name = "dup") + protected Ref baz; + + @Reference + public void setDupMethod(Ref s) { + } + + @Reference(name = "dupMethod") + public void setDupSomeMethod(Ref s) { + } + + @Reference + public void badMethod() { + } + + } + + private class Multiple { + @Reference(required = true) + protected List<Ref> refs1; + + @Reference(required = false) + protected Ref[] refs2; + + @Reference(required = true) + public void setRefs3(Ref[] refs) { + } + + @Reference(required = false) + public void setRefs4(Collection<Ref> refs) { + } + } + + private static class BadStaticRefs { + + @Reference(name="badstaticfield")static int stint; + + @Reference(name="badstaticfield")static void setStint(int theStint) { + stint = theStint; + } + } + + + private static class BadMethAnn { + + @org.oasisopen.sca.annotation.Constructor() + public BadMethAnn(@Property(name = "myProp", required = true)String prop) { + + } + + /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/ + public void BadMethod(@Reference(name = "badMethodArgRef")String methArg) { + + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessorTestCase.java new file mode 100644 index 0000000000..c09b0547c9 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ResourceProcessorTestCase.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaResourceImpl; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class ResourceProcessorTestCase { + + JavaImplementation type; + ResourceProcessor processor = new ResourceProcessor(new DefaultAssemblyFactory()); + + @Test + public void testVisitField() throws Exception { + Field field = Foo.class.getDeclaredField("bar"); + processor.visitField(field, type); + JavaResourceImpl resource = type.getResources().get("bar"); + assertFalse(resource.isOptional()); + assertNull(resource.getMappedName()); + assertEquals(field.getType(), resource.getElement().getType()); + } + + @Test + public void testVisitMethod() throws Exception { + Method method = Foo.class.getMethod("setBar", Bar.class); + processor.visitMethod(method, type); + JavaResourceImpl resource = type.getResources().get("bar"); + assertFalse(resource.isOptional()); + assertNull(resource.getMappedName()); + assertEquals(method.getParameterTypes()[0], resource.getElement().getType()); + } + + @Test + public void testVisitNamedMethod() throws Exception { + Method method = Foo.class.getMethod("setBar2", Bar.class); + processor.visitMethod(method, type); + JavaResourceImpl resource = type.getResources().get("someName"); + assertFalse(resource.isOptional()); + assertEquals("mapped", resource.getMappedName()); + } + + @Test + public void testVisitBadMethod() throws Exception { + Method method = Foo.class.getMethod("setBad"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalResourceException e) { + // expected + } + } + + @Test + public void testDuplicateResources() throws Exception { + Field field = Foo.class.getDeclaredField("bar"); + processor.visitField(field, type); + try { + processor.visitField(field, type); + fail(); + } catch (DuplicateResourceException e) { + //expected + } + } + + @Before + public void setUp() throws Exception { + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + } + + private class Foo { + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource + protected Bar bar; + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource(optional = true) + protected Bar barNotRequired; + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource + public void setBar(Bar bar) { + } + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource(name = "someName", mappedName = "mapped") + public void setBar2(Bar bar) { + } + + @org.apache.tuscany.sca.implementation.java.introspect.impl.Resource + public void setBad() { + } + + } + + private interface Bar { + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessorTestCase.java new file mode 100644 index 0000000000..758584221d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ScopeProcessorTestCase.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaScopeImpl; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class ScopeProcessorTestCase { + + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testCompositeScope() throws IntrospectionException { + ScopeProcessor processor = new ScopeProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + + processor.visitClass(Composite.class, type); + assertEquals(JavaScopeImpl.COMPOSITE, type.getJavaScope()); + } + + @Test + public void testStatelessScope() throws IntrospectionException { + ScopeProcessor processor = new ScopeProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(Stateless.class, type); + assertEquals(JavaScopeImpl.STATELESS, type.getJavaScope()); + } + + @Test + public void testNoScope() throws IntrospectionException { + ScopeProcessor processor = new ScopeProcessor(new DefaultAssemblyFactory()); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(None.class, type); + assertEquals(JavaScopeImpl.STATELESS, type.getJavaScope()); + } + + @Before + public void setUp() throws Exception { + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @org.oasisopen.sca.annotation.Scope("COMPOSITE") + private class Composite { + } + + @org.oasisopen.sca.annotation.Scope("STATELESS") + private class Stateless { + } + + private class None { + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceCallbackTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceCallbackTestCase.java new file mode 100644 index 0000000000..68f3b9e456 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceCallbackTestCase.java @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getService; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.InvalidCallbackException; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.ServiceReference; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Service; + +/** + * @version $Rev$ $Date$ + */ +public class ServiceCallbackTestCase { + private ServiceProcessor processor; + private JavaImplementationFactory javaImplementationFactory; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @Test + public void testMethodCallbackInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(FooImpl.class, type); + org.apache.tuscany.sca.assembly.Service service = getService(type, Foo.class.getSimpleName()); + assertNotNull(service); + Method method = FooImpl.class.getMethod("setCallback", FooCallback.class); + processor.visitMethod(method, type); + assertEquals(method, type.getCallbackMembers().get(FooCallback.class.getName()).iterator().next().getAnchor()); + } + + @Test + public void testFieldCallbackInterface() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(FooImpl.class, type); + org.apache.tuscany.sca.assembly.Service service = getService(type, Foo.class.getSimpleName()); + assertNotNull(service); + Field field = FooImpl.class.getDeclaredField("callback"); + processor.visitField(field, type); + assertEquals(field, type.getCallbackMembers().get(FooCallback.class.getName()).iterator().next().getAnchor()); + } + + @Test + public void testFieldCallbackInterface1() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(FooImpl1.class, type); + org.apache.tuscany.sca.assembly.Service service = getService(type, Foo.class.getSimpleName()); + assertNotNull(service); + Field field1 = FooImpl1.class.getDeclaredField("callbackRef"); + processor.visitField(field1, type); + assertEquals(field1, type.getCallbackMembers().get(FooCallback.class.getName()).iterator().next().getAnchor()); + + } + + @Test + public void testMethodDoesNotMatchCallback() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(BadBarImpl.class, type); + Method method = BadBarImpl.class.getMethod("setWrongInterfaceCallback", String.class); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + @Test + public void testNoParamCallback() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(BadBarImpl.class, type); + Method method = BadBarImpl.class.getMethod("setNoParamCallback"); + try { + processor.visitMethod(method, type); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + @Test + public void testFieldDoesNotMatchCallback() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + processor.visitClass(BadBarImpl.class, type); + Field field = BadBarImpl.class.getDeclaredField("wrongInterfaceCallback"); + try { + processor.visitField(field, type); + fail(); + } catch (IllegalCallbackReferenceException e) { + // expected + } + } + + @Test + public void testBadCallbackInterfaceAnnotation() throws Exception { + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + processor.visitClass(BadFooImpl.class, type); + fail(); + } catch (IntrospectionException e) { + // expected + assertTrue(e.getCause() instanceof InvalidCallbackException); + } + } + + @Callback(FooCallback.class) + private interface Foo { + + } + + private interface FooCallback { + + } + + @Service(Foo.class) + private static class FooImpl implements Foo { + + @Callback + protected FooCallback callback; + + @Callback + public void setCallback(FooCallback cb) { + + } + } + + @Service(Foo.class) + private static class FooImpl1 implements Foo { + @Callback + protected ServiceReference<FooCallback> callbackRef; + } + + private static class BadBarImpl implements Foo { + @Callback + protected String wrongInterfaceCallback; + + @Callback + public void setWrongInterfaceCallback(String cb) { + + } + + @Callback + public void setNoParamCallback() { + + } + + } + + @Callback + private interface BadFoo { + + } + + @Service(BadFoo.class) + private static class BadFooImpl implements BadFoo { + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessorTestCase.java new file mode 100644 index 0000000000..4adf57eeeb --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ServiceProcessorTestCase.java @@ -0,0 +1,219 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import javax.jws.WebService; + +import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.junit.Before; +import org.junit.Test; +import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.Remotable; +import org.oasisopen.sca.annotation.Service; + +/** + * @version $Rev$ $Date$ + */ +public class ServiceProcessorTestCase { + private ServiceProcessor processor; + private JavaImplementation type; + + @Test + public void testMultipleInterfaces() throws Exception { + processor.visitClass(FooMultiple.class, type); + assertEquals(2, type.getServices().size()); + org.apache.tuscany.sca.assembly.Service service = ModelHelper.getService(type, Baz.class.getSimpleName()); + assertEquals(Baz.class, ((JavaInterface)service.getInterfaceContract().getInterface()).getJavaClass()); + assertEquals(Bar.class, ((JavaInterface)service.getInterfaceContract().getCallbackInterface()).getJavaClass()); + assertNotNull(ModelHelper.getService(type, Bar.class.getSimpleName())); + } + + @Test + public void testSingleInterfaces() throws Exception { + processor.visitClass(FooSingle.class, type); + assertEquals(1, type.getServices().size()); + assertNotNull(ModelHelper.getService(type, Baz.class.getSimpleName())); + } + + @Test + public void testMultipleNoService() throws Exception { + processor.visitClass(FooMultipleNoService.class, type); + assertEquals(0, type.getServices().size()); + } + + /** + * Verifies a service with a callback annotation is recognized + */ + @Test + public void testMultipleWithCallbackAnnotation() throws Exception { + processor.visitClass(FooMultipleWithCalback.class, type); + assertEquals(1, type.getServices().size()); + } + + + @Test + public void testMultipleWithWebServiceAnnotation() throws Exception { + processor.visitClass(FooMultipleWithWebService.class, type); + assertEquals(2, type.getServices().size()); + } + + @Test + public void testRemotableNoService() throws Exception { + processor.visitClass(FooRemotableNoService.class, type); + assertEquals(1, type.getServices().size()); + org.apache.tuscany.sca.assembly.Service service = ModelHelper.getService(type, BazRemotable.class.getSimpleName()); + assertEquals(BazRemotable.class, ((JavaInterface)service.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testNonInterface() throws Exception { + processor.visitClass(FooServiceUsingClassImpl.class, type); + } + + @Test + public void testMultiplenamedInterfaces() throws Exception { + processor.visitClass(FooMultipleNamed.class, type); + assertEquals(2, type.getServices().size()); + org.apache.tuscany.sca.assembly.Service sbaz = type.getService("BazName"); + assertNotNull(sbaz); + assertEquals(Baz.class, ((JavaInterface)sbaz.getInterfaceContract().getInterface()).getJavaClass()); + org.apache.tuscany.sca.assembly.Service sbar = type.getService("BarName"); + assertNotNull(sbar); + assertEquals(Bar.class, ((JavaInterface)sbar.getInterfaceContract().getInterface()).getJavaClass()); + } + + @Test + public void testBadService() throws Exception { + try { + processor.visitClass(BadService.class, type); + fail(); + } catch (IntrospectionException e) { + assertTrue(e.getMessage().startsWith("[JCA90059]")); + } + } + + @Test + public void testBadServiceNames() throws Exception { + try { + processor.visitClass(BadServiceNames.class, type); + fail(); + } catch (IntrospectionException e) { + assertTrue(e.getMessage().startsWith("[JCA90050]")); + } + } + + @Test + public void testBadServiceDuplicateNames() throws Exception { + try { + processor.visitClass(BadServiceDuplicateNames.class, type); + fail(); + } catch (IntrospectionException e) { + assertTrue(e.getMessage().startsWith("[JCA90060]")); + } + } + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + processor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry)); + JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory(); + type = javaImplementationFactory.createJavaImplementation(); + } + + @Callback(Bar.class) + private interface Baz { + } + + private interface Bar { + } + + private interface Bar2 { + } + + @WebService + private interface Bar3 { + } + + @Remotable + private interface BazRemotable { + } + + @Service({Baz.class, Bar.class}) + private class FooMultiple implements Baz, Bar { + + } + + @Service(Baz.class) + private class FooSingle implements Baz, Bar { + + } + + private class FooMultipleNoService implements Bar, Bar2 { + + } + + private class FooMultipleWithCalback implements Baz, Bar { + + } + + private class FooMultipleWithWebService implements BazRemotable, Bar3 { + } + + private class FooRemotableNoService implements BazRemotable, Bar { + + } + + @Service(FooSingle.class) + private class FooServiceUsingClassImpl extends FooSingle { + + } + + @Service(value={Baz.class, Bar.class}, names={"BazName", "BarName"}) + private class FooMultipleNamed implements Baz, Bar { + + } + + @Service(value={}) + private class BadService implements Baz { + + } + + @Service(value={Baz.class, Bar.class}, names={"BazName"}) + private class BadServiceNames implements Baz, Bar { + + } + + @Service(value={Baz.class, Bar.class}, names={"BazName", "BazName"}) + private class BadServiceDuplicateNames implements Baz, Bar { + + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/TestAbstractPropertyProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/TestAbstractPropertyProcessorTestCase.java new file mode 100644 index 0000000000..a90a6d18c7 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/TestAbstractPropertyProcessorTestCase.java @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getProperty; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.lang.annotation.Retention; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaConstructorImpl; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory; +import org.apache.tuscany.sca.implementation.java.JavaParameterImpl; +import org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor; +import org.junit.Before; +import org.junit.Test; + + +/** + * @version $Rev$ $Date$ + */ +public class TestAbstractPropertyProcessorTestCase { + + private JavaClassVisitor extension; + private JavaImplementationFactory javaImplementationFactory; + + @Test + public void testVisitMethod() throws Exception { + Method method = Foo.class.getMethod("setBar", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + extension.visitMethod(method, type); + Property prop = getProperty(type, "test"); + assertNotNull(prop); + } + + @Test + public void testVisitNoParamsMethod() throws Exception { + Method method = Foo.class.getMethod("setNoParamsBar"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + extension.visitMethod(method, type); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + @Test + public void testVisitNonVoidMethod() throws Exception { + Method method = Foo.class.getMethod("setBadBar", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + try { + extension.visitMethod(method, type); + fail(); + } catch (IllegalPropertyException e) { + // expected + } + } + + @Test + public void testDuplicateMethod() throws Exception { + Method method = Foo.class.getMethod("setBar", String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + extension.visitMethod(method, type); + try { + extension.visitMethod(method, type); + fail(); + } catch (DuplicatePropertyException e) { + // expected + } + } + + @Test + public void testVisitField() throws Exception { + Field field = Foo.class.getDeclaredField("d"); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + extension.visitField(field, type); + Property prop = getProperty(type, "test"); + assertNotNull(prop); + } + + @Test + public void testVisitConstructor() throws Exception { + Constructor<Foo> ctor = Foo.class.getConstructor(String.class); + JavaImplementation type = javaImplementationFactory.createJavaImplementation(); + JavaConstructorImpl<Foo> def = new JavaConstructorImpl<Foo>(ctor); + JavaParameterImpl parameter = def.getParameters()[0]; + extension.visitConstructorParameter(parameter, type); + assertEquals("test", def.getParameters()[0].getName()); + assertNotNull(getProperty(type, "test")); + } + + @Before + public void setUp() throws Exception { + extension = new TestProcessor(); + javaImplementationFactory = new DefaultJavaImplementationFactory(); + } + + @Retention(RUNTIME) + private @interface Bar { + + } + + private class TestProcessor extends AbstractPropertyProcessor<Bar> { + + public TestProcessor() { + super(new DefaultExtensionPointRegistry(), Bar.class); + } + + @Override + protected void initProperty(Property property, Bar annotation) { + // property.setDefaultValueFactory(EasyMock.createMock(ObjectFactory.class)); + property.setName("test"); + } + + @Override + protected String getName(Bar annotation) { + return "test"; + } + + @Override + protected boolean getRequired(Bar annotation) { + return true; + } + } + + private static class Foo { + + @Bar + protected String d; + + public Foo(String a, @Bar + String b) { + } + + public Foo(@Bar + String d) { + this.d = d; + } + + @Bar + public void setBar(String d) { + this.d = d; + } + + @Bar + public void setNoParamsBar() { + } + + @Bar + public String setBadBar(String d) { + return null; + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/ReadTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/ReadTestCase.java new file mode 100644 index 0000000000..6ea4868983 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/ReadTestCase.java @@ -0,0 +1,242 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.net.URI; +import java.net.URL; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Test reading Java implementations. + * + * @version $Rev$ $Date$ + */ +public class ReadTestCase { + + private static XMLInputFactory inputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static URLArtifactProcessor<Definitions> policyDefinitionsProcessor; + private static CompositeBuilder compositeBuilder; + private static Monitor monitor; + private static ProcessorContext context; + private static BuilderContext builderContext; + + @BeforeClass + public static void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + builderContext = new BuilderContext(extensionPoints); + inputFactory = XMLInputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + + compositeBuilder = extensionPoints.getExtensionPoint(BuilderExtensionPoint.class).getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeBuilder"); + + URLArtifactProcessorExtensionPoint documentProcessors = extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + policyDefinitionsProcessor = documentProcessors.getProcessor(Definitions.class); + + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); + monitor = monitorFactory.createMonitor(); + } + + @Test + public void testReadComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite)staxProcessor.read(reader, context); + assertNotNull(composite); + + // remove dependency on builder + //compositeBuilder.build(composite, builderContext); + + } + + @Ignore("To be fixed") + @Test + public void testPolicyIntents() throws Exception { + ModelResolver resolver = new TestModelResolver(getClass().getClassLoader()); + + URL url = getClass().getResource("definitions.xml"); + URI uri = URI.create("definitions.xml"); + Definitions scaDefns = policyDefinitionsProcessor.read(null, uri, url, context); + + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite)staxProcessor.read(reader, context); + assertNotNull(composite); + + staxProcessor.resolve(scaDefns, resolver, context); + staxProcessor.resolve(composite, resolver, context); + + builderContext.setDefinitions(scaDefns); + compositeBuilder.build(composite, builderContext); + + //intents are computed and aggregate intents from ancestor elements + assertEquals(((PolicySubject)composite.getComponents().get(0)).getRequiredIntents().size(), 3); + assertEquals(((PolicySubject)composite.getComponents().get(5)).getRequiredIntents().size(), 3); + + //assertEquals(((OperationsConfigurator)composite.getComponents().get(0)).getConfiguredOperations().isEmpty(), true); + //assertEquals(((OperationsConfigurator)composite.getComponents().get(5)).getConfiguredOperations().isEmpty(), false); + + + //test for proper aggregation of policy intents on implementation elements + for ( Intent intent : ((PolicySubject)composite.getComponents().get(0).getImplementation()).getRequiredIntents() ) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_2") || + intentName.equals("tuscanyIntent_3")) ) { + fail(); + } + } + + for ( Intent intent : ((PolicySubject)composite.getComponents().get(5)).getRequiredIntents() ) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_4") || + intentName.equals("tuscanyIntent_5")) ) { + fail(); + } + } + + /* + //test for proper aggregation of policy intents and policysets on operations of implementation + OperationsConfigurator opConf = (OperationsConfigurator)composite.getComponents().get(5); + assertEquals(opConf.getConfiguredOperations().get(0).getRequiredIntents().size(), 4); + for ( Intent intent : opConf.getConfiguredOperations().get(0).getRequiredIntents()) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_4") || + intentName.equals("tuscanyIntent_5") || intentName.equals("tuscanyIntent_6") ) ) { + fail(); + } + } + + opConf = (OperationsConfigurator)composite.getComponents().get(6); + assertEquals(opConf.getConfiguredOperations().get(0).getRequiredIntents().size(), 3); + for ( Intent intent : opConf.getConfiguredOperations().get(0).getRequiredIntents()) { + String intentName = intent.getName().getLocalPart(); + if ( !(intentName.equals("tuscanyIntent_1") || intentName.equals("tuscanyIntent_4") || + intentName.equals("tuscanyIntent_6.qualified2") ) ) { + fail(); + } + } + */ + } + + @Test + @Ignore("Broken for now in 2.0 bringup") + public void testPolicySets() throws Exception { + ModelResolver resolver = new TestModelResolver(getClass().getClassLoader()); + + URL url = getClass().getResource("definitions_with_policysets.xml"); + URI uri = URI.create("definitions_with_policysets.xml"); + Definitions policyDefinitions = policyDefinitionsProcessor.read(null, uri, url, context); + + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite)staxProcessor.read(reader, context); + assertNotNull(composite); + + for ( Component component : composite.getComponents() ) { + for ( PolicySet policySet : policyDefinitions.getPolicySets() ) { + component.getPolicySets().add(policySet); + } + } + + staxProcessor.resolve(policyDefinitions, resolver, context); + staxProcessor.resolve(composite, resolver, context); + + builderContext.setDefinitions(policyDefinitions); + compositeBuilder.build(composite, builderContext); + + //test for determination of policysets for implementation + assertEquals(((PolicySubject)composite.getComponents().get(0)).getPolicySets().size(), 1); + for ( PolicySet policySet : ((PolicySubject)composite.getComponents().get(0).getImplementation()).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1")) ) { + fail(); + } + } + + assertEquals(((PolicySubject)composite.getComponents().get(5)).getPolicySets().size(), 2); + for ( PolicySet policySet : ((PolicySubject)composite.getComponents().get(5).getImplementation()).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1") || policySetName.equals("tuscanyPolicySet_2")) ) { + fail(); + } + } + + /* + //test for computation of policysets on operations of implementation + OperationsConfigurator opConf = (OperationsConfigurator)composite.getComponents().get(5); + assertEquals(opConf.getConfiguredOperations().get(0).getPolicySets().size(), 3); + for ( PolicySet policySet : opConf.getConfiguredOperations().get(0).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1") || policySetName.equals("tuscanyPolicySet_2") + || policySetName.equals("tuscanyPolicySet_3")) ) { + fail(); + } + } + + opConf = (OperationsConfigurator)composite.getComponents().get(6); + assertEquals(opConf.getConfiguredOperations().get(0).getPolicySets().size(), 4); + for ( PolicySet policySet : opConf.getConfiguredOperations().get(0).getPolicySets() ) { + String policySetName = policySet.getName().getLocalPart(); + if ( !(policySetName.equals("tuscanyPolicySet_1") || policySetName.equals("tuscanyPolicySet_2") + || policySetName.equals("tuscanyPolicySet_3") + || policySetName.equals("tuscanyPolicySet_4")) ) { + fail(); + } + } + */ + + //new PrintUtil(System.out).print(composite); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/TestModelResolver.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/TestModelResolver.java new file mode 100644 index 0000000000..6be50d138f --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/TestModelResolver.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.xml; + +import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ClassReference; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + + +/** + * A default implementation of an artifact resolver, based on a map. + * + * @version $Rev$ $Date$ + */ +public class TestModelResolver implements ModelResolver { + private static final long serialVersionUID = -7826976465762296634L; + + private Map<Object, Object> map = new HashMap<Object, Object>(); + + private WeakReference<ClassLoader> classLoader; + + public TestModelResolver(ClassLoader classLoader) { + this.classLoader = new WeakReference<ClassLoader>(classLoader); + } + + public <T> T resolveModel(Class<T> modelClass, T unresolved, ProcessorContext context) { + Object resolved = map.get(unresolved); + if (resolved != null) { + + // Return the resolved object + return modelClass.cast(resolved); + + } else if (unresolved instanceof ClassReference) { + + // Load a class on demand + ClassReference classReference = (ClassReference)unresolved; + Class<?> clazz; + try { + clazz = Class.forName(classReference.getClassName(), true, classLoader.get()); + } catch (ClassNotFoundException e) { + + // Return the unresolved object + return unresolved; + } + + // Store a new ClassReference wrapping the loaded class + resolved = new ClassReference(clazz); + map.put(resolved, resolved); + + // Return the resolved ClassReference + return modelClass.cast(resolved); + + } else { + + // Return the unresolved object + return unresolved; + } + } + + public void addModel(Object resolved, ProcessorContext context) { + map.put(resolved, resolved); + } + + public Object removeModel(Object resolved, ProcessorContext context) { + return map.remove(resolved); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java new file mode 100644 index 0000000000..fdd606c58e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.implementation.java.xml; + +import java.io.InputStream; +import java.io.StringWriter; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +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.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test writing Java implementations. + * + * @version $Rev$ $Date$ + */ +public class WriteTestCase { + + private static StAXArtifactProcessor<Object> staxProcessor; + private static XMLInputFactory inputFactory; + private static XMLOutputFactory outputFactory; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + inputFactory = XMLInputFactory.newInstance(); + outputFactory = XMLOutputFactory.newInstance(); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory); + } + + @Test + public void testReadWriteComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + Composite composite = (Composite)staxProcessor.read(inputFactory.createXMLStreamReader(is), context); + Assert.assertNotNull(composite); + StringWriter sw = new StringWriter(); + staxProcessor.write(composite, outputFactory.createXMLStreamWriter(sw), context); + System.out.println(sw.toString()); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/Calculator.composite b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/Calculator.composite new file mode 100644 index 0000000000..87798357e0 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/Calculator.composite @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912" + xmlns:calc="http://sample.calculator" + requires="cns:tuscanyIntent_1" + targetNamespace="http://sample.calculator" + xmlns:cns="http://test" + name="Calculator"> + + <service name="CalculatorService" promote="CalculatorServiceComponent"> + <interface.java interface="calculator.CalculatorService"/> + </service> + + <component name="CalculatorServiceComponent" requires="cns:tuscanyIntent_2"> + <implementation.java class="calculator.CalculatorServiceImpl" requires="cns:tuscanyIntent_3" /> + <reference name="addService" target="AddServiceComponent"/> + <reference name="subtractService" target="SubtractServiceComponent"/> + <reference name="multiplyService" target="MultiplyServiceComponent"/> + <reference name="divideService" target="DivideServiceComponent"/> + </component> + + <component name="AddServiceComponent"> + <implementation.java class="calculator.AddServiceImpl"/> + </component> + + <component name="SubtractServiceComponent"> + <implementation.java class="calculator.SubtractServiceImpl"/> + </component> + + <component name="MultiplyServiceComponent"> + <implementation.java class="calculator.MultiplyServiceImpl"/> + </component> + + <component name="DivideServiceComponent"> + <implementation.java class="calculator.DivideServiceImpl"/> + </component> + + <component name="AnotherCalculatorServiceComponent" requires="cns:tuscanyIntent_4"> + <implementation.java class="calculator.CalculatorServiceImpl" requires="cns:tuscanyIntent_5"> + <operation name="add" requires="cns:tuscanyIntent_6"/> + </implementation.java> + </component> + + <component name="YetAnotherCalculatorServiceComponent" requires="cns:tuscanyIntent_4"> + <implementation.java class="calculator.CalculatorServiceImpl" requires="cns:tuscanyIntent_6.qualified1"> + <operation name="add" requires="cns:tuscanyIntent_6.qualified2" policySets="cns:tuscanyPolicySet_4"/> + </implementation.java> + </component> +</composite> diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/definitions.xml b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/definitions.xml new file mode 100644 index 0000000000..9ecb608816 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/definitions.xml @@ -0,0 +1,100 @@ +<?xml version="1.0" encoding="ASCII"?> +<!-- + * 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. +--> +<sca:definitions xmlns="http://test" + targetNamespace="http://test" + xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"> + +<!-- simple intent --> + <sca:intent name="tuscanyIntent_1" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_2" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_3" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_4" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + +<sca:intent name="tuscanyIntent_5" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_6" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_7" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_8" + constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <!-- qualified intents --> + <sca:intent name="tuscanyIntent_1.qualified" /> + <sca:intent name="tuscanyIntent_2.qualified" /> + <sca:intent name="tuscanyIntent_6.qualified1" /> + <sca:intent name="tuscanyIntent_6.qualified2" /> + + <sca:policySet name="tuscanyPolicySet_4" + provides="tuscanyIntent_6" + appliesTo="/sca:composite/sca:component" + xmlns="http://test" + xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <sca:intentMap provides="tuscanyIntent_6" default="qualified2"> + <sca:qualifier name="qualified2"> + <wsp:Policy> + <!-- policy expression and policy subject for "qualified2" alternative" --> + </wsp:Policy> + </sca:qualifier> + </sca:intentMap> + </sca:policySet> +</sca:definitions>
\ No newline at end of file diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/definitions_with_policysets.xml b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/definitions_with_policysets.xml new file mode 100644 index 0000000000..18bb3078e2 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/implementation-java/src/test/resources/org/apache/tuscany/sca/implementation/java/xml/definitions_with_policysets.xml @@ -0,0 +1,113 @@ +<?xml version="1.0" encoding="ASCII"?> +<!-- + * 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. +--> +<sca:definitions xmlns="http://test" targetNamespace="http://test" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200912"> + + <!-- simple intent --> + <sca:intent name="tuscanyIntent_1" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + <!-- qualified intents --> + <sca:qualifier name="qualified" default="true"/> + </sca:intent> + + <sca:intent name="tuscanyIntent_2" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + <sca:qualifier name="qualified" default="true"/> + </sca:intent> + + <sca:intent name="tuscanyIntent_3" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_4" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_5" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_6" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + <sca:qualifier name="qualified1" default="true"/> + <sca:qualifier name="qualified2" /> + + + </sca:intent> + + <sca:intent name="tuscanyIntent_7" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:intent name="tuscanyIntent_8" constrains="sca:binding sca:implementation.java"> + <sca:description> + Sample Intent + </sca:description> + </sca:intent> + + <sca:policySet name="tuscanyPolicySet_1" provides="tuscanyIntent_1 tuscanyIntent_2 tuscanyIntent_3" + appliesTo="/sca:composite/sca:component" xmlns="http://test" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + </sca:policySet> + + <sca:policySet name="tuscanyPolicySet_2" provides="tuscanyIntent_4 tuscanyIntent_5" appliesTo="/sca:composite/sca:component" + xmlns="http://test" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + </sca:policySet> + + <sca:policySet name="tuscanyPolicySet_3" provides="tuscanyIntent_6" appliesTo="/sca:composite/sca:component" + xmlns="http://test" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <sca:intentMap provides="tuscanyIntent_6" default="qualified1"> + <sca:qualifier name="qualified1"> + <wsp:Policy> + <!-- policy expression and policy subject for "qualified1" alternative --> + </wsp:Policy> + </sca:qualifier> + <sca:qualifier name="qualifed2"> + <wsp:Policy> + <!-- policy expression and policy subject for "qualified2" alternative" --> + </wsp:Policy> + </sca:qualifier> + </sca:intentMap> + </sca:policySet> + + <sca:policySet name="tuscanyPolicySet_4" provides="tuscanyIntent_6" appliesTo="/sca:composite/sca:component" + xmlns="http://test" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <sca:intentMap provides="tuscanyIntent_6" default="qualified2"> + <sca:qualifier name="qualified2"> + <wsp:Policy> + <!-- policy expression and policy subject for "qualified2" alternative" --> + </wsp:Policy> + </sca:qualifier> + </sca:intentMap> + </sca:policySet> + +</sca:definitions>
\ No newline at end of file |