[jboss-cvs] JBossAS SVN: r101415 - in projects/jboss-classpool/trunk: scopedpool and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Feb 24 13:27:22 EST 2010


Author: flavia.rainone at jboss.com
Date: 2010-02-24 13:27:22 -0500 (Wed, 24 Feb 2010)
New Revision: 101415

Added:
   projects/jboss-classpool/trunk/scopedpool/
   projects/jboss-classpool/trunk/scopedpool/pom.xml
   projects/jboss-classpool/trunk/scopedpool/src/
   projects/jboss-classpool/trunk/scopedpool/src/main/
   projects/jboss-classpool/trunk/scopedpool/src/main/java/
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPool.java
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactory.java
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactoryImpl.java
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepository.java
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepositoryImpl.java
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/SoftValueHashMap.java
   projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/package.html
Modified:
   projects/jboss-classpool/trunk/pom.xml
Log:
[JBREFLECT-96] Copied Scoped ClassPool classes from Javassist to a new scopedpool module.

Modified: projects/jboss-classpool/trunk/pom.xml
===================================================================
--- projects/jboss-classpool/trunk/pom.xml	2010-02-24 18:03:02 UTC (rev 101414)
+++ projects/jboss-classpool/trunk/pom.xml	2010-02-24 18:27:22 UTC (rev 101415)
@@ -19,6 +19,7 @@
   </scm>
   
   <modules>
+    <module>scopedpool</module>
     <module>classpool</module>
     <module>jbosscl</module>
     <module>ucl</module>

Added: projects/jboss-classpool/trunk/scopedpool/pom.xml
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/pom.xml	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/pom.xml	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,36 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <groupId>org.jboss.classpool</groupId>
+    <artifactId>jboss-classpool-parent</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>jboss-classpool-scoped</artifactId>
+  <packaging>jar</packaging>
+  <name>JBoss ScopedClassPool</name>
+  <url>http://www.jboss.org/jbossreflect</url>
+  
+  <build>
+    <finalName>${artifactId}</finalName>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <version>2.3</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <version>1.3</version>
+      </plugin>
+    </plugins>
+  </build>
+  
+  <!-- Do not add version information here, use ../pom.xml instead -->
+  <dependencies>
+    <dependency>
+      <groupId>javassist</groupId>
+      <artifactId>javassist</artifactId>
+    </dependency>
+  </dependencies>
+</project>

