summaryrefslogtreecommitdiffstats
path: root/sandbox/old/contrib/persistence/common
diff options
context:
space:
mode:
authordims <dims@13f79535-47bb-0310-9956-ffa450edef68>2008-06-17 00:23:01 +0000
committerdims <dims@13f79535-47bb-0310-9956-ffa450edef68>2008-06-17 00:23:01 +0000
commitbdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch)
tree38a92061c0793434c4be189f1d70c3458b6bc41d /sandbox/old/contrib/persistence/common
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r--sandbox/old/contrib/persistence/common/LICENSE.txt202
-rw-r--r--sandbox/old/contrib/persistence/common/NOTICE.txt14
-rw-r--r--sandbox/old/contrib/persistence/common/pom.xml.off85
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilder.java66
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java86
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java47
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java161
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitBuilder.java41
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitProcessor.java122
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java99
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyJpaException.java50
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java288
-rw-r--r--sandbox/old/contrib/persistence/common/src/main/resources/META-INF/sca/jpa.scdl38
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java29
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Department.java30
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Employee.java30
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScannerTestCase.java32
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitTestCase.java32
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/TestService1.java26
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/resources/META-INF/persistence.xml36
-rw-r--r--sandbox/old/contrib/persistence/common/src/test/resources/META-INF/sca/test1.scdl27
21 files changed, 1541 insertions, 0 deletions
diff --git a/sandbox/old/contrib/persistence/common/LICENSE.txt b/sandbox/old/contrib/persistence/common/LICENSE.txt
new file mode 100644
index 0000000000..d645695673
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/LICENSE.txt
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/sandbox/old/contrib/persistence/common/NOTICE.txt b/sandbox/old/contrib/persistence/common/NOTICE.txt
new file mode 100644
index 0000000000..d83ebbe236
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/NOTICE.txt
@@ -0,0 +1,14 @@
+${pom.name}
+Copyright (c) 2005 - 2006 The Apache Software Foundation
+
+Apache Tuscany is an effort undergoing incubation at The Apache Software
+Foundation (ASF), sponsored by the Apache Web Services PMC. Incubation is
+required of all newly accepted projects until a further review indicates that
+the infrastructure, communications, and decision making process have stabilized
+in a manner consistent with other successful ASF projects. While incubation
+status is not necessarily a reflection of the completeness or stability of the
+code, it does indicate that the project has yet to be fully endorsed by the ASF.
+
+This product includes software developed by
+The Apache Software Foundation (http://www.apache.org/).
+
diff --git a/sandbox/old/contrib/persistence/common/pom.xml.off b/sandbox/old/contrib/persistence/common/pom.xml.off
new file mode 100644
index 0000000000..9b5bf4c6ef
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/pom.xml.off
@@ -0,0 +1,85 @@
+<?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>
+
+ <parent>
+ <groupId>org.apache.tuscany.sca.services.persistence</groupId>
+ <artifactId>parent</artifactId>
+ <version>1.0-incubator-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>persistence-common</artifactId>
+ <packaging>jar</packaging>
+ <name>Apache Tuscany JPA</name>
+ <description>Tuscany JPA Integration.</description>
+ <version>1.0-alpha-incubating-SNAPSHOT</version>
+
+
+ <repositories>
+ <repository>
+ <id>java.net</id>
+ <url>https://maven-repository.dev.java.net/nonav/repository</url>
+ <layout>legacy</layout>
+ </repository>
+ <repository>
+ <id>apache-snapshots</id>
+ <url>http://people.apache.org/repo/m2-snapshot-repository</url>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>persistence-api</artifactId>
+ <version>1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca.kernel</groupId>
+ <artifactId>tuscany-spi</artifactId>
+ <version>1.0-alpha-incubating-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca.services.transaction</groupId>
+ <artifactId>tuscany-transaction-geronimo</artifactId>
+ <version>1.0-alpha-incubating-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca.kernel</groupId>
+ <artifactId>tuscany-api</artifactId>
+ <version>1.0-alpha-incubating-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.openjpa</groupId>
+ <artifactId>openjpa-persistence-jdbc</artifactId>
+ <version>0.9.0-incubating-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilder.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilder.java
new file mode 100644
index 0000000000..684eee8c09
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilder.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.service.persistence.common;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceUnitInfo;
+
+/**
+ * Default implementation of the persistence unit builder.
+ *
+ * @version $Rev$ $Date$
+ */
+public class DefaultPersistenceUnitBuilder implements PersistenceUnitBuilder {
+
+ /** Persistence unit scanner */
+ private PersistenceUnitScanner scanner = new PersistenceUnitScanner();
+
+ /**
+ * Builds the entity manager factory matching the unit name. All persistence.xml
+ * files available for the specified classloader is scanned for the specified
+ * persistence unit. The JPA provider API is used to create the entity manager
+ * factory.
+ *
+ * @param unitName Persistence unit name.
+ * @param classLoader Classloader.
+ * @return Entity manager factory.
+ */
+ public EntityManagerFactory newEntityManagerFactory(String unitName, ClassLoader classLoader) {
+
+ PersistenceUnitInfo info = scanner.getPersistenceUnitInfo(unitName, classLoader);
+ String providerClass = info.getPersistenceProviderClassName();
+ try {
+ Map overrides = new HashMap();
+ PersistenceProvider provider = (PersistenceProvider) Class.forName(providerClass).newInstance();
+ return provider.createContainerEntityManagerFactory(info, overrides);
+ } catch (InstantiationException ex) {
+ throw new TuscanyJpaException(ex);
+ } catch (IllegalAccessException ex) {
+ throw new TuscanyJpaException(ex);
+ } catch (ClassNotFoundException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.java
new file mode 100644
index 0000000000..492bd36689
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/EntityManagerProxy.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.service.persistence.common;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.Properties;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.transaction.Synchronization;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+/**
+ * Proxy for entity manager.
+ *
+ * @version $Rev$ $Date$
+ */
+public class EntityManagerProxy implements InvocationHandler {
+
+ // Thread local cache of entity managers
+ private ThreadLocal<EntityManager> entityManagers = new ThreadLocal<EntityManager>();
+
+ // Properties
+ private Properties prop;
+
+ // Entity manager factory
+ private EntityManagerFactory emf;
+
+ // Transaction manager
+ private TransactionManager txm;
+
+ /**
+ * Initializes the artifacts required to create an EM.
+ *
+ * @param prop EM creation porperty overrides.
+ * @param emf Entity manager factory to use.
+ * @param txm Transaction manager to use.
+ */
+ public EntityManagerProxy(Properties prop, EntityManagerFactory emf, TransactionManager txm) {
+ this.prop = prop;
+ this.txm = txm;
+ this.emf = emf;
+ }
+
+ /**
+ * Proxies the entity manager.
+ */
+ public Object invoke(Object target, Method method, Object[] parameters) throws Throwable {
+
+ EntityManager entityManager = entityManagers.get();
+ if(entityManager != null) {
+ entityManager = emf.createEntityManager(prop);
+ entityManagers.set(entityManager);
+ Transaction tx = txm.getTransaction();
+ if(tx != null) {
+ tx.registerSynchronization(new Synchronization() {
+ public void afterCompletion(int arg0) {
+ entityManagers.set(null);
+ }
+ public void beforeCompletion() {
+ }
+ });
+ }
+ }
+ return method.invoke(entityManager, parameters);
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java
new file mode 100644
index 0000000000..04d1c419ca
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.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.service.persistence.common;
+
+public class JpaConstants {
+
+ public static final String PROPERTY_VALUE = "value";
+
+ public static final String PROPERTY_NAME = "name";
+
+ public static final String PROPERTY = "//persistence-unit/properties/property";
+
+ public static final String TRANSACTION_TYPE = "//persistence-unit/@transaction-type";
+
+ public static final String NAME = "//persistence-unit/@name";
+
+ public static final String PROVIDER = "//persistence-unit/provider";
+
+ public static final String NON_JTA_DATA_SOURCE = "//persistence-unit/non-jta-data-source";
+
+ public static final String MAPPING_FILE = "//persistence-unit/mapping-file";
+
+ public static final String CLASS = "//persistence-unit/class";
+
+ public static final String JTA_DATA_SOURCE = "//persistence-unit/jta-data-source";
+
+ public static final String JAR_FILE = "//persistence-unit/jar-file";
+
+ public static final String EXCLUDE_UNLISTED_CLASSES = "//persistence-unit/exclude-unlisted-classes";
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java
new file mode 100644
index 0000000000..9d247fae43
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java
@@ -0,0 +1,161 @@
+/*
+ * 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.service.persistence.common;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.net.URI;
+import java.util.Properties;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.PersistenceProperty;
+import javax.transaction.TransactionManager;
+
+import org.osoa.sca.annotations.Reference;
+
+import org.apache.tuscany.spi.ObjectFactory;
+import org.apache.tuscany.spi.component.RegistrationException;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+import org.apache.tuscany.spi.idl.InvalidServiceContractException;
+import org.apache.tuscany.spi.idl.java.InterfaceJavaIntrospector;
+import org.apache.tuscany.spi.implementation.java.AbstractPropertyProcessor;
+import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService;
+import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
+import org.apache.tuscany.spi.implementation.java.ProcessingException;
+import org.apache.tuscany.spi.model.ServiceContract;
+
+import org.apache.tuscany.core.component.ComponentManager;
+
+/**
+ * Annotation processor for injecting <code>PersistenceUnit</code> annotations on properties.
+ *
+ * @version $Rev$ $Date$
+ */
+public class PersistenceContextProcessor extends AbstractPropertyProcessor<PersistenceContext> {
+
+ /**
+ * Transaction Manager
+ */
+ private TransactionManager transactionManager;
+ private ComponentManager componentManager;
+ private InterfaceJavaIntrospector introspector;
+
+ /**
+ * Persistence unit builder
+ */
+ private PersistenceUnitBuilder builder = new DefaultPersistenceUnitBuilder();
+
+ /**
+ * Injects the implementation processor service.
+ *
+ * @param service Implementation processor service.
+ */
+ public PersistenceContextProcessor(@Reference ImplementationProcessorService service) {
+ super(PersistenceContext.class, service);
+ }
+
+ @Reference
+ public void setTransactionManager(TransactionManager transactionManager) {
+ this.transactionManager = transactionManager;
+ }
+
+ @Reference
+ public void setComponentManager(ComponentManager componentManager) {
+ this.componentManager = componentManager;
+ }
+
+ @Reference
+ public void setIntrospector(InterfaceJavaIntrospector introspector) {
+ this.introspector = introspector;
+ }
+
+ /**
+ * Defaults to the field name.
+ */
+ @Override
+ protected String getName(PersistenceContext persistenceContext) {
+ return null;
+ }
+
+ /**
+ * Initializes the property.
+ */
+ @SuppressWarnings("unchecked")
+ protected <T> void initProperty(JavaMappedProperty<T> property,
+ PersistenceContext annotation,
+ DeploymentContext context) throws ProcessingException {
+
+ String unitName = annotation.unitName();
+ URI unitUri = context.getComponentId().resolve(unitName);
+ EntityManagerFactory emf = (EntityManagerFactory) componentManager.getComponent(unitUri);
+
+ if (emf == null) {
+ emf = builder.newEntityManagerFactory(unitName, context.getClassLoader());
+ try {
+ ServiceContract<EntityManagerFactory> contract =
+ (ServiceContract) introspector.introspect(EntityManagerFactory.class);
+ componentManager.registerJavaObject(unitUri, contract, emf);
+ } catch (RegistrationException e) {
+ throw new ProcessingException(e);
+ } catch (InvalidServiceContractException e) {
+ throw new ProcessingException(e);
+ }
+ }
+ ObjectFactory factory = new EmObjectFactory(emf, annotation);
+ property.setDefaultValueFactory(factory);
+
+ }
+
+ private class EmObjectFactory implements ObjectFactory<EntityManager> {
+
+ private EntityManagerFactory emf;
+ private PersistenceContext annotation;
+
+ public EmObjectFactory(EntityManagerFactory emf, PersistenceContext annotation) {
+ this.emf = emf;
+ this.annotation = annotation;
+ }
+
+ public EntityManager getInstance() {
+
+ PersistenceContextType type = annotation.type();
+ if (type == PersistenceContextType.TRANSACTION) {
+
+ Properties props = new Properties();
+ for (PersistenceProperty property : annotation.properties()) {
+ props.put(property.name(), property.value());
+ }
+
+ Class[] interfaces = new Class[]{EntityManager.class};
+ InvocationHandler handler = new EntityManagerProxy(props, emf, transactionManager);
+ EntityManager em =
+ (EntityManager) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, handler);
+ return em;
+
+ } else {
+ throw new UnsupportedOperationException("Extended persistence contexts not supported");
+ }
+
+ }
+
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitBuilder.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitBuilder.java
new file mode 100644
index 0000000000..d8d30d6811
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitBuilder.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.service.persistence.common;
+
+import javax.persistence.EntityManagerFactory;
+
+/**
+ * Interface for building persistence units as specified by the JPA specification..
+ *
+ * @version $Rev$ $Date$
+ */
+public interface PersistenceUnitBuilder {
+
+ /**
+ * Builds the entity manager factory matching the unit name. All persistence.xml files available for the specified
+ * classloader is scanned for the specified persistence unit. The JPA provider API is used to create the entity
+ * manager factory.
+ *
+ * @param unitName Persistence unit name.
+ * @param classLoader Classloader.
+ * @return Entity manager factory.
+ */
+ public EntityManagerFactory newEntityManagerFactory(String unitName, ClassLoader classLoader);
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitProcessor.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitProcessor.java
new file mode 100644
index 0000000000..169f48fec7
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitProcessor.java
@@ -0,0 +1,122 @@
+/*
+ * 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.service.persistence.common;
+
+import java.lang.reflect.Field;
+import java.net.URI;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
+
+import org.osoa.sca.annotations.Reference;
+
+import org.apache.tuscany.spi.ObjectFactory;
+import org.apache.tuscany.spi.component.AtomicComponent;
+import org.apache.tuscany.spi.component.RegistrationException;
+import org.apache.tuscany.spi.component.TargetException;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+import org.apache.tuscany.spi.idl.InvalidServiceContractException;
+import org.apache.tuscany.spi.idl.java.InterfaceJavaIntrospector;
+import org.apache.tuscany.spi.implementation.java.ImplementationProcessorExtension;
+import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
+import org.apache.tuscany.spi.implementation.java.JavaMappedReference;
+import org.apache.tuscany.spi.implementation.java.JavaMappedService;
+import org.apache.tuscany.spi.implementation.java.PojoComponentType;
+import org.apache.tuscany.spi.implementation.java.ProcessingException;
+import org.apache.tuscany.spi.implementation.java.Resource;
+import org.apache.tuscany.spi.model.ServiceContract;
+
+import org.apache.tuscany.core.component.ComponentManager;
+
+/**
+ * Annotation processor for injecting <code>PersistenceUnit</code> annotations on properties.
+ *
+ * @version $Rev$ $Date$
+ */
+public class PersistenceUnitProcessor extends ImplementationProcessorExtension {
+ /**
+ * Persistence unit builder
+ */
+ private PersistenceUnitBuilder builder = new DefaultPersistenceUnitBuilder();
+ private ComponentManager componentManager;
+ private InterfaceJavaIntrospector introspector;
+
+ @Reference
+ public void setComponentManager(ComponentManager componentManager) {
+ this.componentManager = componentManager;
+ }
+
+ @Reference
+ public void setIntrospector(InterfaceJavaIntrospector introspector) {
+ this.introspector = introspector;
+ }
+
+
+ @SuppressWarnings({"unchecked"})
+ public void visitField(Field field,
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ DeploymentContext context) throws ProcessingException {
+
+ PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
+ if (annotation == null) {
+ return;
+ }
+ String unitName = annotation.unitName();
+ URI unitUri = context.getComponentId().resolve(unitName);
+ AtomicComponent component = (AtomicComponent) componentManager.getComponent(unitUri);
+ EntityManagerFactory emf;
+ if (component == null) {
+ emf = builder.newEntityManagerFactory(unitName, context.getClassLoader());
+ try {
+ ServiceContract<EntityManagerFactory> contract =
+ (ServiceContract) introspector.introspect(EntityManagerFactory.class);
+ componentManager.registerJavaObject(unitUri, contract, emf);
+ } catch (RegistrationException e) {
+ throw new ProcessingException(e);
+ } catch (InvalidServiceContractException e) {
+ throw new ProcessingException(e);
+ }
+ } else {
+ try {
+ emf = (EntityManagerFactory) component.getTargetInstance();
+ } catch (TargetException e) {
+ throw new ProcessingException(e);
+ }
+ }
+
+ ObjectFactory factory = new EmfObjectFactory(emf);
+ Resource resource = new Resource(unitName, field.getType(), field);
+ resource.setObjectFactory(factory);
+ type.add(resource);
+ }
+
+ private class EmfObjectFactory implements ObjectFactory<EntityManagerFactory> {
+
+ private EntityManagerFactory emf;
+
+ public EmfObjectFactory(EntityManagerFactory emf) {
+ this.emf = emf;
+ }
+
+ public EntityManagerFactory getInstance() {
+ return emf;
+ }
+
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java
new file mode 100644
index 0000000000..00fd02b891
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.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.service.persistence.common;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+/**
+ * Scans the classloader for the specified persistence unit.
+ *
+ * @version $Rev$ $Date$
+ */
+class PersistenceUnitScanner {
+
+ /** Cache of persistence unit info */
+ private Map<String, PersistenceUnitInfo> persistenceUnitInfos = new HashMap<String, PersistenceUnitInfo>();
+
+ /**
+ * Scans the lassloader for the specified persistence unit and creates
+ * an immutable representation of the information present in the matching
+ * persistence.xml file.
+ *
+ * @param unitName Persistence unit name.
+ * @param classLoader Classloader to scan.
+ * @return Persistence unit information.
+ */
+ PersistenceUnitInfo getPersistenceUnitInfo(String unitName, ClassLoader classLoader) {
+
+ synchronized (persistenceUnitInfos) {
+
+ if(persistenceUnitInfos.containsKey(unitName)) {
+ return persistenceUnitInfos.get(unitName);
+ }
+
+ try {
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder db = dbf.newDocumentBuilder();
+
+ Enumeration<URL> persistenceUnitUrls = classLoader.getResources("META-INF/persistence.xml");
+ while (persistenceUnitUrls.hasMoreElements()) {
+
+ URL persistenceUnitUrl = persistenceUnitUrls.nextElement();
+ Document persistenceDom = db.parse(persistenceUnitUrl.openStream());
+
+ String rootJarUrl = persistenceUnitUrl.toString();
+ rootJarUrl = rootJarUrl.substring(0, rootJarUrl.lastIndexOf("META-INF"));
+
+ PersistenceUnitInfo info = new TuscanyPersistenceUnitInfo(persistenceDom, classLoader, rootJarUrl);
+ if (!unitName.equals(info.getPersistenceUnitName())) {
+ continue;
+ }
+
+ persistenceUnitInfos.put(unitName, info);
+ return info;
+
+ }
+ } catch (IOException ex) {
+ throw new TuscanyJpaException(ex);
+ } catch (ParserConfigurationException ex) {
+ throw new TuscanyJpaException(ex);
+ } catch (SAXException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ throw new TuscanyJpaException("Unabel to find persistence unit: " + unitName);
+
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyJpaException.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyJpaException.java
new file mode 100644
index 0000000000..2690f43f7c
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyJpaException.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.service.persistence.common;
+
+import org.apache.tuscany.api.TuscanyRuntimeException;
+
+/**
+ * Unexpected exception in processing JPA annotations.
+ *
+ */
+public class TuscanyJpaException extends TuscanyRuntimeException {
+
+ /**
+ * Serial version UID.
+ */
+ private static final long serialVersionUID = 2772746952304093785L;
+
+ /**
+ * Initializes the message.
+ * @param message Message to be initialized.
+ */
+ public TuscanyJpaException(String message) {
+ super(message);
+ }
+
+ /**
+ * Initializes the underlying cause.
+ * @param cause Cause to be initialized.
+ */
+ public TuscanyJpaException(Throwable cause) {
+ super(cause);
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java
new file mode 100644
index 0000000000..eb1dee24a7
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java
@@ -0,0 +1,288 @@
+/*
+ * 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.service.persistence.common;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.persistence.spi.ClassTransformer;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.sql.DataSource;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import static org.apache.tuscany.service.persistence.common.JpaConstants.*;
+
+/**
+ * Encpasulates the information in the persistence.xml file.
+ *
+ * This class is expected to be interogated by the provider only
+ * during the creation of the entity manager factory. Hence none
+ * of the values are cached, rather every time a property is queried
+ * the underlying DOM is interogated.
+ *
+ */
+class TuscanyPersistenceUnitInfo implements PersistenceUnitInfo {
+
+ /** Persistence DOM */
+ private Node persistenceDom;
+
+ /** Classloader */
+ private ClassLoader classLoader;
+
+ /** Root Url */
+ private String rootUrl;
+
+ /** XPath API */
+ XPath xpath = XPathFactory.newInstance().newXPath();
+
+ /**
+ * Initializes the properties.
+ *
+ * @param persistenceDom
+ * @param classLoader
+ * @param rootUrl
+ */
+ public TuscanyPersistenceUnitInfo(Node persistenceDom, ClassLoader classLoader, String rootUrl) {
+ this.persistenceDom = persistenceDom;
+ this.classLoader = classLoader;
+ this.rootUrl = rootUrl;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#addTransformer(javax.persistence.spi.ClassTransformer)
+ */
+ public void addTransformer(ClassTransformer classTransformer) {
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#excludeUnlistedClasses()
+ */
+ public boolean excludeUnlistedClasses() {
+ return getBooleanValue(persistenceDom, EXCLUDE_UNLISTED_CLASSES);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getClassLoader()
+ */
+ public ClassLoader getClassLoader() {
+ return classLoader;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getJarFileUrls()
+ */
+ public List<URL> getJarFileUrls() {
+
+ List<String> jarFiles = getMultipleValues(persistenceDom, JAR_FILE);
+ try {
+ List<URL> jarUrls = new LinkedList<URL>();
+ for (String jarFile : jarFiles) {
+ jarUrls.add(new URL(jarFile));
+ }
+ return jarUrls;
+ } catch (MalformedURLException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
+ */
+ public DataSource getJtaDataSource() {
+
+ String jtaDsName = getSingleValue(persistenceDom, JTA_DATA_SOURCE);
+ if (jtaDsName == null || "".equals(jtaDsName)) {
+ return null;
+ }
+ return lookupDataSource(jtaDsName);
+
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getManagedClassNames()
+ */
+ public List<String> getManagedClassNames() {
+ return getMultipleValues(persistenceDom, CLASS);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getMappingFileNames()
+ */
+ public List<String> getMappingFileNames() {
+ return getMultipleValues(persistenceDom, MAPPING_FILE);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getNewTempClassLoader()
+ */
+ public ClassLoader getNewTempClassLoader() {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
+ */
+ public DataSource getNonJtaDataSource() {
+
+ String nonJtaDsName = getSingleValue(persistenceDom, NON_JTA_DATA_SOURCE);
+ if (nonJtaDsName == null || "".equals(nonJtaDsName)) {
+ return null;
+ }
+ return lookupDataSource(nonJtaDsName);
+
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getPersistenceProviderClassName()
+ */
+ public String getPersistenceProviderClassName() {
+ return getSingleValue(persistenceDom, PROVIDER);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getPersistenceUnitName()
+ */
+ public String getPersistenceUnitName() {
+ return getSingleValue(persistenceDom, NAME);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getPersistenceUnitRootUrl()
+ */
+ public URL getPersistenceUnitRootUrl() {
+ try {
+ return new URL(rootUrl);
+ } catch (MalformedURLException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getProperties()
+ */
+ public Properties getProperties() {
+ return getProperties(persistenceDom);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.persistence.spi.PersistenceUnitInfo#getTransactionType()
+ */
+ public PersistenceUnitTransactionType getTransactionType() {
+ String transactionType = getSingleValue(persistenceDom, TRANSACTION_TYPE);
+ return "JTA".equals(transactionType) ? PersistenceUnitTransactionType.JTA : PersistenceUnitTransactionType.RESOURCE_LOCAL;
+ }
+
+ /*
+ * Extracts additional properties.
+ */
+ private Properties getProperties(Node root) {
+
+ try {
+ NodeList nodeList = (NodeList) xpath.evaluate(PROPERTY, root, XPathConstants.NODESET);
+ Properties data = new Properties();
+
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Element property = (Element) nodeList.item(i);
+ data.put(property.getAttribute(PROPERTY_NAME), property.getAttribute(PROPERTY_VALUE));
+ }
+
+ return data;
+ } catch (XPathExpressionException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ /*
+ * Gets multiple values for the specified expression.
+ */
+ private List<String> getMultipleValues(Node context, String expression) {
+
+ try {
+ NodeList nodeList = (NodeList) xpath.evaluate(expression, context, XPathConstants.NODESET);
+ List<String> data = new LinkedList<String>();
+
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ data.add(nodeList.item(i).getTextContent());
+ }
+
+ return data;
+ } catch (XPathExpressionException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ /*
+ * Gets single value for the specified expression.
+ */
+ private String getSingleValue(Node context, String expression) {
+
+ try {
+ return xpath.evaluate(expression, context);
+ } catch (XPathExpressionException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ /*
+ * Gets single value for the specified expression.
+ */
+ private boolean getBooleanValue(Node context, String expression) {
+ return Boolean.valueOf(getSingleValue(context, expression));
+ }
+
+ /*
+ * Looks up datasource.
+ */
+ private DataSource lookupDataSource(String dsName) {
+ Context ctx = null;
+ try {
+ ctx = new InitialContext();
+ return (DataSource) ctx.lookup(dsName);
+ } catch (NamingException ex) {
+ throw new TuscanyJpaException(ex);
+ } finally {
+ if (ctx != null) {
+ try {
+ ctx.close();
+ } catch (NamingException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+ }
+ }
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/main/resources/META-INF/sca/jpa.scdl b/sandbox/old/contrib/persistence/common/src/main/resources/META-INF/sca/jpa.scdl
new file mode 100644
index 0000000000..1166ade155
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/main/resources/META-INF/sca/jpa.scdl
@@ -0,0 +1,38 @@
+<?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.
+-->
+<!--
+ Default system configuration for the launcher environment.
+
+ $Rev$ $Date$
+-->
+<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
+ xmlns:system="http://tuscany.apache.org/xmlns/system/1.0-SNAPSHOT"
+ name="org.apache.tuscany.core.Implementation">
+
+ <!-- Implementation processor for JPA annotations. -->
+ <component name="jpa.persistenceUnit">
+ <system:implementation.system class="org.apache.tuscany.service.persistence.common.PersistenceUnitProcessor"/>
+ </component>
+
+ <component name="jpa.persistenceContext">
+ <system:implementation.system class="org.apache.tuscany.service.persistence.common.PersistenceContextProcessor"/>
+ </component>
+
+</composite>
diff --git a/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java
new file mode 100644
index 0000000000..cd3a914ab9
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.service.persistence.common;
+
+import junit.framework.TestCase;
+
+public class DefaultPersistenceUnitBuilderTestCase extends TestCase {
+
+ public void testNewEntityManagerFactory() {
+ new DefaultPersistenceUnitBuilder().newEntityManagerFactory("test", getClass().getClassLoader());
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Department.java b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Department.java
new file mode 100644
index 0000000000..bbbd32fa87
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Department.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.service.persistence.common;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class Department {
+
+ @Id
+ private Long id;
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Employee.java b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Employee.java
new file mode 100644
index 0000000000..d90e3284eb
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/Employee.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.service.persistence.common;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class Employee {
+
+ @Id
+ private Long id;
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScannerTestCase.java b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScannerTestCase.java
new file mode 100644
index 0000000000..b8d7c53d4d
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScannerTestCase.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.service.persistence.common;
+
+import junit.framework.TestCase;
+
+public class PersistenceUnitScannerTestCase extends TestCase {
+
+ public void testGetPersistenceUnitInfo() {
+
+ new PersistenceUnitScanner().getPersistenceUnitInfo("test", getClass().getClassLoader());
+
+
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitTestCase.java b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitTestCase.java
new file mode 100644
index 0000000000..6b6fb7ad43
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/PersistenceUnitTestCase.java
@@ -0,0 +1,32 @@
+package org.apache.tuscany.service.persistence.common;
+
+import junit.framework.TestCase;
+
+public class PersistenceUnitTestCase extends TestCase {
+
+ protected void setUp() throws Exception {
+/*
+ addExtension("tuscany.jpa", getClass().getClassLoader().getResource("META-INF/sca/jpa.scdl"));
+ addExtension("geronimo.jta", getClass().getClassLoader().getResource("META-INF/sca/geronimo.jta.scdl"));
+ setApplicationSCDL(getClass().getClassLoader().getResource("META-INF/sca/test1.scdl"));
+ super.setUp();
+ RuntimeComponent runtime = (RuntimeComponent) component.getParent().getParent();
+ CompositeComponent systemComposite = runtime.getSystemComponent();
+ CompositeComponent topLevelComposite = (CompositeComponent) systemComposite.getSystemChild(TUSCANY_SYSTEM);
+
+ JavaAtomicComponent cmp = (JavaAtomicComponent) component.getChild("TestService1");
+ TestService1 testService1 = (TestService1) cmp.getTargetInstance();
+ testService1.testMethod();
+*/
+ }
+
+ protected void tearDown() throws Exception {
+/*
+ super.tearDown();
+*/
+ }
+
+ public void testGetComponent() {
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/TestService1.java b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/TestService1.java
new file mode 100644
index 0000000000..74052984b9
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/TestService1.java
@@ -0,0 +1,26 @@
+package org.apache.tuscany.service.persistence.common;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
+import javax.transaction.TransactionManager;
+
+import org.apache.tuscany.api.annotation.Resource;
+
+public class TestService1 {
+
+ @Resource
+ protected TransactionManager tx;
+
+ @PersistenceUnit(unitName = "test")
+ protected EntityManagerFactory emf;
+
+ public void testMethod() throws Exception {
+
+ tx.begin();
+ EntityManager em = emf.createEntityManager();
+ tx.commit();
+
+ }
+
+}
diff --git a/sandbox/old/contrib/persistence/common/src/test/resources/META-INF/persistence.xml b/sandbox/old/contrib/persistence/common/src/test/resources/META-INF/persistence.xml
new file mode 100644
index 0000000000..0fc71881db
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/resources/META-INF/persistence.xml
@@ -0,0 +1,36 @@
+<?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.
+ -->
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
+ version="1.0">
+ <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
+ <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
+ <class>org.apache.tuscany.service.persistence.common.Employee</class>
+ <class>org.apache.tuscany.service.persistence.common.Department</class>
+ <properties>
+ <property name="openjpa.ConnectionURL" value="jdbc:hsqldb:tutorial_database"/>
+ <property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver"/>
+ <property name="openjpa.ConnectionUserName" value="sa"/>
+ <property name="openjpa.ConnectionPassword" value=""/>
+ <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
+ </properties>
+ </persistence-unit>
+</persistence> \ No newline at end of file
diff --git a/sandbox/old/contrib/persistence/common/src/test/resources/META-INF/sca/test1.scdl b/sandbox/old/contrib/persistence/common/src/test/resources/META-INF/sca/test1.scdl
new file mode 100644
index 0000000000..4470ca506e
--- /dev/null
+++ b/sandbox/old/contrib/persistence/common/src/test/resources/META-INF/sca/test1.scdl
@@ -0,0 +1,27 @@
+<?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://www.osoa.org/xmlns/sca/1.0"
+ name="LocalWireComposite">
+
+ <component name="TestService1">
+ <implementation.java class="org.apache.tuscany.service.persistence.common.TestService1"/>
+ </component>
+
+</composite> \ No newline at end of file