You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ti...@apache.org on 2010/01/14 18:28:51 UTC

svn commit: r899314 - in /incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl: BundleDelegatingClassLoader.java PersistenceUnitInfoImpl.java TempBundleDelegatingClassLoader.java

Author: timothyjward
Date: Thu Jan 14 17:28:51 2010
New Revision: 899314

URL: http://svn.apache.org/viewvc?rev=899314&view=rev
Log:
ARIES-79: Create and manage EntityManagerFactory services for persistence units

Added:
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/BundleDelegatingClassLoader.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/TempBundleDelegatingClassLoader.java
Modified:
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/PersistenceUnitInfoImpl.java

Added: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/BundleDelegatingClassLoader.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/BundleDelegatingClassLoader.java?rev=899314&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/BundleDelegatingClassLoader.java (added)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/BundleDelegatingClassLoader.java Thu Jan 14 17:28:51 2010
@@ -0,0 +1,53 @@
+/*
+ * 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 WARRANTIESOR 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.aries.jpa.container.unit.impl;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+
+import org.osgi.framework.Bundle;
+/**
+ * This is a simple ClassLoader that delegates to the Bundle
+ * and is used by the PersistenceUnitInfo
+ */
+public class BundleDelegatingClassLoader extends ClassLoader {
+
+  private final Bundle bundle;
+  
+  public BundleDelegatingClassLoader(Bundle b) {
+    bundle = b;
+  }
+  
+  @Override
+  protected Class<?> findClass(String className) throws ClassNotFoundException {
+    return bundle.loadClass(className);
+  }
+
+  @Override
+  protected URL findResource(String resName) {
+    return bundle.getResource(resName);
+  }
+
+  @Override
+  protected Enumeration<URL> findResources(String resName) throws IOException {
+    return bundle.getResources(resName);
+  }
+
+}

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/PersistenceUnitInfoImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/PersistenceUnitInfoImpl.java?rev=899314&r1=899313&r2=899314&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/PersistenceUnitInfoImpl.java (original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/PersistenceUnitInfoImpl.java Thu Jan 14 17:28:51 2010
@@ -41,10 +41,13 @@
 
   private final ParsedPersistenceUnit unit;
   
+  private final BundleDelegatingClassLoader cl;
+  
   public PersistenceUnitInfoImpl (Bundle b, ParsedPersistenceUnit parsedData)
   {
     bundle = b;
     unit = parsedData;
+    cl = new BundleDelegatingClassLoader(b);
   }
   
   public void addTransformer(ClassTransformer arg0) {
@@ -57,8 +60,7 @@
   }
 
   public ClassLoader getClassLoader() {
-    // TODO Auto-generated method stub
-    return null;
+    return cl;
   }
 
   @SuppressWarnings("unchecked")
@@ -96,8 +98,7 @@
   }
 
   public ClassLoader getNewTempClassLoader() {
-    // TODO Auto-generated method stub
-    return null;
+    return new TempBundleDelegatingClassLoader(bundle);
   }
 
   public DataSource getNonJtaDataSource() {

Added: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/TempBundleDelegatingClassLoader.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/TempBundleDelegatingClassLoader.java?rev=899314&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/TempBundleDelegatingClassLoader.java (added)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/unit/impl/TempBundleDelegatingClassLoader.java Thu Jan 14 17:28:51 2010
@@ -0,0 +1,78 @@
+/*
+ * 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 WARRANTIESOR 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.aries.jpa.container.unit.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Enumeration;
+
+import org.osgi.framework.Bundle;
+
+/**
+ * This is a simple temporary ClassLoader that delegates to the Bundle,
+ * but does not call loadClass. It is used by the PersistenceUnitInfo
+ */
+public class TempBundleDelegatingClassLoader extends ClassLoader {
+
+  private final Bundle bundle;
+  
+  public TempBundleDelegatingClassLoader(Bundle b) {
+    bundle = b;
+  }
+  
+  @Override
+  protected Class<?> findClass(String className) throws ClassNotFoundException {
+    String classResName = className.replace('.', '/').concat(".class");
+    
+    //Don't use loadClass, just load the bytes and call defineClass
+    InputStream is = getResourceAsStream(classResName);
+    
+    if(is == null)
+      throw new ClassNotFoundException(className);
+    
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    
+    byte[] buff = new byte[4096];
+    try {
+      int read = is.read(buff);
+      while(read >0) {
+        baos.write(buff, 0, read);
+        read = is.read(buff);
+      }
+    } catch (IOException ioe) {
+      throw new ClassNotFoundException(className, ioe);
+    }
+    
+    buff = baos.toByteArray();
+    
+    return defineClass(className, buff, 0, buff.length);
+  }
+
+  @Override
+  protected URL findResource(String resName) {
+    return bundle.getResource(resName);
+  }
+
+  @Override
+  protected Enumeration<URL> findResources(String resName) throws IOException {
+    return bundle.getResources(resName);
+  }
+}