Added: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPool.java
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPool.java	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPool.java	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,283 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.classpool.scoped;
+
+import java.lang.ref.WeakReference;
+import java.security.ProtectionDomain;
+import java.util.Iterator;
+import java.util.Map;
+import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.LoaderClassPath;
+import javassist.NotFoundException;
+
+/**
+ * A scoped class pool.
+ * 
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class ScopedClassPool extends ClassPool {
+   protected ScopedClassPoolRepository repository;
+
+   protected WeakReference<ClassLoader> classLoader;
+
+   protected LoaderClassPath classPath;
+
+   protected SoftValueHashMap softcache = new SoftValueHashMap();
+
+   boolean isBootstrapCl = true;
+
+   static {
+      ClassPool.doPruning = false;
+      ClassPool.releaseUnmodifiedClassFile = false;
+   }
+
+   /**
+    * Creates a new ScopedClassPool.
+    * 
+    * @param cl          the classloader
+    * @param src         the original class pool
+    * @param repository  the repository
+    * @param isTemp      whether this is a temporary pool used to 
+    *                    resolve references
+    */
+   protected ScopedClassPool(ClassLoader cl, ClassPool src, ScopedClassPoolRepository repository, boolean isTemp)
+   {
+      super(src);
+      this.repository = repository;
+      this.classLoader = new WeakReference<ClassLoader>(cl);
+      if (cl != null) {
+         classPath = new LoaderClassPath(cl);
+         this.insertClassPath(classPath);
+      }
+      childFirstLookup = true;
+      if (!isTemp && cl == null)
+      {
+         isBootstrapCl = true;
+      }
+   }
+
+   /**
+    * Gets the class loader
+    * 
+    * @return the class loader
+    */
+   public ClassLoader getClassLoader() {
+      ClassLoader cl = getClassLoader0();
+      if (cl == null && !isBootstrapCl)
+      {
+         throw new IllegalStateException(
+               "ClassLoader has been garbage collected");
+      }
+      return cl;
+   }
+
+   protected ClassLoader getClassLoader0() {
+      return (ClassLoader)classLoader.get();
+   }
+
+   /**
+    * Closes the class pool
+    */
+   public void close() {
+      this.removeClassPath(classPath);
+      classPath.close();
+      classes.clear();
+      softcache.clear();
+   }
+
+   /**
+    * Flushes a class.
+    * 
+    * @param classname the class to flush
+    */
+   public synchronized void flushClass(String classname) {
+      classes.remove(classname);
+      softcache.remove(classname);
+   }
+
+   /**
+    * Softens a class
+    * 
+    * @param clazz the class
+    */
+   public synchronized void soften(CtClass clazz) {
+      if (repository.isPrune())
+         clazz.prune();
+      classes.remove(clazz.getName());
+      softcache.put(clazz.getName(), clazz);
+   }
+
+   /**
+    * Whether the classloader is loader
+    * 
+    * @return false always
+    */
+   public boolean isUnloadedClassLoader() {
+      return false;
+   }
+
+   /**
+    * Get the cached class
+    * 
+    * @param classname  the class name
+    * @return           the class
+    */
+   protected CtClass getCached(String classname) {
+      CtClass clazz = getCachedLocally(classname);
+      if (clazz == null) {
+         boolean isLocal = false;
+
+         ClassLoader dcl = getClassLoader0();
+         if (dcl != null) {
+            final int lastIndex = classname.lastIndexOf('$');
+            String classResourceName = null;
+            if (lastIndex < 0) {
+               classResourceName = classname.replaceAll("[\\.]", "/")
+               + ".class";
+            }
+            else {
+               classResourceName = classname.substring(0, lastIndex)
+               .replaceAll("[\\.]", "/")
+               + classname.substring(lastIndex) + ".class";
+            }
+
+            isLocal = dcl.getResource(classResourceName) != null;
+         }
+
+         if (!isLocal) {
+            Map<?, ScopedClassPool> registeredCLs = repository.getRegisteredCLs();
+            synchronized (registeredCLs) {
+               Iterator<ScopedClassPool> it = registeredCLs.values().iterator();
+               while (it.hasNext()) {
+                  ScopedClassPool pool = it.next();
+                  if (pool.isUnloadedClassLoader()) {
+                     repository.unregisterClassLoader(pool
+                           .getClassLoader());
+                     continue;
+                  }
+
+                  clazz = pool.getCachedLocally(classname);
+                  if (clazz != null) {
+                     return clazz;
+                  }
+               }
+            }
+         }
+      }
+      // *NOTE* NEED TO TEST WHEN SUPERCLASS IS IN ANOTHER UCL!!!!!!
+      return clazz;
+   }
+
+   /**
+    * Caches a class
+    *  
+    * @param classname the class name
+    * @param c         the ctClass
+    * @param dynamic   whether the class is dynamically generated
+    */
+   protected void cacheCtClass(String classname, CtClass c, boolean dynamic) {
+      if (dynamic) {
+         super.cacheCtClass(classname, c, dynamic);
+      }
+      else {
+         if (repository.isPrune())
+            c.prune();
+         softcache.put(classname, c);
+      }
+   }
+
+   /**
+    * Locks a class into the cache
+    * 
+    * @param c the class
+    */
+   public void lockInCache(CtClass c) {
+      super.cacheCtClass(c.getName(), c, false);
+   }
+
+   /**
+    * Returns the class if it is cached in this pool.
+    * 
+    * @param classname  the class name
+    * @return           the cached class
+    */
+   protected CtClass getCachedLocally(String classname) {
+      CtClass cached = (CtClass)classes.get(classname);
+      if (cached != null)
+         return cached;
+      synchronized (softcache) {
+         return (CtClass)softcache.get(classname);
+      }
+   }
+
+   /**
+    * Gets any local copy of the class
+    * 
+    * @param classname the class name
+    * @return          the class
+    * @throws NotFoundException when the class is not found
+    */
+   public synchronized CtClass getLocally(String classname)
+   throws NotFoundException {
+      softcache.remove(classname);
+      CtClass clazz = (CtClass)classes.get(classname);
+      if (clazz == null) {
+         clazz = createCtClass(classname, true);
+         if (clazz == null)
+            throw new NotFoundException(classname);
+         super.cacheCtClass(classname, clazz, false);
+      }
+
+      return clazz;
+   }
+
+   /**
+    * Converts a javassist class to a java class
+    * 
+    * @param ct      the javassist class
+    * @param loader  the loader
+    * @throws CannotCompileException for any error
+    */
+   public Class<?> toClass(CtClass ct, ClassLoader loader, ProtectionDomain domain)
+   throws CannotCompileException {
+      // We need to pass up the classloader stored in this pool, as the
+      // default implementation uses the Thread context cl.
+      // In the case of JSP's in Tomcat,
+      // org.apache.jasper.servlet.JasperLoader will be stored here, while
+      // it's parent
+      // org.jboss.web.tomcat.tc5.WebCtxLoader$ENCLoader is used as the Thread
+      // context cl. The invocation class needs to
+      // be generated in the JasperLoader classloader since in the case of
+      // method invocations, the package name will be
+      // the same as for the class generated from the jsp, i.e.
+      // org.apache.jsp. For classes belonging to org.apache.jsp,
+      // JasperLoader does NOT delegate to its parent if it cannot find them.
+      lockInCache(ct);
+      return super.toClass(ct, getClassLoader0(), domain);
+   }
+}
\ No newline at end of file


