1 9 package org.jboss.mx.remoting; 10 11 import java.io.IOException ; 12 import java.io.Serializable ; 13 import java.net.InetAddress ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 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 36 public class MBeanServerLocator implements Serializable 37 { 38 static final long serialVersionUID = 7632696197699845344L; 39 40 private final Identity identity; 41 private boolean autoLocate = true; 42 protected transient MBeanServer 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 obj) 55 { 56 if(obj instanceof MBeanServerLocator) 57 { 58 return identity.equals(((MBeanServerLocator) obj).identity); 59 } 60 return false; 61 } 62 63 public String toString() 64 { 65 return "MBeanServerLocator [" + identity.getJMXId() + "]"; 66 } 67 68 73 public String getServerId() 74 { 75 return identity.getJMXId(); 76 } 77 78 83 public final Identity getIdentity() 84 { 85 return identity; 86 } 87 88 93 public String getInstanceId() 94 { 95 return identity.getInstanceId(); 96 } 97 98 103 public InetAddress getAddress() 104 { 105 return identity.getAddress(); 106 } 107 108 113 public MBeanServer getMBeanServer() 114 { 115 if(server == null) 119 { 120 server = resolveServer(); 122 } 123 124 if(server == null) 125 { 126 throw new ConnectionFailedException("Couldn't find server at: " + identity); 128 } 129 else 130 { 131 return server; 132 } 133 } 134 135 141 protected MBeanServer resolveServer() throws ConnectionFailedException 142 { 143 Object server = null; 144 String id = System.getProperty("jboss.identity"); 145 if(id != null && id.equals(identity.getInstanceId())) 146 { 147 ArrayList list = MBeanServerFactory.findMBeanServer(null); 149 if(list.isEmpty() == false) 150 { 151 Iterator iter = list.iterator(); 152 while(iter.hasNext()) 153 { 154 MBeanServer s = (MBeanServer ) iter.next(); 155 try 156 { 157 if(JMXUtil.getServerId(s).equals(identity.getJMXId())) 158 { 159 return s; 160 } 161 } 162 catch(Exception ex) 163 { 164 } 165 } 166 } 167 } 168 server = MBeanServerRegistry.getMBeanServerFor(getServerId()); 170 if(server == null) 171 { 172 NetworkInstance ni[] = NetworkRegistry.getInstance().queryServers(new IdentityFilter(identity)); 174 if(ni != null && ni.length > 0) 175 { 176 InvokerLocator locators[] = ni[0].getLocators(); 177 String jmxId = ni[0].getIdentity().getJMXId(); 178 ArrayList list = MBeanServerFactory.findMBeanServer(jmxId); 179 if(list != null && !list.isEmpty()) 180 { 181 return (MBeanServer ) list.get(0); 182 } 183 try 184 { 185 server = MBeanTransportPreference.getServerByTransport(identity, locators); 186 } 187 catch(Exception e) 188 { 189 e.printStackTrace(); 190 } 191 } 192 } 193 return (server == null) ? null : (MBeanServer ) server; 194 } 195 196 204 private void readObject(java.io.ObjectInputStream stream) 205 throws IOException , ClassNotFoundException 206 { 207 stream.defaultReadObject(); 208 if(autoLocate) 209 { 210 resolveServer(); 211 } 212 } 213 214 220 public void setAutoLocate(boolean autoLocate) 221 { 222 this.autoLocate = autoLocate; 223 } 224 } 225 | Popular Tags |