KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > local > LocalConnectorServer


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.remote.local;
10
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.remote.JMXServiceURL JavaDoc;
17
18 import mx4j.remote.ConnectionResolver;
19 import mx4j.tools.remote.AbstractJMXConnectorServer;
20
21 /**
22  * @version $Revision: 1.8 $
23  */

24 public class LocalConnectorServer extends AbstractJMXConnectorServer
25 {
26    private static Map JavaDoc instances = new HashMap JavaDoc();
27
28    public static LocalConnectionManager find(JMXServiceURL JavaDoc url)
29    {
30       synchronized (LocalConnectorServer.class)
31       {
32          return (LocalConnectionManager)instances.get(url);
33       }
34    }
35
36    private MBeanServer JavaDoc mbeanServer;
37    private LocalConnectionManager connectionManager;
38
39    public LocalConnectorServer(JMXServiceURL JavaDoc url, Map JavaDoc environment, MBeanServer JavaDoc server)
40    {
41       super(url, environment, server);
42    }
43
44    public MBeanServer JavaDoc getMBeanServer()
45    {
46       return mbeanServer;
47    }
48
49    protected void doStart() throws IOException JavaDoc
50    {
51       JMXServiceURL JavaDoc address = getAddress();
52       String JavaDoc protocol = address.getProtocol();
53       Map JavaDoc environment = getEnvironment();
54       ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment);
55       if (resolver == null) throw new MalformedURLException JavaDoc("Unsupported protocol: " + protocol);
56
57       MBeanServer JavaDoc realServer = null;
58       MBeanServer JavaDoc server = super.getMBeanServer();
59
60       MBeanServer JavaDoc resolvedServer = (MBeanServer JavaDoc)resolver.createServer(address, environment);
61       if (resolvedServer == null)
62       {
63          if (server == null) throw new IllegalStateException JavaDoc("This LocalConnectorServer is not attached to an MBeanServer");
64          realServer = server;
65       }
66       else
67       {
68          if (server == null)
69          {
70             realServer = resolvedServer;
71          }
72          else
73          {
74             if (server != resolvedServer) throw new IllegalStateException JavaDoc("This LocalConnectorServer cannot be attached to 2 MBeanServers");
75             realServer = server;
76          }
77       }
78       this.mbeanServer = realServer;
79
80       connectionManager = new LocalConnectionManager(this, environment);
81
82       setAddress(resolver.bindServer(realServer, address, environment));
83
84       // Here is where we give to clients the possibility to access us
85
register(getAddress(), connectionManager);
86    }
87
88    private void register(JMXServiceURL JavaDoc url, LocalConnectionManager manager) throws IOException JavaDoc
89    {
90       synchronized (LocalConnectorServer.class)
91       {
92          if (instances.get(url) != null) throw new IOException JavaDoc("A LocalConnectorServer is already serving at address " + url);
93          instances.put(url, manager);
94       }
95    }
96
97    protected void doStop() throws IOException JavaDoc
98    {
99       connectionManager.close();
100
101       JMXServiceURL JavaDoc address = getAddress();
102       String JavaDoc protocol = address.getProtocol();
103       Map JavaDoc environment = getEnvironment();
104       ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment);
105       if (resolver == null) throw new MalformedURLException JavaDoc("Unsupported protocol: " + protocol);
106       MBeanServer JavaDoc server = getMBeanServer();
107       resolver.unbindServer(server, address, environment);
108       resolver.destroyServer(server, address, environment);
109
110       unregister(address);
111    }
112
113    private void unregister(JMXServiceURL JavaDoc url) throws IOException JavaDoc
114    {
115       synchronized (LocalConnectorServer.class)
116       {
117          Object JavaDoc removed = instances.remove(url);
118          if (removed == null) throw new IOException JavaDoc("No LocalConnectorServer is present for address " + url);
119       }
120    }
121 }
122
Popular Tags