KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > remoting > MBeanTransportPreference


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.mx.remoting;
10
11 import java.util.StringTokenizer JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import org.jboss.logging.Logger;
14 import org.jboss.remoting.ConnectionFailedException;
15 import org.jboss.remoting.InvokerLocator;
16 import org.jboss.remoting.ident.Identity;
17
18 /**
19  * MBeanTransportPreference is a utility class that will take into account the VM's setup transport
20  * preferences when trying to create a preferred connection back to a remote MBeanServer. Since there
21  * are cases when multiple invoker transports can exist and all be valid, this will help determine which
22  * order the transports should be attempted. You wouldn't want to connect via SOAP, in most cases, when
23  * TCP/IP via Sockets is available. There are cases however you do want to explicitly choose SOAP vs.
24  * Sockets or RMI, and in which case your preference order might be <tt>soap, socket, rmi.</tt>
25  *
26  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
27  * @version $Revision: 30251 $
28  */

29 public class MBeanTransportPreference
30 {
31    private static final transient Logger log = Logger.getLogger(MBeanTransportPreference.class.getName());
32
33    // NOTE: we need to maybe think this through on how this really should work long term..
34
private static String JavaDoc _preferences = System.getProperty("jboss.transport.preferences", "socket,rmi,soap");
35    private static String JavaDoc preferences[] = initialize(_preferences);
36    private static MBeanServer JavaDoc ourServer;
37    private static Identity ourIdentity;
38
39    public static void setLocalServer(MBeanServer JavaDoc server, Identity identity)
40    {
41       if(log.isTraceEnabled())
42       {
43          log.trace("setLocalServer called - server=" + server + ",identity=" + identity);
44       }
45       ourServer = server;
46       ourIdentity = identity;
47    }
48
49    private static String JavaDoc[] initialize(String JavaDoc list)
50    {
51       if(list == null)
52       {
53          return new String JavaDoc[1];
54       }
55       StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(list, ",");
56       String JavaDoc pref [] = new String JavaDoc[tok.countTokens()];
57       int c = 0;
58       while(tok.hasMoreTokens())
59       {
60          String JavaDoc token = tok.nextToken();
61          pref[c++] = token.trim();
62       }
63       return pref;
64    }
65
66    /**
67     * set the order to use when selecting transports to connect to a remote server
68     *
69     * @param order
70     */

71    public static void setTransportPreferences(String JavaDoc order[])
72    {
73       preferences = (order == null || order.length <= 0) ? initialize(_preferences) : order;
74    }
75
76    /**
77     * get the order to use when selecting transports to connect to a remote server
78     *
79     * @return
80     */

81    public static String JavaDoc[] getTransportPreferences()
82    {
83       return preferences;
84    }
85
86    /**
87     * return a server transport to a MBeanServer on a remote server, using the transport
88     * preference order specified by the user
89     *
90     * @param identity
91     * @param locators
92     * @return
93     * @throws ConnectionFailedException
94     */

95    public static MBeanServer JavaDoc getServerByTransport(Identity identity, InvokerLocator locators[])
96          throws ConnectionFailedException
97    {
98       if(log.isTraceEnabled())
99       {
100          log.trace("getServerByTransport for identity=" + identity + ", ours is=" + ourIdentity);
101       }
102       if(ourIdentity == null)
103       {
104          if(ourServer == null)
105          {
106             ourServer = JMXUtil.getMBeanServer();
107          }
108          ourIdentity = Identity.get(ourServer);
109       }
110       if(identity.isSameJVM(ourIdentity))
111       {
112          return ourServer;
113       }
114       for(int c = 0; c < preferences.length; c++)
115       {
116          String JavaDoc transport = preferences[c];
117
118          if(transport != null)
119          {
120             for(int x = 0; x < locators.length; x++)
121             {
122                if(locators[x].getProtocol().equals(transport))
123                {
124                   // attempt connect to this first one in our pref list
125
try
126                   {
127                      MBeanServer JavaDoc svr = MBeanServerRegistry.getMBeanServerFor(locators[x]);
128                      if(svr != null)
129                      {
130                         return svr;
131                      }
132                      svr = MBeanServerClientInvokerProxy.create(locators[x], ourIdentity.getJMXId(), identity.getJMXId());
133                      if(svr != null)
134                      {
135                         return svr;
136                      }
137                   }
138                   catch(Throwable JavaDoc ex)
139                   {
140                   }
141                }
142             }
143          }
144       }
145       for(int x = 0; x < locators.length; x++)
146       {
147          // attempt connect to this first one in our pref list
148
try
149          {
150             if(log.isTraceEnabled())
151             {
152                log.trace("attempting to connect via locator[" + x + "] (" + locators[x] + ") to: " + identity);
153             }
154             MBeanServer JavaDoc svr = MBeanServerRegistry.getMBeanServerFor(locators[x]);
155             if(svr != null)
156             {
157                return svr;
158             }
159             svr = MBeanServerClientInvokerProxy.create(locators[x], ourIdentity.getJMXId(), identity.getJMXId());
160             if(svr != null)
161             {
162                return svr;
163             }
164          }
165          catch(Throwable JavaDoc ex)
166          {
167             log.debug("Error connecting ... ", ex);
168          }
169       }
170       throw new ConnectionFailedException("No transport/connection available to connect to: " + identity);
171    }
172 }
173
Popular Tags