1 /*
2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.management.loading;
27 
28 // Java import
29 import com.sun.jmx.defaults.JmxProperties;
30 
31 import com.sun.jmx.defaults.ServiceName;
32 
33 import com.sun.jmx.remote.util.EnvHelp;
34 
35 import java.io.Externalizable;
36 import java.io.File;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.ObjectInput;
41 import java.io.ObjectInputStream;
42 import java.io.ObjectOutput;
43 import java.lang.reflect.Constructor;
44 import java.net.MalformedURLException;
45 import java.net.URL;
46 import java.net.URLStreamHandlerFactory;
47 import java.nio.file.Files;
48 import java.security.AccessController;
49 import java.security.PrivilegedAction;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.HashMap;
53 import java.util.HashSet;
54 import java.util.List;
55 import java.lang.System.Logger.Level;
56 import java.util.Map;
57 import java.util.Set;
58 import java.util.StringTokenizer;
59 
60 import javax.management.InstanceAlreadyExistsException;
61 import javax.management.InstanceNotFoundException;
62 import javax.management.MBeanException;
63 import javax.management.MBeanRegistration;
64 import javax.management.MBeanRegistrationException;
65 import javax.management.MBeanServer;
66 import javax.management.NotCompliantMBeanException;
67 import javax.management.ObjectInstance;
68 import javax.management.ObjectName;
69 import javax.management.ReflectionException;
70 
71 import static com.sun.jmx.defaults.JmxProperties.MLET_LIB_DIR;
72 import static com.sun.jmx.defaults.JmxProperties.MLET_LOGGER;
73 import com.sun.jmx.defaults.ServiceName;
74 import javax.management.ServiceNotFoundException;
75 
76 /**
77  * Allows you to instantiate and register one or several MBeans in the MBean server
78  * coming from a remote URL. M-let is a shortcut for management applet. The m-let service does this
79  * by loading an m-let text file, which specifies information on the MBeans to be obtained.
80  * The information on each MBean is specified in a single instance of a tag, called the MLET tag.
81  * The location of the m-let text file is specified by a URL.
82  * <p>
83  * The <CODE>MLET</CODE> tag has the following syntax:
84  * <p>
85  * &lt;<CODE>MLET</CODE><BR>
86  *      <CODE>CODE = </CODE><VAR>class</VAR><CODE> | OBJECT = </CODE><VAR>serfile</VAR><BR>
87  *      <CODE>ARCHIVE = &quot;</CODE><VAR>archiveList</VAR><CODE>&quot;</CODE><BR>
88  *      <CODE>[CODEBASE = </CODE><VAR>codebaseURL</VAR><CODE>]</CODE><BR>
89  *      <CODE>[NAME = </CODE><VAR>mbeanname</VAR><CODE>]</CODE><BR>
90  *      <CODE>[VERSION = </CODE><VAR>version</VAR><CODE>]</CODE><BR>
91  * &gt;<BR>
92  *      <CODE>[</CODE><VAR>arglist</VAR><CODE>]</CODE><BR>
93  * &lt;<CODE>/MLET</CODE>&gt;
94  * <p>
95  * where:
96  * <DL>
97  * <DT><CODE>CODE = </CODE><VAR>class</VAR></DT>
98  * <DD>
99  * This attribute specifies the full Java class name, including package name, of the MBean to be obtained.
100  * The compiled <CODE>.class</CODE> file of the MBean must be contained in one of the <CODE>.jar</CODE> files specified by the <CODE>ARCHIVE</CODE>
101  * attribute. Either <CODE>CODE</CODE> or <CODE>OBJECT</CODE> must be present.
102  * </DD>
103  * <DT><CODE>OBJECT = </CODE><VAR>serfile</VAR></DT>
104  * <DD>
105  * This attribute specifies the <CODE>.ser</CODE> file that contains a serialized representation of the MBean to be obtained.
106  * This file must be contained in one of the <CODE>.jar</CODE> files specified by the <CODE>ARCHIVE</CODE> attribute. If the <CODE>.jar</CODE> file contains a directory hierarchy, specify the path of the file within this hierarchy. Otherwise  a match will not be found. Either <CODE>CODE</CODE> or <CODE>OBJECT</CODE> must be present.
107  * </DD>
108  * <DT><CODE>ARCHIVE = &quot;</CODE><VAR>archiveList</VAR><CODE>&quot;</CODE></DT>
109  * <DD>
110  * This mandatory attribute specifies one or more <CODE>.jar</CODE> files
111  * containing MBeans or other resources used by
112  * the MBean to be obtained. One of the <CODE>.jar</CODE> files must contain the file specified by the <CODE>CODE</CODE> or <CODE>OBJECT</CODE> attribute.
113  * If archivelist contains more than one file:
114  * <UL>
115  * <LI>Each file must be separated from the one that follows it by a comma (,).
116  * <LI><VAR>archivelist</VAR> must be enclosed in double quote marks.
117  * </UL>
118  * All <CODE>.jar</CODE> files in <VAR>archivelist</VAR> must be stored in the directory specified by the code base URL.
119  * </DD>
120  * <DT><CODE>CODEBASE = </CODE><VAR>codebaseURL</VAR></DT>
121  * <DD>
122  * This optional attribute specifies the code base URL of the MBean to be obtained. It identifies the directory that contains
123  * the <CODE>.jar</CODE> files specified by the <CODE>ARCHIVE</CODE> attribute. Specify this attribute only if the <CODE>.jar</CODE> files are not in the same
124  * directory as the m-let text file. If this attribute is not specified, the base URL of the m-let text file is used.
125  * </DD>
126  * <DT><CODE>NAME = </CODE><VAR>mbeanname</VAR></DT>
127  * <DD>
128  * This optional attribute specifies the object name to be assigned to the
129  * MBean instance when the m-let service registers it. If
130  * <VAR>mbeanname</VAR> starts with the colon character (:), the domain
131  * part of the object name is the default domain of the MBean server,
132  * as returned by {@link javax.management.MBeanServer#getDefaultDomain()}.
133  * </DD>
134  * <DT><CODE>VERSION = </CODE><VAR>version</VAR></DT>
135  * <DD>
136  * This optional attribute specifies the version number of the MBean and
137  * associated <CODE>.jar</CODE> files to be obtained. This version number can
138  * be used to specify that the <CODE>.jar</CODE> files are loaded from the
139  * server to update those stored locally in the cache the next time the m-let
140  * text file is loaded. <VAR>version</VAR> must be a series of non-negative
141  * decimal integers each separated by a period from the one that precedes it.
142  * </DD>
143  * <DT><VAR>arglist</VAR></DT>
144  * <DD>
145  * This optional attribute specifies a list of one or more parameters for the
146  * MBean to be instantiated. This list describes the parameters to be passed the MBean's constructor.
147  * Use the following syntax to specify each item in
148  * <VAR>arglist</VAR>:
149  * <DL>
150  * <DT>&lt;<CODE>ARG TYPE=</CODE><VAR>argumentType</VAR> <CODE>VALUE=</CODE><VAR>value</VAR>&gt;</DT>
151  * <DD>where:
152  * <UL>
153  * <LI><VAR>argumentType</VAR> is the type of the argument that will be passed as parameter to the MBean's constructor.</UL>
154  * </DD>
155  * </DL>
156  * <P>The arguments' type in the argument list should be a Java primitive type or a Java basic type
157  * (<CODE>java.lang.Boolean, java.lang.Byte, java.lang.Short, java.lang.Long, java.lang.Integer, java.lang.Float, java.lang.Double, java.lang.String</CODE>).
158  * </DD>
159  * </DL>
160  *
161  * When an m-let text file is loaded, an
162  * instance of each MBean specified in the file is created and registered.
163  * <P>
164  * The m-let service extends the <CODE>java.net.URLClassLoader</CODE> and can be used to load remote classes
165  * and jar files in the VM of the agent.
166  * <p><STRONG>Note - </STRONG> The <CODE>MLet</CODE> class loader uses the {@link javax.management.MBeanServerFactory#getClassLoaderRepository(javax.management.MBeanServer)}
167  * to load classes that could not be found in the loaded jar files.
168  *
169  * @since 1.5
170  */
171 public class MLet extends java.net.URLClassLoader
172      implements MLetMBean, MBeanRegistration, Externalizable {
173 
174      private static final long serialVersionUID = 3636148327800330130L;
175 
176      /*
177      * ------------------------------------------
178      *   PRIVATE VARIABLES
179      * ------------------------------------------
180      */
181 
182      /**
183       * The reference to the MBean server.
184       * @serial
185       */
186      private MBeanServer server = null;
187 
188 
189      /**
190       * The list of instances of the <CODE>MLetContent</CODE>
191       * class found at the specified URL.
192       * @serial
193       */
194      private List<MLetContent> mletList = new ArrayList<MLetContent>();
195 
196 
197      /**
198       * The directory used for storing libraries locally before they are loaded.
199       */
200      private String libraryDirectory;
201 
202 
203      /**
204       * The object name of the MLet Service.
205       * @serial
206       */
207      private ObjectName mletObjectName = null;
208 
209      /**
210       * The URLs of the MLet Service.
211       * @serial
212       */
213      private URL[] myUrls = null;
214 
215      /**
216       * What ClassLoaderRepository, if any, to use if this MLet
217       * doesn't find an asked-for class.
218       */
219      private transient ClassLoaderRepository currentClr;
220 
221      /**
222       * True if we should consult the {@link ClassLoaderRepository}
223       * when we do not find a class ourselves.
224       */
225      private transient boolean delegateToCLR;
226 
227      /**
228       * objects maps from primitive classes to primitive object classes.
229       */
230      private Map<String,Class<?>> primitiveClasses =
231          new HashMap<String,Class<?>>(8) ;
232      {
Boolean.TYPE.toString()233          primitiveClasses.put(Boolean.TYPE.toString(), Boolean.class);
Character.TYPE.toString()234          primitiveClasses.put(Character.TYPE.toString(), Character.class);
Byte.TYPE.toString()235          primitiveClasses.put(Byte.TYPE.toString(), Byte.class);
Short.TYPE.toString()236          primitiveClasses.put(Short.TYPE.toString(), Short.class);
Integer.TYPE.toString()237          primitiveClasses.put(Integer.TYPE.toString(), Integer.class);
Long.TYPE.toString()238          primitiveClasses.put(Long.TYPE.toString(), Long.class);
Float.TYPE.toString()239          primitiveClasses.put(Float.TYPE.toString(), Float.class);
Double.TYPE.toString()240          primitiveClasses.put(Double.TYPE.toString(), Double.class);
241 
242      }
243 
244 
245      /*
246       * ------------------------------------------
247       *  CONSTRUCTORS
248       * ------------------------------------------
249       */
250 
251      /*
252       * The constructor stuff would be considerably simplified if our
253       * parent, URLClassLoader, specified that its one- and
254       * two-argument constructors were equivalent to its
255       * three-argument constructor with trailing null arguments.  But
256       * it doesn't, which prevents us from having all the constructors
257       * but one call this(...args...).
258       */
259 
260      /**
261       * Constructs a new MLet using the default delegation parent ClassLoader.
262       */
MLet()263      public MLet() {
264          this(new URL[0]);
265      }
266 
267      /**
268       * Constructs a new MLet for the specified URLs using the default
269       * delegation parent ClassLoader.  The URLs will be searched in
270       * the order specified for classes and resources after first
271       * searching in the parent class loader.
272       *
273       * @param  urls  The URLs from which to load classes and resources.
274       *
275       */
MLet(URL[] urls)276      public MLet(URL[] urls) {
277          this(urls, true);
278      }
279 
280      /**
281       * Constructs a new MLet for the given URLs. The URLs will be
282       * searched in the order specified for classes and resources
283       * after first searching in the specified parent class loader.
284       * The parent argument will be used as the parent class loader
285       * for delegation.
286       *
287       * @param  urls  The URLs from which to load classes and resources.
288       * @param  parent The parent class loader for delegation.
289       *
290       */
MLet(URL[] urls, ClassLoader parent)291      public MLet(URL[] urls, ClassLoader parent) {
292          this(urls, parent, true);
293      }
294 
295      /**
296       * Constructs a new MLet for the specified URLs, parent class
297       * loader, and URLStreamHandlerFactory. The parent argument will
298       * be used as the parent class loader for delegation. The factory
299       * argument will be used as the stream handler factory to obtain
300       * protocol handlers when creating new URLs.
301       *
302       * @param  urls  The URLs from which to load classes and resources.
303       * @param  parent The parent class loader for delegation.
304       * @param  factory  The URLStreamHandlerFactory to use when creating URLs.
305       *
306       */
MLet(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory)307      public MLet(URL[] urls,
308                  ClassLoader parent,
309                  URLStreamHandlerFactory factory) {
310          this(urls, parent, factory, true);
311      }
312 
313      /**
314       * Constructs a new MLet for the specified URLs using the default
315       * delegation parent ClassLoader.  The URLs will be searched in
316       * the order specified for classes and resources after first
317       * searching in the parent class loader.
318       *
319       * @param  urls  The URLs from which to load classes and resources.
320       * @param  delegateToCLR  True if, when a class is not found in
321       * either the parent ClassLoader or the URLs, the MLet should delegate
322       * to its containing MBeanServer's {@link ClassLoaderRepository}.
323       *
324       */
MLet(URL[] urls, boolean delegateToCLR)325      public MLet(URL[] urls, boolean delegateToCLR) {
326          super(urls);
327          init(delegateToCLR);
328      }
329 
330      /**
331       * Constructs a new MLet for the given URLs. The URLs will be
332       * searched in the order specified for classes and resources
333       * after first searching in the specified parent class loader.
334       * The parent argument will be used as the parent class loader
335       * for delegation.
336       *
337       * @param  urls  The URLs from which to load classes and resources.
338       * @param  parent The parent class loader for delegation.
339       * @param  delegateToCLR  True if, when a class is not found in
340       * either the parent ClassLoader or the URLs, the MLet should delegate
341       * to its containing MBeanServer's {@link ClassLoaderRepository}.
342       *
343       */
MLet(URL[] urls, ClassLoader parent, boolean delegateToCLR)344      public MLet(URL[] urls, ClassLoader parent, boolean delegateToCLR) {
345          super(urls, parent);
346          init(delegateToCLR);
347      }
348 
349      /**
350       * Constructs a new MLet for the specified URLs, parent class
351       * loader, and URLStreamHandlerFactory. The parent argument will
352       * be used as the parent class loader for delegation. The factory
353       * argument will be used as the stream handler factory to obtain
354       * protocol handlers when creating new URLs.
355       *
356       * @param  urls  The URLs from which to load classes and resources.
357       * @param  parent The parent class loader for delegation.
358       * @param  factory  The URLStreamHandlerFactory to use when creating URLs.
359       * @param  delegateToCLR  True if, when a class is not found in
360       * either the parent ClassLoader or the URLs, the MLet should delegate
361       * to its containing MBeanServer's {@link ClassLoaderRepository}.
362       *
363       */
MLet(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory, boolean delegateToCLR)364      public MLet(URL[] urls,
365                  ClassLoader parent,
366                  URLStreamHandlerFactory factory,
367                  boolean delegateToCLR) {
368          super(urls, parent, factory);
369          init(delegateToCLR);
370      }
371 
init(boolean delegateToCLR)372      private void init(boolean delegateToCLR) {
373          this.delegateToCLR = delegateToCLR;
374 
375          try {
376              libraryDirectory = System.getProperty(MLET_LIB_DIR);
377              if (libraryDirectory == null)
378                  libraryDirectory = getTmpDir();
379          } catch (SecurityException e) {
380              // OK : We don't do AccessController.doPrivileged, but we don't
381              //      stop the user from creating an MLet just because they
382              //      can't read the MLET_LIB_DIR or java.io.tmpdir properties
383              //      either.
384          }
385      }
386 
387 
388      /*
389       * ------------------------------------------
390       *  PUBLIC METHODS
391       * ------------------------------------------
392       */
393 
394 
395      /**
396       * Appends the specified URL to the list of URLs to search for classes and
397       * resources.
398       */
addURL(URL url)399      public void addURL(URL url) {
400          if (!Arrays.asList(getURLs()).contains(url))
401              super.addURL(url);
402      }
403 
404      /**
405       * Appends the specified URL to the list of URLs to search for classes and
406       * resources.
407       * @exception ServiceNotFoundException The specified URL is malformed.
408       */
addURL(String url)409      public void addURL(String url) throws ServiceNotFoundException {
410          try {
411              URL ur = new URL(url);
412              if (!Arrays.asList(getURLs()).contains(ur))
413                  super.addURL(ur);
414          } catch (MalformedURLException e) {
415              if (MLET_LOGGER.isLoggable(Level.DEBUG)) {
416                  MLET_LOGGER.log(Level.DEBUG, "Malformed URL: " + url, e);
417              }
418              throw new
419                  ServiceNotFoundException("The specified URL is malformed");
420          }
421      }
422 
423      /** Returns the search path of URLs for loading classes and resources.
424       * This includes the original list of URLs specified to the constructor,
425       * along with any URLs subsequently appended by the addURL() method.
426       */
getURLs()427      public URL[] getURLs() {
428          return super.getURLs();
429      }
430 
431      /**
432       * Loads a text file containing MLET tags that define the MBeans to
433       * be added to the MBean server. The location of the text file is specified by
434       * a URL. The MBeans specified in the MLET file will be instantiated and
435       * registered in the MBean server.
436       *
437       * @param url The URL of the text file to be loaded as URL object.
438       *
439       * @return  A set containing one entry per MLET tag in the m-let text file loaded.
440       * Each entry specifies either the ObjectInstance for the created MBean, or a throwable object
441       * (that is, an error or an exception) if the MBean could not be created.
442       *
443       * @exception ServiceNotFoundException One of the following errors has occurred: The m-let text file does
444       * not contain an MLET tag, the m-let text file is not found, a mandatory
445       * attribute of the MLET tag is not specified, the value of url is
446       * null.
447       * @exception IllegalStateException MLet MBean is not registered with an MBeanServer.
448       */
getMBeansFromURL(URL url)449      public Set<Object> getMBeansFromURL(URL url)
450              throws ServiceNotFoundException  {
451          if (url == null) {
452              throw new ServiceNotFoundException("The specified URL is null");
453          }
454          return getMBeansFromURL(url.toString());
455      }
456 
457      /**
458       * Loads a text file containing MLET tags that define the MBeans to
459       * be added to the MBean server. The location of the text file is specified by
460       * a URL. The MBeans specified in the MLET file will be instantiated and
461       * registered in the MBean server.
462       *
463       * @param url The URL of the text file to be loaded as String object.
464       *
465       * @return A set containing one entry per MLET tag in the m-let
466       * text file loaded.  Each entry specifies either the
467       * ObjectInstance for the created MBean, or a throwable object
468       * (that is, an error or an exception) if the MBean could not be
469       * created.
470       *
471       * @exception ServiceNotFoundException One of the following
472       * errors has occurred: The m-let text file does not contain an
473       * MLET tag, the m-let text file is not found, a mandatory
474       * attribute of the MLET tag is not specified, the url is
475       * malformed.
476       * @exception IllegalStateException MLet MBean is not registered
477       * with an MBeanServer.
478       *
479       */
getMBeansFromURL(String url)480      public Set<Object> getMBeansFromURL(String url)
481              throws ServiceNotFoundException  {
482 
483          if (server == null) {
484              throw new IllegalStateException("This MLet MBean is not " +
485                                              "registered with an MBeanServer.");
486          }
487          // Parse arguments
488          if (url == null) {
489              MLET_LOGGER.log(Level.TRACE, "URL is null");
490              throw new ServiceNotFoundException("The specified URL is null");
491          } else {
492              url = url.replace(File.separatorChar,'/');
493          }
494          if (MLET_LOGGER.isLoggable(Level.TRACE)) {
495              MLET_LOGGER.log(Level.TRACE, "<URL = " + url + ">");
496          }
497 
498          // Parse URL
499          try {
500              MLetParser parser = new MLetParser();
501              mletList = parser.parseURL(url);
502          } catch (Exception e) {
503              final String msg =
504                  "Problems while parsing URL [" + url +
505                  "], got exception [" + e.toString() + "]";
506              MLET_LOGGER.log(Level.TRACE, msg);
507              throw EnvHelp.initCause(new ServiceNotFoundException(msg), e);
508          }
509 
510          // Check that the list of MLets is not empty
511          if (mletList.size() == 0) {
512              final String msg =
513                  "File " + url + " not found or MLET tag not defined in file";
514              MLET_LOGGER.log(Level.TRACE, msg);
515              throw new ServiceNotFoundException(msg);
516          }
517 
518          // Walk through the list of MLets
519          Set<Object> mbeans = new HashSet<Object>();
520          for (MLetContent elmt : mletList) {
521              // Initialize local variables
522              String code = elmt.getCode();
523              if (code != null) {
524                  if (code.endsWith(".class")) {
525                      code = code.substring(0, code.length() - 6);
526                  }
527              }
528              String name = elmt.getName();
529              URL codebase = elmt.getCodeBase();
530              String version = elmt.getVersion();
531              String serName = elmt.getSerializedObject();
532              String jarFiles = elmt.getJarFiles();
533              URL documentBase = elmt.getDocumentBase();
534 
535              // Display debug information
536              if (MLET_LOGGER.isLoggable(Level.TRACE)) {
537                  final StringBuilder strb = new StringBuilder()
538                  .append("\n\tMLET TAG     = ").append(elmt.getAttributes())
539                  .append("\n\tCODEBASE     = ").append(codebase)
540                  .append("\n\tARCHIVE      = ").append(jarFiles)
541                  .append("\n\tCODE         = ").append(code)
542                  .append("\n\tOBJECT       = ").append(serName)
543                  .append("\n\tNAME         = ").append(name)
544                  .append("\n\tVERSION      = ").append(version)
545                  .append("\n\tDOCUMENT URL = ").append(documentBase);
546                  MLET_LOGGER.log(Level.TRACE, strb::toString);
547              }
548 
549              // Load classes from JAR files
550              StringTokenizer st = new StringTokenizer(jarFiles, ",", false);
551              while (st.hasMoreTokens()) {
552                  String tok = st.nextToken().trim();
553                  if (MLET_LOGGER.isLoggable(Level.TRACE)) {
554                      MLET_LOGGER.log(Level.TRACE,
555                              "Load archive for codebase <" + codebase +
556                              ">, file <" + tok + ">");
557                  }
558                  // Check which is the codebase to be used for loading the jar file.
559                  // If we are using the base MLet implementation then it will be
560                  // always the remote server but if the service has been extended in
561                  // order to support caching and versioning then this method will
562                  // return the appropriate one.
563                  //
564                  try {
565                      codebase = check(version, codebase, tok, elmt);
566                  } catch (Exception ex) {
567                      MLET_LOGGER.log(Level.DEBUG,
568                              "Got unexpected exception", ex);
569                      mbeans.add(ex);
570                      continue;
571                  }
572 
573                  // Appends the specified JAR file URL to the list of
574                  // URLs to search for classes and resources.
575                  try {
576                      if (!Arrays.asList(getURLs())
577                          .contains(new URL(codebase.toString() + tok))) {
578                          addURL(codebase + tok);
579                      }
580                  } catch (MalformedURLException me) {
581                      // OK : Ignore jar file if its name provokes the
582                      // URL to be an invalid one.
583                  }
584 
585              }
586              // Instantiate the class specified in the
587              // CODE or OBJECT section of the MLet tag
588              //
589              Object o;
590              ObjectInstance objInst;
591 
592              if (code != null && serName != null) {
593                  final String msg =
594                      "CODE and OBJECT parameters cannot be specified at the " +
595                      "same time in tag MLET";
596                  MLET_LOGGER.log(Level.TRACE, msg);
597                  mbeans.add(new Error(msg));
598                  continue;
599              }
600              if (code == null && serName == null) {
601                  final String msg =
602                      "Either CODE or OBJECT parameter must be specified in " +
603                      "tag MLET";
604                  MLET_LOGGER.log(Level.TRACE, msg);
605                  mbeans.add(new Error(msg));
606                  continue;
607              }
608              try {
609                  if (code != null) {
610 
611                      List<String> signat = elmt.getParameterTypes();
612                      List<String> stringPars = elmt.getParameterValues();
613                      List<Object> objectPars = new ArrayList<Object>();
614 
615                      for (int i = 0; i < signat.size(); i++) {
616                          objectPars.add(constructParameter(stringPars.get(i),
617                                                            signat.get(i)));
618                      }
619                      if (signat.isEmpty()) {
620                          if (name == null) {
621                              objInst = server.createMBean(code, null,
622                                                           mletObjectName);
623                          } else {
624                              objInst = server.createMBean(code,
625                                                           new ObjectName(name),
626                                                           mletObjectName);
627                          }
628                      } else {
629                          Object[] parms = objectPars.toArray();
630                          String[] signature = new String[signat.size()];
631                          signat.toArray(signature);
632                          if (MLET_LOGGER.isLoggable(Level.TRACE)) {
633                              final StringBuilder strb = new StringBuilder();
634                              for (int i = 0; i < signature.length; i++) {
635                                  strb.append("\n\tSignature     = ")
636                                  .append(signature[i])
637                                  .append("\t\nParams        = ")
638                                  .append(parms[i]);
639                              }
640                              MLET_LOGGER.log(Level.TRACE, strb::toString);
641                          }
642                          if (name == null) {
643                              objInst =
644                                  server.createMBean(code, null, mletObjectName,
645                                                     parms, signature);
646                          } else {
647                              objInst =
648                                  server.createMBean(code, new ObjectName(name),
649                                                     mletObjectName, parms,
650                                                     signature);
651                          }
652                      }
653                  } else {
654                      o = loadSerializedObject(codebase,serName);
655                      if (name == null) {
656                          server.registerMBean(o, null);
657                      } else {
658                          server.registerMBean(o,  new ObjectName(name));
659                      }
660                      objInst = new ObjectInstance(name, o.getClass().getName());
661                  }
662              } catch (ReflectionException  ex) {
663                  MLET_LOGGER.log(Level.TRACE, "ReflectionException", ex);
664                  mbeans.add(ex);
665                  continue;
666              } catch (InstanceAlreadyExistsException  ex) {
667                  MLET_LOGGER.log(Level.TRACE,
668                          "InstanceAlreadyExistsException", ex);
669                  mbeans.add(ex);
670                  continue;
671              } catch (MBeanRegistrationException ex) {
672                  MLET_LOGGER.log(Level.TRACE, "MBeanRegistrationException", ex);
673                  mbeans.add(ex);
674                  continue;
675              } catch (MBeanException  ex) {
676                  MLET_LOGGER.log(Level.TRACE, "MBeanException", ex);
677                  mbeans.add(ex);
678                  continue;
679              } catch (NotCompliantMBeanException  ex) {
680                  MLET_LOGGER.log(Level.TRACE,
681                          "NotCompliantMBeanException", ex);
682                  mbeans.add(ex);
683                  continue;
684              } catch (InstanceNotFoundException   ex) {
685                  MLET_LOGGER.log(Level.TRACE,
686                          "InstanceNotFoundException", ex);
687                  mbeans.add(ex);
688                  continue;
689              } catch (IOException ex) {
690                  MLET_LOGGER.log(Level.TRACE, "IOException", ex);
691                  mbeans.add(ex);
692                  continue;
693              } catch (SecurityException ex) {
694                  MLET_LOGGER.log(Level.TRACE, "SecurityException", ex);
695                  mbeans.add(ex);
696                  continue;
697              } catch (Exception ex) {
698                  MLET_LOGGER.log(Level.TRACE, "Exception", ex);
699                  mbeans.add(ex);
700                  continue;
701              } catch (Error ex) {
702                  MLET_LOGGER.log(Level.TRACE, "Error", ex);
703                  mbeans.add(ex);
704                  continue;
705              }
706              mbeans.add(objInst);
707          }
708          return mbeans;
709      }
710 
711      /**
712       * Gets the current directory used by the library loader for
713       * storing native libraries before they are loaded into memory.
714       *
715       * @return The current directory used by the library loader.
716       *
717       * @see #setLibraryDirectory
718       *
719       * @throws UnsupportedOperationException if this implementation
720       * does not support storing native libraries in this way.
721       */
getLibraryDirectory()722      public synchronized String getLibraryDirectory() {
723          return libraryDirectory;
724      }
725 
726      /**
727       * Sets the directory used by the library loader for storing
728       * native libraries before they are loaded into memory.
729       *
730       * @param libdir The directory used by the library loader.
731       *
732       * @see #getLibraryDirectory
733       *
734       * @throws UnsupportedOperationException if this implementation
735       * does not support storing native libraries in this way.
736       */
setLibraryDirectory(String libdir)737      public synchronized void setLibraryDirectory(String libdir) {
738          libraryDirectory = libdir;
739      }
740 
741      /**
742       * Allows the m-let to perform any operations it needs before
743       * being registered in the MBean server. If the ObjectName is
744       * null, the m-let provides a default name for its registration
745       * &lt;defaultDomain&gt;:type=MLet
746       *
747       * @param server The MBean server in which the m-let will be registered.
748       * @param name The object name of the m-let.
749       *
750       * @return  The name of the m-let registered.
751       *
752       * @exception java.lang.Exception This exception should be caught by the MBean server and re-thrown
753       *as an MBeanRegistrationException.
754       */
preRegister(MBeanServer server, ObjectName name)755      public ObjectName preRegister(MBeanServer server, ObjectName name)
756              throws Exception {
757 
758          // Initialize local pointer to the MBean server
759          setMBeanServer(server);
760 
761          // If no name is specified return a default name for the MLet
762          if (name == null) {
763              name = new ObjectName(server.getDefaultDomain() + ":" + ServiceName.MLET);
764          }
765 
766         this.mletObjectName = name;
767         return this.mletObjectName;
768      }
769 
770      /**
771       * Allows the m-let to perform any operations needed after having been
772       * registered in the MBean server or after the registration has failed.
773       *
774       * @param registrationDone Indicates whether or not the m-let has
775       * been successfully registered in the MBean server. The value
776       * false means that either the registration phase has failed.
777       *
778       */
postRegister(Boolean registrationDone)779      public void postRegister (Boolean registrationDone) {
780      }
781 
782      /**
783       * Allows the m-let to perform any operations it needs before being unregistered
784       * by the MBean server.
785       *
786       * @exception java.lang.Exception This exception should be caught
787       * by the MBean server and re-thrown as an
788       * MBeanRegistrationException.
789       */
preDeregister()790      public void preDeregister() throws java.lang.Exception {
791      }
792 
793 
794      /**
795       * Allows the m-let to perform any operations needed after having been
796       * unregistered in the MBean server.
797       */
postDeregister()798      public void postDeregister() {
799      }
800 
801      /**
802       * <p>Save this MLet's contents to the given {@link ObjectOutput}.
803       * Not all implementations support this method.  Those that do not
804       * throw {@link UnsupportedOperationException}.  A subclass may
805       * override this method to support it or to change the format of
806       * the written data.</p>
807       *
808       * <p>The format of the written data is not specified, but if
809       * an implementation supports {@link #writeExternal} it must
810       * also support {@link #readExternal} in such a way that what is
811       * written by the former can be read by the latter.</p>
812       *
813       * @param out The object output stream to write to.
814       *
815       * @exception IOException If a problem occurred while writing.
816       * @exception UnsupportedOperationException If this
817       * implementation does not support this operation.
818       */
writeExternal(ObjectOutput out)819      public void writeExternal(ObjectOutput out)
820              throws IOException, UnsupportedOperationException {
821          throw new UnsupportedOperationException("MLet.writeExternal");
822      }
823 
824      /**
825       * <p>Restore this MLet's contents from the given {@link ObjectInput}.
826       * Not all implementations support this method.  Those that do not
827       * throw {@link UnsupportedOperationException}.  A subclass may
828       * override this method to support it or to change the format of
829       * the read data.</p>
830       *
831       * <p>The format of the read data is not specified, but if an
832       * implementation supports {@link #readExternal} it must also
833       * support {@link #writeExternal} in such a way that what is
834       * written by the latter can be read by the former.</p>
835       *
836       * @param in The object input stream to read from.
837       *
838       * @exception IOException if a problem occurred while reading.
839       * @exception ClassNotFoundException if the class for the object
840       * being restored cannot be found.
841       * @exception UnsupportedOperationException if this
842       * implementation does not support this operation.
843       */
readExternal(ObjectInput in)844      public void readExternal(ObjectInput in)
845              throws IOException, ClassNotFoundException,
846                     UnsupportedOperationException {
847          throw new UnsupportedOperationException("MLet.readExternal");
848      }
849 
850      /*
851       * ------------------------------------------
852       *  PACKAGE METHODS
853       * ------------------------------------------
854       */
855 
856      /**
857       * <p>Load a class, using the given {@link ClassLoaderRepository} if
858       * the class is not found in this MLet's URLs.  The given
859       * ClassLoaderRepository can be null, in which case a {@link
860       * ClassNotFoundException} occurs immediately if the class is not
861       * found in this MLet's URLs.</p>
862       *
863       * @param name The name of the class we want to load.
864       * @param clr  The ClassLoaderRepository that will be used to search
865       *             for the given class, if it is not found in this
866       *             ClassLoader.  May be null.
867       * @return The resulting Class object.
868       * @exception ClassNotFoundException The specified class could not be
869       *            found in this ClassLoader nor in the given
870       *            ClassLoaderRepository.
871       *
872       */
loadClass(String name, ClassLoaderRepository clr)873      public synchronized Class<?> loadClass(String name,
874                                             ClassLoaderRepository clr)
875               throws ClassNotFoundException {
876          final ClassLoaderRepository before=currentClr;
877          try {
878              currentClr = clr;
879              return loadClass(name);
880          } finally {
881              currentClr = before;
882          }
883      }
884 
885      /*
886       * ------------------------------------------
887       *  PROTECTED METHODS
888       * ------------------------------------------
889       */
890 
891      /**
892       * This is the main method for class loaders that is being redefined.
893       *
894       * @param name The name of the class.
895       *
896       * @return The resulting Class object.
897       *
898       * @exception ClassNotFoundException The specified class could not be
899       *            found.
900       */
findClass(String name)901      protected Class<?> findClass(String name) throws ClassNotFoundException {
902          /* currentClr is context sensitive - used to avoid recursion
903             in the class loader repository.  (This is no longer
904             necessary with the new CLR semantics but is kept for
905             compatibility with code that might have called the
906             two-parameter loadClass explicitly.)  */
907          return findClass(name, currentClr);
908      }
909 
910      /**
911       * Called by {@link MLet#findClass(java.lang.String)}.
912       *
913       * @param name The name of the class that we want to load/find.
914       * @param clr The ClassLoaderRepository that can be used to search
915       *            for the given class. This parameter is
916       *            <code>null</code> when called from within the
917       *            {@link javax.management.MBeanServerFactory#getClassLoaderRepository(javax.management.MBeanServer) Class Loader Repository}.
918       * @exception ClassNotFoundException The specified class could not be
919       *            found.
920       *
921       **/
findClass(String name, ClassLoaderRepository clr)922      Class<?> findClass(String name, ClassLoaderRepository clr)
923          throws ClassNotFoundException {
924          Class<?> c = null;
925          MLET_LOGGER.log(Level.TRACE, name);
926          // Try looking in the JAR:
927          try {
928              c = super.findClass(name);
929              if (MLET_LOGGER.isLoggable(Level.TRACE)) {
930                  MLET_LOGGER.log(Level.TRACE,
931                          "Class " + name + " loaded through MLet classloader");
932              }
933          } catch (ClassNotFoundException e) {
934              // Drop through
935              if (MLET_LOGGER.isLoggable(Level.TRACE)) {
936                  MLET_LOGGER.log(Level.TRACE,
937                          "Class " + name + " not found locally");
938              }
939          }
940          // if we are not called from the ClassLoaderRepository
941          if (c == null && delegateToCLR && clr != null) {
942              // Try the classloader repository:
943              //
944              try {
945                  if (MLET_LOGGER.isLoggable(Level.TRACE)) {
946                      MLET_LOGGER.log(Level.TRACE,
947                              "Class " + name + " : looking in CLR");
948                  }
949                  c = clr.loadClassBefore(this, name);
950                  // The loadClassBefore method never returns null.
951                  // If the class is not found we get an exception.
952                  if (MLET_LOGGER.isLoggable(Level.TRACE)) {
953                      MLET_LOGGER.log(Level.TRACE,
954                              "Class " + name + " loaded through " +
955                              "the default classloader repository");
956                  }
957              } catch (ClassNotFoundException e) {
958                  // Drop through
959                  if (MLET_LOGGER.isLoggable(Level.TRACE)) {
960                      MLET_LOGGER.log(Level.TRACE,
961                              "Class " + name + " not found in CLR");
962                  }
963              }
964          }
965          if (c == null) {
966              MLET_LOGGER.log(Level.TRACE, "Failed to load class " + name);
967              throw new ClassNotFoundException(name);
968          }
969          return c;
970      }
971 
972      /**
973       * Returns the absolute path name of a native library. The VM
974       * invokes this method to locate the native libraries that belong
975       * to classes loaded with this class loader. Libraries are
976       * searched in the JAR files using first just the native library
977       * name and if not found the native library name together with
978       * the architecture-specific path name
979       * (<code>OSName/OSArch/OSVersion/lib/nativelibname</code>), i.e.
980       * <p>
981       * the library stat on Solaris SPARC 5.7 will be searched in the JAR file as:
982       * <OL>
983       * <LI>libstat.so
984       * <LI>SunOS/sparc/5.7/lib/libstat.so
985       * </OL>
986       * the library stat on Windows NT 4.0 will be searched in the JAR file as:
987       * <OL>
988       * <LI>stat.dll
989       * <LI>WindowsNT/x86/4.0/lib/stat.dll
990       * </OL>
991       *
992       * <p>More specifically, let <em>{@code nativelibname}</em> be the result of
993       * {@link System#mapLibraryName(java.lang.String)
994       * System.mapLibraryName}{@code (libname)}.  Then the following names are
995       * searched in the JAR files, in order:<br>
996       * <em>{@code nativelibname}</em><br>
997       * {@code <os.name>/<os.arch>/<os.version>/lib/}<em>{@code nativelibname}</em><br>
998       * where {@code <X>} means {@code System.getProperty(X)} with any
999       * spaces in the result removed, and {@code /} stands for the
1000       * file separator character ({@link File#separator}).
1001       * <p>
1002       * If this method returns <code>null</code>, i.e. the libraries
1003       * were not found in any of the JAR files loaded with this class
1004       * loader, the VM searches the library along the path specified
1005       * as the <code>java.library.path</code> property.
1006       *
1007       * @param libname The library name.
1008       *
1009       * @return The absolute path of the native library.
1010       */
findLibrary(String libname)1011      protected String findLibrary(String libname) {
1012 
1013          String abs_path;
1014          String mth = "findLibrary";
1015 
1016          // Get the platform-specific string representing a native library.
1017          //
1018          String nativelibname = System.mapLibraryName(libname);
1019 
1020          //
1021          // See if the native library is accessible as a resource through the JAR file.
1022          //
1023          if (MLET_LOGGER.isLoggable(Level.TRACE)) {
1024              MLET_LOGGER.log(Level.TRACE,
1025                      "Search " + libname + " in all JAR files");
1026          }
1027 
1028          // First try to locate the library in the JAR file using only
1029          // the native library name.  e.g. if user requested a load
1030          // for "foo" on Solaris SPARC 5.7 we try to load "libfoo.so"
1031          // from the JAR file.
1032          //
1033          if (MLET_LOGGER.isLoggable(Level.TRACE)) {
1034              MLET_LOGGER.log(Level.TRACE,
1035                      "loadLibraryAsResource(" + nativelibname + ")");
1036          }
1037          abs_path = loadLibraryAsResource(nativelibname);
1038          if (abs_path != null) {
1039              if (MLET_LOGGER.isLoggable(Level.TRACE)) {
1040                  MLET_LOGGER.log(Level.TRACE,
1041                          nativelibname + " loaded, absolute path = " + abs_path);
1042              }
1043              return abs_path;
1044          }
1045 
1046          // Next try to locate it using the native library name and
1047          // the architecture-specific path name.  e.g. if user
1048          // requested a load for "foo" on Solaris SPARC 5.7 we try to
1049          // load "SunOS/sparc/5.7/lib/libfoo.so" from the JAR file.
1050          //
1051          nativelibname = removeSpace(System.getProperty("os.name")) + File.separator +
1052              removeSpace(System.getProperty("os.arch")) + File.separator +
1053              removeSpace(System.getProperty("os.version")) + File.separator +
1054              "lib" + File.separator + nativelibname;
1055          if (MLET_LOGGER.isLoggable(Level.TRACE)) {
1056              MLET_LOGGER.log(Level.TRACE,
1057                      "loadLibraryAsResource(" + nativelibname + ")");
1058          }
1059 
1060          abs_path = loadLibraryAsResource(nativelibname);
1061          if (abs_path != null) {
1062              if (MLET_LOGGER.isLoggable(Level.TRACE)) {
1063                  MLET_LOGGER.log(Level.TRACE,
1064                          nativelibname + " loaded, absolute path = " + abs_path);
1065              }
1066              return abs_path;
1067          }
1068 
1069          //
1070          // All paths exhausted, library not found in JAR file.
1071          //
1072 
1073          if (MLET_LOGGER.isLoggable(Level.TRACE)) {
1074              MLET_LOGGER.log(Level.TRACE,
1075                      libname + " not found in any JAR file");
1076              MLET_LOGGER.log(Level.TRACE,
1077                      "Search " + libname + " along the path " +
1078                      "specified as the java.library.path property");
1079          }
1080 
1081          // Let the VM search the library along the path
1082          // specified as the java.library.path property.
1083          //
1084          return null;
1085      }
1086 
1087 
1088      /*
1089       * ------------------------------------------
1090       *  PRIVATE METHODS
1091       * ------------------------------------------
1092       */
1093 
getTmpDir()1094      private String getTmpDir() {
1095          // JDK 1.4
1096          String tmpDir = System.getProperty("java.io.tmpdir");
1097          if (tmpDir != null) return tmpDir;
1098 
1099          // JDK < 1.4
1100          File tmpFile = null;
1101          try {
1102              // Try to guess the system temporary dir...
1103              tmpFile = File.createTempFile("tmp","jmx");
1104              if (tmpFile == null) return null;
1105              final File tmpDirFile = tmpFile.getParentFile();
1106              if (tmpDirFile == null) return null;
1107              return tmpDirFile.getAbsolutePath();
1108          } catch (Exception x) {
1109              MLET_LOGGER.log(Level.DEBUG,
1110                      "Failed to determine system temporary dir");
1111              return null;
1112          } finally {
1113              // Cleanup ...
1114              if (tmpFile!=null) {
1115                  try {
1116                      boolean deleted = tmpFile.delete();
1117                      if (!deleted) {
1118                          MLET_LOGGER.log(Level.DEBUG,
1119                                  "Failed to delete temp file");
1120                      }
1121                  } catch (Exception x) {
1122                      MLET_LOGGER.log(Level.DEBUG,
1123                              "Failed to delete temporary file", x);
1124                  }
1125              }
1126         }
1127      }
1128 
1129      /**
1130       * Search the specified native library in any of the JAR files
1131       * loaded by this classloader.  If the library is found copy it
1132       * into the library directory and return the absolute path.  If
1133       * the library is not found then return null.
1134       */
loadLibraryAsResource(String libname)1135      private synchronized String loadLibraryAsResource(String libname) {
1136          try {
1137              InputStream is = getResourceAsStream(
1138                      libname.replace(File.separatorChar,'/'));
1139              if (is != null) {
1140                  try {
1141                      File directory = new File(libraryDirectory);
1142                      directory.mkdirs();
1143                      File file = Files.createTempFile(directory.toPath(),
1144                                                       libname + ".", null)
1145                                       .toFile();
1146                      file.deleteOnExit();
1147                      FileOutputStream fileOutput = new FileOutputStream(file);
1148                      try {
1149                          byte[] buf = new byte[4096];
1150                          int n;
1151                          while ((n = is.read(buf)) >= 0) {
1152                             fileOutput.write(buf, 0, n);
1153                          }
1154                      } finally {
1155                          fileOutput.close();
1156                      }
1157                      if (file.exists()) {
1158                          return file.getAbsolutePath();
1159                      }
1160                  } finally {
1161                      is.close();
1162                  }
1163              }
1164          } catch (Exception e) {
1165              MLET_LOGGER.log(Level.DEBUG,
1166                      "Failed to load library : " + libname, e);
1167              return null;
1168          }
1169          return null;
1170      }
1171 
1172    /**
1173     * Removes any white space from a string. This is used to
1174     * convert strings such as "Windows NT" to "WindowsNT".
1175     */
removeSpace(String s)1176      private static String removeSpace(String s) {
1177          return s.trim().replace(" ", "");
1178      }
1179 
1180      /**
1181       * <p>This method is to be overridden when extending this service to
1182       * support caching and versioning.  It is called from {@link
1183       * #getMBeansFromURL getMBeansFromURL} when the version,
1184       * codebase, and jarfile have been extracted from the MLet file,
1185       * and can be used to verify that it is all right to load the
1186       * given MBean, or to replace the given URL with a different one.</p>
1187       *
1188       * <p>The default implementation of this method returns
1189       * <code>codebase</code> unchanged.</p>
1190       *
1191       * @param version The version number of the <CODE>.jar</CODE>
1192       * file stored locally.
1193       * @param codebase The base URL of the remote <CODE>.jar</CODE> file.
1194       * @param jarfile The name of the <CODE>.jar</CODE> file to be loaded.
1195       * @param mlet The <CODE>MLetContent</CODE> instance that
1196       * represents the <CODE>MLET</CODE> tag.
1197       *
1198       * @return the codebase to use for the loaded MBean.  The returned
1199       * value should not be null.
1200       *
1201       * @exception Exception if the MBean is not to be loaded for some
1202       * reason.  The exception will be added to the set returned by
1203       * {@link #getMBeansFromURL getMBeansFromURL}.
1204       *
1205       */
check(String version, URL codebase, String jarfile, MLetContent mlet)1206      protected URL check(String version, URL codebase, String jarfile,
1207                          MLetContent mlet)
1208              throws Exception {
1209          return codebase;
1210      }
1211 
1212     /**
1213      * Loads the serialized object specified by the <CODE>OBJECT</CODE>
1214      * attribute of the <CODE>MLET</CODE> tag.
1215      *
1216      * @param codebase The <CODE>codebase</CODE>.
1217      * @param filename The name of the file containing the serialized object.
1218      * @return The serialized object.
1219      * @exception ClassNotFoundException The specified serialized
1220      * object could not be found.
1221      * @exception IOException An I/O error occurred while loading
1222      * serialized object.
1223      */
loadSerializedObject(URL codebase, String filename)1224      private Object loadSerializedObject(URL codebase, String filename)
1225              throws IOException, ClassNotFoundException {
1226         if (filename != null) {
1227             filename = filename.replace(File.separatorChar,'/');
1228         }
1229         if (MLET_LOGGER.isLoggable(Level.TRACE)) {
1230             MLET_LOGGER.log(Level.TRACE, codebase.toString() + filename);
1231         }
1232         InputStream is = getResourceAsStream(filename);
1233         if (is != null) {
1234             try {
1235                 ObjectInputStream ois = new MLetObjectInputStream(is, this);
1236                 Object serObject = ois.readObject();
1237                 ois.close();
1238                 return serObject;
1239             } catch (IOException e) {
1240                 if (MLET_LOGGER.isLoggable(Level.DEBUG)) {
1241                     MLET_LOGGER.log(Level.DEBUG,
1242                             "Exception while deserializing " + filename, e);
1243                 }
1244                 throw e;
1245             } catch (ClassNotFoundException e) {
1246                 if (MLET_LOGGER.isLoggable(Level.DEBUG)) {
1247                     MLET_LOGGER.log(Level.DEBUG,
1248                             "Exception while deserializing " + filename, e);
1249                 }
1250                 throw e;
1251             }
1252         } else {
1253             if (MLET_LOGGER.isLoggable(Level.DEBUG)) {
1254                 MLET_LOGGER.log(Level.DEBUG, "Error: File " + filename +
1255                         " containing serialized object not found");
1256             }
1257             throw new Error("File " + filename + " containing serialized object not found");
1258         }
1259      }
1260 
1261      /**
1262       * Converts the String value of the constructor's parameter to
1263       * a basic Java object with the type of the parameter.
1264       */
constructParameter(String param, String type)1265      private  Object constructParameter(String param, String type) {
1266          // check if it is a primitive type
1267          Class<?> c = primitiveClasses.get(type);
1268          if (c != null) {
1269             try {
1270                 Constructor<?> cons =
1271                     c.getConstructor(String.class);
1272                 Object[] oo = new Object[1];
1273                 oo[0]=param;
1274                 return(cons.newInstance(oo));
1275 
1276             } catch (Exception  e) {
1277                 MLET_LOGGER.log(Level.DEBUG, "Got unexpected exception", e);
1278             }
1279         }
1280         if (type.compareTo("java.lang.Boolean") == 0)
1281              return Boolean.valueOf(param);
1282         if (type.compareTo("java.lang.Byte") == 0)
1283              return Byte.valueOf(param);
1284         if (type.compareTo("java.lang.Short") == 0)
1285              return Short.valueOf(param);
1286         if (type.compareTo("java.lang.Long") == 0)
1287              return Long.valueOf(param);
1288         if (type.compareTo("java.lang.Integer") == 0)
1289              return Integer.valueOf(param);
1290         if (type.compareTo("java.lang.Float") == 0)
1291              return Float.valueOf(param);
1292         if (type.compareTo("java.lang.Double") == 0)
1293              return Double.valueOf(param);
1294         if (type.compareTo("java.lang.String") == 0)
1295              return param;
1296 
1297         return param;
1298      }
1299 
setMBeanServer(final MBeanServer server)1300     private synchronized void setMBeanServer(final MBeanServer server) {
1301         this.server = server;
1302         PrivilegedAction<ClassLoaderRepository> act =
1303             new PrivilegedAction<ClassLoaderRepository>() {
1304                 public ClassLoaderRepository run() {
1305                     return server.getClassLoaderRepository();
1306                 }
1307             };
1308         currentClr = AccessController.doPrivileged(act);
1309     }
1310 
1311 }
1312