jmx.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 729 行 · 第 1/2 页

JAVA
729
字号
   * Conditionally registers an MBean with the server.   *   * @param object the object to be registered as an MBean   * @param name the name of the mbean.   *   * @return the instantiated object.   */  public static void unregister(String name)    throws InstanceNotFoundException,	   MalformedObjectNameException,	   MBeanRegistrationException	     {    ObjectName objectName = getObjectName(name);    getMBeanServer().unregisterMBean(objectName);    // return register(object, objectName);  }  /**   * Returns an ObjectName based on a short name.   */  public static ObjectName getObjectName(String name)    throws MalformedObjectNameException  {    return getMBeanServer().createContext().getObjectName(name);  }  /**   * Parses a name.   */  public static LinkedHashMap<String,String> parseProperties(String name)  {    LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();        parseProperties(map, name);    return map;  }  /**   * Parses a name.   */  public static void parseProperties(Map<String,String> properties,				     String name)  {    parseProperties(properties, name, 0);  }  /**   * Parses a name.   */  private static void    parseProperties(Map<String,String> properties, String name, int i)  {    CharBuffer cb = CharBuffer.allocate();        int len = name.length();        while (i < len) {      for (; i < len && Character.isWhitespace(name.charAt(i)); i++) {      }      cb.clear();      int ch;      for (; i < len && (ch = name.charAt(i)) != '=' && ch != ',' &&	     ! Character.isWhitespace((char) ch); i++) {	cb.append((char) ch);      }      String key = cb.toString();      if (key.length() == 0) {	throw new IllegalArgumentException(L.l("`{0}' is an illegal name syntax.",					       name));      }      for (; i < len && Character.isWhitespace(name.charAt(i)); i++) {      }      if (len <= i || (ch = name.charAt(i)) == ',') {	properties.put(key, "");      }      else if (ch == '=') {	for (i++; i < len && Character.isWhitespace(name.charAt(i)); i++) {	}	if (len <= i || (ch = name.charAt(i)) == ',') {	  properties.put(key, "");	}	else if (ch == '"' || ch == '\'') {	  int end = ch;	  cb.clear();	  for (i++; i < len && (ch = name.charAt(i)) != end; i++) {	    if (ch == '\\') {	      ch = name.charAt(++i);	      cb.append((char) ch);	    }	    else	      cb.append((char) ch);	  }	  if (ch != end)	    throw new IllegalArgumentException(L.l("`{0}' is an illegal name syntax.",						   name));	  i++;	  String value = cb.toString();	  properties.put(key, value);	}	else {	  cb.clear();	  	  for (; i < len && (ch = name.charAt(i)) != ','; i++)	    cb.append((char) ch);	  properties.put(key, cb.toString());	}      }      else {	throw new IllegalArgumentException(L.l("`{0}' is an illegal name syntax.",					       name));      }      for (; i < len && Character.isWhitespace(name.charAt(i)); i++) {      }            if (i < len && name.charAt(i) != ',')	throw new IllegalArgumentException(L.l("`{0}' is an illegal name syntax.",					       name));      i++;    }  }  /**   * Creates the clean name   */  public static ObjectName getObjectName(String domain,					 Map<String,String> properties)    throws MalformedObjectNameException  {    StringBuilder cb = new StringBuilder();    cb.append(domain);    cb.append(':');    boolean isFirst = true;    Pattern escapePattern = Pattern.compile("[,=:\"*?]");    // sort type first    String type = properties.get("type");    if (type != null) {      cb.append("type=");      if (escapePattern.matcher(type).find())	type = ObjectName.quote(type);      cb.append(type);      isFirst = false;    }    for (String key : properties.keySet()) {      if (key.equals("type"))	continue;            if (! isFirst)	cb.append(',');      isFirst = false;      cb.append(key);      cb.append('=');      String value = properties.get(key);      if (value.length() == 0	  || (escapePattern.matcher(value).find()	      && ! (value.startsWith("\"") && value.endsWith("\"")))) {	value = ObjectName.quote(value);      }            cb.append(value);    }    return new ObjectName(cb.toString());  }  /**   * Returns the local view.   */  /*  public static MBeanView getLocalView()  {    MBeanContext context = MBeanContext.getLocal();    return context.getView();  }  */  /**   * Returns the local view.   */  /*  public static MBeanView getLocalView(ClassLoader loader)  {    MBeanContext context = MBeanContext.getLocal(loader);    return context.getView();  }  */  /**   * Returns the local manged object.   */  public static Object find(String localName)    throws MalformedObjectNameException  {    return find(getMBeanServer().createContext().getObjectName(localName));  }  /**   * Returns the local manged object.   */  public static Object find(ObjectName name)  {    return find(name, Thread.currentThread().getContextClassLoader());  }  /**   * Returns the local manged object.   */  public static Object findGlobal(String localName)    throws MalformedObjectNameException  {    return findGlobal(getMBeanServer().createContext().getObjectName(localName));  }  /**   * Returns the local manged object.   */  public static Object findGlobal(ObjectName name)  {    return find(name, ClassLoader.getSystemClassLoader(), getGlobalMBeanServer());  }  /**   * Returns the local manged object.   */  public static Object find(ObjectName name, ClassLoader loader)  {    return find(name, loader, getMBeanServer());  }  /**   * Returns the local manged object.   */  public static Object find(ObjectName name,			    ClassLoader loader,			    MBeanServer mbeanServer)  {    try {      ObjectInstance obj = mbeanServer.getObjectInstance(name);      if (obj == null)	return null;      String className = obj.getClassName();      Class cl = Class.forName(className, false, loader);      Class ifc;            if (cl.isInterface())	ifc = cl;      else	ifc = getMBeanInterface(cl);      if (ifc == null)	return null;      boolean isBroadcast = true;      Object proxy;      proxy = JmxInvocationHandler.newProxyInstance(mbeanServer,						    loader,						    name,						    ifc,						    true);      return proxy;    } catch (InstanceNotFoundException e) {      log.log(Level.FINE, e.toString(), e);      return null;    } catch (ClassNotFoundException e) {      log.log(Level.FINE, e.toString(), e);      return null;    }  }  /**   * Returns the local manged object.   */  public static ArrayList<Object> query(ObjectName namePattern)  {    Set<ObjectName> names = getMBeanServer().queryNames(namePattern, null);    ArrayList<Object> proxy = new ArrayList<Object>();    Iterator<ObjectName> iter = names.iterator();    while (iter.hasNext()) {      ObjectName name = iter.next();      proxy.add(find(name));    }        return proxy;  }  /**   * Queues a task.   */  public static void queueAbsolute(TimerTask job, long time)  {    JobThread.queue(job, time);  }  /**   * Queues a task.   */  public static void queueRelative(TimerTask job, long delta)  {    queueAbsolute(job, Alarm.getCurrentTime() + delta);  }  /**   * Dequeues a task.   */  public static void dequeue(TimerTask job)  {    JobThread.dequeue(job);  }  // static  public Jmx() {}  private static void initStaticMBeans()  {    try {      Class cl = Class.forName("java.lang.Management.ManagementFactory");      Method method = cl.getMethod("getPlatformMBeanServer", new Class[0]);      method.invoke(null, new Object[0]);    } catch (Throwable e) {    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?