001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.service;
009:
010: import javax.management.ObjectName;
011: import javax.management.MBeanServer;
012: import javax.management.Notification;
013: import javax.management.AttributeChangeNotification;
014:
015: /**
016: *
017: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
018: */
019:
020: public abstract class ServiceSupport extends ComponentSupport implements
021: ServiceSupportMBean {
022:
023: public ServiceSupport() {
024: super ();
025: }
026:
027: public ServiceSupport(String name) {
028: super (name);
029: }
030:
031: public boolean isRunning() {
032: return state == State.STARTED;
033: }
034:
035: public void start() throws Exception {
036: if (!State.canStart(state)) {
037: logger.warn(name + " can not start, state = " + state);
038: return;
039: }
040:
041: logger.info("starting...");
042:
043: sendNotification(new AttributeChangeNotification(this ,
044: sequence++, System.currentTimeMillis(), getName()
045: + " starting", "State", "java.lang.Integer",
046: new Integer(state.getId()), new Integer(State.STARTING
047: .getId())));
048: state = State.STARTING;
049:
050: try {
051: doStart();
052: } catch (Exception e) {
053: sendNotification(new AttributeChangeNotification(this ,
054: sequence++, System.currentTimeMillis(), getName()
055: + " starting failed", "State",
056: "java.lang.Integer", new Integer(state.getId()),
057: new Integer(State.INTERRUPTED.getId())));
058: state = State.INTERRUPTED;
059: logger.error("starting failed", e);
060: throw e;
061: }
062: sendNotification(new AttributeChangeNotification(this ,
063: sequence++, System.currentTimeMillis(), getName()
064: + " started", "State", "java.lang.Integer",
065: new Integer(state.getId()), new Integer(State.STARTED
066: .getId())));
067: state = State.STARTED;
068: logger.info("started.");
069: }
070:
071: public void stop() throws Exception {
072: if (!State.canStop(state)) {
073: logger.warn(name + " can not stop, state = " + state);
074: return;
075: }
076:
077: logger.info("stopping...");
078:
079: //AS It seems that the first attribute is not needed anymore and use a long instead of a Date
080: sendNotification(new AttributeChangeNotification(this ,
081: sequence++, System.currentTimeMillis(), getName()
082: + " stopping", "State", "java.lang.Integer",
083: new Integer(state.getId()), new Integer(State.STOPPING
084: .getId())));
085: state = State.STOPPING;
086:
087: try {
088: doStop();
089: } catch (Exception e) {
090: sendNotification(new AttributeChangeNotification(this ,
091: sequence++, System.currentTimeMillis(), getName()
092: + " stopping failed", "State",
093: "java.lang.Integer", new Integer(state.getId()),
094: new Integer(State.INTERRUPTED.getId())));
095: state = State.INTERRUPTED;
096: logger.error("stopping failed", e);
097: throw e;
098: }
099: sendNotification(new AttributeChangeNotification(this ,
100: sequence++, System.currentTimeMillis(), getName()
101: + " stopped", "State", "java.lang.Integer",
102: new Integer(state.getId()), new Integer(State.STOPPED
103: .getId())));
104: state = State.STOPPED;
105: logger.info("stopped");
106:
107: }
108:
109: /**
110: * ObjectName must specified in the MLet, or use registerMBean specified explicitly
111: */
112: public ObjectName preRegister(MBeanServer server, ObjectName name)
113: throws Exception {
114: if (name == null)
115: throw new IllegalArgumentException(
116: "ObjectName can not be null.");
117:
118: // get log service instance
119: this .server = server;
120: objectName = name;
121:
122: return name;
123: }
124:
125: public void postRegister(Boolean registrationDone) {
126: if (!registrationDone.booleanValue()) {
127: try {
128: this .stop();
129: this .destroy();
130: } catch (Exception e) {
131: logger.error(e.getMessage(), e);
132: }
133: } else {
134: // initilize & start
135: try {
136: this .init();
137: this .start();
138: } catch (Exception e) {
139: logger.error(e.getMessage(), e);
140: }
141:
142: }
143: }
144:
145: public void preDeregister() throws Exception {
146:
147: }
148:
149: public void postDeregister() {
150: try {
151: stop();
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155:
156: }
157:
158: public void handleNotification(Notification notification, Object obj) {
159: logger.info("Recieved notification, type: "
160: + notification.getType() + ", " + "message: "
161: + notification.getMessage() + ", " + "source: "
162: + notification.getSource());
163: }
164:
165: /**
166: * do actually start action
167: */
168: protected abstract void doStart() throws Exception;
169:
170: /**
171: * do actually stop action
172: */
173: protected abstract void doStop() throws Exception;
174:
175: }
|