KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.net.InetAddress JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerFactory JavaDoc;
18 import org.jboss.remoting.ConnectionFailedException;
19 import org.jboss.remoting.InvokerLocator;
20 import org.jboss.remoting.ident.Identity;
21 import org.jboss.remoting.network.NetworkInstance;
22 import org.jboss.remoting.network.NetworkRegistry;
23 import org.jboss.remoting.network.filter.IdentityFilter;
24
25 /**
26  * MBeanServerLocator is an object that is used to identify and locate
27  * an MBeanServer on the network via JMX Remoting. <P>
28  * <p/>
29  * The MBeanServerLocator can be serialized and passed across the network,
30  * as long as the target server has access back to the MBeanServer via
31  * JMX Remoting Connector and has been detected by a JMX Remoting Detector. <P>
32  *
33  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
34  * @version $Revision: 30392 $
35  */

36 public class MBeanServerLocator implements Serializable JavaDoc
37 {
38    static final long serialVersionUID = 7632696197699845344L;
39    
40    private final Identity identity;
41    private boolean autoLocate = true;
42    protected transient MBeanServer JavaDoc server;
43
44    public MBeanServerLocator(Identity identity)
45    {
46       this.identity = identity;
47    }
48
49    public int hashCode()
50    {
51       return identity.hashCode();
52    }
53
54    public boolean equals(Object JavaDoc obj)
55    {
56       if(obj instanceof MBeanServerLocator)
57       {
58          return identity.equals(((MBeanServerLocator) obj).identity);
59       }
60       return false;
61    }
62
63    public String JavaDoc toString()
64    {
65       return "MBeanServerLocator [" + identity.getJMXId() + "]";
66    }
67
68    /**
69     * return the MBeanServer ID
70     *
71     * @return
72     */

73    public String JavaDoc getServerId()
74    {
75       return identity.getJMXId();
76    }
77
78    /**
79     * return the identity of the server
80     *
81     * @return
82     */

83    public final Identity getIdentity()
84    {
85       return identity;
86    }
87
88    /**
89     * return the MBeanServer InstanceID
90     *
91     * @return
92     */

93    public String JavaDoc getInstanceId()
94    {
95       return identity.getInstanceId();
96    }
97
98    /**
99     * return the InetAddress for the MBeanServer
100     *
101     * @return
102     */

103    public InetAddress JavaDoc getAddress()
104    {
105       return identity.getAddress();
106    }
107
108    /**
109     * return a proxy to the MBeanServer
110     *
111     * @return
112     */

113    public MBeanServer JavaDoc getMBeanServer()
114    {
115       // we use a weak reference so that if the MBeanServer is available to
116
// be garbage collected, we shouldn't stand in the way ... and hold a strong
117
// reference to it
118
if(server == null)
119       {
120          // try to get a reference
121
server = resolveServer();
122       }
123
124       if(server == null)
125       {
126          // if the reference is still null, return null
127
throw new ConnectionFailedException("Couldn't find server at: " + identity);
128       }
129       else
130       {
131          return server;
132       }
133    }
134
135    /**
136     * try and resolve the serverid to a MBeanServer instance or proxy to a
137     * remote server
138     *
139     * @return
140     */

141    protected MBeanServer JavaDoc resolveServer() throws ConnectionFailedException
142    {
143       Object JavaDoc server = null;
144       String JavaDoc id = System.getProperty("jboss.identity");
145       if(id != null && id.equals(identity.getInstanceId()))
146       {
147          // it's local
148
ArrayList JavaDoc list = MBeanServerFactory.findMBeanServer(null);
149          if(list.isEmpty() == false)
150          {
151             Iterator JavaDoc iter = list.iterator();
152             while(iter.hasNext())
153             {
154                MBeanServer JavaDoc s = (MBeanServer JavaDoc) iter.next();
155                try
156                {
157                   if(JMXUtil.getServerId(s).equals(identity.getJMXId()))
158                   {
159                      return s;
160                   }
161                }
162                catch(Exception JavaDoc ex)
163                {
164                }
165             }
166          }
167       }
168       // try and get from registry
169
server = MBeanServerRegistry.getMBeanServerFor(getServerId());
170       if(server == null)
171       {
172          // not in registry, query for the network instance and try and create
173
NetworkInstance ni[] = NetworkRegistry.getInstance().queryServers(new IdentityFilter(identity));
174          if(ni != null && ni.length > 0)
175          {
176             InvokerLocator locators[] = ni[0].getLocators();
177             String JavaDoc jmxId = ni[0].getIdentity().getJMXId();
178             ArrayList JavaDoc list = MBeanServerFactory.findMBeanServer(jmxId);
179             if(list != null && !list.isEmpty())
180             {
181                return (MBeanServer JavaDoc) list.get(0);
182             }
183             try
184             {
185                server = MBeanTransportPreference.getServerByTransport(identity, locators);
186             }
187             catch(Exception JavaDoc e)
188             {
189                e.printStackTrace();
190             }
191          }
192       }
193       return (server == null) ? null : (MBeanServer JavaDoc) server;
194    }
195
196    /**
197     * resolve the server object automagically on deserialization
198     *
199     * @param stream
200     * @throws java.io.IOException
201     * @throws java.lang.ClassNotFoundException
202     *
203     */

204    private void readObject(java.io.ObjectInputStream JavaDoc stream)
205          throws IOException JavaDoc, ClassNotFoundException JavaDoc
206    {
207       stream.defaultReadObject();
208       if(autoLocate)
209       {
210          resolveServer();
211       }
212    }
213
214    /**
215     * set true (default) to automatically locate the appropriate MBeanServer on deserialization
216     * or false to only locate on demand to the call to <tt>getMBeanServer</tt>.
217     *
218     * @param autoLocate
219     */

220    public void setAutoLocate(boolean autoLocate)
221    {
222       this.autoLocate = autoLocate;
223    }
224 }
225
Popular Tags