Ejemplo n.º 1
0
 public static void main(String[] args) {
   int errorCount = 0;
   for (int i = 0; i < NTESTS; i++) {
     try {
       System.out.println("Test " + i + ":");
       test(i);
     } catch (Throwable e) {
       errorCount++;
       boolean first = true;
       do {
         System.err.println(first ? "Exception:" : "Caused by:");
         first = false;
         e.printStackTrace();
         Throwable nexte;
         nexte = e.getCause();
         if (nexte == null) { // old JMX
           if (e instanceof MBeanException) nexte = ((MBeanException) e).getTargetException();
         }
         e = nexte;
       } while (e != null);
     }
   }
   if (errorCount == 0) {
     System.out.println("All ModelMBean tests successfuly passed");
     System.out.println("Bye! Bye!");
     // JTReg doesn't like System.exit(0);
     return;
   } else {
     System.err.println("ERROR: " + errorCount + " tests failed");
     System.exit(errorCount);
   }
 }
  /**
   * Handles a received {@link MBeanServerConnection} invocation
   *
   * @param channel The channel the request was received on
   * @param remoteAddress The remote address of the caller
   * @param buffer THe buffer received
   */
  public static void handleJMXRequest(
      Channel channel, SocketAddress remoteAddress, ChannelBuffer buffer) {
    buffer.resetReaderIndex();
    /* The request write */
    //		cb.writeByte(OpCode.JMX_REQUEST.op());  // 1
    //		cb.writeBytes(domainInfoData);   // domain data
    //		cb.writeInt(reqId);					// 4
    //		cb.writeByte(methodToKey.get(method)); // 1
    //		cb.writeInt(sargs.length);  			// 4
    //		cb.writeBytes(sargs);		           // sargs.length
    Object result = null;
    MBeanServerConnection server = null;
    buffer.skipBytes(1);
    byte domainIndicator = buffer.readByte();
    if (domainIndicator == 0) {
      server = JMXHelper.getHeliosMBeanServer();
    } else {
      byte[] domainBytes = new byte[domainIndicator];
      buffer.readBytes(domainBytes);
      String domain = new String(domainBytes);
      server = JMXHelper.getLocalMBeanServer(true, domain);
      if (server == null) {
        result = new SmallException("Failed to locate MBeanServer for domain [" + domain + "]");
      }
    }
    int reqId = buffer.readInt();
    byte methodId = buffer.readByte();
    if (result == null) {
      int payloadSize = buffer.readInt();
      byte[] payload = new byte[payloadSize];
      buffer.readBytes(payload);
      Object[] params = getInput(payload);
      Method targetMethod = null;
      try {
        targetMethod = keyToMethod.get(methodId);
        if (targetMethod == null) {
          result =
              new SmallException(
                  "Failed to handle MBeanServerConnection invocation because method Op Code ["
                      + methodId
                      + "] was not recognized");
        } else {
          if ("addNotificationListener".equals(targetMethod.getName())
              && !targetMethod.getParameterTypes()[1].equals(ObjectName.class)) {

          } else if ("removeNotificationListener".equals(targetMethod.getName())
              && !targetMethod.getParameterTypes()[1].equals(ObjectName.class)) {

          } else {
            result = targetMethod.invoke(server, params);
          }
        }
      } catch (Throwable t) {
        SimpleLogger.warn("Failed to invoke [", targetMethod, "]", t);
        result = new SmallException(t.toString());
      }
    }
    writeJMXResponse(reqId, methodId, channel, remoteAddress, result);
  }
Ejemplo n.º 3
0
 /** If the exception is wrapped, unwrap it. */
 public static Throwable getActualException(Throwable e) {
   if (e instanceof ExecutionException) e = e.getCause();
   if (e instanceof MBeanException
       || e instanceof RuntimeMBeanException
       || e instanceof RuntimeOperationsException
       || e instanceof ReflectionException) {
     Throwable t = e.getCause();
     if (t != null) return t;
   }
   return e;
 }
