001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.deploy;
031:
032: import com.caucho.lifecycle.Lifecycle;
033: import com.caucho.lifecycle.LifecycleListener;
034: import com.caucho.lifecycle.LifecycleNotification;
035: import com.caucho.management.server.AbstractManagedObject;
036: import com.caucho.management.server.DeployControllerMXBean;
037: import com.caucho.util.Alarm;
038:
039: import javax.management.ListenerNotFoundException;
040: import javax.management.MBeanNotificationInfo;
041: import javax.management.NotificationBroadcasterSupport;
042: import javax.management.NotificationEmitter;
043: import javax.management.NotificationFilter;
044: import javax.management.NotificationListener;
045: import java.util.Date;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048:
049: /**
050: * A deploy controller for an environment.
051: */
052: abstract public class DeployControllerAdmin<C extends EnvironmentDeployController>
053: extends AbstractManagedObject implements
054: DeployControllerMXBean, NotificationEmitter, LifecycleListener,
055: java.io.Serializable {
056: private transient final C _controller;
057:
058: // XXX: why transient?
059: private transient final NotificationBroadcasterSupport _broadcaster;
060:
061: private long _sequence = 0;
062:
063: public DeployControllerAdmin(C controller) {
064: _controller = controller;
065: _broadcaster = new NotificationBroadcasterSupport();
066: controller.addLifecycleListener(this );
067: }
068:
069: /**
070: * Returns the controller.
071: */
072: protected C getController() {
073: return _controller;
074: }
075:
076: protected void register() {
077: registerSelf();
078: }
079:
080: protected void unregister() {
081: unregisterSelf();
082: }
083:
084: public String getName() {
085: return _controller.getMBeanId();
086: }
087:
088: public String getStartupMode() {
089: return _controller.getStartupMode();
090: }
091:
092: public String getRedeployMode() {
093: return _controller.getRedeployMode();
094: }
095:
096: public long getRedeployCheckInterval() {
097: return _controller.getRedeployCheckInterval();
098: }
099:
100: public String getState() {
101: return getController().getState();
102: }
103:
104: public Date getStartTime() {
105: return new Date(getController().getStartTime());
106: }
107:
108: /**
109: * Starts the controller.
110: */
111: public void start() throws Exception {
112: getController().start();
113: }
114:
115: public void stop() throws Exception {
116: getController().stop();
117: }
118:
119: public void restart() throws Exception {
120: getController().stop();
121: getController().start();
122: }
123:
124: public void update() throws Exception {
125: getController().update();
126: }
127:
128: /**
129: * Returns the root directory
130: */
131: public String getRootDirectory() {
132: return _controller.getRootDirectory().getNativePath();
133: }
134:
135: public void addNotificationListener(NotificationListener listener,
136: NotificationFilter filter, Object handback)
137: throws IllegalArgumentException {
138: _broadcaster
139: .addNotificationListener(listener, filter, handback);
140: }
141:
142: public void removeNotificationListener(NotificationListener listener)
143: throws ListenerNotFoundException {
144: _broadcaster.removeNotificationListener(listener);
145: }
146:
147: public void removeNotificationListener(
148: NotificationListener listener, NotificationFilter filter,
149: Object handback) throws ListenerNotFoundException {
150: _broadcaster.removeNotificationListener(listener, filter,
151: handback);
152: }
153:
154: public MBeanNotificationInfo[] getNotificationInfo() {
155: // XXX: temporary hack
156: MBeanNotificationInfo status = new MBeanNotificationInfo(
157: new String[] { "jmx.attribute.change" }, "status",
158: "status attribute changes");
159:
160: return new MBeanNotificationInfo[] { status };
161: }
162:
163: synchronized public void lifecycleEvent(int oldState, int newState) {
164: Logger log = _controller.getLog();
165:
166: long timestamp = Alarm.getCurrentTime();
167:
168: String oldValue = Lifecycle.getStateName(oldState);
169: String newValue = Lifecycle.getStateName(newState);
170: String message = newValue;
171:
172: if (log.isLoggable(Level.FINEST))
173: log.log(Level.FINEST, toString() + " lifecycleEvent `"
174: + newValue + "'");
175:
176: if (newState == Lifecycle.IS_ACTIVE) {
177: LifecycleNotification notif;
178: notif = new LifecycleNotification(
179: LifecycleNotification.AFTER_START, this ,
180: _sequence++, timestamp, toString() + " started");
181:
182: _broadcaster.sendNotification(notif);
183: }
184:
185: if (oldState == Lifecycle.IS_ACTIVE) {
186: LifecycleNotification notif;
187: notif = new LifecycleNotification(
188: LifecycleNotification.BEFORE_STOP, this ,
189: _sequence++, timestamp, toString() + " stopping");
190:
191: _broadcaster.sendNotification(notif);
192: }
193:
194: /*
195: AttributeChangeNotification notification
196: = new AttributeChangeNotification(this, _sequence++,
197: timestamp, message, "State", "java.lang.String", oldValue, newValue);
198:
199: _broadcaster.sendNotification(notification);
200: */
201: }
202:
203: public String toString() {
204: String name = getClass().getName();
205:
206: int i = name.lastIndexOf('.') + 1;
207:
208: if (i > 0 && i < name.length())
209: name = name.substring(i);
210:
211: return name + "[" + getObjectName() + "]";
212: }
213: }
|