Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *        JacORB - a free Java ORB
   *
   *   Copyright (C) 1999-2012 Gerald Brose / The JacORB Team.
   *
   *   This library is free software; you can redistribute it and/or
   *   modify it under the terms of the GNU Library General Public
   *   License as published by the Free Software Foundation; either
   *   version 2 of the License, or (at your option) any later version.
  *
  *   This library is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *   Library General Public License for more details.
  *
  *   You should have received a copy of the GNU Library General Public
  *   License along with this library; if not, write to the Free
  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  */
 
 package org.jacorb.notification.jmx.mx4j;
 
 import java.util.List;
 import java.util.Map;
 
 
 import  org.tanukisoftware.wrapper.WrapperListener;
 import  org.tanukisoftware.wrapper.WrapperManager;
 import  org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;

MX4J specific startup class for JMX-enabled Notification Service

Author(s):
Alphonse Bendt
 
 public class JMXMain implements WrapperListener
 {
     public static final String DEFAULT_DOMAIN = "NotificationService";
 
     private static boolean sUseHTTPConnector = false;
 
     private static boolean sUseMX4J;
 
 
     private final List connectors_ = new ArrayList();
 
     private ORB orb_;
 
     private MBeanServer mbeanServer_;
 
     private Logger logger_;
 
     private JMXMain()
     {
         super();
     }
 
     private void stopConnectors()
     {
         for (final Iterator i = .iterator(); i.hasNext();)
         {
             final ObjectName name = (ObjectNamei.next();
             try
             {
                .invoke(name"stop"nullnull);
            } catch (Exception e)
            {
                .warn("Unable to stop Connnector " + namee);
            }
        }
    }
    private void startHTTPConnector() throws Exception
    {
        final ObjectName _connectorName = new ObjectName("connectors:protocol=http");
        .createMBean("mx4j.tools.adaptor.http.HttpAdaptor"_connectorNamenull);
        // TODO make port default configurable
        .setAttribute(_connectorNamenew Attribute("Port"new Integer(8001)));
        .setAttribute(_connectorNamenew Attribute("Host""localhost"));
        .invoke(_connectorName"start"nullnull);
        final ObjectName _processorName = new ObjectName("Server:name=XSLTProcessor");
        .createMBean("mx4j.tools.adaptor.http.XSLTProcessor"_processorNamenull);
        .setAttribute(_connectorNamenew Attribute("ProcessorName"_processorName));
        .add(_connectorName);
    }
    private void startIIOPConnector() throws ExceptionIOException
    {
        final JMXServiceURL _serviceURL = new JMXServiceURL("service:jmx:iiop://localhost/jndi/COSNotification");
        final Map _environment = new HashMap();
        _environment.put(."com.sun.jndi.cosnaming.CNCtxFactory");
        // fetch NameService Ref via ORB
        org.omg.CORBA.Object _nameService = .resolve_initial_references("NameService");
        final String _nameServiceIOR = .object_to_string(_nameService);
        _environment.put(._nameServiceIOR);
        _environment.put("java.naming.corba.orb");
        InitialContext context = new InitialContext(new Hashtable(_environment));
        try
        {
            context.unbind("COSNotification");
        }
        finally
        {
            context.close();
        }
        // create the JMXCconnectorServer
        final JMXConnectorServer _connectorServer =
            JMXConnectorServerFactory.newJMXConnectorServer(_serviceURL_environment);
        // register the JMXConnectorServer in the MBeanServer
        final ObjectName _connectorServerName = ObjectName.getInstance("connectors:protocol=iiop");
        .registerMBean(_connectorServer_connectorServerName);
        _connectorServer.start();
        .add(_connectorServerName);
    }
    public Integer start(String[] args)
    {
        try
        {
            initORB(args);
             = ObjectName.getInstance(
                    + ":type=EventChannelFactory");
             = ManagementFactory.getPlatformMBeanServer();
            registerNotificationService(args);
            registerWrapperManager();
            startIIOPConnector();
            if ()
            {
                startHTTPConnector();
            }
            return null;
        } catch (Exception e)
        {
            if ( != null)
            {
                .error("Unable to Start Service"e);
            }
            WrapperManager.log(WrapperManager.WRAPPER_LOG_LEVEL_FATAL, "Unable to Start Service" + e);
            stopConnectors();
            .shutdown(true);
            throw new RuntimeException(e);
        }
    }
    private void registerNotificationService(String[] argsthrows NotCompliantMBeanException,
            ReflectionException
    {
        final MX4JCOSNotificationServiceMBean _notificationService = new MX4JCOSNotificationService(,
                new JMXManageableMBeanProvider(), args);
        final StandardMBean _mbean = new StandardMBean(_notificationService,
                MX4JCOSNotificationServiceMBean.class);
        .invoke("start"nullnull);
    }
    private void initORB(String[] argsthrows InvalidNameAdapterInactive
    {
        final Properties _props = ConsoleMain.parseProperties(args);
         = ORB.init(args_props);
        PortableRemoteObjectDelegateImpl.setORB();
                getClass().getName());
        final Thread _orbRunner = new Thread("ORB-Thread")
        {
            public void run()
            {
                .run();
            }
        };
        final POA _poa = POAHelper.narrow(.resolve_initial_references("RootPOA"));
        _poa.the_POAManager().activate();
        _orbRunner.start();
    }
    public int stop(int code)
    {
        try
        {
            .invoke("stop"nullnull);
            stopConnectors();
            .shutdown(true);
        } catch (Exception e)
        {
            .error("Error while stopping Service"e);
            WrapperManager.log(WrapperManager.WRAPPER_LOG_LEVEL_ERROR, "Unable to Stop Service" + e);
            return 1;
        }
        return 0;
    }
    public void controlEvent(int event)
    {
        if (WrapperManager.isControlledByNativeWrapper())
        {
            // The Wrapper will take care of this event
            return;
        }
        // We are not being controlled by the Wrapper, so
        // handle the event ourselves.
        if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT)
                || (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT)
                || (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT))
        {
            org.tanukisoftware.wrapper.WrapperManager.stop(0);
        }
    }
    private void registerWrapperManager() throws JMException
    {
        if (!WrapperManager.isControlledByNativeWrapper())
        {
            return;
        }
        WrapperManager.log(WrapperManager.WRAPPER_LOG_LEVEL_INFO,
                "Registering WrapperManager MBean");
        final ObjectName wrapperManagerName = ObjectName.getInstance(
                + ":service=WrapperManager");
        final StandardMBean wrapperManagerBean = new StandardMBean(
                new org.tanukisoftware.wrapper.jmx.WrapperManager(), WrapperManagerMBean.class);
        .registerMBean(wrapperManagerBeanwrapperManagerName);
    }
    public static void main(String[] argsthrows Exception
    {
        final List list = new ArrayList(Arrays.asList(args));
        if (list.remove("-mx4j"))
        {
             = true;
            System.setProperty("javax.management.builder.initial""mx4j.server.MX4JMBeanServerBuilder");
        }
        if (list.remove("-mx4j:http") && )
        {
             = true;
        }
        System.setProperty("javax.rmi.CORBA.PortableRemoteObjectClass"PortableRemoteObjectDelegateImpl.class.getName());
        final JMXMain main = new JMXMain();
        WrapperManager.start(main, (String[])list.toArray(new String[list.size()]));
    }
New to GrepCode? Check out our FAQ X