KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > AbstractJMXConnector


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;
10
11 import java.io.IOException JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.management.ListenerNotFoundException JavaDoc;
15 import javax.management.MBeanServerConnection JavaDoc;
16 import javax.management.NotificationFilter JavaDoc;
17 import javax.management.NotificationListener JavaDoc;
18 import javax.management.remote.JMXConnector JavaDoc;
19 import javax.management.remote.JMXServiceURL JavaDoc;
20 import javax.security.auth.Subject JavaDoc;
21
22 import mx4j.remote.ConnectionNotificationEmitter;
23
24 /**
25  * Abstract implementation of the JMXConnector interface.
26  * It gives support for emitting connection notifications and implements JMXConnector methods
27  * using the template method pattern.
28  *
29  * @version $Revision: 1.8 $
30  */

31 public abstract class AbstractJMXConnector implements JMXConnector JavaDoc, Serializable JavaDoc
32 {
33    /**
34     * @serial
35     */

36    private final JMXServiceURL JavaDoc address;
37    private transient boolean connected;
38    private transient boolean closed;
39    private transient ConnectionNotificationEmitter emitter;
40
41    /**
42     * Creates a new JMXConnector that will connect to the given JMXServiceURL
43     */

44    protected AbstractJMXConnector(JMXServiceURL JavaDoc address) throws IOException JavaDoc
45    {
46       if (address == null) throw new IOException JavaDoc("JMXServiceURL cannot be null");
47       this.address = address;
48    }
49
50    /**
51     * Returns the JMXServiceURL this JMXConnector will connect to.
52     */

53    protected JMXServiceURL JavaDoc getAddress()
54    {
55       return address;
56    }
57
58    public void connect() throws IOException JavaDoc, SecurityException JavaDoc
59    {
60       connect(null);
61    }
62
63    public void connect(Map JavaDoc environment) throws IOException JavaDoc, SecurityException JavaDoc
64    {
65       synchronized (this)
66       {
67          if (isConnected()) return;
68          if (isClosed()) throw new IOException JavaDoc("This connector has already been closed");
69
70          doConnect(environment);
71
72          connected = true;
73       }
74
75       sendConnectionNotificationOpened();
76    }
77
78    protected abstract void doConnect(Map JavaDoc environment) throws IOException JavaDoc, SecurityException JavaDoc;
79
80    public void close() throws IOException JavaDoc
81    {
82       synchronized (this)
83       {
84          if (isClosed()) return;
85          closed = true;
86          connected = false;
87
88          doClose();
89       }
90
91       sendConnectionNotificationClosed();
92    }
93
94    /**
95     * Template method to be implemented by subclasses to close this JMXConnector
96     */

97    protected abstract void doClose() throws IOException JavaDoc;
98
99    public MBeanServerConnection JavaDoc getMBeanServerConnection() throws IOException JavaDoc
100    {
101       return getMBeanServerConnection(null);
102    }
103
104    public MBeanServerConnection JavaDoc getMBeanServerConnection(Subject JavaDoc delegate) throws IOException JavaDoc
105    {
106       if (!isConnected()) throw new IOException JavaDoc("Connection has not been established");
107       return doGetMBeanServerConnection(delegate);
108    }
109
110    /**
111     * Template method to be implemented by subclasses to return an MBeanServerConnection
112     * for the given delegate subject.
113     * This method should return an MBeanServerConnection that delegates method calls to a
114     * {@link JMXConnection} (or an equivalent client side connection object).
115     * The JMXConnection object to which calls are delegated can in turn be a chain of
116     * objects that decorate the call performing some other operation; the final object in
117     * the chain is the one that really communicates with the server side, and it is normally
118     * called <protocol>ClientInvoker.
119     */

120    protected abstract MBeanServerConnection JavaDoc doGetMBeanServerConnection(Subject JavaDoc delegate) throws IOException JavaDoc;
121
122    public void addConnectionNotificationListener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
123    {
124       getConnectionNotificationEmitter().addNotificationListener(listener, filter, handback);
125    }
126
127    public void removeConnectionNotificationListener(NotificationListener JavaDoc listener) throws ListenerNotFoundException JavaDoc
128    {
129       getConnectionNotificationEmitter().removeNotificationListener(listener);
130    }
131
132    public void removeConnectionNotificationListener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws ListenerNotFoundException JavaDoc
133    {
134       getConnectionNotificationEmitter().removeNotificationListener(listener, filter, handback);
135    }
136
137    private void sendConnectionNotificationOpened()
138    {
139       getConnectionNotificationEmitter().sendConnectionNotificationOpened();
140    }
141
142    protected void sendConnectionNotificationClosed()
143    {
144       getConnectionNotificationEmitter().sendConnectionNotificationClosed();
145    }
146
147    /**
148     * Creates a notification emitter used to emit connection notifications.
149     * This method is called once per JMXConnector.
150     */

151    protected ConnectionNotificationEmitter createConnectionNotificationEmitter()
152    {
153       return new ConnectionNotificationEmitter(this);
154    }
155
156    protected ConnectionNotificationEmitter getConnectionNotificationEmitter()
157    {
158       synchronized (this)
159       {
160          if (emitter == null) emitter = createConnectionNotificationEmitter();
161       }
162       return emitter;
163    }
164
165    /**
166     * Returns whether the {@link #connect} or {@link #connect(Map)} method has been called on this JMXConnector.
167     */

168    protected synchronized boolean isConnected()
169    {
170       return connected;
171    }
172
173    /**
174     * Returns whether the {@link #close} method has been called.
175     */

176    protected synchronized boolean isClosed()
177    {
178       return closed;
179    }
180 }
181
Popular Tags