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: TcpipEndpoint.java 12180 2008-03-03 07:58:37Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.component.tcpip;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.Vector;
029: import java.util.concurrent.SynchronousQueue;
030:
031: import javax.jbi.messaging.DeliveryChannel;
032: import javax.jbi.messaging.MessageExchange;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import com.bostechcorp.cbesb.common.runtime.ConfigurationException;
038: import com.bostechcorp.cbesb.common.util.ErrorUtil;
039: import com.bostechcorp.cbesb.common.util.RuntimeClassLoader;
040: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.IComponentProcessor;
041: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.LifeCycleEndpoint;
042: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.StatusConstants;
043: import com.bostechcorp.cbesb.runtime.ccsl.lib.CcslUtil;
044: import com.bostechcorp.cbesb.runtime.ccsl.lib.ITcpipHandler;
045: import com.bostechcorp.cbesb.runtime.component.tcpip.processors.ConsumerProcessor;
046: import com.bostechcorp.cbesb.runtime.component.tcpip.processors.ProviderProcessor;
047: import com.bostechcorp.cbesb.runtime.component.tcpip.wsdl.TcpipConfig;
048:
049: public class TcpipEndpoint extends LifeCycleEndpoint {
050: private TcpipConfig config;
051: private Vector<TcpipSocket> sockets = new Vector<TcpipSocket>();
052: private TcpipSocketManager socketManager;
053: private int maxSockets;
054: private Class handlerClass;
055: private SynchronousQueue<MessageExchange> sendQueue = new SynchronousQueue<MessageExchange>(
056: true);
057: private SynchronousQueue<Object> returnQueue = new SynchronousQueue<Object>(
058: true);
059: protected final transient Log logger = LogFactory
060: .getLog(getClass());
061:
062: /**********************************************************************************
063: * These attributes and methods customize the LifeCycleEndpoint for this component
064: **********************************************************************************
065: */
066:
067: /*
068: * The display parameters are general information for the admin console to display.
069: */
070: public String[] getDisplayParameterTitles() {
071: String[] result = { "Host", "Mode", "Port", "SSL" };
072: return result;
073: }
074:
075: /*
076: * The values returned here correspond to the titles above.
077: */
078: public String[] getDisplayParameters() {
079: String ssl = (config.isUseSsl()) ? " Y " : " N ";
080: String host = "";
081: String port = "";
082: String mode = "Client";
083: if (config.getConnectionMode() == TcpipConfig.CONNECTION_MODE_SERVER) {
084: port = String.format("%6d", config.getListenPort());
085: mode = "Server";
086:
087: } else {
088: host = config.getHost();
089: port = String.format("%6d", config.getPort());
090: }
091: String[] result = { host, mode, port, ssl };
092: return result;
093: }
094:
095: /*
096: * This returns a list of properties that can be read.
097: */
098: public String[] getGetableProperties() {
099: TcpIpPropertiesEnumeration[] fps = TcpIpPropertiesEnumeration
100: .values();
101: List<String> result = new ArrayList<String>();
102: for (int i = 0; i < fps.length; i++) {
103: result.add(fps[i].name());
104: }
105: String[] sr = new String[result.size()];
106: return result.toArray(sr);
107: }
108:
109: /*
110: * This gets one property from the list above.
111: */
112: public String getProperty(int index) {
113: return TcpIpPropertiesEnumeration.values()[index]
114: .getValue(this );
115: }
116:
117: /*
118: * This gets one property from the list above.
119: */
120: public String getProperty(String property) {
121: return TcpIpPropertiesEnumeration.valueOf(property).getValue(
122: this );
123: }
124:
125: /*
126: * This returns a list of properties that can be set.
127: */
128: public String[] getSetableProperties() {
129: TcpIpPropertiesEnumeration[] fps = TcpIpPropertiesEnumeration
130: .values();
131: List<String> result = new ArrayList<String>();
132: for (int i = 0; i < fps.length; i++) {
133: if (fps[i].isSetable())
134: result.add(fps[i].name());
135: }
136: String[] sr = new String[result.size()];
137: return result.toArray(sr);
138: }
139:
140: /*
141: * This sets one property from the list above.
142: */
143: public void setProperty(int index, String value) {
144: TcpIpPropertiesEnumeration.values()[index]
145: .setValue(this , value);
146: }
147:
148: /*
149: * This sets one property from the list above.
150: */
151: public void setProperty(String property, String value) {
152: TcpIpPropertiesEnumeration.valueOf(property).setValue(this ,
153: value);
154: }
155:
156: /**************************************************
157: * Done with LifeCycleEndpoint methods
158: ***************************************************/
159:
160: public DeliveryChannel getDeliveryChannel() {
161: return channel;
162: }
163:
164: public SynchronousQueue<MessageExchange> getSendQueue() {
165: return sendQueue;
166: }
167:
168: public SynchronousQueue<Object> getReturnQueue() {
169: return returnQueue;
170: }
171:
172: public TcpipConfig getConfig() {
173: return config;
174: }
175:
176: public void setConfig(TcpipConfig arg0) {
177: config = arg0;
178: }
179:
180: public void addSocket(TcpipSocket sock) {
181: sockets.add(sock);
182: if (logger.isDebugEnabled())
183: logger.debug("add a new socket '" + sock + "'. sockets="
184: + sockets.size());
185: setState(StatusConstants.STATUS_UP);
186: }
187:
188: public void removeSocket(TcpipSocket sock) {
189: sockets.remove(sock);
190: if (logger.isDebugEnabled())
191: logger.debug("remove a socket '" + sock + "'. sockets="
192: + sockets.size());
193: if (!forceShutdown) {
194: if (socketManager == null && sockets.size() < maxSockets)
195: try {
196: socketManager = TcpipSocketManager
197: .getInstance(this );
198: } catch (Exception e) {
199: ErrorUtil.printError(
200: "error restarting socket manager", e);
201: }
202: if (sockets.size() < 1) {
203: setState(StatusConstants.STATUS_OPENING);
204: }
205: }
206: }
207:
208: public int getSocketCount() {
209: return sockets.size();
210: }
211:
212: public void clearSocketManager() {
213: socketManager = null;
214: }
215:
216: protected IComponentProcessor createProviderProcessor() {
217: return new ProviderProcessor(this );
218: }
219:
220: protected IComponentProcessor createConsumerProcessor() {
221: return new ConsumerProcessor(this );
222: }
223:
224: public void start() throws Exception {
225: setState("Opening");
226: forceShutdown = false;
227:
228: if (handlerClass == null)
229: loadProtocolHandler();
230: socketManager = TcpipSocketManager.getInstance(this );
231: maxSockets = socketManager.getMaxSockets();
232: // This code is not right. The status will be in Up when AddSocket() method is called.
233: // setState("Up");
234: }
235:
236: public void stop() {
237: forceShutdown = true;
238: setState(StatusConstants.STATUS_CLOSING);
239: // Force stop the server if there is one
240: if (socketManager != null) {
241: socketManager.forceStop();
242: socketManager = null;
243: }
244: logger.debug("socket manager stopped");
245: // Force stop any active connections
246: for (TcpipSocket socket : sockets) {
247: socket.forceStop();
248: }
249: sockets.clear();
250: logger.debug("socket stopped");
251:
252: setState(StatusConstants.STATUS_DOWN);
253: }
254:
255: public ITcpipHandler getProtocolHandlerInstance() throws Exception {
256: return (ITcpipHandler) handlerClass.newInstance();
257: }
258:
259: private void loadProtocolHandler() throws Exception {
260: // load the handler class
261: String suRootPath = getServiceUnit().getRootPath();
262: String serviceAssemblyName = CcslUtil
263: .getAssemblyName(suRootPath);
264: String handlerClassName = getConfig().getProtocolHandler();
265: ClassLoader cl = RuntimeClassLoader.getClassLoader(
266: serviceAssemblyName, this );
267: handlerClass = Class.forName(handlerClassName, true, cl);
268: // verify the correct type here before we go too far
269: Object temp = handlerClass.newInstance();
270: try {
271: ITcpipHandler temp1 = (ITcpipHandler) temp;
272: logger.debug("TcpipEndpoint loaded protocol handler "
273: + temp1);
274: } catch (ClassCastException e) {
275: ConfigurationException ce = new ConfigurationException(
276: "Protocol handler does not implement ITcpipHandler"
277: + e.getMessage(), e);
278: ce
279: .setRemedy("Select a handler class that implements ITcpipHandler.");
280: throw ce;
281: }
282: }
283: }
|