KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collections JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.remote.JMXConnectorServer JavaDoc;
16 import javax.management.remote.JMXServiceURL JavaDoc;
17
18 import mx4j.log.Log;
19 import mx4j.log.Logger;
20 import mx4j.remote.MX4JRemoteUtils;
21
22 /**
23  * Extends the implementation of JMXConnectorServer by implementing most
24  * JMXConnectorServer methods following the JSR 160 specification and delegating
25  * implementation specific operations using the template method pattern.
26  *
27  * @version $Revision: 1.8 $
28  * @see ConnectionManager
29  */

30 public abstract class AbstractJMXConnectorServer extends JMXConnectorServer JavaDoc
31 {
32    private JMXServiceURL JavaDoc url;
33    private final Map JavaDoc environment;
34    private volatile boolean active;
35    private volatile boolean stopped;
36
37    public AbstractJMXConnectorServer(JMXServiceURL JavaDoc url, Map JavaDoc environment, MBeanServer JavaDoc server)
38    {
39       super(server);
40       this.url = url;
41       this.environment = environment;
42    }
43
44    public synchronized JMXServiceURL JavaDoc getAddress()
45    {
46       return url;
47    }
48
49    /**
50     * Sets the JMXServiceURL that represent the address of this JMXConnectorServer
51     */

52    protected synchronized void setAddress(JMXServiceURL JavaDoc url)
53    {
54       this.url = url;
55    }
56
57    public synchronized Map JavaDoc getAttributes()
58    {
59       Map JavaDoc env = MX4JRemoteUtils.removeNonSerializableEntries(getEnvironment());
60       return Collections.unmodifiableMap(env);
61    }
62
63    /**
64     * Returns the environment Map as is, without removing non-serializable entries like {@link #getAttributes} does.
65     */

66    protected synchronized Map JavaDoc getEnvironment()
67    {
68       return environment;
69    }
70
71    public boolean isActive()
72    {
73       return active;
74    }
75
76    /**
77     * Returns whether the {@link #stop} method of this JMXConnectorServer has been called.
78     */

79    protected boolean isStopped()
80    {
81       return stopped;
82    }
83
84    public synchronized void start() throws IOException JavaDoc, IllegalStateException JavaDoc
85    {
86       Logger logger = getLogger();
87
88       if (isActive())
89       {
90          if (logger.isEnabledFor(Logger.TRACE)) logger.trace("This JMXConnectorServer has already been started");
91          return;
92       }
93       if (isStopped())
94       {
95          if (logger.isEnabledFor(Logger.TRACE)) logger.trace("This JMXConnectorServer has already been stopped");
96          throw new IOException JavaDoc("This RMIConnectorServer has already been stopped");
97       }
98
99       doStart();
100
101       active = true;
102
103       if (logger.isEnabledFor(Logger.INFO)) logger.info("JMXConnectorServer started at: " + getAddress());
104    }
105
106    /**
107     * Template method implemented by subclasses to start this JMXConnectorServer
108     */

109    protected abstract void doStart() throws IOException JavaDoc, IllegalStateException JavaDoc;
110
111    public synchronized void stop() throws IOException JavaDoc
112    {
113       if (!isActive() || isStopped()) return;
114
115       stopped = true;
116       active = false;
117
118       doStop();
119
120       Logger logger = getLogger();
121       if (logger.isEnabledFor(Logger.INFO)) logger.info("JMXConnectorServer stopped at: " + getAddress());
122    }
123
124    /**
125     * Template method implemented by subclasses to stop this JMXConnectorServer
126     */

127    protected abstract void doStop() throws IOException JavaDoc;
128
129    protected Logger getLogger()
130    {
131       return Log.getLogger(getClass().getName());
132    }
133
134    public void connectionOpened(String JavaDoc connectionId, String JavaDoc message, Object JavaDoc userData)
135    {
136       super.connectionOpened(connectionId, message, userData);
137    }
138
139    public void connectionClosed(String JavaDoc connectionId, String JavaDoc message, Object JavaDoc userData)
140    {
141       super.connectionClosed(connectionId, message, userData);
142    }
143
144    public void connectionFailed(String JavaDoc connectionId, String JavaDoc message, Object JavaDoc userData)
145    {
146       super.connectionFailed(connectionId, message, userData);
147    }
148 }
149
Popular Tags