KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > deploy > DeployControllerAdmin


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.deploy;
31
32 import com.caucho.lifecycle.Lifecycle;
33 import com.caucho.lifecycle.LifecycleListener;
34 import com.caucho.lifecycle.LifecycleNotification;
35 import com.caucho.management.server.AbstractManagedObject;
36 import com.caucho.management.server.DeployControllerMXBean;
37 import com.caucho.util.Alarm;
38
39 import javax.management.ListenerNotFoundException JavaDoc;
40 import javax.management.MBeanNotificationInfo JavaDoc;
41 import javax.management.NotificationBroadcasterSupport JavaDoc;
42 import javax.management.NotificationEmitter JavaDoc;
43 import javax.management.NotificationFilter JavaDoc;
44 import javax.management.NotificationListener JavaDoc;
45 import java.util.Date JavaDoc;
46 import java.util.logging.Level JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49 /**
50  * A deploy controller for an environment.
51  */

52 abstract public class DeployControllerAdmin<C extends EnvironmentDeployController>
53   extends AbstractManagedObject
54   implements DeployControllerMXBean,
55              NotificationEmitter JavaDoc,
56              LifecycleListener,
57              java.io.Serializable JavaDoc
58 {
59   private transient final C _controller;
60
61   // XXX: why transient?
62
private transient final NotificationBroadcasterSupport JavaDoc _broadcaster;
63
64   private long _sequence = 0;
65
66   public DeployControllerAdmin(C controller)
67   {
68     _controller = controller;
69     _broadcaster = new NotificationBroadcasterSupport JavaDoc();
70     controller.addLifecycleListener(this);
71   }
72
73   /**
74    * Returns the controller.
75    */

76   protected C getController()
77   {
78     return _controller;
79   }
80
81   protected void register()
82   {
83     registerSelf();
84   }
85
86   protected void unregister()
87   {
88     unregisterSelf();
89   }
90
91   public String JavaDoc getName()
92   {
93     return _controller.getMBeanId();
94   }
95
96   public String JavaDoc getStartupMode()
97   {
98     return _controller.getStartupMode();
99   }
100
101   public String JavaDoc getRedeployMode()
102   {
103     return _controller.getRedeployMode();
104   }
105
106   public long getRedeployCheckInterval()
107   {
108     return _controller.getRedeployCheckInterval();
109   }
110
111   public String JavaDoc getState()
112   {
113     return getController().getState();
114   }
115
116   public Date JavaDoc getStartTime()
117   {
118     return new Date JavaDoc(getController().getStartTime());
119   }
120
121   /**
122    * Starts the controller.
123    */

124   public void start()
125     throws Exception JavaDoc
126   {
127     getController().start();
128   }
129
130   public void stop()
131     throws Exception JavaDoc
132   {
133     getController().stop();
134   }
135
136   public void restart()
137     throws Exception JavaDoc
138   {
139     getController().stop();
140     getController().start();
141   }
142
143   public void update()
144     throws Exception JavaDoc
145   {
146     getController().update();
147   }
148
149   /**
150    * Returns the root directory
151    */

152   public String JavaDoc getRootDirectory()
153   {
154     return _controller.getRootDirectory().getNativePath();
155   }
156
157   public void addNotificationListener(NotificationListener JavaDoc listener,
158                                       NotificationFilter JavaDoc filter,
159                                       Object JavaDoc handback)
160     throws IllegalArgumentException JavaDoc
161   {
162     _broadcaster.addNotificationListener(listener, filter, handback);
163   }
164
165   public void removeNotificationListener(NotificationListener JavaDoc listener)
166     throws ListenerNotFoundException JavaDoc
167   {
168     _broadcaster.removeNotificationListener(listener);
169   }
170
171   public void removeNotificationListener(NotificationListener JavaDoc listener,
172                                          NotificationFilter JavaDoc filter,
173                                          Object JavaDoc handback)
174     throws ListenerNotFoundException JavaDoc
175   {
176     _broadcaster.removeNotificationListener(listener, filter, handback);
177   }
178
179   public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
180   {
181     // XXX: temporary hack
182
MBeanNotificationInfo JavaDoc status = new MBeanNotificationInfo JavaDoc(new String JavaDoc[] { "jmx.attribute.change" }, "status", "status attribute changes");
183
184     return new MBeanNotificationInfo JavaDoc[] { status };
185   }
186
187   synchronized public void lifecycleEvent(int oldState, int newState)
188   {
189     Logger JavaDoc log = _controller.getLog();
190
191     long timestamp = Alarm.getCurrentTime();
192
193     String JavaDoc oldValue = Lifecycle.getStateName(oldState);
194     String JavaDoc newValue = Lifecycle.getStateName(newState);
195     String JavaDoc message = newValue;
196
197     if (log.isLoggable(Level.FINEST))
198       log.log(Level.FINEST, toString() + " lifecycleEvent `" + newValue + "'");
199
200     if (newState == Lifecycle.IS_ACTIVE) {
201       LifecycleNotification notif;
202       notif = new LifecycleNotification(LifecycleNotification.AFTER_START,
203                                         this, _sequence++, timestamp,
204                                         toString () + " started");
205
206       _broadcaster.sendNotification(notif);
207     }
208
209     if (oldState == Lifecycle.IS_ACTIVE) {
210       LifecycleNotification notif;
211       notif = new LifecycleNotification(LifecycleNotification.BEFORE_STOP,
212                                         this, _sequence++, timestamp,
213                                         toString() + " stopping");
214
215       _broadcaster.sendNotification(notif);
216     }
217
218     /*
219     AttributeChangeNotification notification
220       = new AttributeChangeNotification(this, _sequence++,
221                     timestamp, message, "State", "java.lang.String", oldValue, newValue);
222
223     _broadcaster.sendNotification(notification);
224     */

225   }
226
227   public String JavaDoc toString()
228   {
229     String JavaDoc name = getClass().getName();
230
231     int i = name.lastIndexOf('.') + 1;
232
233     if (i > 0 && i < name.length())
234       name = name.substring(i);
235
236     return name + "[" + getObjectName() + "]";
237   }
238 }
239
Popular Tags