001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: MainComponent.java 3832 2006-12-14 21:10:55Z elu $
023: */
024:
025: package com.bostechcorp.cbesb.runtime.component.bootstrap;
026:
027: import java.io.ByteArrayInputStream;
028: import java.sql.SQLException;
029: import java.util.List;
030:
031: import javax.jbi.JBIException;
032: import javax.jbi.component.ComponentContext;
033: import javax.jbi.component.ComponentLifeCycle;
034: import javax.jbi.management.MBeanNames;
035: import javax.jbi.messaging.DeliveryChannel;
036: import javax.jbi.messaging.MessageExchange;
037: import javax.jbi.servicedesc.ServiceEndpoint;
038: import javax.management.MBeanServer;
039: import javax.management.ObjectName;
040: import javax.xml.parsers.DocumentBuilder;
041: import javax.xml.parsers.DocumentBuilderFactory;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045: import org.w3c.dom.Document;
046: import org.w3c.dom.DocumentFragment;
047: import org.w3c.dom.Node;
048:
049: import com.bostechcorp.cbesb.common.util.ErrorUtil;
050: import com.bostechcorp.cbesb.common.util.runtimedb.DaoConfig;
051: import com.bostechcorp.cbesb.common.util.runtimedb.vo.EndpointStatVO;
052: import com.bostechcorp.cbesb.common.version.Version;
053: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbComponent;
054: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbExtendedLifeCycle;
055: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbServiceUnitManager;
056: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.LifeCycleExtension;
057: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.LifeCycleExtensionMBean;
058: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.notification.StatisticsHashMap;
059: import com.bostechcorp.cbesb.runtime.component.bootstrap.notification.NotificationReceiver;
060: import com.bostechcorp.cbesb.runtime.component.bootstrap.notification.StatisticProcessor;
061: import com.ibatis.sqlmap.client.SqlMapClient;
062:
063: public class BootstrapComponent extends CbComponent implements
064: ComponentLifeCycle {
065: ComponentContext context;
066: DeliveryChannel channel;
067: MessageReceiver mReceiver;
068: NotificationReceiver nReceiver;
069: StatisticProcessor eProcessor;
070: protected ObjectName mbeanName;
071: protected LifeCycleExtension extension;
072:
073: protected final transient Log logger = LogFactory
074: .getLog(getClass());
075:
076: private static final String bootstrapServerWsdl = "<?xml version='1.0' encoding='UTF-8'?>"
077: + "\r\n"
078: + "<definitions name='Bootstrap' "
079: + "\r\n"
080: + " targetNamespace='http://bostechcorp.com/wsdl/bootstrap/' "
081: + "\r\n"
082: + " xmlns:tns='http://bostechcorp.com/wsdl/bootstrap/' "
083: + "\r\n"
084: + " xmlns:install='http://bostechcorp.com/wsdl/bootstrap/'"
085: + "\r\n"
086: + " xmlns='http://schemas.xmlsoap.org/wsdl/'"
087: + "\r\n"
088: + " xmlns:jbi='http://servicemix.org/wsdl/jbi/'>"
089: + "\r\n"
090: + ""
091: + "\r\n"
092: + "<portType name='BootstrapInterface'>"
093: + "\r\n"
094: + "</portType>"
095: + "\r\n"
096: + ""
097: + "\r\n"
098: + "<binding name='BootstrapBinding' type='tns:BootstrapInterface'>"
099: + "\r\n"
100: + "</binding>"
101: + "\r\n"
102: + ""
103: + "\r\n"
104: + "<service name='BootstrapService'>"
105: + "\r\n"
106: + " <port name='BootstrapEndpoint' binding='tns:BootstrapBinding'>"
107: + "\r\n"
108: + " <jbi:endpoint role='provider' defaultOperation='tns:Bootstrap'/>"
109: + "\r\n"
110: + " </port>"
111: + "\r\n"
112: + "</service>"
113: + "\r\n"
114: + "</definitions>";
115:
116: private static Document myServiceDoc;
117:
118: static {
119: try {
120: DocumentBuilderFactory dbFactory = DocumentBuilderFactory
121: .newInstance();
122:
123: DocumentBuilder builder = dbFactory.newDocumentBuilder();
124: dbFactory.setNamespaceAware(true);
125: Node node = builder.parse(new ByteArrayInputStream(
126: bootstrapServerWsdl.getBytes("utf-8")));
127: node.normalize();
128: myServiceDoc = (Document) node;
129: } catch (Exception e) {
130: ErrorUtil.printError("Exception in MainComponent: ", e);
131: }
132: }
133:
134: // public ComponentLifeCycle getLifeCycle() {
135: // return this;
136: // }
137:
138: public ComponentLifeCycle getLifeCycle() {
139: return this ;
140: }
141:
142: public CbServiceUnitManager getServiceUnitManager() {
143: // return null;
144: return new BootstrapServiceUnitManager(this );
145: }
146:
147: public Document getServiceDescription(ServiceEndpoint arg0) {
148: return myServiceDoc;
149: }
150:
151: public boolean isExchangeWithConsumerOkay(ServiceEndpoint arg0,
152: MessageExchange arg1) {
153:
154: return false;
155: }
156:
157: public boolean isExchangeWithProviderOkay(ServiceEndpoint arg0,
158: MessageExchange arg1) {
159:
160: return false;
161: }
162:
163: public ServiceEndpoint resolveEndpointReference(
164: DocumentFragment arg0) {
165:
166: return null;
167: }
168:
169: // public ObjectName getExtensionMBeanName() {
170: // return null;
171: // }
172:
173: // public void init(ComponentContext ctx) throws JBIException {
174: // if (ctx != null) {
175: // mContext = ctx;
176: // mChannel = ctx.getDeliveryChannel();
177: //// System.out.println("Installer component workspaceRoot :"+ctx.getWorkspaceRoot());
178: //
179: // }
180: // }
181:
182: public void shutDown() throws JBIException {
183: if (this .mbeanName != null) {
184: MBeanServer server = this .context.getMBeanServer();
185: if (server == null) {
186: throw new JBIException("null mBeanServer");
187: }
188:
189: if (server.isRegistered(this .mbeanName)) {
190: try {
191: server.unregisterMBean(this .mbeanName);
192:
193: } catch (Exception e) {
194: throw new JBIException(e);
195:
196: }
197: }
198:
199: }
200: }
201:
202: public void start() throws JBIException {
203: // Start the database server
204: // Start the listener
205: logger.debug("start bootstrap component");
206: DaoConfig daoConfig = new DaoConfig();
207: SqlMapClient sqlMap = daoConfig.getSqlMapInstance();
208: try {
209: List<EndpointStatVO> result = sqlMap.queryForList(
210: "queryEndpointStat", null);
211: for (EndpointStatVO endpointStatVO : result) {
212: endpointStatVO.setInitMessagesPerSecond(endpointStatVO
213: .getMessagesPerSecond());
214: endpointStatVO.setInitMessagesCount(endpointStatVO
215: .getMessagesCount());
216: logger.debug(endpointStatVO.getEndpointName() + ":"
217: + endpointStatVO.getMessagesCount() + ":"
218: + endpointStatVO.getMessagesPerSecond());
219: StatisticsHashMap.getStatMap().put(
220: endpointStatVO.getEndpointName(),
221: endpointStatVO);
222: }
223: } catch (SQLException e) {
224: ErrorUtil
225: .printError("Read table EndpointStatInfo error", e);
226: }
227:
228: mReceiver = new MessageReceiver(context);
229: logger.debug("start notification reciever");
230: nReceiver = new NotificationReceiver(context);
231: logger.debug("start statistic processor");
232: eProcessor = new StatisticProcessor(this );
233:
234: }
235:
236: public void stop() throws JBIException {
237: logger.debug("stopping component...........");
238: mReceiver.stopProcessing();
239: mReceiver = null;
240: nReceiver.stopProcessing();
241: nReceiver = null;
242: eProcessor.stopProcessing();
243: eProcessor = null;
244: }
245:
246: public ObjectName getExtensionMBeanName() {
247: return mbeanName;
248: }
249:
250: public void init(ComponentContext arg0) throws JBIException {
251: try {
252: if (logger.isDebugEnabled()) {
253: logger.debug("Initializing component");
254: }
255: this .context = arg0;
256: this .channel = context.getDeliveryChannel();
257:
258: doInit();
259: if (logger.isDebugEnabled()) {
260: logger.debug("Component initialized");
261: }
262: } catch (JBIException e) {
263: throw e;
264: } catch (Exception e) {
265: throw new JBIException("Error calling init", e);
266: }
267:
268: }
269:
270: protected void doInit() throws Exception {
271: // Register extension mbean
272: Object mbean = getExtensionMBean();
273: if (mbean != null) {
274: MBeanServer server = this .context.getMBeanServer();
275: if (server == null) {
276: // TODO: log a warning ?
277: //throw new JBIException("null mBeanServer");
278: } else {
279: this .mbeanName = createExtensionMBeanName();
280: if (server.isRegistered(this .mbeanName)) {
281: server.unregisterMBean(this .mbeanName);
282: }
283: server.registerMBean(mbean, this .mbeanName);
284: }
285: }
286:
287: Version ccslVersInfo = Version
288: .getInstance(LifeCycleExtensionMBean.class);
289: if (ccslVersInfo != null) {
290: logger.info("ChainBuilder ESB CCSL version: "
291: + ccslVersInfo.toString() + " built on "
292: + ccslVersInfo.getBuildTimestamp());
293: this .setCcslLibVersionInfo(ccslVersInfo);
294:
295: } else {
296: logger.warn("Unable to load CCSL version.");
297: }
298:
299: // load the real component
300: logger.debug("CCSL is loading component class "
301: + this .getComponentName());
302: Version versInfo = Version.getInstance(getClass());
303: if (versInfo != null) {
304: logger.info(this .getComponentName() + " Component version:"
305: + versInfo.toString() + " built on "
306: + versInfo.getBuildTimestamp());
307: this .setVersionInfo(versInfo);
308:
309: }
310:
311: }
312:
313: protected ObjectName createExtensionMBeanName() throws Exception {
314: mbeanName = this .context.getMBeanNames()
315: .createCustomComponentMBeanName(
316: MBeanNames.COMPONENT_LIFE_CYCLE_EXTENSION);
317: return mbeanName;
318: }
319:
320: public Object getExtensionMBean() {
321: CbExtendedLifeCycle extendedLifeCycle = new CbExtendedLifeCycle(
322: this );
323: if (extension == null)
324: extension = new LifeCycleExtension(extendedLifeCycle);
325: return (LifeCycleExtensionMBean) extension;
326: }
327:
328: }
|