Loading MBeans from a URL requires some preparation and additional files. In some cases, we don't have the ability to create files ahead of time or modify them when we need different classes. In these cases, we would just like to load a class from a jar file and create its MBean.
The MLetSrv is not a class loader, we only ask it to load a class from a URL and it instantiates its private class loader for doing this. Even though the internal class loader object used by the m-let loader is a public type, it should not be instantiated to act as a class loader. The m-let loader stores internal information about its private class loaders, and it won't be able to handle one outside of its control.
Instead, use the class loader name that is returned when an MBean is successfully loaded. You can specify this class loader name when creating a class through the MBean server. You will be able to create new MBeans from the same class or from other classes in the associated archive (jar file).
This implies that you must first call the performLoadURL with a known URL and a known m-let file. The m-let loader will create one class loader for each code-base specified in the file, and one for the code-base of the file itself. For example, the class loader name returned with the "Square" MBean name is the one used to load its class from the Square.jar file in the same directory as the HTML file. We can create other instances of that MBean now just through the MBean server, without needing to call the m-let loader.
The following code sample uses the object name references that were declared and assigned in Example 12-3.
// Create a new Square MBean from its class in the Square.jar file String squareClass = "Square"; ObjectName squareName = new ObjectName( "MLetExample:name=" + squareClass + ",id=2"); Object squareParams[] = {new Integer(12)}; String squareSignature[] = {"java.lang.Integer"}; server.createMBean(squareClass, squareName, squareMLetClassLoader, squareParams, squareSignature); // Create a new EquilateralTriangle MBean from its class in the // EquilateralTriangle.jar file String triangleClass = "EquilateralTriangle"; ObjectName triangleName = new ObjectName( "MLetExample:name=" + triangleClass + ",id=3"); Object triangleParams[] = {new Integer(20)}; String triangleSignature[] = {"java.lang.Integer"}; server.createMBean(triangleClass, triangleName, triangleMLetClassLoader, triangleParams, triangleSignature); |
Loading classes directly in this manner implies that the code of the agent or of the MBean must be programmed with the knowledge of the class named in the m-let file, and if needed, the knowledge of other classes in the jar file from which the class was finally loaded.