001: /**********************************************************************
002: Copyright (c) 2006 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: ***********************************************************************/package org.jpox.management;
018:
019: import org.jpox.OMFContext;
020: import org.jpox.exceptions.JPOXException;
021: import org.jpox.util.JPOXLogger;
022: import org.jpox.util.StringUtils;
023:
024: /**
025: * Management interface for JPOX. Management operations and attributes
026: * are exposed through this interface that holds statistics linked to a PMF instance.
027: *
028: * The mechanics for starting and stopping JMX servers are not defined here,
029: * and must be done by plug-ins, by providing the implementation of
030: * {@link ManagementServer}.
031: *
032: * This Manager controls the lifecycle of management servers.
033: *
034: * A management server is started when an instance of this class is created,
035: * and its shutdown when the close operation is invoked
036: *
037: * The management server startup is triggered when the Manager gets enabled.
038: */
039: public class ManagementManager {
040: /** the ObjectManagerFactory context that owns this management **/
041: final private OMFContext omfContext;
042:
043: /** whether this is closed **/
044: private boolean closed = false;
045:
046: /** the Management Server **/
047: private ManagementServer mgmtServer;
048:
049: /**
050: * Constructor for Management
051: * @param ctxt the ObjectManagerFactory context that we are operating for
052: */
053: public ManagementManager(OMFContext ctxt) {
054: this .omfContext = ctxt;
055: startManagementServer();
056: }
057:
058: /**
059: * Object the Management Server
060: * @return the Management Server
061: */
062: public ManagementServer getManagementServer() {
063: return mgmtServer;
064: }
065:
066: /**
067: * Whether this Manager is not closed
068: * @return true if this Manager is open
069: */
070: public boolean isOpen() {
071: return !closed;
072: }
073:
074: /**
075: * Close a instance.
076: * @throws JPOXException if the manager is closed
077: */
078: public synchronized void close() {
079: assertNotClosed();
080: stopManagementServer();
081: this .closed = true;
082: }
083:
084: /**
085: * Assert that this instance is open
086: */
087: private void assertNotClosed() {
088: if (this .closed) {
089: throw new JPOXException(
090: "Management instance is closed and cannot be used. You must adquire a new OMFContext")
091: .setFatal();
092: }
093: }
094:
095: /**
096: * Start Management Server
097: */
098: private void startManagementServer() {
099: if (mgmtServer == null) {
100: try {
101: mgmtServer = (ManagementServer) omfContext
102: .getPluginManager().createExecutableExtension(
103: "org.jpox.management_server", "name",
104: "default", "class", null, null);
105: if (mgmtServer != null) {
106: JPOXLogger.MANAGEMENT
107: .info("Starting Management Server");
108: mgmtServer.start();
109: }
110: } catch (Exception e) {
111: mgmtServer = null;
112: JPOXLogger.MANAGEMENT
113: .error("Error instantiating or connecting to Management Server : "
114: + StringUtils
115: .getStringFromStackTrace(e));
116: }
117: }
118: }
119:
120: /**
121: * Shutdown Management Server
122: */
123: private void stopManagementServer() {
124: if (mgmtServer != null) {
125: JPOXLogger.MANAGEMENT.info("Stopping Management Server");
126: mgmtServer.stop();
127: }
128: }
129: }
|