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: */
025: package com.bostechcorp.cbesb.common.util.generators;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Vector;
030:
031: import org.jdom.Element;
032: import org.jdom.Namespace;
033:
034: import com.bostechcorp.cbesb.common.sa.component.AbstractComponent;
035: import com.bostechcorp.cbesb.common.sa.component.CBRComponent;
036: import com.bostechcorp.cbesb.common.sa.component.SequencerComponent;
037: import com.bostechcorp.cbesb.common.sa.service.Connection;
038:
039: /**
040: * @author LPS
041: *
042: */
043: public class ServiceAssemblyDOMJbiXMLGenerator extends
044: AbstractDOMGenerator {
045:
046: private final String FILENAME = "/META-INF/jbi.xml";
047:
048: // tags
049: private final String JBI_T = "jbi";
050: private final String JBI_VERSION_A = "version";
051:
052: private final String SERVICE_ASSEMBLY_T = "service-assembly";
053: private final String SERVICE_UNIT_T = "service-unit";
054: private final String IDENTIFICATION_T = "identification";
055: private final String NAME_T = "name";
056: private final String DESCRIPTION_T = "description";
057: private final String TARGET_T = "target";
058: private final String ARTIFACTS_ZIP_T = "artifacts-zip";
059: private final String COMPONENTS_NAME_T = "component-name";
060:
061: private final String CONNECTIONS_T = "connections";
062: private final String CONNECTION_T = "connection";
063: private final String CONSUMER_T = "consumer";
064: private final String SERVICE_NAME_A = "service-name";
065: private final String ENDPOINT_NAME_A = "endpoint-name";
066: private final String PROVIDER_T = "provider";
067: private final String JBI_NAMESPACE_NS = "http://java.sun.com/xml/ns/jbi";
068:
069: //
070: Element serviceAssembly;
071:
072: Element connectionsElement;
073:
074: //
075: private String saName = "";
076:
077: private String saDescription = "";
078:
079: //
080: public ServiceAssemblyDOMJbiXMLGenerator(String path,
081: String saName, String saDescription) {
082: super (path);
083: this .saName = saName;
084: this .saDescription = saDescription;
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see com.bostechcorp.cbesb.common.util.AbstractDOMGenerator#buildDocumentStructure()
091: */
092: @Override
093: protected void buildDocumentStructure() {
094: rootElement = jdomFactory.element(JBI_T);
095: rootElement.setAttribute(JBI_VERSION_A, "1.0");
096: serviceAssembly = jdomFactory.element(SERVICE_ASSEMBLY_T);
097: rootElement.addContent(serviceAssembly);
098: }
099:
100: public void setContentFromLists(
101: Vector<AbstractComponent> componentList,
102: Vector<Connection> connections,
103: HashMap<String, AbstractComponent> componentMap) {
104: //setting identification
105: Element identification = jdomFactory.element(IDENTIFICATION_T);
106: Element iname = jdomFactory.element(NAME_T);
107: iname.addContent(saName);
108: Element idescription = jdomFactory.element(DESCRIPTION_T);
109: idescription.addContent(saDescription);
110: identification.addContent(iname);
111: identification.addContent(idescription);
112: serviceAssembly.addContent(identification);
113: //setting the rest of ..
114: setJbiNamespaces(componentList);
115: setServiceUnits(componentList);
116: setConnections(componentList, connections, componentMap);
117: }
118:
119: private void setConnections(
120: Vector<AbstractComponent> componentList,
121: Vector<Connection> connections,
122: HashMap<String, AbstractComponent> componentMap) {
123: connectionsElement = jdomFactory.element(CONNECTIONS_T);
124: serviceAssembly.addContent(connectionsElement);
125:
126: Iterator connectionIt = connections.iterator();
127: while (connectionIt.hasNext()) {
128: Connection connection = (Connection) connectionIt.next();
129: AbstractComponent consumer = (AbstractComponent) componentMap
130: .get(connection.getConsumerName());
131: AbstractComponent provider = (AbstractComponent) componentMap
132: .get(connection.getProviderName());
133: if ((consumer != null)
134: && (consumer instanceof SequencerComponent))
135: continue;
136: if ((consumer != null)
137: && (consumer instanceof CBRComponent))
138: continue;
139:
140: Element connectionElement = jdomFactory
141: .element(CONNECTION_T);
142:
143: if (consumer != null) {
144: String serviceName = consumer
145: .getServiceNameByRole(true);
146: String endpointName = consumer
147: .getConsumerEndpointName();
148:
149: Element consumerElement = jdomFactory
150: .element(CONSUMER_T);
151: consumerElement.setAttribute(SERVICE_NAME_A, "su"
152: + (consumer.getServiceUnitNumber() + 1) + ":"
153: + saName + "_" + serviceName);
154: consumerElement.setAttribute(ENDPOINT_NAME_A, saName
155: + "_" + endpointName);
156:
157: connectionElement.addContent(consumerElement);
158: }
159: if (provider != null) {
160: String serviceName = provider
161: .getServiceNameByRole(false);
162: String endpointName = provider
163: .getProviderEndpointName();
164:
165: Element providerElement = jdomFactory
166: .element(PROVIDER_T);
167: providerElement.setAttribute(SERVICE_NAME_A, "su"
168: + (provider.getServiceUnitNumber() + 1) + ":"
169: + saName + "_" + serviceName);
170: providerElement.setAttribute(ENDPOINT_NAME_A, saName
171: + "_" + endpointName);
172: connectionElement.addContent(providerElement);
173: }
174: this .connectionsElement.addContent(connectionElement);
175: }
176: }
177:
178: private void setServiceUnits(Vector<AbstractComponent> componentList) {
179: Iterator componentIt = componentList.iterator();
180: while (componentIt.hasNext()) {
181: AbstractComponent component = (AbstractComponent) componentIt
182: .next();
183:
184: Element serviceunit = jdomFactory.element(SERVICE_UNIT_T);
185: serviceAssembly.addContent(serviceunit);
186:
187: //identification
188: Element identification = jdomFactory
189: .element(IDENTIFICATION_T);
190: Element iname = jdomFactory.element(NAME_T);
191: iname.addContent(saName + "_" + component.getName());
192: Element idescription = jdomFactory.element(DESCRIPTION_T);
193: idescription.addContent(component.getDescription());
194: identification.addContent(iname);
195: identification.addContent(idescription);
196:
197: //target
198: Element target = jdomFactory.element(TARGET_T);
199: Element artifacts = jdomFactory.element(ARTIFACTS_ZIP_T);
200: artifacts.addContent(saName + "_" + component.getName()
201: + ".zip");
202: Element componentNameT = jdomFactory
203: .element(COMPONENTS_NAME_T);
204: componentNameT.addContent(component.getComponentName());
205: target.addContent(artifacts);
206: target.addContent(componentNameT);
207: //
208:
209: serviceunit.addContent(identification);
210: serviceunit.addContent(target);
211: }
212:
213: }
214:
215: private void setJbiNamespaces(
216: Vector<AbstractComponent> componentList) {
217: rootElement.setNamespace(Namespace
218: .getNamespace(JBI_NAMESPACE_NS));
219: if (componentList.size() > 0)
220: for (int i = 1; i <= componentList.size(); i++) {
221: AbstractComponent component = (AbstractComponent) componentList
222: .get(i - 1);
223: component.setServiceUnitNumber(i - 1);
224:
225: rootElement.addNamespaceDeclaration(Namespace
226: .getNamespace("su" + i,
227: "http://bostechcorp.com/SU/" + saName
228: + "_" + component.getName()));
229: }
230:
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see com.bostechcorp.cbesb.common.util.AbstractDOMGenerator#getFileOut()
237: */
238: @Override
239: protected String getFileOut() {
240: return filePath + FILENAME;
241: }
242:
243: }
|