You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScopedClassPoolRepositoryImpl.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 2006 JBoss Inc. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.scopedpool;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import java.util.WeakHashMap;
  21. import javassist.ClassPool;
  22. import javassist.LoaderClassPath;
  23. /**
  24. * An implementation of <code>ScopedClassPoolRepository</code>.
  25. * It is an singleton.
  26. *
  27. * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
  28. * @version $Revision: 1.3 $
  29. */
  30. public class ScopedClassPoolRepositoryImpl implements ScopedClassPoolRepository {
  31. /** The instance */
  32. private static final ScopedClassPoolRepositoryImpl instance = new ScopedClassPoolRepositoryImpl();
  33. /** Whether to prune */
  34. private boolean prune = true;
  35. /** Whether to prune when added to the classpool's cache */
  36. boolean pruneWhenCached;
  37. /** The registered classloaders */
  38. protected Map registeredCLs = Collections
  39. .synchronizedMap(new WeakHashMap());
  40. /** The default class pool */
  41. protected ClassPool classpool;
  42. /** The factory for creating class pools */
  43. protected ScopedClassPoolFactory factory = new ScopedClassPoolFactoryImpl();
  44. /**
  45. * Get the instance.
  46. *
  47. * @return the instance.
  48. */
  49. public static ScopedClassPoolRepository getInstance() {
  50. return instance;
  51. }
  52. /**
  53. * Singleton.
  54. */
  55. private ScopedClassPoolRepositoryImpl() {
  56. classpool = ClassPool.getDefault();
  57. // FIXME This doesn't look correct
  58. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  59. classpool.insertClassPath(new LoaderClassPath(cl));
  60. }
  61. /**
  62. * Returns the value of the prune attribute.
  63. *
  64. * @return the prune.
  65. */
  66. public boolean isPrune() {
  67. return prune;
  68. }
  69. /**
  70. * Set the prune attribute.
  71. *
  72. * @param prune a new value.
  73. */
  74. public void setPrune(boolean prune) {
  75. this.prune = prune;
  76. }
  77. /**
  78. * Create a scoped classpool.
  79. *
  80. * @param cl the classloader.
  81. * @param src the original classpool.
  82. * @return the classpool
  83. */
  84. public ScopedClassPool createScopedClassPool(ClassLoader cl, ClassPool src) {
  85. return factory.create(cl, src, this);
  86. }
  87. public ClassPool findClassPool(ClassLoader cl) {
  88. if (cl == null)
  89. return registerClassLoader(ClassLoader.getSystemClassLoader());
  90. return registerClassLoader(cl);
  91. }
  92. /**
  93. * Register a classloader.
  94. *
  95. * @param ucl the classloader.
  96. * @return the classpool
  97. */
  98. public ClassPool registerClassLoader(ClassLoader ucl) {
  99. synchronized (registeredCLs) {
  100. // FIXME: Probably want to take this method out later
  101. // so that AOP framework can be independent of JMX
  102. // This is in here so that we can remove a UCL from the ClassPool as
  103. // a
  104. // ClassPool.classpath
  105. if (registeredCLs.containsKey(ucl)) {
  106. return (ClassPool)registeredCLs.get(ucl);
  107. }
  108. ScopedClassPool pool = createScopedClassPool(ucl, classpool);
  109. registeredCLs.put(ucl, pool);
  110. return pool;
  111. }
  112. }
  113. /**
  114. * Get the registered classloaders.
  115. */
  116. public Map getRegisteredCLs() {
  117. clearUnregisteredClassLoaders();
  118. return registeredCLs;
  119. }
  120. /**
  121. * This method will check to see if a register classloader has been
  122. * undeployed (as in JBoss)
  123. */
  124. public void clearUnregisteredClassLoaders() {
  125. ArrayList toUnregister = null;
  126. synchronized (registeredCLs) {
  127. Iterator it = registeredCLs.values().iterator();
  128. while (it.hasNext()) {
  129. ScopedClassPool pool = (ScopedClassPool)it.next();
  130. if (pool.isUnloadedClassLoader()) {
  131. it.remove();
  132. ClassLoader cl = pool.getClassLoader();
  133. if (cl != null) {
  134. if (toUnregister == null) {
  135. toUnregister = new ArrayList();
  136. }
  137. toUnregister.add(cl);
  138. }
  139. }
  140. }
  141. if (toUnregister != null) {
  142. for (int i = 0; i < toUnregister.size(); i++) {
  143. unregisterClassLoader((ClassLoader)toUnregister.get(i));
  144. }
  145. }
  146. }
  147. }
  148. public void unregisterClassLoader(ClassLoader cl) {
  149. synchronized (registeredCLs) {
  150. ScopedClassPool pool = (ScopedClassPool)registeredCLs.remove(cl);
  151. if (pool != null)
  152. pool.close();
  153. }
  154. }
  155. public void insertDelegate(ScopedClassPoolRepository delegate) {
  156. // Noop - this is the end
  157. }
  158. public void setClassPoolFactory(ScopedClassPoolFactory factory) {
  159. this.factory = factory;
  160. }
  161. public ScopedClassPoolFactory getClassPoolFactory() {
  162. return factory;
  163. }
  164. }