001: package com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging;
002:
003: import java.io.File;
004: import java.util.Collection;
005: import java.util.Set;
006: import java.util.Vector;
007:
008: import javax.jbi.messaging.MessageExchange.Role;
009: import javax.management.NotificationBroadcasterSupport;
010:
011: import org.apache.log4j.xml.DOMConfigurator;
012:
013: import com.bostechcorp.cbesb.common.util.runtimedb.EndpointStatisticsDB;
014: import com.bostechcorp.cbesb.common.version.Version;
015: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.notification.StatisticsHashMap;
016:
017: public class LifeCycleExtension extends NotificationBroadcasterSupport
018: implements LifeCycleExtensionMBean {
019:
020: protected CbExtendedLifeCycle lifeCycle;
021:
022: protected String logConfig;
023:
024: public LifeCycleExtension(CbExtendedLifeCycle lifeCycle) {
025: this .lifeCycle = lifeCycle;
026: }
027:
028: public String getState(String key) {
029: LifeCycleEndpoint lcep = (LifeCycleEndpoint) lifeCycle.component
030: .getRegistry().getEndpoint(key);
031: return lcep.getState();
032: }
033:
034: public String[] getEndpoints() {
035: ComponentRegistry reg = lifeCycle.component.getRegistry();
036: Set<String> epMap = reg.endpoints.keySet();
037: return epMap.toArray(new String[0]);
038: }
039:
040: public void stopEndpoint(String key) throws Exception {
041: LifeCycleEndpoint lcep = getEndpointByName(key);
042: if (lcep == null)
043: return;
044: lcep.stop();
045: }
046:
047: public void startEndpoint(String key) throws Exception {
048: LifeCycleEndpoint lcep = getEndpointByName(key);
049: if (lcep == null)
050: return;
051: lcep.start();
052: }
053:
054: public String[] getDisplayParameterTitles() {
055:
056: String[] eplist = this .getEndpoints();
057: if (eplist == null || eplist.length < 1)
058: return new String[] {};
059: String key = eplist[0];
060: /**
061: * the key always like :{http://bostechcorp.com/SU/hl7demo_Seq3}hl7demo_Seq3_Service:hl7demo_Seq3
062: */
063: LifeCycleEndpoint lcep = (LifeCycleEndpoint) lifeCycle.component
064: .getRegistry().getEndpoint(key);
065: return lcep.getDisplayParameterTitles();
066: }
067:
068: public String[] getDisplayParameters(String endpointName) {
069: LifeCycleEndpoint lcep = getEndpointByName(endpointName);
070: if (lcep == null)
071: return null;
072: return lcep.getDisplayParameters();
073:
074: }
075:
076: public String[] getGetableProperties() {
077: String key = this .getEndpoints()[0];
078: LifeCycleEndpoint lcep = (LifeCycleEndpoint) lifeCycle.component
079: .getRegistry().getEndpoint(key);
080: return lcep.getGetableProperties();
081: }
082:
083: public String[][] getGetablePropertyObject(String endpointName) {
084: LifeCycleEndpoint lcep = getEndpointByName(endpointName);
085: if (lcep == null)
086: return null;
087: return lcep.getGetablePropertyObjects();
088: }
089:
090: public String getProperty(String key, String property) {
091: LifeCycleEndpoint lcep = (LifeCycleEndpoint) lifeCycle.component
092: .getRegistry().getEndpoint(key);
093: return lcep.getProperty(property);
094: }
095:
096: public String[] getSetableProperties() {
097: String key = this .getEndpoints()[0];
098: LifeCycleEndpoint lcep = (LifeCycleEndpoint) lifeCycle.component
099: .getRegistry().getEndpoint(key);
100: return lcep.getSetableProperties();
101: }
102:
103: public void setProperty(String key, String property, String value) {
104: LifeCycleEndpoint lcep = getEndpointByName(key);
105: if (lcep == null)
106: return;
107: lcep.setProperty(property, value);
108: }
109:
110: /**
111: * return a array, each element is su name:endpoint Name:state:role name
112: */
113: public String[] getEndpointNames() {
114: ComponentRegistry reg = lifeCycle.component.getRegistry();
115: Collection<CbEndpoint> epVals = reg.endpoints.values();
116: Vector<String> result = new Vector<String>();
117:
118: for (CbEndpoint ep : epVals) {
119: LifeCycleEndpoint lcep = (LifeCycleEndpoint) ep;
120: String role = "provider";
121: if (lcep.getRole() == Role.CONSUMER)
122: role = "consumer";
123: StringBuffer buf = new StringBuffer("");
124: buf.append(lcep.getServiceUnit().getName()).append(":")
125: .append(lcep.getEndpoint()).append(":").append(
126: lcep.getState()).append(":").append(role);
127: result.add(buf.toString());
128:
129: }
130:
131: return result.toArray(new String[result.size()]);
132: }
133:
134: /**
135: * return a endpoint array for single assembly, each element is su name:endpoint Name:state:role name
136: */
137: public String[] getEndpointNamesBySA(String saName) {
138: ComponentRegistry reg = lifeCycle.component.getRegistry();
139: Collection<CbEndpoint> epVals = reg.endpoints.values();
140: Vector<String> result = new Vector<String>();
141:
142: for (CbEndpoint ep : epVals) {
143: LifeCycleEndpoint lcep = (LifeCycleEndpoint) ep;
144: if (!lcep.getSaName().equals(saName))
145: continue;
146: String role = "provider";
147: if (lcep.getRole() == Role.CONSUMER)
148: role = "consumer";
149: StringBuffer buf = new StringBuffer("");
150: buf.append(lcep.getServiceUnit().getName()).append(":")
151: .append(lcep.getEndpoint()).append(":").append(
152: lcep.getState()).append(":").append(role);
153: result.add(buf.toString());
154:
155: }
156:
157: return result.toArray(new String[result.size()]);
158: }
159:
160: public void setGetablePropertyObject(String endpointName,
161: String[][] propertyObjects) {
162: LifeCycleEndpoint lcep = getEndpointByName(endpointName);
163: if (lcep == null)
164: return;
165: for (int i = 0; i < propertyObjects.length; i++) {
166: String[] object = propertyObjects[i];
167: if (object[0] == null)
168: continue;
169: lcep.setProperty(object[0], object[1]);
170: }
171:
172: }
173:
174: private LifeCycleEndpoint getEndpointByName(String endpointName) {
175: String[] keys = this .getEndpoints();
176: for (int i = 0; i < keys.length; i++) {
177: LifeCycleEndpoint lcep = (LifeCycleEndpoint) lifeCycle.component
178: .getRegistry().getEndpoint(keys[i]);
179: if (lcep.getEndpoint().equals(endpointName))
180: return lcep;
181: }
182: return null;
183: }
184:
185: public void setAllEndpoints(String[] allEndpoints) {
186: // do nothing
187:
188: }
189:
190: public Version getVersionInfo() {
191: return lifeCycle.component.versionInfo;
192: }
193:
194: public Version getCCSLVersionInfo() {
195: return lifeCycle.component.ccslLibVersionInfo;
196: }
197:
198: public String getVMFreePercent() {
199: double value = (Runtime.getRuntime().freeMemory() * 100) * 1.0
200: / Runtime.getRuntime().totalMemory();
201: return String.valueOf(value);
202: }
203:
204: public String getVMFree() {
205: return String.valueOf(Runtime.getRuntime().freeMemory());
206: }
207:
208: public void setLogConfig(String logConfig) {
209: File logConfigFile = new File(logConfig);
210: if (logConfigFile.exists() && logConfigFile.isFile()) {
211: this .logConfig = logConfigFile.getAbsolutePath();
212: DOMConfigurator.configure(logConfigFile.getAbsolutePath());
213: }
214: }
215:
216: public String getLogConfig() {
217: return logConfig;
218: }
219:
220: /**
221: * if saname=="all",reset all SA's statistics info,
222: * otherwise reset one SA's statistics info which name is saName
223: * @param saName
224: * @throws Exception
225: */
226: public void resetEndpointStatistics(String saName) throws Exception {
227: if (saName.equalsIgnoreCase("all")) {
228: EndpointStatisticsDB.deleteAllEndpointStatistics();
229: StatisticsHashMap.getStatMap().clear();
230: } else {
231: EndpointStatisticsDB.deleteEndpointStatistics(saName);
232: String prefix = saName + "_";
233: for (String endpointName : StatisticsHashMap.getStatMap()
234: .keySet()) {
235: if (endpointName.startsWith(prefix)) {
236: StatisticsHashMap.getStatMap().remove(endpointName);
237: }
238: }
239:
240: }
241: }
242: }
|