Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, Red Hat, Inc. and/or its affiliates,
   * and individual contributors as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2010,
  * @author JBoss, by Red Hat.
  */
 package com.arjuna.ats.arjuna.tools.osb.api.proxy;
 
 import java.util.Map;
 
 
Miscellaneous methods for obtaining remote proxies to the JBossTS Recovery and Participant stores
 
 public class StoreManagerProxy {
     // class and bean names of MBeans representing the JBossTS recovery and participant stores
     public static final String RECOVERY_BEAN_NAME = "jboss.jta:type=com.arjuna.ats.arjuna.tools.osb.api.mbeans.RecoveryStoreBean,name=store1";
     public static final String PARTICIPANT_BEAN_NAME = "jboss.jta:type=com.arjuna.ats.arjuna.tools.osb.api.mbeans.ParticipantStoreBean,name=store1";
 
 //    public static final String SERVICE_URL = "service:jmx:rmi:///jndi/rmi://localhost:9999/server";
 
     private static Map<StringStoreManagerProxyproxies = Collections.synchronizedMap(new HashMap<StringStoreManagerProxy>());
     private static JMXConnectorServer jmxCServer;
 
     private MBeanServerConnection mbsc;	// MBean server implementing the object store MBeans
 
     private JMXConnector jmxc;
     private RecoveryStoreProxy rsProxy;	// proxy for the recovery store
     private ParticipantStoreProxy psProxy;	// proxy for the participant store
     private ObjectName recoveryStoreON;	// object name of the recover store MBean
     private ObjectName participantStoreON;	// object name of the participant store MBean
     private NotificationListener recoveryListener = null;
     private NotificationListener participantListener = null;

    
Construct a holder for Participant and Recovery Store proxies. There is one instance for each connection to a JVM. In practice there will only ever be one instance.

Parameters:
serviceUrl the url for the MBean server to connect to. Use default to connect to the local MBean Server
listener optionally register a listener for notifications
Throws:
javax.management.JMException if there are JMX errors during registration of MBeans and notification listeners
java.io.IOException if there are errors on the connection to the MBean Server
 
     private StoreManagerProxy(String serviceUrlNotificationListener listenerthrows JMExceptionIOException {
         if ("default".equals(serviceUrl)) {
              = JMXServer.getAgent().getServer();
         } else {
             // create an RMI connector
             JMXServiceURL url = new JMXServiceURL(serviceUrl);
             // connect to the target MBean server
              = JMXConnectorFactory.connect(urlnull);
              = .getMBeanServerConnection();
         }
 
          = ObjectName.getInstance();
          = ObjectName.getInstance();
 
          = new RecoveryStoreProxy(JMX.newMBeanProxyRecoveryStoreBeanMBean.classtrue));
 
         if (listener != null) {
             .addNotificationListener(listenernullnull);
             .addNotificationListener(listenernullnull);
         }
     }

    
Mechanism for JMX clients to remove listeners and to close the JMX connection if the client create one

Throws:
javax.management.JMException if there are errors removing listeners
java.io.IOException if there are errors removing listeners or closing the JMX connection
    private void close() throws JMExceptionIOException {
        ..println("Remove notification listener...");
        // Remove notification listener on RecoveryStore MBean
        if (this. != null)
        if (this. != null)
         =  = null;
        // close the connection to the JMX server
        if ( != null) {
            .close();
             = null;
        }
    }

    
Helper method for remote clients to connect to an MBean Server

Parameters:
serviceUrl the url on which the target MBean Server resides
Throws:
java.io.IOException if the serviceUrl is invalid or if the connection cannot be started
    public static void startServerConnector(String serviceUrlthrows IOException {
         = JMXConnectorServerFactory.newJMXConnectorServer(
                new JMXServiceURL(serviceUrl), null, JMXServer.getAgent().getServer());
        // accept JMX connections
        .start();
    }
    public static void stopServerConnector() throws IOException {
        .stop();
    }

    
MBean registration helper method

Parameters:
name MBean object name
bean MBean implementation
register whether to register or unregister the MBean
Returns:
true if the bean was successfully registered or unregistered
    public static boolean registerBean(ObjectName nameObject beanboolean register) {
        try {
            MBeanServer mbs = JMXServer.getAgent().getServer();
            boolean isRegistered = mbs.isRegistered(name);
            ..println((register ? "" : "un") + "registering bean " + name);
            if (register && isRegistered) {
                ..println(name + " is already registered");
                return true;
            } else if (!register && !isRegistered) {
                ..println(name + " is not registered");
                return true;
            } else if (register) {
                mbs.registerMBean(beanname);
            } else {
                mbs.unregisterMBean(name);
            }
            return true;
        } catch (JMException e) {
            ..println("MBean registration error: " + e.getMessage());
            return false;
        }
    }
    // Obtain a JMX proxy to the ObjectStoreAPI
    private static synchronized StoreManagerProxy getProxy(String serviceUrlNotificationListener listenerthrows IOExceptionJMException {
        if (serviceUrl == null)
            serviceUrl = "default";
        if (!.containsKey(serviceUrl))
            .put(serviceUrlnew StoreManagerProxy(serviceUrllistener));
        return .get(serviceUrl);
    }

    
release proxies to the object stores

Throws:
javax.management.JMException if there are errors removing listeners
java.io.IOException if there are errors removing listeners or closing the JMX connection
    public static void releaseProxy() throws JMExceptionIOException {
        releaseProxy("default");
    }

    
release proxies to the object stores

Parameters:
serviceUrl the service url of the MBean Server where the proxies are located
Throws:
javax.management.JMException if there are errors removing listeners
java.io.IOException if there are errors removing listeners or closing the JMX connection
    public static void releaseProxy(String serviceUrlthrows JMExceptionIOException {
        StoreManagerProxy psm = .remove(serviceUrl);
        if (psm != null)
            psm.close();
    }

    
Get a recovery store proxy from the local MBeanServer

Returns:
a proxy for the target RecoveryStore
Throws:
javax.management.JMException if there are JMX errors during registration of MBeans
java.io.IOException if there are errors on the connection to the MBean Server
    public static synchronized RecoveryStoreProxy getRecoveryStore() throws IOExceptionJMException {
        return getRecoveryStore(null);
    }

    
Get a recovery store proxy from the local MBeanServer

Parameters:
listener listener an optional notification listener (use null if one is not required)
Returns:
a proxy for the target RecoveryStore
Throws:
javax.management.JMException if there are JMX errors during registration of MBeans and notification listeners
java.io.IOException if there are errors on the connection to the MBean Server
    public static synchronized RecoveryStoreProxy getRecoveryStore(NotificationListener listenerthrows IOExceptionJMException {
        return getProxy("default"listener).;
    }

    
Get a RecoveryStore proxy.

Parameters:
serviceUrl the location of the MBean Server
listener an optional notification listener (use null if one is not required)
Returns:
a proxy for the target RecoveryStore
Throws:
javax.management.JMException if there are JMX errors during registration of MBeans and notification listeners
java.io.IOException if there are errors on the connection to the MBean Server
    public static synchronized RecoveryStoreProxy getRecoveryStore(String serviceUrlNotificationListener listenerthrows IOExceptionJMException {
        return getProxy(serviceUrllistener).;
    }

    
Get a participant store proxy from the local MBeanServer

Returns:
a proxy for the target ParticipantStore
Throws:
javax.management.JMException if there are JMX errors during registration of MBeans
java.io.IOException if there are errors on the connection to the MBean Server
    public static synchronized ParticipantStoreProxy getParticipantStore() throws IOExceptionJMException {
        return getParticipantStore(null);
    }

    
Get a participant store proxy from the local MBeanServer

Parameters:
listener listener an optional notification listener (use null if one is not required)
Returns:
a proxy for the target ParticipantStore
Throws:
javax.management.JMException if there are JMX errors during registration of MBeans and notification listeners
java.io.IOException if there are errors on the connection to the MBean Server
    public static synchronized ParticipantStoreProxy getParticipantStore(NotificationListener listenerthrows IOExceptionJMException {
        return getProxy("default"listener).;
    }

    
Get a participant store proxy.

Parameters:
serviceUrl the location of the MBean Server
listener an optional notification listener (use null if one is not required)
Returns:
a proxy for the target ParticipantStore
Throws:
javax.management.JMException if there are JMX errors during registration of MBeans and notification listeners
java.io.IOException if there are errors on the connection to the MBean Server
    public static synchronized ParticipantStoreProxy getParticipantStore(String serviceUrlNotificationListener listenerthrows IOExceptionJMException {
        return getProxy(serviceUrllistener).;
    }
New to GrepCode? Check out our FAQ X