Property changes on: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPool.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactory.java
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactory.java	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactory.java	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.classpool.scoped;
+
+import javassist.ClassPool;
+
+/**
+ * A factory interface.
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision$
+ */
+public interface ScopedClassPoolFactory {
+   /**
+    * Makes an instance.
+    */
+   ScopedClassPool create(ClassLoader cl, ClassPool src,
+         ScopedClassPoolRepository repository);
+
+   /**
+    * Makes an instance.
+    */
+   ScopedClassPool create(ClassPool src,
+         ScopedClassPoolRepository repository);
+}


Property changes on: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactory.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactoryImpl.java
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactoryImpl.java	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactoryImpl.java	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.classpool.scoped;
+
+import javassist.ClassPool;
+
+/**
+ * An implementation of factory.
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class ScopedClassPoolFactoryImpl implements ScopedClassPoolFactory {
+   /**
+    * Makes an instance.
+    */
+   public ScopedClassPool create(ClassLoader cl, ClassPool src,
+         ScopedClassPoolRepository repository) {
+      return new ScopedClassPool(cl, src, repository, false);
+   }
+
+   /**
+    * Makes an instance.
+    */
+   public ScopedClassPool create(ClassPool src,
+         ScopedClassPoolRepository repository) {
+      return new ScopedClassPool(null, src, repository, true);
+   }
+}


Property changes on: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolFactoryImpl.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepository.java
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepository.java	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepository.java	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.classpool.scoped;
+
+import java.util.Map;
+
+import javassist.ClassPool;
+
+/**
+ * An interface to <code>ScopedClassPoolRepositoryImpl</code>.
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision$
+ */
+public interface ScopedClassPoolRepository {
+   /**
+    * Records a factory.
+    */
+   void setClassPoolFactory(ScopedClassPoolFactory factory);
+
+   /**
+    * Obtains the recorded factory.
+    */
+   ScopedClassPoolFactory getClassPoolFactory();
+
+   /**
+    * Returns whether or not the class pool is pruned.
+    * 
+    * @return the prune.
+    */
+   boolean isPrune();
+
+   /**
+    * Sets the prune flag.
+    * 
+    * @param prune     a new value.
+    */
+   void setPrune(boolean prune);
+
+   /**
+    * Create a scoped classpool.
+    * 
+    * @param cl    the classloader.
+    * @param src   the original classpool.
+    * @return the classpool.
+    */
+   ScopedClassPool createScopedClassPool(ClassLoader cl, ClassPool src);
+
+   /**
+    * Finds a scoped classpool registered under the passed in classloader.
+    * 
+    * @param cl    the classloader.
+    * @return the classpool.
+    */
+   ClassPool findClassPool(ClassLoader cl);
+
+   /**
+    * Register a classloader.
+    * 
+    * @param ucl   the classloader.
+    * @return the classpool.
+    */
+   ClassPool registerClassLoader(ClassLoader ucl);
+
+   /**
+    * Get the registered classloaders.
+    * 
+    * @return the registered classloaders.
+    */
+   Map<ClassLoader, ScopedClassPool> getRegisteredCLs();
+
+   /**
+    * This method will check to see if a register classloader has been
+    * undeployed (as in JBoss).
+    */
+   void clearUnregisteredClassLoaders();
+
+   /**
+    * Unregisters a classpool and unregisters its classloader.
+    * 
+    * @param cl    the classloader the pool is stored under.
+    */
+   void unregisterClassLoader(ClassLoader cl);
+}


