You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by me...@apache.org on 2006/12/03 18:39:44 UTC
svn commit: r481835 -
/incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java
Author: meerajk
Date: Sun Dec 3 09:39:42 2006
New Revision: 481835
URL: http://svn.apache.org/viewvc?view=rev&rev=481835
Log:
Initial cut for the persistence context annotation.
Added:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java (with props)
Added: incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java?view=auto&rev=481835
==============================================================================
--- incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java (added)
+++ incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java Sun Dec 3 09:39:42 2006
@@ -0,0 +1,93 @@
+/*
+ * 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.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+
+import org.apache.tuscany.spi.ObjectFactory;
+import org.apache.tuscany.spi.annotation.Autowire;
+import org.apache.tuscany.spi.component.CompositeComponent;
+import org.apache.tuscany.spi.deployer.DeploymentContext;
+import org.apache.tuscany.spi.implementation.java.AbstractPropertyProcessor;
+import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService;
+import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
+
+/**
+ * Annotation processor for injecting <code>PersistenceUnit</code>
+ * annotations on properties.
+ *
+ */
+public class PersistenceContextProcessor extends AbstractPropertyProcessor<PersistenceContext> {
+
+ /** Persistence unit builder */
+ private PersistenceUnitBuilder builder = new DefaultPersistenceUnitBuilder();
+
+ /**
+ * Injects the implementation processor service.
+ * @param service Implementation processor service.
+ */
+ public PersistenceContextProcessor(@Autowire ImplementationProcessorService service) {
+ super(PersistenceContext.class, service);
+ }
+
+ /**
+ * 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, CompositeComponent parent, DeploymentContext context) {
+
+ String unitName = annotation.unitName();
+ EntityManagerFactory emf = (EntityManagerFactory) parent.getSystemChild(unitName);
+
+ if (emf == null) {
+ emf = builder.newEntityManagerFactory(unitName, context.getClassLoader());
+ parent.registerJavaObject(unitName, EntityManagerFactory.class, emf);
+ }
+ ObjectFactory factory = new EmObjectFactory(emf);
+ property.setDefaultValueFactory(factory);
+
+ }
+
+ private class EmObjectFactory implements ObjectFactory<EntityManager> {
+
+ private EntityManagerFactory emf;
+
+ public EmObjectFactory(EntityManagerFactory emf) {
+ this.emf = emf;
+ }
+
+ public EntityManager getInstance() {
+ EntityManager em = emf.createEntityManager();
+ em.joinTransaction();
+ return em;
+ }
+
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceContextProcessor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org