Ejemplo n.º 4
0
  public static void main(String[] args) throws Exception {
    System.out.println(
        ">>> Test how for the MBeanServerInvocationHandler to "
            + "unwrap a user specific exception.");

    final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    final ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(new Test(), name);
    TestMBean proxy =
        (TestMBean)
            MBeanServerInvocationHandler.newProxyInstance(mbs, name, TestMBean.class, false);

    // test the method "getter"
    System.out.println(">>> Test the method getter to get an IOException.");
    try {
      proxy.getIOException();
    } catch (IOException e) {
      System.out.println(">>> Test passed: got expected exception:");
      // e.printStackTrace(System.out);
    } catch (Throwable t) {
      System.out.println(">>> Test failed: got wrong exception:");
      t.printStackTrace(System.out);

      throw new RuntimeException("Did not get an expected IOException.");
    }

    // test the method "setter"
    System.out.println(">>> Test the method setter to get a RuntimeException.");
    try {
      proxy.setRuntimeException("coucou");
    } catch (UnsupportedOperationException ue) {
      System.out.println(">>> Test passed: got expected exception:");
      // ue.printStackTrace(System.out);
    } catch (Throwable t) {
      System.out.println(">>> Test failed: got wrong exception:");
      t.printStackTrace(System.out);

      throw new RuntimeException("Did not get an expected Runtimeexception.");
    }

    // test the method "invoke"
    System.out.println(">>> Test the method invoke to get an Error.");
    try {
      proxy.invokeError();
    } catch (AssertionError ae) {
      System.out.println(">>> Test passed: got expected exception:");
      // ue.printStackTrace(System.out);
    } catch (Throwable t) {
      System.out.println(">>> Test failed: got wrong exception:");
      t.printStackTrace(System.out);

      throw new RuntimeException("Did not get an expected Error.");
    }
  }
Ejemplo n.º 5
0
 public void run() {
   started = true;
   Thread.currentThread().setName("Q2-" + getInstanceId().toString());
   try {
     /*
      * The following code determines whether a MBeanServer exists
      * already. If so then the first one in the list is used.
      * I have not yet find a way to interrogate the server for
      * information other than MBeans so to pick a specific one
      * would be difficult.
      */
     ArrayList mbeanServerList = MBeanServerFactory.findMBeanServer(null);
     if (mbeanServerList.isEmpty()) {
       server = MBeanServerFactory.createMBeanServer(JMX_NAME);
     } else {
       server = (MBeanServer) mbeanServerList.get(0);
     }
     final ObjectName loaderName = new ObjectName(Q2_CLASS_LOADER);
     try {
       loader =
           (QClassLoader)
               java.security.AccessController.doPrivileged(
                   new java.security.PrivilegedAction() {
                     public Object run() {
                       return new QClassLoader(server, libDir, loaderName, mainClassLoader);
                     }
                   });
       server.registerMBean(loader, loaderName);
       loader = loader.scan(false);
     } catch (Throwable t) {
       if (log != null) log.error("initial-scan", t);
       else t.printStackTrace();
     }
     factory = new QFactory(loaderName, this);
     initSystemLogger();
     addShutdownHook();
     q2Thread = Thread.currentThread();
     q2Thread.setContextClassLoader(loader);
     if (cli != null) cli.start();
     initConfigDecorator();
     for (int i = 1; !shutdown; i++) {
       try {
         boolean forceNewClassLoader = scan();
         QClassLoader oldClassLoader = loader;
         loader = loader.scan(forceNewClassLoader);
         if (loader != oldClassLoader) {
           oldClassLoader = null; // We want't this to be null so it gets GCed.
           System.gc(); // force a GC
           log.info(
               "new classloader ["
                   + Integer.toString(loader.hashCode(), 16)
                   + "] has been created");
         }
         deploy();
         checkModified();
         relax(SCAN_INTERVAL);
         if (i % (3600000 / SCAN_INTERVAL) == 0) logVersion();
       } catch (Throwable t) {
         log.error("start", t);
         relax();
       }
     }
     undeploy();
     try {
       server.unregisterMBean(loaderName);
     } catch (InstanceNotFoundException e) {
       log.error(e);
     }
     if (decorator != null) {
       decorator.uninitialize();
     }
     if (exit && !shuttingDown) System.exit(0);
   } catch (Exception e) {
     if (log != null) log.error(e);
     else e.printStackTrace();
     System.exit(1);
   }
 }