Property changes on: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepository.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepositoryImpl.java
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepositoryImpl.java	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepositoryImpl.java	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,194 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.classpool.scoped;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import javassist.ClassPool;
+import javassist.LoaderClassPath;
+
+/**
+ * An implementation of <code>ScopedClassPoolRepository</code>.
+ * It is an singleton.
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision$
+ */
+public class ScopedClassPoolRepositoryImpl implements ScopedClassPoolRepository {
+   /** The instance */
+   private static final ScopedClassPoolRepositoryImpl instance = new ScopedClassPoolRepositoryImpl();
+
+   /** Whether to prune */
+   private boolean prune = true;
+
+   /** Whether to prune when added to the classpool's cache */
+   boolean pruneWhenCached;
+
+   /** The registered classloaders */
+   protected Map<ClassLoader, ScopedClassPool> registeredCLs = Collections
+   .synchronizedMap(new WeakHashMap<ClassLoader, ScopedClassPool>());
+
+   /** The default class pool */
+   protected ClassPool classpool;
+
+   /** The factory for creating class pools */
+   protected ScopedClassPoolFactory factory = new ScopedClassPoolFactoryImpl();
+
+   /**
+    * Get the instance.
+    * 
+    * @return the instance.
+    */
+   public static ScopedClassPoolRepository getInstance() {
+      return instance;
+   }
+
+   /**
+    * Singleton.
+    */
+   private ScopedClassPoolRepositoryImpl() {
+      classpool = ClassPool.getDefault();
+      // FIXME This doesn't look correct
+      ClassLoader cl = Thread.currentThread().getContextClassLoader();
+      classpool.insertClassPath(new LoaderClassPath(cl));
+   }
+
+   /**
+    * Returns the value of the prune attribute.
+    * 
+    * @return the prune.
+    */
+   public boolean isPrune() {
+      return prune;
+   }
+
+   /**
+    * Set the prune attribute.
+    * 
+    * @param prune     a new value.
+    */
+   public void setPrune(boolean prune) {
+      this.prune = prune;
+   }
+
+   /**
+    * Create a scoped classpool.
+    * 
+    * @param cl    the classloader.
+    * @param src   the original classpool.
+    * @return      the classpool
+    */
+   public ScopedClassPool createScopedClassPool(ClassLoader cl, ClassPool src) {
+      return factory.create(cl, src, this);
+   }
+
+   public ClassPool findClassPool(ClassLoader cl) {
+      if (cl == null)
+         return registerClassLoader(ClassLoader.getSystemClassLoader());
+
+      return registerClassLoader(cl);
+   }
+
+   /**
+    * Register a classloader.
+    * 
+    * @param ucl       the classloader.
+    * @return          the classpool
+    */
+   public ClassPool registerClassLoader(ClassLoader ucl) {
+      synchronized (registeredCLs) {
+         // FIXME: Probably want to take this method out later
+         // so that AOP framework can be independent of JMX
+         // This is in here so that we can remove a UCL from the ClassPool as
+         // a
+         // ClassPool.classpath
+         if (registeredCLs.containsKey(ucl)) {
+            return registeredCLs.get(ucl);
+         }
+         ScopedClassPool pool = createScopedClassPool(ucl, classpool);
+         registeredCLs.put(ucl, pool);
+         return pool;
+      }
+   }
+
+   /**
+    * Get the registered classloaders.
+    */
+   public Map<ClassLoader, ScopedClassPool> getRegisteredCLs() {
+      clearUnregisteredClassLoaders();
+      return registeredCLs;
+   }
+
+   /**
+    * This method will check to see if a register classloader has been
+    * undeployed (as in JBoss)
+    */
+   public void clearUnregisteredClassLoaders() {
+      ArrayList<ClassLoader> toUnregister = null;
+      synchronized (registeredCLs) {
+         Iterator<ScopedClassPool> it = registeredCLs.values().iterator();
+         while (it.hasNext()) {
+            ScopedClassPool pool = it.next();
+            if (pool.isUnloadedClassLoader()) {
+               it.remove();
+               ClassLoader cl = pool.getClassLoader();
+               if (cl != null) {
+                  if (toUnregister == null) {
+                     toUnregister = new ArrayList<ClassLoader>();
+                  }
+                  toUnregister.add(cl);
+               }
+            }
+         }
+         if (toUnregister != null) {
+            for (int i = 0; i < toUnregister.size(); i++) {
+               unregisterClassLoader(toUnregister.get(i));
+            }
+         }
+      }
+   }
+
+   public void unregisterClassLoader(ClassLoader cl) {
+      synchronized (registeredCLs) {
+         ScopedClassPool pool = registeredCLs.remove(cl);
+         if (pool != null)
+            pool.close();
+      }
+   }
+
+   public void insertDelegate(ScopedClassPoolRepository delegate) {
+      // Noop - this is the end
+   }
+
+   public void setClassPoolFactory(ScopedClassPoolFactory factory) {
+      this.factory = factory;
+   }
+
+   public ScopedClassPoolFactory getClassPoolFactory() {
+      return factory;
+   }
+}


