You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2015/04/22 20:32:50 UTC
tomee git commit: TOMEE-1555 @AutoJPA
Repository: tomee
Updated Branches:
refs/heads/master 4efb36aed -> bcda96d58
TOMEE-1555 @AutoJPA
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/bcda96d5
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/bcda96d5
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/bcda96d5
Branch: refs/heads/master
Commit: bcda96d58c7a8aa8009f3192a4f2bf36a37fc5af
Parents: 4efb36a
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Wed Apr 22 20:32:42 2015 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Wed Apr 22 20:32:42 2015 +0200
----------------------------------------------------------------------
.../openejb/api/configuration/AutoJPA.java | 35 +++++++
.../openejb/api/configuration/AutoJPAs.java | 28 ++++++
.../openejb/config/ConfigurationDeployer.java | 98 ++++++++++++++++++++
.../openejb/config/ConfigurationFactory.java | 2 +
.../openejb/configuration/AutoJPATest.java | 77 +++++++++++++++
5 files changed, 240 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/bcda96d5/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPA.java
----------------------------------------------------------------------
diff --git a/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPA.java b/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPA.java
new file mode 100644
index 0000000..15eebcb
--- /dev/null
+++ b/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPA.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.openejb.api.configuration;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface AutoJPA {
+ String unitName() default "jpa";
+ String provider() default "auto";
+ String jtaDataSource() default "auto";
+ String nonJtaDataSource() default "auto";
+ String entitiesPackage() default "auto";
+ String[] properties() default {};
+ boolean ddlAuto() default true;
+ boolean jta() default true;
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/bcda96d5/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPAs.java
----------------------------------------------------------------------
diff --git a/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPAs.java b/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPAs.java
new file mode 100644
index 0000000..f9f01b7
--- /dev/null
+++ b/container/openejb-api/src/main/java/org/apache/openejb/api/configuration/AutoJPAs.java
@@ -0,0 +1,28 @@
+/*
+ * 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.openejb.api.configuration;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface AutoJPAs {
+ AutoJPA[] value() default {};
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/bcda96d5/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationDeployer.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationDeployer.java b/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationDeployer.java
new file mode 100644
index 0000000..680c2ad
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationDeployer.java
@@ -0,0 +1,98 @@
+/*
+ * 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.openejb.config;
+
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.api.configuration.AutoJPA;
+import org.apache.openejb.api.configuration.AutoJPAs;
+import org.apache.openejb.jee.jpa.unit.Persistence;
+import org.apache.openejb.jee.jpa.unit.PersistenceUnit;
+import org.apache.openejb.jee.jpa.unit.TransactionType;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+import org.apache.openejb.util.PropertyPlaceHolderHelper;
+
+public class ConfigurationDeployer implements DynamicDeployer {
+ @Override
+ public AppModule deploy(final AppModule appModule) throws OpenEJBException {
+ for (final EjbModule module : appModule.getEjbModules()) {
+ if (module.getFinder() == null) {
+ continue;
+ }
+
+ for (final Class<?> configClass : module.getFinder().findAnnotatedClasses(AutoJPA.class)) {
+ configureJpa(appModule, configClass.getAnnotation(AutoJPA.class));
+ }
+ for (final Class<?> configClass : module.getFinder().findAnnotatedClasses(AutoJPAs.class)) {
+ for (final AutoJPA autoJPA : configClass.getAnnotation(AutoJPAs.class).value()) {
+ configureJpa(appModule, autoJPA);
+ }
+ }
+ }
+ return appModule;
+ }
+
+ private void configureJpa(final AppModule appModule, final AutoJPA annotation) {
+ if (annotation == null) {
+ return;
+ }
+
+ final String unitName = PropertyPlaceHolderHelper.simpleValue(annotation.unitName());
+ for (final PersistenceModule module : appModule.getPersistenceModules()) {
+ for (final PersistenceUnit unit : module.getPersistence().getPersistenceUnit()) {
+ if (unitName.equals(unit.getName())) {
+ Logger.getInstance(LogCategory.OPENEJB_STARTUP, ConfigurationDeployer.class).info("Unit[" + unitName + "] overriden by a persistence.xml with root url: " + module.getRootUrl());
+ return;
+ }
+ }
+ }
+
+ final PersistenceUnit unit = new PersistenceUnit();
+ unit.setName(unitName);
+ if (!"auto".equals(annotation.jtaDataSource())) {
+ unit.setJtaDataSource(PropertyPlaceHolderHelper.simpleValue(annotation.jtaDataSource()));
+ }
+ if (!"auto".equals(annotation.nonJtaDataSource())) {
+ unit.setNonJtaDataSource(PropertyPlaceHolderHelper.simpleValue(annotation.nonJtaDataSource()));
+ }
+ for (final String prop : annotation.properties()) {
+ final int equalIndex = prop.indexOf('=');
+ unit.setProperty(PropertyPlaceHolderHelper.simpleValue(prop.substring(0, equalIndex)), PropertyPlaceHolderHelper.simpleValue(prop.substring(equalIndex + 1, prop.length())));
+ }
+ unit.setProperty("openejb.jpa.auto-scan", "true");
+ if (!"auto".equals(annotation.entitiesPackage())) {
+ unit.setProperty("openejb.jpa.auto-scan.package", PropertyPlaceHolderHelper.simpleValue(annotation.entitiesPackage()));
+ }
+ if (annotation.ddlAuto()) {
+ unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
+ unit.setProperty("javax.persistence.schema-generation.database.action", "create");
+ }
+ if (annotation.jta()) {
+ unit.setTransactionType(TransactionType.JTA);
+ } else {
+ unit.setTransactionType(TransactionType.RESOURCE_LOCAL);
+ unit.setNonJtaDataSource("autoNonJtaDb"); // otherwise type is forced to JTA
+ }
+ if (!"auto".equals(annotation.provider())) {
+ unit.setProvider(annotation.provider());
+ }
+
+ final Persistence persistence = new Persistence();
+ persistence.addPersistenceUnit(unit);
+ appModule.addPersistenceModule(new PersistenceModule(appModule, "@Configuration#" + unitName, persistence));
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/bcda96d5/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java b/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
index bfbbc84..e35f430 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
@@ -229,6 +229,8 @@ public class ConfigurationFactory implements OpenEjbConfigurationFactory {
chain.add(beanProperties);
+ chain.add(new ConfigurationDeployer()); // after annotation deployer and read descriptors are the only constraints
+
chain.add(new ProxyBeanClassUpdate());
chain.add(new GeneratedClientModules.Prune());
http://git-wip-us.apache.org/repos/asf/tomee/blob/bcda96d5/container/openejb-core/src/test/java/org/apache/openejb/configuration/AutoJPATest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/configuration/AutoJPATest.java b/container/openejb-core/src/test/java/org/apache/openejb/configuration/AutoJPATest.java
new file mode 100644
index 0000000..be35e31
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/configuration/AutoJPATest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.openejb.configuration;
+
+import org.apache.openejb.api.configuration.AutoJPA;
+import org.apache.openejb.api.configuration.AutoJPAs;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.persistence.JtaEntityManager;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.SimpleLog;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.persistence.Entity;
+import javax.persistence.EntityManager;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.PersistenceContext;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@SimpleLog
+@Classes(innerClassesAsBean = true)
+@RunWith(ApplicationComposer.class)
+public class AutoJPATest {
+ @PersistenceContext(unitName = "jpa")
+ private EntityManager em1;
+
+ @PersistenceContext(unitName = "jpa2")
+ private EntityManager em2;
+
+ @Test
+ public void run() {
+ assertNotNull(em1);
+ assertNotNull(em2);
+ assertTrue(JtaEntityManager.class.isInstance(em1));
+ }
+
+ @AutoJPAs({
+ @AutoJPA,
+ @AutoJPA(unitName = "jpa2", jta = false)
+ })
+ public static class MyConfig {
+ }
+
+ @Entity
+ public static class AnEntity {
+ @Id
+ @GeneratedValue
+ private long id;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(final long id) {
+ this.id = id;
+ }
+ }
+}