You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ge...@apache.org on 2010/01/30 01:11:41 UTC
svn commit: r904688 - in /openwebbeans/trunk/webbeans-openejb/src:
main/java/org/apache/webbeans/ejb/resource/
test/java/org/apache/webbeans/ejb/ test/resources/META-INF/
Author: gerdogdu
Date: Sat Jan 30 00:11:40 2010
New Revision: 904688
URL: http://svn.apache.org/viewvc?rev=904688&view=rev
Log:
Update for reflecting SPI module and for change related with geronimo module removing
Added:
openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/
openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/OpenEjbResourceServiceImpl.java (with props)
openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceFactory.java (with props)
openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceInjectionProcessor.java (with props)
openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/MyEntity.java (with props)
openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/OpenEJBIntegrationTest.java (with props)
openwebbeans/trunk/webbeans-openejb/src/test/resources/META-INF/persistence.xml (with props)
Added: openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/OpenEjbResourceServiceImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/OpenEjbResourceServiceImpl.java?rev=904688&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/OpenEjbResourceServiceImpl.java (added)
+++ openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/OpenEjbResourceServiceImpl.java Sat Jan 30 00:11:40 2010
@@ -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.webbeans.ejb.resource;
+
+import java.lang.reflect.Field;
+
+import org.apache.webbeans.spi.ResourceService;
+
+public class OpenEjbResourceServiceImpl implements ResourceService
+{
+
+ @Override
+ public Object getResource(Field field)
+ {
+ return ResourceFactory.getInstance().getResourceObject(field);
+ }
+
+
+}
Propchange: openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/OpenEjbResourceServiceImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceFactory.java?rev=904688&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceFactory.java (added)
+++ openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceFactory.java Sat Jan 30 00:11:40 2010
@@ -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.webbeans.ejb.resource;
+
+import java.lang.reflect.Field;
+
+import javax.naming.Context;
+
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+
+public class ResourceFactory
+{
+ private static ResourceFactory factory = null;
+
+ private ResourceInjectionProcessor processor = null;
+
+ private static Context context = null;
+
+ static
+ {
+ try
+ {
+ context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
+
+ } catch(Exception e) {
+
+ context = null;
+ }
+
+ }
+
+ public static ResourceFactory getInstance()
+ {
+ if(factory == null)
+ {
+ factory = new ResourceFactory();
+ factory.processor = new ResourceInjectionProcessor(context);
+ }
+
+ return factory;
+ }
+
+ public Object getResourceObject(Field field) throws RuntimeException
+ {
+ try
+ {
+ return this.processor.getResourceObject(field);
+
+ } catch(Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
Propchange: openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceInjectionProcessor.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceInjectionProcessor.java?rev=904688&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceInjectionProcessor.java (added)
+++ openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceInjectionProcessor.java Sat Jan 30 00:11:40 2010
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
+ * or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.apache.webbeans.ejb.resource;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.enterprise.inject.CreationException;
+import javax.naming.Context;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+import javax.xml.ws.WebServiceRef;
+
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.persistence.JtaEntityManager;
+import org.apache.openejb.persistence.JtaEntityManagerRegistry;
+
+
+class ResourceInjectionProcessor
+{
+ private Context context = null;
+
+ ResourceInjectionProcessor(Context context)
+ {
+ this.context = context;
+ }
+
+ public Object getResourceObject(Field field) throws IllegalAccessException, InvocationTargetException, NamingException
+ {
+ if (context == null)
+ {
+ // No resource injection
+ return null;
+ }
+
+ if (field.isAnnotationPresent(Resource.class))
+ {
+ Resource annotation = field.getAnnotation(Resource.class);
+
+ return lookupFieldResource(context, "openejb/Resource/"+annotation.name());
+
+ }
+
+ if (field.isAnnotationPresent(EJB.class))
+ {
+ EJB annotation = field.getAnnotation(EJB.class);
+
+ return lookupFieldResource(context, annotation.name());
+ }
+
+ if (field.isAnnotationPresent(WebServiceRef.class))
+ {
+ WebServiceRef annotation = field.getAnnotation(WebServiceRef.class);
+
+ return lookupFieldResource(context, annotation.name());
+ }
+
+ if (field.isAnnotationPresent(PersistenceContext.class))
+ {
+ PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
+
+ return this.getPersistenceContext(context, annotation.unitName());
+
+ }
+
+ if (field.isAnnotationPresent(PersistenceUnit.class))
+ {
+ PersistenceUnit annotation = field.getAnnotation(PersistenceUnit.class);
+
+ return this.getPersistenceUnit(context, annotation.unitName());
+ }
+
+ return null;
+ }
+
+ /**
+ * Inject resources in specified field.
+ */
+ protected Object lookupFieldResource(javax.naming.Context context, String name) throws NamingException, IllegalAccessException
+ {
+ Object lookedupResource = null;
+
+ if ((name != null) && (name.length() > 0))
+ {
+ lookedupResource = context.lookup("java:/" + name);
+ }
+
+ return lookedupResource;
+ }
+
+ private EntityManager getPersistenceContext(Context context, String unitName) {
+ // get JtaEntityManagerRegistry
+ JtaEntityManagerRegistry jtaEntityManagerRegistry = SystemInstance.get().getComponent(JtaEntityManagerRegistry.class);
+
+ EntityManagerFactory factory = getPersistenceUnit(context, unitName);
+
+ JtaEntityManager jtaEntityManager = new JtaEntityManager(jtaEntityManagerRegistry, factory, null, false);
+
+ return jtaEntityManager;
+ }
+
+ private EntityManagerFactory getPersistenceUnit(Context context, String unitName) {
+ EntityManagerFactory factory;
+ try {
+
+ NamingEnumeration<NameClassPair> persUnits = context.list("java:openejb/PersistenceUnit");
+
+ if (persUnits == null)
+ {
+ throw new CreationException("No PersistenceUnit found in java:openejb/PersistenceUnit!");
+ }
+
+ String shortestMatch = null;
+
+ while (persUnits.hasMore())
+ {
+ NameClassPair puNc = persUnits.next();
+
+ if (puNc.getName().startsWith(unitName))
+ {
+ if (shortestMatch == null || shortestMatch.length() > puNc.getName().length())
+ {
+ shortestMatch = puNc.getName();
+ }
+ }
+
+ }
+
+ if (shortestMatch == null)
+ {
+ throw new CreationException("PersistenceUnit '" + unitName + "' not found");
+ }
+
+ factory = (EntityManagerFactory) context.lookup("java:openejb/PersistenceUnit/" + shortestMatch);
+
+
+ } catch (NamingException e) {
+ throw new CreationException("PersistenceUnit '" + unitName + "' not found", e );
+ }
+ return factory;
+ }
+
+
+
+}
Propchange: openwebbeans/trunk/webbeans-openejb/src/main/java/org/apache/webbeans/ejb/resource/ResourceInjectionProcessor.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/MyEntity.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/MyEntity.java?rev=904688&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/MyEntity.java (added)
+++ openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/MyEntity.java Sat Jan 30 00:11:40 2010
@@ -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.webbeans.ejb;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class MyEntity {
+
+ @Id
+ @GeneratedValue
+ private int id;
+
+ private String name;
+
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId( int id ) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName( String name ) {
+ this.name = name;
+ }
+
+
+}
Propchange: openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/MyEntity.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/OpenEJBIntegrationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/OpenEJBIntegrationTest.java?rev=904688&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/OpenEJBIntegrationTest.java (added)
+++ openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/OpenEJBIntegrationTest.java Sat Jan 30 00:11:40 2010
@@ -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.webbeans.ejb;
+
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.webbeans.ejb.resource.ResourceFactory;
+import org.junit.Test;
+
+
+public class OpenEJBIntegrationTest extends TestCase{
+
+ @Test
+ public void testIntegration() throws Exception
+ {
+ Properties p = new Properties();
+ p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
+
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ p.put("movieDatabaseUnmanaged", "new://Resource?type=DataSource");
+ p.put("movieDatabaseUnmanaged.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabaseUnmanaged.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+ p.put("movieDatabaseUnmanaged.JtaManaged", "false");
+
+ Context context = new InitialContext(p);
+
+ ResourceFactory rf = ResourceFactory.getInstance();
+
+ Class<TestBean> c = TestBean.class;
+ EntityManagerFactory emf = (EntityManagerFactory) rf.getResourceObject(c.getField("emf"));
+ Assert.assertNotNull( emf );
+
+ EntityManager em = (EntityManager) rf.getResourceObject(c.getField("em"));
+ Assert.assertNotNull( em );
+ }
+
+ public class TestBean
+ {
+ public @PersistenceUnit(unitName="TestUnit") EntityManagerFactory emf;
+ public @PersistenceContext(unitName="TestUnit") EntityManager em;
+ }
+}
Propchange: openwebbeans/trunk/webbeans-openejb/src/test/java/org/apache/webbeans/ejb/OpenEJBIntegrationTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openwebbeans/trunk/webbeans-openejb/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-openejb/src/test/resources/META-INF/persistence.xml?rev=904688&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-openejb/src/test/resources/META-INF/persistence.xml (added)
+++ openwebbeans/trunk/webbeans-openejb/src/test/resources/META-INF/persistence.xml Sat Jan 30 00:11:40 2010
@@ -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="TestUnit">
+ <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
+<!-- <jta-data-source>testDatabase</jta-data-source>-->
+ <properties>
+ <property name="openjpa.jdbc.DBDictionary" value="hsql" />
+ <property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver" />
+ <property name="openjpa.ConnectionURL" value="jdbc:hsqldb:mem:test" />
+ <property name="openjpa.ConnectionUserName" value="sa" />
+ <property name="openjpa.ConnectionPassword" value="" />
+
+ </properties>
+
+ </persistence-unit>
+</persistence>
Propchange: openwebbeans/trunk/webbeans-openejb/src/test/resources/META-INF/persistence.xml
------------------------------------------------------------------------------
svn:eol-style = native