1 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 ; 13 import java.util.TimerTask ; 14 15 import javax.management.AttributeChangeNotification ; 16 import javax.management.MBeanNotificationInfo ; 17 import javax.management.NotCompliantMBeanException ; 18 19 public class TCServerInfo extends AbstractTerracottaMBean implements TCServerInfoMBean, TCServerActivationListener { 20 21 private static final MBeanNotificationInfo [] NOTIFICATION_INFO; 22 static { 23 final String [] notifTypes = new String [] { AttributeChangeNotification.ATTRIBUTE_CHANGE }; 24 final String name = AttributeChangeNotification .class.getName(); 25 final String description = "An attribute of this MBean has changed"; 26 NOTIFICATION_INFO = new MBeanNotificationInfo [] { new MBeanNotificationInfo (notifTypes, name, description) }; 27 } 28 29 private final TCServer server; 30 private final ProductInfo productInfo; 31 private final String buildID; 32 private long nextSequenceNumber; 33 34 public TCServerInfo(final TCServer server) throws NotCompliantMBeanException { 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 } 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 72 public void shutdown() { 73 final Timer timer = new Timer (); 74 final TimerTask task = new TimerTask () { 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 [] getNotificationInfo() { 87 return NOTIFICATION_INFO; 88 } 89 90 private synchronized void _sendNotification(String msg, String attr, String type, Object oldVal, Object newVal) { 91 sendNotification(new AttributeChangeNotification (this, nextSequenceNumber++, System.currentTimeMillis(), msg, attr, 92 type, oldVal, newVal)); 93 } 94 95 public String 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 getVersion() { 106 return productInfo.toShortString(); 107 } 108 109 public String getBuildID() { 110 return buildID; 111 } 112 113 public String getCopyright() { 114 return productInfo.copyright(); 115 } 116 117 public String getDescriptionOfCapabilities() { 118 return server.getDescriptionOfCapabilities(); 119 } 120 121 public L2Info[] getL2Info() { 122 return server.infoForAllL2s(); 123 } 124 125 private static String makeBuildID(final ProductInfo productInfo) { 126 String timeStamp = productInfo.buildTimestampAsString(); 127 String cset = productInfo.buildChangeset(); 128 String ctag = productInfo.buildChangeTag(); 129 String user = productInfo.buildUser(); 130 String host = productInfo.buildHost(); 131 String branch = productInfo.buildBranch(); 132 return timeStamp + " (" + cset + (ctag != null ? " (" + ctag + ")" : "") + " by " + user + "@" + host + " from " 133 + branch + ")"; 134 } 135 136 public String getHealthStatus() { 137 return "OK"; 140 } 141 142 } 143 | Popular Tags |