Package com.orientechnologies.orient.server.handler

Source Code of com.orientechnologies.orient.server.handler.OJMXPlugin

/*
      *
      *  *  Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
      *  *
      *  *  Licensed under the Apache License, Version 2.0 (the "License");
      *  *  you may not use this file except in compliance with the License.
      *  *  You may obtain a copy of the License at
      *  *
      *  *       http://www.apache.org/licenses/LICENSE-2.0
      *  *
      *  *  Unless required by applicable law or agreed to in writing, software
      *  *  distributed under the License is distributed on an "AS IS" BASIS,
      *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      *  *  See the License for the specific language governing permissions and
      *  *  limitations under the License.
      *  *
      *  * For more information: http://www.orientechnologies.com
      *
      */

package com.orientechnologies.orient.server.handler;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.OConstants;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.exception.OConfigurationException;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.config.OServerParameterConfiguration;
import com.orientechnologies.orient.server.managed.OrientServer;
import com.orientechnologies.orient.server.plugin.OServerPluginAbstract;

public class OJMXPlugin extends OServerPluginAbstract {
   private OrientServer managedServer;
   private ObjectName   onProfiler;
   private ObjectName   onServer;
   private boolean      profilerManaged;

   public OJMXPlugin() {
   }

   @Override
   public void config(final OServer oServer, final OServerParameterConfiguration[] iParams) {
     for (OServerParameterConfiguration param : iParams) {
       if (param.name.equalsIgnoreCase("enabled")) {
         if (!Boolean.parseBoolean(param.value))
           // DISABLE IT
           return;
       } else if (param.name.equalsIgnoreCase("profilerManaged"))
         profilerManaged = Boolean.parseBoolean(param.value);
     }

     OLogManager.instance().info(this, "JMX plugin installed and active: profilerManaged=%s", profilerManaged);

     final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

     try {
       if (profilerManaged) {
         // REGISTER THE PROFILER
         onProfiler = new ObjectName("OrientDB:type=Profiler");
         mBeanServer.registerMBean(Orient.instance().getProfiler(), onProfiler);
       }

       // REGISTER SERVER
       onServer = new ObjectName("OrientDB:type=Server");
       managedServer = new OrientServer();
       mBeanServer.registerMBean(managedServer, onServer);

     } catch (Exception e) {
       throw new OConfigurationException("Cannot initialize JMX server", e);
     }
   }

   /*
    * (non-Javadoc)
    *
    * @see com.orientechnologies.orient.server.handler.OServerHandlerAbstract#shutdown()
    */
   @Override
   public void shutdown() {
     try {
       MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
       if (onProfiler != null)
         if (mBeanServer.isRegistered(onProfiler))
           mBeanServer.unregisterMBean(onProfiler);

       if (onServer != null)
         if (mBeanServer.isRegistered(onServer))
           mBeanServer.unregisterMBean(onServer);
     } catch (Exception e) {
       OLogManager.instance().error(this, "OrientDB Server v" + OConstants.ORIENT_VERSION + " unregisterMBean error.", e);
     }

   }

   @Override
   public String getName() {
     return "jmx";
   }

   public OrientServer getManagedServer() {
     return managedServer;
   }
}
TOP

Related Classes of com.orientechnologies.orient.server.handler.OJMXPlugin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.