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$
023: */
024: package com.bostechcorp.cbesb.common.sa.component;
025:
026: import java.io.File;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Vector;
031: import java.util.regex.Matcher;
032: import java.util.regex.Pattern;
033:
034: import javax.wsdl.WSDLException;
035:
036: import com.bostechcorp.cbesb.common.i18n.Message;
037: import com.bostechcorp.cbesb.common.i18n.Messages;
038: import com.bostechcorp.cbesb.common.sa.service.Connection;
039: import com.bostechcorp.cbesb.common.sa.service.EndPoint;
040: import com.bostechcorp.cbesb.common.sa.service.PropertiesKey;
041: import com.bostechcorp.cbesb.common.sa.service.ServiceAssembly;
042: import com.bostechcorp.cbesb.common.util.FileUtil;
043: import com.bostechcorp.cbesb.common.util.generators.AbstractComponentDOMCcslXMLGenerator;
044: import com.bostechcorp.cbesb.common.util.generators.AbstractComponentDOMJbiXMLGenerator;
045: import com.bostechcorp.cbesb.common.util.generators.AbstractComponentSimpleBuildXMLGenerator;
046: import com.bostechcorp.cbesb.common.util.generators.wsdl.generators.AbstractWsdlGenaretor;
047:
048: public abstract class AbstractComponent {
049:
050: protected Vector<EndPoint> endPointList = new Vector<EndPoint>();
051: /**
052: *it is required to hold the connection with the parent
053: *service assembly for the JBI deployment action.
054: * actualy only containerAssembly.getConnectionList() is necessary
055: */
056: protected ServiceAssembly containerAssembly = null;
057:
058: public AbstractComponent() {
059: super ();
060: }
061:
062: protected String name = "";
063: protected String componentType = "";
064: protected String description = "";
065: protected String interfaceName = "";
066: //The serviceName should be private, because the custom component may be overide it, it must use getSeviceName to get it.
067: private String serviceName = "";
068:
069: //used to identify the su's number
070: protected int SUNumber;
071:
072: public String getComponentType() {
073: return componentType;
074: }
075:
076: public void setComponentType(String componentType) {
077: this .componentType = componentType;
078: }
079:
080: public String getDescription() {
081: return description;
082: }
083:
084: public void setDescription(String description) {
085: if (description != null)
086: this .description = description;
087: }
088:
089: public String getInterfaceName() {
090: return interfaceName;
091: }
092:
093: public void setInterfaceName(String interfaceName) {
094: if (interfaceName != null)
095: this .interfaceName = interfaceName;
096: }
097:
098: public String getName() {
099: return name;
100: }
101:
102: public void setName(String name) {
103: if (name != null)
104: this .name = name;
105: }
106:
107: public String getServiceName() {
108: return serviceName;
109: }
110:
111: public void setServiceName(String serviceName) {
112: if (serviceName != null)
113: this .serviceName = serviceName;
114: }
115:
116: public void setServiceUnitNumber(int i) {
117: this .SUNumber = i;
118: }
119:
120: public int getServiceUnitNumber() {
121: return SUNumber;
122: }
123:
124: public Vector<EndPoint> getEndPointList() {
125: return endPointList;
126: }
127:
128: public void addEndPoint(EndPoint endPoint) {
129: this .endPointList.add(endPoint);
130: }
131:
132: abstract public String getComponentName();
133:
134: public void generateWSDL(String path, String saName)
135: throws Exception {
136: for (int i = 0; i < getEndPointList().size(); i++) {
137: EndPoint endPoint = this .endPointList.get(i);
138: //generateDefinition(path, endPoint, saName);
139: AbstractWsdlGenaretor generator = getWsdlGenerator(path,
140: saName, endPoint);
141: generator.writeToFile();
142: }
143: }
144:
145: protected abstract AbstractWsdlGenaretor getWsdlGenerator(
146: String path, String saName, EndPoint ep)
147: throws WSDLException;
148:
149: // DONE: explore to use JDOM to read XML template file and modify the JDOM
150: // object and serialize into XML back; Use XPath for substitue
151: //
152: // protected void generatePortTypeWSDL(TabbingPrintWriter writer, String saName) {
153: // writer.println("<portType name='" + saName + "_" + this.interfaceName
154: // + "'>");
155: // writer.println("</portType>");
156: //
157: // }
158:
159: // protected void generateBindingWSDL(TabbingPrintWriter writer, String saName) {
160: // writer.println("<binding name='" + saName + "_" + this.name
161: // + "Binding' type='tns:" + saName + "_" + this.interfaceName
162: // + "'>");
163: // writer.addTab();
164: // writer.println("<" + this.componentType + ":binding/>");
165: // writer.delTab();
166: // writer.println("</binding>");
167: // }
168:
169: public void deploy(String suPath, String saName) throws Exception {
170: String path = suPath + "/" + saName + "_" + this .name;
171: FileUtil.buildDirectory(new File(path));
172: generateWSDL(path, saName);
173: generateBuildXML(path, saName);
174: generateJBIXML(path, saName, this .interfaceName,
175: this .serviceName, this .name);
176: generateCcsl(path, saName);
177: }
178:
179: protected void generateJBIXML(String path, String saName,
180: String interfaceName, String serviceName, String suName)
181: throws Exception {
182: //if getComponentName() contains "-BC-" then it is a binding component
183: Boolean isBindingComponent = getComponentName()
184: .contains("-BC-");
185:
186: /*
187: AbstractComponentSimpleJbiXMLGenerator generator =
188: new AbstractComponentSimpleJbiXMLGenerator(path);
189: generator.generateFile();
190: */
191: AbstractComponentDOMJbiXMLGenerator jbiGenerator = new AbstractComponentDOMJbiXMLGenerator(
192: path, isBindingComponent, saName);
193:
194: //first set all the endpoint info
195:
196: for (Iterator iter = getEndPointList().iterator(); iter
197: .hasNext();) {
198: EndPoint element = (EndPoint) iter.next();
199:
200: if (element.isConumer()) {
201: jbiGenerator
202: .addConsumerPart(filterOutgoingConnectionList(suName));
203: }
204: if (element.isProvider()) {
205: jbiGenerator.addProviderPart(suName, interfaceName,
206: serviceName, element.getName());
207: }
208: }
209:
210: jbiGenerator.write();
211:
212: }
213:
214: private List<AbstractComponent> filterOutgoingConnectionList(
215: String suName) {
216: List<AbstractComponent> outgoingList = new ArrayList<AbstractComponent>();
217:
218: for (Connection connection : this .containerAssembly
219: .getConnectionList()) {
220: if (connection.getConsumerName().equals(suName)) {
221: outgoingList.add(this .containerAssembly
222: .getComponentByName(connection
223: .getProviderName()));
224: }
225: }
226: return outgoingList;
227: }
228:
229: protected void generateBuildXML(String path, String saName)
230: throws Exception {
231:
232: AbstractComponentSimpleBuildXMLGenerator generator = new AbstractComponentSimpleBuildXMLGenerator(
233: path, saName + "_" + this .getName() + ".zip");
234: generator.generateFile();
235: }
236:
237: public String getServiceNameByRole(boolean bConsumer) {
238: return this .getServiceName();
239:
240: }
241:
242: public String getEndpointName(EndPoint endPoint) {
243: return endPoint.getName();
244:
245: }
246:
247: public String getConsumerEndpointName() {
248: for (int j = 0; j < getEndPointList().size(); j++) {
249: EndPoint endPoint = this .endPointList.get(j);
250: if (endPoint.isConumer())
251: return endPoint.getName();
252: }
253: return null;
254:
255: }
256:
257: public String getProviderEndpointName() {
258: for (int j = 0; j < getEndPointList().size(); j++) {
259: EndPoint endPoint = this .endPointList.get(j);
260: if (endPoint.isProvider())
261: return endPoint.getName();
262: }
263: return null;
264:
265: }
266:
267: protected void generateCcsl(String path, String saName)
268: throws Exception {
269: Vector<EndPoint> ccslEndPointList = new Vector<EndPoint>();
270: for (int j = 0; j < getEndPointList().size(); j++) {
271: EndPoint endPoint = this .endPointList.get(j);
272: if ("true".equals(endPoint.getSettings().getProperty(
273: PropertiesKey.USECCSL))) {
274: ccslEndPointList.add(endPoint);
275: }
276: }
277: String prefix = PropertiesKey.CCSL;
278: AbstractComponentDOMCcslXMLGenerator dom = new AbstractComponentDOMCcslXMLGenerator(
279: path, saName);
280:
281: if (ccslEndPointList.size() != 0) {
282: for (int i = 0; i < ccslEndPointList.size(); i++) {
283: EndPoint endPoint = ccslEndPointList.get(i);
284: if (endPoint.getName() == null
285: || endPoint.getName().equals("")) {
286: throw new Exception(new Message(
287: Messages.FLOWEDITOR_MISSING_ENDPOINTNAME,
288: this .getName()).getMessage());
289: }
290:
291: dom
292: .addEndpoint(
293: "http://bostechcorp.com/SU/" + saName
294: + "_" + this .name,
295: saName
296: + "_"
297: + getServiceNameByRole(endPoint
298: .isConumer()),
299: saName + "_"
300: + getEndpointName(endPoint),
301: endPoint
302: .getSettings()
303: .getProperty(
304: prefix
305: + PropertiesKey.CCSL_ADDRECORD),
306: endPoint
307: .getSettings()
308: .getProperty(
309: prefix
310: + PropertiesKey.CCSL_STRIPRECORD),
311: endPoint
312: .getSettings()
313: .getProperty(
314: prefix
315: + PropertiesKey.CCSL_SAVEERRORS),
316: endPoint
317: .getSettings()
318: .getProperty(
319: prefix
320: + PropertiesKey.CCSL_USESENDMESSAGE));
321:
322: if ("true".equals(endPoint.getSettings().getProperty(
323: prefix + PropertiesKey.CCSL_USEUPOC))) {
324: String usePresend = endPoint
325: .getSettings()
326: .getProperty(
327: prefix
328: + PropertiesKey.CCSL_USEPRESEND);
329: if (usePresend != null && usePresend.equals("true")) {
330: dom
331: .addUpocAttribute(
332: "presend",
333: endPoint
334: .getSettings()
335: .getProperty(
336: prefix
337: + PropertiesKey.CCSL_PRESEND
338: + PropertiesKey.CCSL_UPOC_TYPE),
339: endPoint
340: .getSettings()
341: .getProperty(
342: prefix
343: + PropertiesKey.CCSL_PRESEND
344: + PropertiesKey.CCSL_UPOC_CLASS),
345: endPoint
346: .getSettings()
347: .getProperty(
348: prefix
349: + PropertiesKey.CCSL_PRESEND
350: + PropertiesKey.CCSL_UPOC_METHOD));
351: }
352: String usePostsend = endPoint
353: .getSettings()
354: .getProperty(
355: prefix
356: + PropertiesKey.CCSL_USEPOSTSEND);
357: if (usePostsend != null
358: && usePostsend.equals("true")) {
359: dom
360: .addUpocAttribute(
361: "postsend",
362: endPoint
363: .getSettings()
364: .getProperty(
365: prefix
366: + PropertiesKey.CCSL_POSTSEND
367: + PropertiesKey.CCSL_UPOC_TYPE),
368: endPoint
369: .getSettings()
370: .getProperty(
371: prefix
372: + PropertiesKey.CCSL_POSTSEND
373: + PropertiesKey.CCSL_UPOC_CLASS),
374: endPoint
375: .getSettings()
376: .getProperty(
377: prefix
378: + PropertiesKey.CCSL_POSTSEND
379: + PropertiesKey.CCSL_UPOC_METHOD));
380: }
381: String usePostaccept = endPoint
382: .getSettings()
383: .getProperty(
384: prefix
385: + PropertiesKey.CCSL_USEPOSTACCEPT);
386:
387: if (usePostaccept != null
388: && usePostaccept.equals("true")) {
389: dom
390: .addUpocAttribute(
391: "postaccept",
392: endPoint
393: .getSettings()
394: .getProperty(
395: prefix
396: + PropertiesKey.CCSL_POSTACCEPT
397: + PropertiesKey.CCSL_UPOC_TYPE),
398: endPoint
399: .getSettings()
400: .getProperty(
401: prefix
402: + PropertiesKey.CCSL_POSTACCEPT
403: + PropertiesKey.CCSL_UPOC_CLASS),
404: endPoint
405: .getSettings()
406: .getProperty(
407: prefix
408: + PropertiesKey.CCSL_POSTACCEPT
409: + PropertiesKey.CCSL_UPOC_METHOD));
410: }
411: }
412:
413: // dom.buildDocumentStructure();
414:
415: }
416: dom.write();
417: }
418: }
419:
420: public static boolean nameValidate(String name) {
421: Pattern pattern = Pattern
422: .compile("([0-9]|[_]|([a-z]|[A-Z])).*");
423: Matcher matcher = pattern.matcher(name);
424: if (matcher.matches()) {
425: Pattern pattern2 = Pattern.compile("([a-zA-Z0-9]|[_])+");
426: Matcher matcher2 = pattern2.matcher(name);
427: if (matcher2.matches()) {
428: return true;
429: } else
430: return false;
431: } else
432: return false;
433: }
434:
435: public void validate(List<String> list) {
436: validateBase(list);
437: validateCCSL(list);
438: }
439:
440: public void validateBase(List<String> list) {
441: if (!nameValidate(getName())) {
442: list.add(Messages.get(Messages.FLOWEDITOR_COMP_ENAME,
443: getName()));
444: }
445:
446: if (getInterfaceName() == null || "".equals(getInterfaceName())) {
447: list.add(new Message(
448: Messages.FLOWEDITOR_MISSING_INTERFACENAME,
449: getName()).getMessage());
450:
451: }
452: if (getServiceName() == null || "".equals(getServiceName())) {
453:
454: list.add(new Message(
455: Messages.FLOWEDITOR_MISSING_SERVICENAME, getName())
456: .getMessage());
457: }
458:
459: }
460:
461: public void validateCCSL(List<String> list) {
462: for (int i = 0; i < getEndPointList().size(); i++) {
463: EndPoint endPoint = getEndPointList().get(i);
464: String prefix = PropertiesKey.CCSL;
465: if (endPoint.getSettings().getProperty(
466: PropertiesKey.USECCSL) != null
467: && endPoint.getSettings().getProperty(
468: PropertiesKey.USECCSL).equals("true")) {
469: if (!endPoint.getSettings().getProperty(
470: prefix + PropertiesKey.CCSL_ADDRECORD).equals(
471: "false")
472: && !endPoint
473: .getSettings()
474: .getProperty(
475: prefix
476: + PropertiesKey.CCSL_STRIPRECORD)
477: .equals("false")
478: && !endPoint
479: .getSettings()
480: .getProperty(
481: prefix
482: + PropertiesKey.CCSL_USESENDMESSAGE)
483: .equals("false")) {
484: list.add(new Message(Messages.COMPONENT_WRONG_CCSL,
485: getName()).getMessage());
486: }
487:
488: }
489: }
490: }
491:
492: /**
493: * @return the containerAssembly
494: */
495: public ServiceAssembly getContainerAssembly() {
496: return containerAssembly;
497: }
498:
499: /**
500: * @param containerAssembly the containerAssembly to set
501: */
502: public void setContainerAssembly(ServiceAssembly containerAssembly) {
503: this.containerAssembly = containerAssembly;
504: }
505:
506: }
|