[jboss-cvs] JBossAS SVN: r58747 - branches/JBoss_3_2_7_CP/jmx/src/main/org/jboss/mx/loading

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 29 12:40:00 EST 2006


Author: scott.stark at jboss.org
Date: 2006-11-29 12:39:58 -0500 (Wed, 29 Nov 2006)
New Revision: 58747

Added:
   branches/JBoss_3_2_7_CP/jmx/src/main/org/jboss/mx/loading/ClassPreloadService.java
Log:
ASPATCH-122, add the ClassPreloadService to the jboss-jmx.jar

Added: branches/JBoss_3_2_7_CP/jmx/src/main/org/jboss/mx/loading/ClassPreloadService.java
===================================================================
--- branches/JBoss_3_2_7_CP/jmx/src/main/org/jboss/mx/loading/ClassPreloadService.java	2006-11-29 17:38:00 UTC (rev 58746)
+++ branches/JBoss_3_2_7_CP/jmx/src/main/org/jboss/mx/loading/ClassPreloadService.java	2006-11-29 17:39:58 UTC (rev 58747)
@@ -0,0 +1,205 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt 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.mx.loading;
+
+import java.net.URL;
+import java.io.InputStream;
+import java.io.IOException;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipEntry;
+import java.util.Arrays;
+import java.util.ArrayList;
+
+import org.jboss.logging.Logger;
+
+/**
+ A simple service that can be used preload all classes in the classpath
+ of the thread context class loader seen in the start method as the thread
+ context class loader. A simple xmbean fragment for deploying the service
+ is:
+ 
+    <mbean code="org.jboss.mx.loading.ClassPreloadService"
+      name="jboss.mx:service=ClassPreloadService"
+      xmbean-dd="">
+      <xmbean>
+          <attribute access="read-write"
+             getMethod="getIncludePatterns" setMethod="setIncludePatterns">
+             <description>The suffixes for classpath includes</description>
+             <name>IncludePatterns</name>
+             <type>[Ljava.lang.String;</type>
+          </attribute>
+          <attribute access="read-write"
+             getMethod="getExcludePatterns" setMethod="setExcludePatterns">
+             <description>The suffixes for classpath excludes</description>
+             <name>ExcludePatterns</name>
+             <type>[Ljava.lang.String;</type>
+          </attribute>
+         <operation>
+            <name>start</name>
+         </operation>
+      </xmbean>
+       <attribute name="ExcludePatterns">jbossmq.jar</attribute>
+       <attribute name="IncludePatterns"></attribute>
+   </mbean>
+
+ @author Scott.Stark at jboss.org
+ @version $Revision$
+ */
+public class ClassPreloadService
+{
+   static Logger log = Logger.getLogger(ClassPreloadService.class);
+   /** The suffixes for classpath elements to include */
+   private String[] includePattern = {};
+   /** The suffixes for classpath elements to exclude */
+   private String[] excludePattern = {};
+   boolean trace;
+
+   public String[] getIncludePatterns()
+   {
+      return includePattern;
+   }
+   public void setIncludePatterns(String[] includePattern)
+   {
+      this.includePattern = includePattern;
+   }
+
+   public String[] getExcludePatterns()
+   {
+      return excludePattern;
+   }
+   public void setExcludePatterns(String[] excludePattern)
+   {
+      this.excludePattern = excludePattern;
+   }
+
+   public URL[] getRawClassPath()
+   {
+      ClassLoader loader = Thread.currentThread().getContextClassLoader();
+      URL[] fullCP = ClassLoaderUtils.getClassLoaderURLs(loader);
+      return fullCP;
+   }
+
+   /**
+    Load all classes seen the TCL classpath. This entails a scan of every
+    archive in the TCL classpath URLs for .class entries.
+    */
+   public void start()
+   {
+      trace = log.isTraceEnabled();
+      log.debug("Starting, includes="+ Arrays.asList(includePattern)
+         +", excludes="+excludePattern);
+
+      ClassLoader loader = Thread.currentThread().getContextClassLoader();
+      URL[] rawCP = ClassLoaderUtils.getClassLoaderURLs(loader);
+      URL[] cp = filterCP(rawCP, includePattern, excludePattern);
+
+      int loadedClasses = 0;
+      int loadErrors = 0;
+      for(int n = 0; n < cp.length; n ++)
+      {
+         URL u = cp[n];
+         try
+         {
+            InputStream is = u.openStream();
+            ZipInputStream zis = new ZipInputStream(is);
+            ZipEntry ze = zis.getNextEntry();
+            while (ze != null)
+            {
+               String name = ze.getName();
+               if (name.endsWith(".class"))
+               {
+                  int length = name.length();
+                  String cname = name.replace('/', '.').substring(0, length - 6);
+                  try
+                  {
+                     Class c = loader.loadClass(cname);
+                     loadedClasses ++;
+                     if (trace)
+                        log.trace("loaded class: " + cname);
+                  }
+                  catch (Throwable e)
+                  {
+                     loadErrors ++;
+                     if( trace )
+                        log.trace("Failed to load class, "+e.getMessage());
+                  }
+               }
+               ze = zis.getNextEntry();
+            }
+            zis.close();
+         }
+         catch (IOException ignore)
+         {
+            // Not a jar
+         }
+      }
+      log.info("Loaded "+loadedClasses+" classes, "+loadErrors+" CNFEs");
+   }
+
+   public URL[] filterCP(URL[] rawCP, String[] includes, String[] excludes)
+   {
+      if( trace )
+         log.trace("filterCP, rawCP="+Arrays.asList(rawCP));
+      ArrayList tmp = new ArrayList();
+      int count = rawCP != null ? rawCP.length : 0;
+      for(int m = 0; m < count; m ++)
+      {
+         URL pathURL = rawCP[m];
+         String path = pathURL.toString();
+         boolean excluded = false;
+
+         // Excludes take priority over includes
+         for(int n = 0; n < excludes.length; n ++)
+         {
+            String p = excludes[n];
+            if( path.endsWith(p) )
+            {
+               excluded = true;
+               break;
+            }
+         }
+         if( excluded )
+         {
+            log.debug("Excluded: "+pathURL);
+            continue;
+         }
+
+         // If there are no explicit includes, accept the non-excluded paths
+         boolean included = includes.length == 0;
+         for(int n = 0; n < includes.length; n ++)
+         {
+            String p = includes[n];
+            if( path.endsWith(p) )
+               tmp.add(pathURL);
+         }
+         if( included )
+         {
+            log.debug("Included: "+pathURL);
+            tmp.add(pathURL);
+         }
+      }
+      URL[] cp = new URL[tmp.size()];
+      tmp.toArray(cp);
+      return cp;
+   }
+
+}


Property changes on: branches/JBoss_3_2_7_CP/jmx/src/main/org/jboss/mx/loading/ClassPreloadService.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF




More information about the jboss-cvs-commits mailing list