rev 10195 : 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
Reviewed-by: chegar, psandoz
Contributed-by: otaviojava@java.net

   1 /*
   2  * Copyright (c) 1999, 2013, 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.util.logging.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      {
 233          primitiveClasses.put(Boolean.TYPE.toString(), Boolean.class);
 234          primitiveClasses.put(Character.TYPE.toString(), Character.class);
 235          primitiveClasses.put(Byte.TYPE.toString(), Byte.class);
 236          primitiveClasses.put(Short.TYPE.toString(), Short.class);
 237          primitiveClasses.put(Integer.TYPE.toString(), Integer.class);
 238          primitiveClasses.put(Long.TYPE.toString(), Long.class);
 239          primitiveClasses.put(Float.TYPE.toString(), Float.class);
 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       */
 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       */
 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       */
 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       */
 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       */
 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       */
 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       */
 364      public MLet(URL[] urls,
 365                  ClassLoader parent,
 366                  URLStreamHandlerFactory factory,
 367                  boolean delegateToCLR) {
 368          super(urls, parent, factory);
 369          init(delegateToCLR);
 370      }
 371 
 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       */
 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       */
 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.FINEST)) {
 416                  MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
 417                          "addUrl", "Malformed URL: " + url, e);
 418              }
 419              throw new
 420                  ServiceNotFoundException("The specified URL is malformed");
 421          }
 422      }
 423 
 424      /** Returns the search path of URLs for loading classes and resources.
 425       * This includes the original list of URLs specified to the constructor,
 426       * along with any URLs subsequently appended by the addURL() method.
 427       */
 428      public URL[] getURLs() {
 429          return super.getURLs();
 430      }
 431 
 432      /**
 433       * Loads a text file containing MLET tags that define the MBeans to
 434       * be added to the MBean server. The location of the text file is specified by
 435       * a URL. The MBeans specified in the MLET file will be instantiated and
 436       * registered in the MBean server.
 437       *
 438       * @param url The URL of the text file to be loaded as URL object.
 439       *
 440       * @return  A set containing one entry per MLET tag in the m-let text file loaded.
 441       * Each entry specifies either the ObjectInstance for the created MBean, or a throwable object
 442       * (that is, an error or an exception) if the MBean could not be created.
 443       *
 444       * @exception ServiceNotFoundException One of the following errors has occurred: The m-let text file does
 445       * not contain an MLET tag, the m-let text file is not found, a mandatory
 446       * attribute of the MLET tag is not specified, the value of url is
 447       * null.
 448       * @exception IllegalStateException MLet MBean is not registered with an MBeanServer.
 449       */
 450      public Set<Object> getMBeansFromURL(URL url)
 451              throws ServiceNotFoundException  {
 452          if (url == null) {
 453              throw new ServiceNotFoundException("The specified URL is null");
 454          }
 455          return getMBeansFromURL(url.toString());
 456      }
 457 
 458      /**
 459       * Loads a text file containing MLET tags that define the MBeans to
 460       * be added to the MBean server. The location of the text file is specified by
 461       * a URL. The MBeans specified in the MLET file will be instantiated and
 462       * registered in the MBean server.
 463       *
 464       * @param url The URL of the text file to be loaded as String object.
 465       *
 466       * @return A set containing one entry per MLET tag in the m-let
 467       * text file loaded.  Each entry specifies either the
 468       * ObjectInstance for the created MBean, or a throwable object
 469       * (that is, an error or an exception) if the MBean could not be
 470       * created.
 471       *
 472       * @exception ServiceNotFoundException One of the following
 473       * errors has occurred: The m-let text file does not contain an
 474       * MLET tag, the m-let text file is not found, a mandatory
 475       * attribute of the MLET tag is not specified, the url is
 476       * malformed.
 477       * @exception IllegalStateException MLet MBean is not registered
 478       * with an MBeanServer.
 479       *
 480       */
 481      public Set<Object> getMBeansFromURL(String url)
 482              throws ServiceNotFoundException  {
 483 
 484          String mth = "getMBeansFromURL";
 485 
 486          if (server == null) {
 487              throw new IllegalStateException("This MLet MBean is not " +
 488                                              "registered with an MBeanServer.");
 489          }
 490          // Parse arguments
 491          if (url == null) {
 492              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(),
 493                      mth, "URL is null");
 494              throw new ServiceNotFoundException("The specified URL is null");
 495          } else {
 496              url = url.replace(File.separatorChar,'/');
 497          }
 498          if (MLET_LOGGER.isLoggable(Level.FINER)) {
 499              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(),
 500                      mth, "<URL = " + url + ">");
 501          }
 502 
 503          // Parse URL
 504          try {
 505              MLetParser parser = new MLetParser();
 506              mletList = parser.parseURL(url);
 507          } catch (Exception e) {
 508              final String msg =
 509                  "Problems while parsing URL [" + url +
 510                  "], got exception [" + e.toString() + "]";
 511              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
 512              throw EnvHelp.initCause(new ServiceNotFoundException(msg), e);
 513          }
 514 
 515          // Check that the list of MLets is not empty
 516          if (mletList.size() == 0) {
 517              final String msg =
 518                  "File " + url + " not found or MLET tag not defined in file";
 519              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
 520              throw new ServiceNotFoundException(msg);
 521          }
 522 
 523          // Walk through the list of MLets
 524          Set<Object> mbeans = new HashSet<Object>();
 525          for (MLetContent elmt : mletList) {
 526              // Initialize local variables
 527              String code = elmt.getCode();
 528              if (code != null) {
 529                  if (code.endsWith(".class")) {
 530                      code = code.substring(0, code.length() - 6);
 531                  }
 532              }
 533              String name = elmt.getName();
 534              URL codebase = elmt.getCodeBase();
 535              String version = elmt.getVersion();
 536              String serName = elmt.getSerializedObject();
 537              String jarFiles = elmt.getJarFiles();
 538              URL documentBase = elmt.getDocumentBase();
 539 
 540              // Display debug information
 541              if (MLET_LOGGER.isLoggable(Level.FINER)) {
 542                  final StringBuilder strb = new StringBuilder()
 543                  .append("\n\tMLET TAG     = ").append(elmt.getAttributes())
 544                  .append("\n\tCODEBASE     = ").append(codebase)
 545                  .append("\n\tARCHIVE      = ").append(jarFiles)
 546                  .append("\n\tCODE         = ").append(code)
 547                  .append("\n\tOBJECT       = ").append(serName)
 548                  .append("\n\tNAME         = ").append(name)
 549                  .append("\n\tVERSION      = ").append(version)
 550                  .append("\n\tDOCUMENT URL = ").append(documentBase);
 551                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(),
 552                          mth, strb.toString());
 553              }
 554 
 555              // Load classes from JAR files
 556              StringTokenizer st = new StringTokenizer(jarFiles, ",", false);
 557              while (st.hasMoreTokens()) {
 558                  String tok = st.nextToken().trim();
 559                  if (MLET_LOGGER.isLoggable(Level.FINER)) {
 560                      MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 561                              "Load archive for codebase <" + codebase +
 562                              ">, file <" + tok + ">");
 563                  }
 564                  // Check which is the codebase to be used for loading the jar file.
 565                  // If we are using the base MLet implementation then it will be
 566                  // always the remote server but if the service has been extended in
 567                  // order to support caching and versioning then this method will
 568                  // return the appropriate one.
 569                  //
 570                  try {
 571                      codebase = check(version, codebase, tok, elmt);
 572                  } catch (Exception ex) {
 573                      MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
 574                              mth, "Got unexpected exception", ex);
 575                      mbeans.add(ex);
 576                      continue;
 577                  }
 578 
 579                  // Appends the specified JAR file URL to the list of
 580                  // URLs to search for classes and resources.
 581                  try {
 582                      if (!Arrays.asList(getURLs())
 583                          .contains(new URL(codebase.toString() + tok))) {
 584                          addURL(codebase + tok);
 585                      }
 586                  } catch (MalformedURLException me) {
 587                      // OK : Ignore jar file if its name provokes the
 588                      // URL to be an invalid one.
 589                  }
 590 
 591              }
 592              // Instantiate the class specified in the
 593              // CODE or OBJECT section of the MLet tag
 594              //
 595              Object o;
 596              ObjectInstance objInst;
 597 
 598              if (code != null && serName != null) {
 599                  final String msg =
 600                      "CODE and OBJECT parameters cannot be specified at the " +
 601                      "same time in tag MLET";
 602                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
 603                  mbeans.add(new Error(msg));
 604                  continue;
 605              }
 606              if (code == null && serName == null) {
 607                  final String msg =
 608                      "Either CODE or OBJECT parameter must be specified in " +
 609                      "tag MLET";
 610                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
 611                  mbeans.add(new Error(msg));
 612                  continue;
 613              }
 614              try {
 615                  if (code != null) {
 616 
 617                      List<String> signat = elmt.getParameterTypes();
 618                      List<String> stringPars = elmt.getParameterValues();
 619                      List<Object> objectPars = new ArrayList<Object>();
 620 
 621                      for (int i = 0; i < signat.size(); i++) {
 622                          objectPars.add(constructParameter(stringPars.get(i),
 623                                                            signat.get(i)));
 624                      }
 625                      if (signat.isEmpty()) {
 626                          if (name == null) {
 627                              objInst = server.createMBean(code, null,
 628                                                           mletObjectName);
 629                          } else {
 630                              objInst = server.createMBean(code,
 631                                                           new ObjectName(name),
 632                                                           mletObjectName);
 633                          }
 634                      } else {
 635                          Object[] parms = objectPars.toArray();
 636                          String[] signature = new String[signat.size()];
 637                          signat.toArray(signature);
 638                          if (MLET_LOGGER.isLoggable(Level.FINEST)) {
 639                              final StringBuilder strb = new StringBuilder();
 640                              for (int i = 0; i < signature.length; i++) {
 641                                  strb.append("\n\tSignature     = ")
 642                                  .append(signature[i])
 643                                  .append("\t\nParams        = ")
 644                                  .append(parms[i]);
 645                              }
 646                              MLET_LOGGER.logp(Level.FINEST,
 647                                      MLet.class.getName(),
 648                                      mth, strb.toString());
 649                          }
 650                          if (name == null) {
 651                              objInst =
 652                                  server.createMBean(code, null, mletObjectName,
 653                                                     parms, signature);
 654                          } else {
 655                              objInst =
 656                                  server.createMBean(code, new ObjectName(name),
 657                                                     mletObjectName, parms,
 658                                                     signature);
 659                          }
 660                      }
 661                  } else {
 662                      o = loadSerializedObject(codebase,serName);
 663                      if (name == null) {
 664                          server.registerMBean(o, null);
 665                      } else {
 666                          server.registerMBean(o,  new ObjectName(name));
 667                      }
 668                      objInst = new ObjectInstance(name, o.getClass().getName());
 669                  }
 670              } catch (ReflectionException  ex) {
 671                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 672                          "ReflectionException", ex);
 673                  mbeans.add(ex);
 674                  continue;
 675              } catch (InstanceAlreadyExistsException  ex) {
 676                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 677                          "InstanceAlreadyExistsException", ex);
 678                  mbeans.add(ex);
 679                  continue;
 680              } catch (MBeanRegistrationException ex) {
 681                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 682                          "MBeanRegistrationException", ex);
 683                  mbeans.add(ex);
 684                  continue;
 685              } catch (MBeanException  ex) {
 686                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 687                          "MBeanException", ex);
 688                  mbeans.add(ex);
 689                  continue;
 690              } catch (NotCompliantMBeanException  ex) {
 691                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 692                          "NotCompliantMBeanException", ex);
 693                  mbeans.add(ex);
 694                  continue;
 695              } catch (InstanceNotFoundException   ex) {
 696                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 697                          "InstanceNotFoundException", ex);
 698                  mbeans.add(ex);
 699                  continue;
 700              } catch (IOException ex) {
 701                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 702                          "IOException", ex);
 703                  mbeans.add(ex);
 704                  continue;
 705              } catch (SecurityException ex) {
 706                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 707                          "SecurityException", ex);
 708                  mbeans.add(ex);
 709                  continue;
 710              } catch (Exception ex) {
 711                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 712                          "Exception", ex);
 713                  mbeans.add(ex);
 714                  continue;
 715              } catch (Error ex) {
 716                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
 717                          "Error", ex);
 718                  mbeans.add(ex);
 719                  continue;
 720              }
 721              mbeans.add(objInst);
 722          }
 723          return mbeans;
 724      }
 725 
 726      /**
 727       * Gets the current directory used by the library loader for
 728       * storing native libraries before they are loaded into memory.
 729       *
 730       * @return The current directory used by the library loader.
 731       *
 732       * @see #setLibraryDirectory
 733       *
 734       * @throws UnsupportedOperationException if this implementation
 735       * does not support storing native libraries in this way.
 736       */
 737      public synchronized String getLibraryDirectory() {
 738          return libraryDirectory;
 739      }
 740 
 741      /**
 742       * Sets the directory used by the library loader for storing
 743       * native libraries before they are loaded into memory.
 744       *
 745       * @param libdir The directory used by the library loader.
 746       *
 747       * @see #getLibraryDirectory
 748       *
 749       * @throws UnsupportedOperationException if this implementation
 750       * does not support storing native libraries in this way.
 751       */
 752      public synchronized void setLibraryDirectory(String libdir) {
 753          libraryDirectory = libdir;
 754      }
 755 
 756      /**
 757       * Allows the m-let to perform any operations it needs before
 758       * being registered in the MBean server. If the ObjectName is
 759       * null, the m-let provides a default name for its registration
 760       * &lt;defaultDomain&gt;:type=MLet
 761       *
 762       * @param server The MBean server in which the m-let will be registered.
 763       * @param name The object name of the m-let.
 764       *
 765       * @return  The name of the m-let registered.
 766       *
 767       * @exception java.lang.Exception This exception should be caught by the MBean server and re-thrown
 768       *as an MBeanRegistrationException.
 769       */
 770      public ObjectName preRegister(MBeanServer server, ObjectName name)
 771              throws Exception {
 772 
 773          // Initialize local pointer to the MBean server
 774          setMBeanServer(server);
 775 
 776          // If no name is specified return a default name for the MLet
 777          if (name == null) {
 778              name = new ObjectName(server.getDefaultDomain() + ":" + ServiceName.MLET);
 779          }
 780 
 781         this.mletObjectName = name;
 782         return this.mletObjectName;
 783      }
 784 
 785      /**
 786       * Allows the m-let to perform any operations needed after having been
 787       * registered in the MBean server or after the registration has failed.
 788       *
 789       * @param registrationDone Indicates whether or not the m-let has
 790       * been successfully registered in the MBean server. The value
 791       * false means that either the registration phase has failed.
 792       *
 793       */
 794      public void postRegister (Boolean registrationDone) {
 795      }
 796 
 797      /**
 798       * Allows the m-let to perform any operations it needs before being unregistered
 799       * by the MBean server.
 800       *
 801       * @exception java.lang.Exception This exception should be caught
 802       * by the MBean server and re-thrown as an
 803       * MBeanRegistrationException.
 804       */
 805      public void preDeregister() throws java.lang.Exception {
 806      }
 807 
 808 
 809      /**
 810       * Allows the m-let to perform any operations needed after having been
 811       * unregistered in the MBean server.
 812       */
 813      public void postDeregister() {
 814      }
 815 
 816      /**
 817       * <p>Save this MLet's contents to the given {@link ObjectOutput}.
 818       * Not all implementations support this method.  Those that do not
 819       * throw {@link UnsupportedOperationException}.  A subclass may
 820       * override this method to support it or to change the format of
 821       * the written data.</p>
 822       *
 823       * <p>The format of the written data is not specified, but if
 824       * an implementation supports {@link #writeExternal} it must
 825       * also support {@link #readExternal} in such a way that what is
 826       * written by the former can be read by the latter.</p>
 827       *
 828       * @param out The object output stream to write to.
 829       *
 830       * @exception IOException If a problem occurred while writing.
 831       * @exception UnsupportedOperationException If this
 832       * implementation does not support this operation.
 833       */
 834      public void writeExternal(ObjectOutput out)
 835              throws IOException, UnsupportedOperationException {
 836          throw new UnsupportedOperationException("MLet.writeExternal");
 837      }
 838 
 839      /**
 840       * <p>Restore this MLet's contents from the given {@link ObjectInput}.
 841       * Not all implementations support this method.  Those that do not
 842       * throw {@link UnsupportedOperationException}.  A subclass may
 843       * override this method to support it or to change the format of
 844       * the read data.</p>
 845       *
 846       * <p>The format of the read data is not specified, but if an
 847       * implementation supports {@link #readExternal} it must also
 848       * support {@link #writeExternal} in such a way that what is
 849       * written by the latter can be read by the former.</p>
 850       *
 851       * @param in The object input stream to read from.
 852       *
 853       * @exception IOException if a problem occurred while reading.
 854       * @exception ClassNotFoundException if the class for the object
 855       * being restored cannot be found.
 856       * @exception UnsupportedOperationException if this
 857       * implementation does not support this operation.
 858       */
 859      public void readExternal(ObjectInput in)
 860              throws IOException, ClassNotFoundException,
 861                     UnsupportedOperationException {
 862          throw new UnsupportedOperationException("MLet.readExternal");
 863      }
 864 
 865      /*
 866       * ------------------------------------------
 867       *  PACKAGE METHODS
 868       * ------------------------------------------
 869       */
 870 
 871      /**
 872       * <p>Load a class, using the given {@link ClassLoaderRepository} if
 873       * the class is not found in this MLet's URLs.  The given
 874       * ClassLoaderRepository can be null, in which case a {@link
 875       * ClassNotFoundException} occurs immediately if the class is not
 876       * found in this MLet's URLs.</p>
 877       *
 878       * @param name The name of the class we want to load.
 879       * @param clr  The ClassLoaderRepository that will be used to search
 880       *             for the given class, if it is not found in this
 881       *             ClassLoader.  May be null.
 882       * @return The resulting Class object.
 883       * @exception ClassNotFoundException The specified class could not be
 884       *            found in this ClassLoader nor in the given
 885       *            ClassLoaderRepository.
 886       *
 887       */
 888      public synchronized Class<?> loadClass(String name,
 889                                             ClassLoaderRepository clr)
 890               throws ClassNotFoundException {
 891          final ClassLoaderRepository before=currentClr;
 892          try {
 893              currentClr = clr;
 894              return loadClass(name);
 895          } finally {
 896              currentClr = before;
 897          }
 898      }
 899 
 900      /*
 901       * ------------------------------------------
 902       *  PROTECTED METHODS
 903       * ------------------------------------------
 904       */
 905 
 906      /**
 907       * This is the main method for class loaders that is being redefined.
 908       *
 909       * @param name The name of the class.
 910       *
 911       * @return The resulting Class object.
 912       *
 913       * @exception ClassNotFoundException The specified class could not be
 914       *            found.
 915       */
 916      protected Class<?> findClass(String name) throws ClassNotFoundException {
 917          /* currentClr is context sensitive - used to avoid recursion
 918             in the class loader repository.  (This is no longer
 919             necessary with the new CLR semantics but is kept for
 920             compatibility with code that might have called the
 921             two-parameter loadClass explicitly.)  */
 922          return findClass(name, currentClr);
 923      }
 924 
 925      /**
 926       * Called by {@link MLet#findClass(java.lang.String)}.
 927       *
 928       * @param name The name of the class that we want to load/find.
 929       * @param clr The ClassLoaderRepository that can be used to search
 930       *            for the given class. This parameter is
 931       *            <code>null</code> when called from within the
 932       *            {@link javax.management.MBeanServerFactory#getClassLoaderRepository(javax.management.MBeanServer) Class Loader Repository}.
 933       * @exception ClassNotFoundException The specified class could not be
 934       *            found.
 935       *
 936       **/
 937      Class<?> findClass(String name, ClassLoaderRepository clr)
 938          throws ClassNotFoundException {
 939          Class<?> c = null;
 940          MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), "findClass", name);
 941          // Try looking in the JAR:
 942          try {
 943              c = super.findClass(name);
 944              if (MLET_LOGGER.isLoggable(Level.FINER)) {
 945                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(),
 946                          "findClass",
 947                          "Class " + name + " loaded through MLet classloader");
 948              }
 949          } catch (ClassNotFoundException e) {
 950              // Drop through
 951              if (MLET_LOGGER.isLoggable(Level.FINEST)) {
 952                  MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
 953                          "findClass",
 954                          "Class " + name + " not found locally");
 955              }
 956          }
 957          // if we are not called from the ClassLoaderRepository
 958          if (c == null && delegateToCLR && clr != null) {
 959              // Try the classloader repository:
 960              //
 961              try {
 962                  if (MLET_LOGGER.isLoggable(Level.FINEST)) {
 963                      MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
 964                              "findClass",
 965                              "Class " + name + " : looking in CLR");
 966                  }
 967                  c = clr.loadClassBefore(this, name);
 968                  // The loadClassBefore method never returns null.
 969                  // If the class is not found we get an exception.
 970                  if (MLET_LOGGER.isLoggable(Level.FINER)) {
 971                      MLET_LOGGER.logp(Level.FINER, MLet.class.getName(),
 972                              "findClass",
 973                              "Class " + name + " loaded through " +
 974                              "the default classloader repository");
 975                  }
 976              } catch (ClassNotFoundException e) {
 977                  // Drop through
 978                  if (MLET_LOGGER.isLoggable(Level.FINEST)) {
 979                      MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
 980                              "findClass",
 981                              "Class " + name + " not found in CLR");
 982                  }
 983              }
 984          }
 985          if (c == null) {
 986              MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
 987                      "findClass", "Failed to load class " + name);
 988              throw new ClassNotFoundException(name);
 989          }
 990          return c;
 991      }
 992 
 993      /**
 994       * Returns the absolute path name of a native library. The VM
 995       * invokes this method to locate the native libraries that belong
 996       * to classes loaded with this class loader. Libraries are
 997       * searched in the JAR files using first just the native library
 998       * name and if not found the native library name together with
 999       * the architecture-specific path name
1000       * (<code>OSName/OSArch/OSVersion/lib/nativelibname</code>), i.e.
1001       * <p>
1002       * the library stat on Solaris SPARC 5.7 will be searched in the JAR file as:
1003       * <OL>
1004       * <LI>libstat.so
1005       * <LI>SunOS/sparc/5.7/lib/libstat.so
1006       * </OL>
1007       * the library stat on Windows NT 4.0 will be searched in the JAR file as:
1008       * <OL>
1009       * <LI>stat.dll
1010       * <LI>WindowsNT/x86/4.0/lib/stat.dll
1011       * </OL>
1012       *
1013       * <p>More specifically, let <em>{@code nativelibname}</em> be the result of
1014       * {@link System#mapLibraryName(java.lang.String)
1015       * System.mapLibraryName}{@code (libname)}.  Then the following names are
1016       * searched in the JAR files, in order:<br>
1017       * <em>{@code nativelibname}</em><br>
1018       * {@code <os.name>/<os.arch>/<os.version>/lib/}<em>{@code nativelibname}</em><br>
1019       * where {@code <X>} means {@code System.getProperty(X)} with any
1020       * spaces in the result removed, and {@code /} stands for the
1021       * file separator character ({@link File#separator}).
1022       * <p>
1023       * If this method returns <code>null</code>, i.e. the libraries
1024       * were not found in any of the JAR files loaded with this class
1025       * loader, the VM searches the library along the path specified
1026       * as the <code>java.library.path</code> property.
1027       *
1028       * @param libname The library name.
1029       *
1030       * @return The absolute path of the native library.
1031       */
1032      protected String findLibrary(String libname) {
1033 
1034          String abs_path;
1035          String mth = "findLibrary";
1036 
1037          // Get the platform-specific string representing a native library.
1038          //
1039          String nativelibname = System.mapLibraryName(libname);
1040 
1041          //
1042          // See if the native library is accessible as a resource through the JAR file.
1043          //
1044          if (MLET_LOGGER.isLoggable(Level.FINER)) {
1045              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
1046                      "Search " + libname + " in all JAR files");
1047          }
1048 
1049          // First try to locate the library in the JAR file using only
1050          // the native library name.  e.g. if user requested a load
1051          // for "foo" on Solaris SPARC 5.7 we try to load "libfoo.so"
1052          // from the JAR file.
1053          //
1054          if (MLET_LOGGER.isLoggable(Level.FINER)) {
1055              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
1056                      "loadLibraryAsResource(" + nativelibname + ")");
1057          }
1058          abs_path = loadLibraryAsResource(nativelibname);
1059          if (abs_path != null) {
1060              if (MLET_LOGGER.isLoggable(Level.FINER)) {
1061                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
1062                          nativelibname + " loaded, absolute path = " + abs_path);
1063              }
1064              return abs_path;
1065          }
1066 
1067          // Next try to locate it using the native library name and
1068          // the architecture-specific path name.  e.g. if user
1069          // requested a load for "foo" on Solaris SPARC 5.7 we try to
1070          // load "SunOS/sparc/5.7/lib/libfoo.so" from the JAR file.
1071          //
1072          nativelibname = removeSpace(System.getProperty("os.name")) + File.separator +
1073              removeSpace(System.getProperty("os.arch")) + File.separator +
1074              removeSpace(System.getProperty("os.version")) + File.separator +
1075              "lib" + File.separator + nativelibname;
1076          if (MLET_LOGGER.isLoggable(Level.FINER)) {
1077              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
1078                      "loadLibraryAsResource(" + nativelibname + ")");
1079          }
1080 
1081          abs_path = loadLibraryAsResource(nativelibname);
1082          if (abs_path != null) {
1083              if (MLET_LOGGER.isLoggable(Level.FINER)) {
1084                  MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
1085                          nativelibname + " loaded, absolute path = " + abs_path);
1086              }
1087              return abs_path;
1088          }
1089 
1090          //
1091          // All paths exhausted, library not found in JAR file.
1092          //
1093 
1094          if (MLET_LOGGER.isLoggable(Level.FINER)) {
1095              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
1096                      libname + " not found in any JAR file");
1097              MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth,
1098                      "Search " + libname + " along the path " +
1099                      "specified as the java.library.path property");
1100          }
1101 
1102          // Let the VM search the library along the path
1103          // specified as the java.library.path property.
1104          //
1105          return null;
1106      }
1107 
1108 
1109      /*
1110       * ------------------------------------------
1111       *  PRIVATE METHODS
1112       * ------------------------------------------
1113       */
1114 
1115      private String getTmpDir() {
1116          // JDK 1.4
1117          String tmpDir = System.getProperty("java.io.tmpdir");
1118          if (tmpDir != null) return tmpDir;
1119 
1120          // JDK < 1.4
1121          File tmpFile = null;
1122          try {
1123              // Try to guess the system temporary dir...
1124              tmpFile = File.createTempFile("tmp","jmx");
1125              if (tmpFile == null) return null;
1126              final File tmpDirFile = tmpFile.getParentFile();
1127              if (tmpDirFile == null) return null;
1128              return tmpDirFile.getAbsolutePath();
1129          } catch (Exception x) {
1130              MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1131                      "getTmpDir", "Failed to determine system temporary dir");
1132              return null;
1133          } finally {
1134              // Cleanup ...
1135              if (tmpFile!=null) {
1136                  try {
1137                      boolean deleted = tmpFile.delete();
1138                      if (!deleted) {
1139                          MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1140                                  "getTmpDir", "Failed to delete temp file");
1141                      }
1142                  } catch (Exception x) {
1143                      MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1144                              "getTmpDir", "Failed to delete temporary file", x);
1145                  }
1146              }
1147         }
1148      }
1149 
1150      /**
1151       * Search the specified native library in any of the JAR files
1152       * loaded by this classloader.  If the library is found copy it
1153       * into the library directory and return the absolute path.  If
1154       * the library is not found then return null.
1155       */
1156      private synchronized String loadLibraryAsResource(String libname) {
1157          try {
1158              InputStream is = getResourceAsStream(
1159                      libname.replace(File.separatorChar,'/'));
1160              if (is != null) {
1161                  try {
1162                      File directory = new File(libraryDirectory);
1163                      directory.mkdirs();
1164                      File file = Files.createTempFile(directory.toPath(),
1165                                                       libname + ".", null)
1166                                       .toFile();
1167                      file.deleteOnExit();
1168                      FileOutputStream fileOutput = new FileOutputStream(file);
1169                      try {
1170                          byte[] buf = new byte[4096];
1171                          int n;
1172                          while ((n = is.read(buf)) >= 0) {
1173                             fileOutput.write(buf, 0, n);
1174                          }
1175                      } finally {
1176                          fileOutput.close();
1177                      }
1178                      if (file.exists()) {
1179                          return file.getAbsolutePath();
1180                      }
1181                  } finally {
1182                      is.close();
1183                  }
1184              }
1185          } catch (Exception e) {
1186              MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1187                      "loadLibraryAsResource",
1188                      "Failed to load library : " + libname, e);
1189              return null;
1190          }
1191          return null;
1192      }
1193 
1194    /**
1195     * Removes any white space from a string. This is used to
1196     * convert strings such as "Windows NT" to "WindowsNT".
1197     */
1198      private static String removeSpace(String s) {
1199          return s.trim().replace(" ", "");
1200      }
1201 
1202      /**
1203       * <p>This method is to be overridden when extending this service to
1204       * support caching and versioning.  It is called from {@link
1205       * #getMBeansFromURL getMBeansFromURL} when the version,
1206       * codebase, and jarfile have been extracted from the MLet file,
1207       * and can be used to verify that it is all right to load the
1208       * given MBean, or to replace the given URL with a different one.</p>
1209       *
1210       * <p>The default implementation of this method returns
1211       * <code>codebase</code> unchanged.</p>
1212       *
1213       * @param version The version number of the <CODE>.jar</CODE>
1214       * file stored locally.
1215       * @param codebase The base URL of the remote <CODE>.jar</CODE> file.
1216       * @param jarfile The name of the <CODE>.jar</CODE> file to be loaded.
1217       * @param mlet The <CODE>MLetContent</CODE> instance that
1218       * represents the <CODE>MLET</CODE> tag.
1219       *
1220       * @return the codebase to use for the loaded MBean.  The returned
1221       * value should not be null.
1222       *
1223       * @exception Exception if the MBean is not to be loaded for some
1224       * reason.  The exception will be added to the set returned by
1225       * {@link #getMBeansFromURL getMBeansFromURL}.
1226       *
1227       */
1228      protected URL check(String version, URL codebase, String jarfile,
1229                          MLetContent mlet)
1230              throws Exception {
1231          return codebase;
1232      }
1233 
1234     /**
1235      * Loads the serialized object specified by the <CODE>OBJECT</CODE>
1236      * attribute of the <CODE>MLET</CODE> tag.
1237      *
1238      * @param codebase The <CODE>codebase</CODE>.
1239      * @param filename The name of the file containing the serialized object.
1240      * @return The serialized object.
1241      * @exception ClassNotFoundException The specified serialized
1242      * object could not be found.
1243      * @exception IOException An I/O error occurred while loading
1244      * serialized object.
1245      */
1246      private Object loadSerializedObject(URL codebase, String filename)
1247              throws IOException, ClassNotFoundException {
1248         if (filename != null) {
1249             filename = filename.replace(File.separatorChar,'/');
1250         }
1251         if (MLET_LOGGER.isLoggable(Level.FINER)) {
1252             MLET_LOGGER.logp(Level.FINER, MLet.class.getName(),
1253                     "loadSerializedObject", codebase.toString() + filename);
1254         }
1255         InputStream is = getResourceAsStream(filename);
1256         if (is != null) {
1257             try {
1258                 ObjectInputStream ois = new MLetObjectInputStream(is, this);
1259                 Object serObject = ois.readObject();
1260                 ois.close();
1261                 return serObject;
1262             } catch (IOException e) {
1263                 if (MLET_LOGGER.isLoggable(Level.FINEST)) {
1264                     MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1265                             "loadSerializedObject",
1266                             "Exception while deserializing " + filename, e);
1267                 }
1268                 throw e;
1269             } catch (ClassNotFoundException e) {
1270                 if (MLET_LOGGER.isLoggable(Level.FINEST)) {
1271                     MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1272                             "loadSerializedObject",
1273                             "Exception while deserializing " + filename, e);
1274                 }
1275                 throw e;
1276             }
1277         } else {
1278             if (MLET_LOGGER.isLoggable(Level.FINEST)) {
1279                 MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1280                         "loadSerializedObject", "Error: File " + filename +
1281                         " containing serialized object not found");
1282             }
1283             throw new Error("File " + filename + " containing serialized object not found");
1284         }
1285      }
1286 
1287      /**
1288       * Converts the String value of the constructor's parameter to
1289       * a basic Java object with the type of the parameter.
1290       */
1291      private  Object constructParameter(String param, String type) {
1292          // check if it is a primitive type
1293          Class<?> c = primitiveClasses.get(type);
1294          if (c != null) {
1295             try {
1296                 Constructor<?> cons =
1297                     c.getConstructor(String.class);
1298                 Object[] oo = new Object[1];
1299                 oo[0]=param;
1300                 return(cons.newInstance(oo));
1301 
1302             } catch (Exception  e) {
1303                 MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(),
1304                         "constructParameter", "Got unexpected exception", e);
1305             }
1306         }
1307         if (type.compareTo("java.lang.Boolean") == 0)
1308              return Boolean.valueOf(param);
1309         if (type.compareTo("java.lang.Byte") == 0)
1310              return new Byte(param);
1311         if (type.compareTo("java.lang.Short") == 0)
1312              return new Short(param);
1313         if (type.compareTo("java.lang.Long") == 0)
1314              return Long.valueOf(param);
1315         if (type.compareTo("java.lang.Integer") == 0)
1316              return new Integer(param);
1317         if (type.compareTo("java.lang.Float") == 0)
1318              return new Float(param);
1319         if (type.compareTo("java.lang.Double") == 0)
1320              return new Double(param);
1321         if (type.compareTo("java.lang.String") == 0)
1322              return param;
1323 
1324         return param;
1325      }
1326 
1327     private synchronized void setMBeanServer(final MBeanServer server) {
1328         this.server = server;
1329         PrivilegedAction<ClassLoaderRepository> act =
1330             new PrivilegedAction<ClassLoaderRepository>() {
1331                 public ClassLoaderRepository run() {
1332                     return server.getClassLoaderRepository();
1333                 }
1334             };
1335         currentClr = AccessController.doPrivileged(act);
1336     }
1337 
1338 }
--- EOF ---