KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > beans > TCServerInfo


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.management.beans;
5
6 import com.tc.config.schema.L2Info;
7 import com.tc.management.AbstractTerracottaMBean;
8 import com.tc.server.TCServer;
9 import com.tc.server.TCServerActivationListener;
10 import com.tc.util.ProductInfo;
11
12 import java.util.Timer JavaDoc;
13 import java.util.TimerTask JavaDoc;
14
15 import javax.management.AttributeChangeNotification JavaDoc;
16 import javax.management.MBeanNotificationInfo JavaDoc;
17 import javax.management.NotCompliantMBeanException JavaDoc;
18
19 public class TCServerInfo extends AbstractTerracottaMBean implements TCServerInfoMBean, TCServerActivationListener {
20
21   private static final MBeanNotificationInfo JavaDoc[] NOTIFICATION_INFO;
22   static {
23     final String JavaDoc[] notifTypes = new String JavaDoc[] { AttributeChangeNotification.ATTRIBUTE_CHANGE };
24     final String JavaDoc name = AttributeChangeNotification JavaDoc.class.getName();
25     final String JavaDoc description = "An attribute of this MBean has changed";
26     NOTIFICATION_INFO = new MBeanNotificationInfo JavaDoc[] { new MBeanNotificationInfo JavaDoc(notifTypes, name, description) };
27   }
28
29   private final TCServer server;
30   private final ProductInfo productInfo;
31   private final String JavaDoc buildID;
32   private long nextSequenceNumber;
33
34   public TCServerInfo(final TCServer server) throws NotCompliantMBeanException JavaDoc {
35     super(TCServerInfoMBean.class, true);
36     this.server = server;
37     productInfo = ProductInfo.getThisProductInfo();
38     buildID = makeBuildID(productInfo);
39     nextSequenceNumber = 1;
40     server.setActivationListener(this);
41   }
42   
43   public void reset() {
44     // nothing to reset
45
}
46
47   public boolean isStarted() {
48     return server.isStarted();
49   }
50
51   public boolean isActive() {
52     return server.isActive();
53   }
54
55   public long getStartTime() {
56     return server.getStartTime();
57   }
58
59   public long getActivateTime() {
60     return server.getActivateTime();
61   }
62
63   public void stop() {
64     server.stop();
65     _sendNotification("TCServer stopped", "Started", "jmx.terracotta.L2.stopped", Boolean.TRUE, Boolean.FALSE);
66   }
67
68   /**
69    * This schedules the shutdown to occur one second after we return from this call because otherwise JMX will be
70    * shutdown and we'll get all sorts of other errors trying to return from this call.
71    */

72   public void shutdown() {
73     final Timer JavaDoc timer = new Timer JavaDoc();
74     final TimerTask JavaDoc task = new TimerTask JavaDoc() {
75       public void run() {
76         server.shutdown();
77       }
78     };
79     timer.schedule(task, 1000);
80   }
81
82   public void serverActivated(TCServer theServer) {
83     _sendNotification("TCServer active", "Active", "jmx.terracotta.L2.active", Boolean.FALSE, Boolean.TRUE);
84   }
85
86   public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
87     return NOTIFICATION_INFO;
88   }
89
90   private synchronized void _sendNotification(String JavaDoc msg, String JavaDoc attr, String JavaDoc type, Object JavaDoc oldVal, Object JavaDoc newVal) {
91     sendNotification(new AttributeChangeNotification JavaDoc(this, nextSequenceNumber++, System.currentTimeMillis(), msg, attr,
92                                                      type, oldVal, newVal));
93   }
94
95   public String JavaDoc toString() {
96     if (isStarted()) {
97       return "starting, startTime(" + getStartTime() + ")";
98     } else if (isActive()) {
99       return "active, activateTime(" + getActivateTime() + ")";
100     } else {
101       return "stopped";
102     }
103   }
104
105   public String JavaDoc getVersion() {
106     return productInfo.toShortString();
107   }
108
109   public String JavaDoc getBuildID() {
110     return buildID;
111   }
112
113   public String JavaDoc getCopyright() {
114     return productInfo.copyright();
115   }
116
117   public String JavaDoc getDescriptionOfCapabilities() {
118     return server.getDescriptionOfCapabilities();
119   }
120   
121   public L2Info[] getL2Info() {
122     return server.infoForAllL2s();
123   }
124
125   private static String JavaDoc makeBuildID(final ProductInfo productInfo) {
126     String JavaDoc timeStamp = productInfo.buildTimestampAsString();
127     String JavaDoc cset = productInfo.buildChangeset();
128     String JavaDoc ctag = productInfo.buildChangeTag();
129     String JavaDoc user = productInfo.buildUser();
130     String JavaDoc host = productInfo.buildHost();
131     String JavaDoc branch = productInfo.buildBranch();
132     return timeStamp + " (" + cset + (ctag != null ? " (" + ctag + ")" : "") + " by " + user + "@" + host + " from "
133            + branch + ")";
134   }
135
136   public String JavaDoc getHealthStatus() {
137     // FIXME: the returned value should eventually contain a true representative status of L2 server.
138
// for now just return 'OK' to indicate that the process is up-and-running..
139
return "OK";
140   }
141
142 }
143
Popular Tags