Property changes on: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/ScopedClassPoolRepositoryImpl.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/SoftValueHashMap.java
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/SoftValueHashMap.java	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/SoftValueHashMap.java	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,227 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.classpool.scoped;
+
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
+import java.util.AbstractMap;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javassist.CtClass;
+
+/**
+ * This Map will remove entries when the value in the map has been cleaned from
+ * garbage collection
+ * 
+ * @version <tt>$Revision$</tt>
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ */
+public class SoftValueHashMap extends AbstractMap<String, CtClass> implements Map<String, CtClass> {
+   private static class SoftValueRef extends SoftReference<CtClass> {
+      public Object key;
+
+      private SoftValueRef(String key, CtClass val, ReferenceQueue<CtClass> q) {
+         super(val, q);
+         this.key = key;
+      }
+
+      private static SoftValueRef create(String key, CtClass val,
+            ReferenceQueue<CtClass> q) {
+         if (val == null)
+            return null;
+         else
+            return new SoftValueRef(key, val, q);
+      }
+
+   }
+
+   /**
+    * Returns a set of the mappings contained in this hash table.
+    */
+   public Set<Entry<String, CtClass>> entrySet() {
+      // problem with types and entry Set contract
+      /*processQueue();
+      return hash.entrySet();*/
+      throw new UnsupportedOperationException();
+   }
+
+   /* Hash table mapping WeakKeys to values */
+   private Map<String, SoftValueRef> hash;
+
+   /* Reference queue for cleared WeakKeys */
+   private ReferenceQueue<CtClass> queue = new ReferenceQueue<CtClass>();
+
+   /*
+    * Remove all invalidated entries from the map, that is, remove all entries
+    * whose values have been discarded.
+    */
+   private void processQueue() {
+      SoftValueRef ref;
+      while ((ref = (SoftValueRef)queue.poll()) != null) {
+         if (ref == (SoftValueRef)hash.get(ref.key)) {
+            // only remove if it is the *exact* same WeakValueRef
+            //
+            hash.remove(ref.key);
+         }
+      }
+   }
+
+   /* -- Constructors -- */
+
+   /**
+    * Constructs a new, empty <code>WeakHashMap</code> with the given initial
+    * capacity and the given load factor.
+    * 
+    * @param initialCapacity the initial capacity of the <code>WeakHashMap</code>
+    * 
+    * @param loadFactor      the load factor of the <code>WeakHashMap</code>
+    * 
+    * @throws IllegalArgumentException if the initial capacity is less than zero,
+    *                        or if the load factor is nonpositive
+    */
+   public SoftValueHashMap(int initialCapacity, float loadFactor) {
+      hash = new HashMap<String, SoftValueRef>(initialCapacity, loadFactor);
+   }
+
+   /**
+    * Constructs a new, empty <code>WeakHashMap</code> with the given initial
+    * capacity and the default load factor, which is <code>0.75</code>.
+    * 
+    * @param initialCapacity the initial capacity of the <code>WeakHashMap</code>
+    * 
+    * @throws IllegalArgumentException if the initial capacity is less than zero
+    */
+   public SoftValueHashMap(int initialCapacity) {
+      hash = new HashMap<String, SoftValueRef>(initialCapacity);
+   }
+
+   /**
+    * Constructs a new, empty <code>WeakHashMap</code> with the default
+    * initial capacity and the default load factor, which is <code>0.75</code>.
+    */
+   public SoftValueHashMap() {
+      hash = new HashMap<String, SoftValueRef>();
+   }
+
+   /**
+    * Constructs a new <code>WeakHashMap</code> with the same mappings as the
+    * specified <tt>Map</tt>. The <code>WeakHashMap</code> is created with
+    * an initial capacity of twice the number of mappings in the specified map
+    * or 11 (whichever is greater), and a default load factor, which is
+    * <tt>0.75</tt>.
+    * 
+    * @param t     the map whose mappings are to be placed in this map.
+    */
+   public SoftValueHashMap(Map<String, CtClass> t) {
+      this(Math.max(2 * t.size(), 11), 0.75f);
+      putAll(t);
+   }
+
+   /* -- Simple queries -- */
+
+   /**
+    * Returns the number of key-value mappings in this map. <strong>Note:</strong>
+    * <em>In contrast with most implementations of the
+    * <code>Map</code> interface, the time required by this operation is
+    * linear in the size of the map.</em>
+    */
+   public int size() {
+      processQueue();
+      return hash.size();
+   }
+
+   /**
+    * Returns <code>true</code> if this map contains no key-value mappings.
+    */
+   public boolean isEmpty() {
+      processQueue();
+      return hash.isEmpty();
+   }
+
+   /**
+    * Returns <code>true</code> if this map contains a mapping for the
+    * specified key.
+    * 
+    * @param key the key whose presence in this map is to be tested
+    */
+   public boolean containsKey(Object key) {
+      processQueue();
+      return hash.containsKey(key);
+   }
+
+   /* -- Lookup and modification operations -- */
+
+   /**
+    * Returns the value to which this map maps the specified <code>key</code>.
+    * If this map does not contain a value for this key, then return
+    * <code>null</code>.
+    * 
+    * @param key the key whose associated value, if any, is to be returned
+    */
+   public CtClass get(Object key) {
+      processQueue();
+      SoftReference<CtClass> ref = hash.get(key);
+      return ref == null? null: ref.get();
+   }
+
+   /**
+    * Updates this map so that the given <code>key</code> maps to the given
+    * <code>value</code>. If the map previously contained a mapping for
+    * <code>key</code> then that mapping is replaced and the previous value
+    * is returned.
+    * 
+    * @param key   the key that is to be mapped to the given <code>value</code>
+    * @param value the value to which the given <code>key</code> is to be
+    *              mapped
+    * @return      the previous value to which this key was mapped, or
+    *              {@code null} if if there was no mapping for the key
+    */
+   public CtClass put(String key, CtClass value) {
+      processQueue();
+      SoftValueRef rtn = hash.put(key, SoftValueRef.create(key, value, queue));
+      return rtn == null? null: rtn.get();
+   }
+
+   /**
+    * Removes the mapping for the given <code>key</code> from this map, if
+    * present.
+    * 
+    * @param key the key whose mapping is to be removed
+    * @return    the value to which this key was mapped, or {@code null} if
+    *            there is no mapping for the key.
+    */
+   public SoftValueRef remove(String key) {
+      processQueue();
+      return hash.remove(key);
+   }
+
+   /**
+    * Removes all mappings from this map.
+    */
+   public void clear() {
+      processQueue();
+      hash.clear();
+   }
+}
\ No newline at end of file


Property changes on: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/SoftValueHashMap.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/package.html
===================================================================
--- projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/package.html	                        (rev 0)
+++ projects/jboss-classpool/trunk/scopedpool/src/main/java/org/jboss/classpool/scoped/package.html	2010-02-24 18:27:22 UTC (rev 101415)
@@ -0,0 +1,7 @@
+<html>
+<body>
+<p>A custom class pool for several JBoss products.
+It is not part of Javassist.
+</p>
+</body>
+</html>




More information about the jboss-cvs-commits mailing list