Class ClasspathMonitor
- All Implemented Interfaces:
AbstractMonitorMBean
,ClasspathMonitorMBean
,Shutdownable
,Runnable
To get the boot classpath the system property "sun.boot.class.path" is used to get them. This will work of course only for the SunVM.
After an idea from Java Tip 105.
Original this monitor class was part of the PatternTesting project where
startup time and performance was increased with v1.5 dramatically. To speed
up startup time a FutureTask
is used to set up internal structures.
For a better performance doublets are now detected with parallel threads.
Also some expensive operations are cached now.
Because some stuff are done in parallel we no longer use a WeakHashMap but
ConcurrentHashMap
to avoid synchronization problems. It is expected
that the size of cached objects is small enough and we will not run in any
OutOfMemory problems.
- Author:
- oliver
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
You can register the instance as shutdown hook.void
This operation dumps the different MBean attributes to the given directory.String[]
To get the boot classpath the sytem property "sun.boot.class.path" is used to get them.<T> Collection
<Class<? extends T>> getClassList
(String packageName, Class<T> type) Gets the classes of the given type.Dumps the internal fields of the classloader (after an idea from "Java ist auch eine Insel").Returns some information about the classloader.String[]
The classpath is stored in the system property "java.class.path".String[]
Scans the classpath for all classes.getClasspathClassList
(String packageName) Gets the classes of a given package name as collection.The classpath is stored in the system property "java.class.path" as sorted set.Collection
<Class<?>> getConcreteClassList
(String packageName) Deprecated, for removal: This API element is subject to removal in a future version.dead classes may be loadedgetDoublet
(Class<?> clazz, int nr) Gets the doublet.String[]
Gets the doublet classpath.URI[]
Gets the doublet classpath as array of URIsGets the doublet list.String[]
Gets the doublets.getFirstDoublet
(Class<?> clazz) Gets the first doublet.String[]
Incompatible classes are doublets with different byte codes.Looks for each doublet if it has a different doublets.String[]
Gets the incompatible classpath.URI[]
Gets the incompatible classpath as array of URIs.static ClasspathMonitor
Yes, it is a Singleton because it offers only some services.String[]
As MBean a string array could be displayed by the 'jconsole'.Gets the loaded classes as string.Returns a list of classes which were loaded by the classloader.Package[]
This method is used by PatternTesting Samples (in packages.jsp).String[]
Gets the loaded packages.If you want to dump all packages you can use this method.String[]
getManifestEntries
(Class<?> clazz) If a MANIFEST is found for a given class the attributes in this file should be returned as a string array.String[]
getManifestEntries
(String classname) If a MANIFEST is found for a given class the attributes in this file should be returned as a string array.int
getNoClasses
(Class<?> cl) Gets the no classes.int
getNoClasses
(String classname) Gets the no classes.static URL
getResource
(String name) Gets the resource.getResources
(String name) Gets the resources.getSerialVersionUID
(Class<?> clazz) Gets the serial version uid.getSerialVersionUID
(String classname) Gets the serial version uid.String[]
Unused classes are classes which are not loaded but which are found in the classpath.String[]
The unused classpath is the path which is not used by the classloader.The unused classpath is the path which are not used by the classloader.String[]
It might be that the used classpath changes during the calculation of it.Gets the loaded classpath (without the bootclasspath) as sorted set.URI[]
It might be that the used classpath changes during the calculation of it.boolean
Checks if is classloader supported.boolean
Is the given class a doublet, i.e. can it be found more than once in the classpath?boolean
Checks if the given classname is loaded.boolean
Is multi threading enabled?static boolean
If you want to ask JMX if bean is already registered you can ask the MBeanHelper (if you know the object name) or you can ask this method.void
logMe()
Logs the different array to the log output.static void
With this method you can register theClasspathMonitor
with the default name.static void
registerAsMBean
(String name) With this method you can register theClasspathMonitor
with your own name.static void
registerAsMBean
(ObjectName name) With this method you can register theClasspathMonitor
with your own name.static void
If you have registered the instance you can now de-register it.void
setMultiThreadingEnabled
(boolean enabled) Here you can enable or disable the (experimental) multi threading mode to see if it is really faster on a mult-core machine.toString()
We don't want to use the toString() implementation of the Thread class so we use our own one.static void
Unregister ClasspathMonitor as MBean.whichClass
(Class<?> clazz) Where is the given class found in the classpath?whichClass
(String name) Which class.whichClassJar
(Class<?> clazz) Which class jar.whichClassJar
(String classname) Which class jar.whichClassPath
(Class<?> clazz) Returns the jar file or path where the given class was found.Which class path.whichClassPath
(String classname) Returns the jar file or path where the given classname was found.Returns the path of the given clazz.Returns the path of the given resource image.whichResourceJar
(String resource) Which resource jar.static JarFile
whichResourceJar
(URI resource) Which resource jar.whichResourcePath
(String resource) Returns the jar file or path where the given resource was found.Methods inherited from class clazzfish.monitor.AbstractMonitor
copyResource, dump, dump, dumpArray, dumpArray, dumpHeadline, dumpMe, dumpMe, getDumpDir, isMBean, registerMeAsMBean, registerMeAsMBean, registerMeAsMBean, run, toStringArray, unregisterMeAsMBean
Methods inherited from class clazzfish.monitor.util.Shutdowner
addMeAsShutdownHook, isShutdownHook, removeMeAsShutdownHook
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, threadId, yield
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface clazzfish.monitor.AbstractMonitorMBean
dumpMe, dumpMe, getDumpDir
Methods inherited from interface clazzfish.monitor.util.Shutdownable
addMeAsShutdownHook, isShutdownHook, removeMeAsShutdownHook
-
Constructor Details
-
ClasspathMonitor
protected ClasspathMonitor()Instantiates a new classpath monitor.Note: The constructor is protected because it is needed by the benchmark subproject which compares different implementations.
-
-
Method Details
-
getInstance
Yes, it is a Singleton because it offers only some services. So we don't need the object twice.- Returns:
- the only instance
-
registerAsMBean
public static void registerAsMBean()With this method you can register theClasspathMonitor
with the default name.You can only register the
ClasspathMonitor
once only. If you want to register it with another name you have to first unregister it.- See Also:
-
registerAsMBean
With this method you can register theClasspathMonitor
with your own name. This is e.g. useful if you have an application server with several applications.You can register the
ClasspathMonitor
once only. If you want to register it with another name you have to first unregister it.- Parameters:
name
- e.g "my.company.ClasspathMonitor"- See Also:
-
registerAsMBean
With this method you can register theClasspathMonitor
with your own name. This is e.g. useful if you have an application server with several applications.You can only register the
ClasspathMonitor
once only. If you want to register it with another name you have to first unregister it.- Parameters:
name
- the name- Since:
- 1.6
- See Also:
-
unregisterAsMBean
public static void unregisterAsMBean()Unregister ClasspathMonitor as MBean. -
isRegisteredAsMBean
public static boolean isRegisteredAsMBean()If you want to ask JMX if bean is already registered you can ask the MBeanHelper (if you know the object name) or you can ask this method.- Returns:
- true if MBean is already registered.
- Since:
- 1.0
-
whichClass
Which class.- Specified by:
whichClass
in interfaceClasspathMonitorMBean
- Parameters:
name
- the classname- Returns:
- the URI
- See Also:
-
whichClass
-
whichClassPath
-
whichClassPath
-
whichClassPath
-
whichResourcePath
-
whichClassJar
-
whichClassJar
-
whichPath
-
whichPath
-
whichResourceJar
-
whichResourceJar
-
getResources
Gets the resources.- Parameters:
name
- the resource name- Returns:
- the resources
-
getResource
-
getNoClasses
Gets the no classes.- Parameters:
cl
- the class- Returns:
- number of classes
-
getNoClasses
Gets the no classes.- Specified by:
getNoClasses
in interfaceClasspathMonitorMBean
- Parameters:
classname
- the classname- Returns:
- number of classes
- See Also:
-
isDoublet
Is the given class a doublet, i.e. can it be found more than once in the classpath?Note: Because this method is needed by other methods like
getIncompatibleClassList()
the result of it is cached since 1.5. This speeds up this method about the factor 2000 and more:Implementation | Mode | Score | Unit ---------------+-------+----------+------- old (till 1.4) | thrpt | 35514 | ops/s new (cached) | thrpt | 92968300 | ops/s
This results were calculated with the help of JMH and was executed on a Dell Latitude e6520 with i5 processor (i5-2540M CPU @ 2.60GHz).
Since 1.6.2 this functionality is now located in the extracted
DoubletDigger
class.- Specified by:
isDoublet
in interfaceClasspathMonitorMBean
- Parameters:
clazz
- the class to be checked- Returns:
- true if class is found more than once in the classpath
- See Also:
-
getFirstDoublet
Gets the first doublet.- Specified by:
getFirstDoublet
in interfaceClasspathMonitorMBean
- Parameters:
clazz
- the class- Returns:
- the first doublet
- See Also:
-
getDoublet
Gets the doublet.- Specified by:
getDoublet
in interfaceClasspathMonitorMBean
- Parameters:
clazz
- the clazznr
- the nr of the doublet (starting at 0)- Returns:
- the doublet
-
getDoubletList
-
getDoublets
Gets the doublets.- Specified by:
getDoublets
in interfaceClasspathMonitorMBean
- Returns:
- the doublets
- See Also:
-
getDoubletClasspath
Gets the doublet classpath.- Specified by:
getDoubletClasspath
in interfaceClasspathMonitorMBean
- Returns:
- the doublet classpath
-
getDoubletClasspathURIs
Gets the doublet classpath as array of URIs- Returns:
- an array with the doublets
-
getClassLoaderDetails
Dumps the internal fields of the classloader (after an idea from "Java ist auch eine Insel").- Returns:
- the class loader details
-
isClassloaderSupported
public boolean isClassloaderSupported()Checks if is classloader supported.- Returns:
- true if it is a known classloader
-
getClassloaderInfo
Returns some information about the classloader. At least the user should be informed if it is a unknown classloader which is not supported or not tested.- Specified by:
getClassloaderInfo
in interfaceClasspathMonitorMBean
- Returns:
- e.g. "unknown classloader xxx - classpath can be wrong"
-
getLoadedPackages
Gets the loaded packages.- Specified by:
getLoadedPackages
in interfaceClasspathMonitorMBean
- Returns:
- the loaded packages
- See Also:
-
getLoadedPackageArray
This method is used by PatternTesting Samples (in packages.jsp). So it is left here as wrapper around ClasspathDigger.- Returns:
- array of packages
-
getLoadedPackagesAsString
If you want to dump all packages you can use this method. The output will be sorted.- Returns:
- each package in a single line
-
getLoadedClassList
-
getLoadedClasses
As MBean a string array could be displayed by the 'jconsole'. A class array not.- Specified by:
getLoadedClasses
in interfaceClasspathMonitorMBean
- Returns:
- the classes as sorted string array
-
getLoadedClassesAsString
Gets the loaded classes as string.- Returns:
- the loaded classes as string
-
isLoaded
Checks if the given classname is loaded. Why does we use not Class as parameter here? If you would allow a parameter of type "Class" this class will be problably loaded before and this method will return always true!- Specified by:
isLoaded
in interfaceClasspathMonitorMBean
- Parameters:
classname
- name of the class- Returns:
- true if class is loaded
-
getUnusedClasses
Unused classes are classes which are not loaded but which are found in the classpath.- Specified by:
getUnusedClasses
in interfaceClasspathMonitorMBean
- Returns:
- unused classes
- See Also:
-
getClasspathClasses
Scans the classpath for all classes. For performance reason we return no longer a copy but the origin array. Don't do changes for the returned array!- Specified by:
getClasspathClasses
in interfaceClasspathMonitorMBean
- Returns:
- all classes as String array
-
getClasspathClassList
Gets the classes of a given package name as collection.- Parameters:
packageName
- the package name- Returns:
- the classpath class list
-
getClassList
Gets the classes of the given type.- Type Parameters:
T
- the generic type- Parameters:
packageName
- the package nametype
- the type- Returns:
- the classes
-
getConcreteClassList
@Deprecated(since="2.3", forRemoval=true) public Collection<Class<?>> getConcreteClassList(String packageName) Deprecated, for removal: This API element is subject to removal in a future version.dead classes may be loadedGets a list of concrete classes. These are classes which can be instantiated, i.e. they are not abstract and have a default constructor.TODO: Will be removed in 2.5
- Parameters:
packageName
- the package name- Returns:
- the concrete class list
-
getUsedClasspathSet
Gets the loaded classpath (without the bootclasspath) as sorted set. Since v1.5 this method is about 5 times faster than the old implementation by caching the result from previous calls.Here the result from running some benchmarks with the old and new implementation: Profiling with JMH gives the following result:
Implementation | Mode | Score | Error (99.9%) | Unit ---------------+-------+-------------------+---------------+------- new (v1.5) | thrpt | 6.495745459743192 | 0.14376149437 | ops/ms old (v1.4) | thrpt | 1.167003226917474 | 0.01126705718 | ops/ms new (v1.5) | avgt | 0.149637883043804 | 0.00084667052 | ms/op old (v1.4) | avgt | 0.860845377760151 | 0.02615918865 | ms/op
As framework for the benchmarks JMH was used.
- Returns:
- the loaded classpath (excluding the bootclasspath)
-
getUsedClasspath
It might be that the used classpath changes during the calculation of it. So it is only a good approximation how it looks at the moment of the call.- Specified by:
getUsedClasspath
in interfaceClasspathMonitorMBean
- Returns:
- the used classpath
-
getUsedClasspathURIs
It might be that the used classpath changes during the calculation of it. So it is only a good approximation how it looks at the moment of the call.- Returns:
- the used classpath as array of URIs
-
getUnusedClasspath
The unused classpath is the path which is not used by the classloader.- Specified by:
getUnusedClasspath
in interfaceClasspathMonitorMBean
- Returns:
- the unused classpath (sorted)
-
getUnusedClasspathSet
-
getBootClasspath
To get the boot classpath the sytem property "sun.boot.class.path" is used to get them. This will work of course only for the SunVM and only till Java 8.- Specified by:
getBootClasspath
in interfaceClasspathMonitorMBean
- Returns:
- the boot classpath as String array
-
getClasspath
The classpath is stored in the system property "java.class.path". And this is the classpath which will be returned.- Specified by:
getClasspath
in interfaceClasspathMonitorMBean
- Returns:
- the classpath as String array
-
getClasspathSet
-
getSerialVersionUID
Gets the serial version uid.- Specified by:
getSerialVersionUID
in interfaceClasspathMonitorMBean
- Parameters:
classname
- name of the class- Returns:
- the serialVersionUID
- Throws:
IllegalAccessException
- if class can't be accessed
-
getSerialVersionUID
Gets the serial version uid.- Parameters:
clazz
- the clazz- Returns:
- the serial version uid
- Throws:
IllegalAccessException
- the illegal access exception
-
getManifestEntries
If a MANIFEST is found for a given class the attributes in this file should be returned as a string array. E.g. for commons-lang-2.3.jar the string array may looks likeManifest-Version: 1.0 Ant-Version: Apache Ant 1.6.5 Created-By: 1.3.1_09-85 ("Apple Computer, Inc.") Package: org.apache.commons.lang Extension-Name: commons-lang Specification-Version: 2.3 Specification-Vendor: Apache Software Foundation Specification-Title: Commons Lang Implementation-Version: 2.3 Implementation-Vendor: Apache Software Foundation Implementation-Title: Commons Lang Implementation-Vendor-Id: org.apache X-Compile-Source-JDK: 1.3 X-Compile-Target-JDK: 1.1
- Parameters:
clazz
- the clazz- Returns:
- the attribute entries of the Manifest (or emtpy array if no Manifest or no attributes are found)
-
getManifestEntries
If a MANIFEST is found for a given class the attributes in this file should be returned as a string array. E.g. for commons-lang-2.3.jar the string array may look likeManifest-Version: 1.0 Ant-Version: Apache Ant 1.6.5 Created-By: 1.3.1_09-85 ("Apple Computer, Inc.") Package: org.apache.commons.lang Extension-Name: commons-lang Specification-Version: 2.3 Specification-Vendor: Apache Software Foundation Specification-Title: Commons Lang Implementation-Version: 2.3 Implementation-Vendor: Apache Software Foundation Implementation-Title: Commons Lang Implementation-Vendor-Id: org.apache X-Compile-Source-JDK: 1.3 X-Compile-Target-JDK: 1.1
- Specified by:
getManifestEntries
in interfaceClasspathMonitorMBean
- Parameters:
classname
- (must be in the classpath, otherwise you'll get a IllegalArgumentException)- Returns:
- the attribute entries of the Manifest (or emtpy array if no Manifest or no attributes are found)
-
getIncompatibleClassList
Looks for each doublet if it has a different doublets. For the performance reason it looks in the incompatibleClassList from the last time if it is already found. This is done because normally the number of incompatible classes does not decrease.- Returns:
- a sorted list of incompatible classes
-
getIncompatibleClasses
Incompatible classes are doublets with different byte codes.- Specified by:
getIncompatibleClasses
in interfaceClasspathMonitorMBean
- Returns:
- doublet classes with different byte codes.
-
getIncompatibleClasspath
Gets the incompatible classpath.- Specified by:
getIncompatibleClasspath
in interfaceClasspathMonitorMBean
- Returns:
- the classpathes where incompatible classes were found
-
getIncompatibleClasspathURIs
Gets the incompatible classpath as array of URIs.- Returns:
- the classpathes where incompatible classes were found
-
addAsShutdownHook
public static void addAsShutdownHook()You can register the instance as shutdown hook. If the VM is terminated the attributes are logged and dumped to a text file in the tmp directory.- See Also:
-
removeAsShutdownHook
public static void removeAsShutdownHook()If you have registered the instance you can now de-register it.- Since:
- 1.0
- See Also:
-
logMe
public void logMe()Logs the different array to the log output.- Specified by:
logMe
in interfaceAbstractMonitorMBean
- Specified by:
logMe
in classAbstractMonitor
-
dumpMe
This operation dumps the different MBean attributes to the given directory.Note: Till 1.5 the different attributes are dumped to a single file. Now each attribute is dumped into its own file. The given file parameter is the directory for these files.
To dump the different attributes (which are mainly given as array) we use the internal 'dumpArray()' method. This method uses reflection to call the corresponding getter method for that attribute. The reason for this approach is that we can better control exceptions if they happens in one of the getter method. If it happens it is ignored (but logged) and the next attribute is dumped.
- Overrides:
dumpMe
in classAbstractMonitor
- Parameters:
dumpDir
- the directory where the attributes are dumped to.- Throws:
IOException
- Signals that an I/O exception has occurred.- Since:
- 1.5
-
toString
We don't want to use the toString() implementation of the Thread class so we use our own one.- Overrides:
toString
in classAbstractMonitor
- Returns:
- the string
- See Also:
-
isMultiThreadingEnabled
public boolean isMultiThreadingEnabled()Is multi threading enabled? Multi threading is automatically enabled if more than one processor is found. Otherwise you can use set via the system property "multiThreadingEnabled=true" to activate it.- Returns:
- true if multi threading is enabled for this class.
- Since:
- 0.9.7
- See Also:
-
setMultiThreadingEnabled
public void setMultiThreadingEnabled(boolean enabled) Here you can enable or disable the (experimental) multi threading mode to see if it is really faster on a mult-core machine.- Parameters:
enabled
- true if multithreading is enabled- Since:
- 0.9.7
- See Also:
-