001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.project.anttasks;
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.ByteArrayInputStream;
025: import java.io.OutputStream;
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import org.apache.tools.ant.BuildException;
033:
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036:
037: import org.netbeans.modules.xslt.project.XsltproConstants;
038: import org.netbeans.modules.xslt.project.anttasks.jbi.ServiceEntry;
039:
040: import javax.xml.namespace.QName;
041: import org.netbeans.modules.xml.wsdl.model.PortType;
042: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
043: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
044: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
045: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
046: import org.netbeans.modules.xslt.tmap.model.api.Invoke;
047: import org.netbeans.modules.xslt.tmap.model.api.Operation;
048: import org.netbeans.modules.xslt.tmap.model.api.PartnerLinkTypeReference;
049: import org.netbeans.modules.xslt.tmap.model.api.Service;
050: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
051: import org.netbeans.modules.xslt.tmap.model.api.WSDLReference;
052: import org.netbeans.modules.xslt.project.CommandlineXsltProjectXmlCatalogProvider;
053:
054: /**
055: *
056: * @author Vitaly Bychkov
057: * @author Sreenivasan Genipudi
058: */
059: public abstract class AbstractJBIGenerator {
060: /**
061: * Map of namespace to its prefix
062: */
063: private Map<String, String> mNameSpacePrefix = new HashMap<String, String>();
064:
065: // Member variable representing source directory
066: /**
067: * Source directory
068: */
069: private String mSourceDirectory = null;
070: // Member variable representing build directory
071: /**
072: * Build directory
073: */
074: private String mBuildDirectory = null;
075:
076: /**
077: * Logger instance
078: */
079: private Logger logger = Logger.getLogger(AbstractJBIGenerator.class
080: .getName());
081:
082: private List<ServiceEntry> mProviders = new ArrayList<ServiceEntry>();
083:
084: private List<ServiceEntry> mConsumers = new ArrayList<ServiceEntry>();
085:
086: //Constants used in generating JBI.XML
087: /**
088: * Constant
089: */
090: public static final String JBI_ELEM_NAME = "jbi"; // NOI18N
091: /**
092: * Constant
093: */
094: public static final String SERVICES_ELEM_NAME = "services"; // NOI18N
095: /**
096: * Constant
097: */
098: public static final String PROVIDES_ELEM_NAME = "provides"; // NOI18N
099: /**
100: * Constant
101: */
102: public static final String CONSUMES_ELEM_NAME = "consumes"; // NOI18N
103: /**
104: * Constant
105: */
106: public static final String BINDING_ATTR_NAME = "binding-component"; // NOI18N
107: /**
108: * Constant
109: */
110: public static final String INTERFACE_ATTR_NAME = "interface-name"; // NOI18N
111: /**
112: * Constant
113: */
114: public static final String ENDPOINT_ATTR_NAME = "endpoint-name"; // NOI18N
115: /**
116: * Constant
117: */
118: public static final String SERVICE_ATTR_NAME = "service-name"; // NOI18N
119:
120: public static final String VERSION_ATTR_NAME = "version"; // NOI18N
121: public static final String VERSION_ATTR_VALUE = "1.0"; // NOI18N
122: public static final String NS_ATTR_NAME = "xmlns"; // NOI18N
123: public static final String NS_ATTR_VALUE = "http://java.sun.com/xml/ns/jbi"; // NOI18N
124: public static final String NS_XSI_ATTR_NAME = "xmlns:xsi"; // NOI18N
125: public static final String NS_XSI_ATTR_VALUE = "http://www.w3.org/2001/XMLSchema-instance"; // NOI18N
126: public static final String XSI_ATTR_NAME = "xsi:schemaLocation"; // NOI18N
127: public static final String XSI_ATTR_VALUE = "http://java.sun.com/xml/ns/jbi jbi.xsd"; // NOI18N
128:
129: public static final String NAMESPACE_PREFIX = "ns"; // NOI18N
130: public static final String COLON_SEPARATOR = ":"; // NOI18N
131:
132: public AbstractJBIGenerator(String srcDir, String buildDir) {
133: mSourceDirectory = srcDir;
134: mBuildDirectory = buildDir;
135: }
136:
137: /**
138: * Set the build directory
139: * @param buildDir build directory
140: */
141: public void setBuildDirectory(String buildDir) {
142: mBuildDirectory = buildDir;
143: }
144:
145: /**
146: * Get the build directory
147: * @return String value of the build directory
148: */
149: public String getBuildDirectory() {
150: return mBuildDirectory;
151: }
152:
153: /**
154: * Set the source directory
155: * @param srcDir source directory
156: */
157: public void setSourceDirectory(String srcDir) {
158: this .mSourceDirectory = srcDir;
159: }
160:
161: /**
162: * Get the source directory
163: * @return String value of the source directory
164: */
165: public String getSourceDirectory() {
166: return this .mSourceDirectory;
167: }
168:
169: /**
170: * Generate JBI.xml
171: */
172: public void generate() {
173: if (mBuildDirectory == null || mSourceDirectory == null) {
174: throw new BuildException(
175: "No directory is set for build || source files.");
176: }
177:
178: if (isOldProject()) {
179: OldProjectTransformer oldProjectTransformer = new OldProjectTransformer(
180: getSourceDirectory(), getBuildDirectory());
181: oldProjectTransformer.execute();
182: }
183:
184: process();
185: try {
186: generateJBIDescriptor();
187: } catch (IOException ex) {
188: throw new BuildException(ex);
189: }
190: }
191:
192: // TODO m | r
193: private boolean isOldProject() {
194: File tMapFile = getTransformmapFile();
195: return tMapFile == null || !tMapFile.isFile();
196: }
197:
198: protected abstract TMapModel getTMapModel();
199:
200: protected abstract <T extends ReferenceableWSDLComponent> T resolveReference(
201: NamedComponentReference<T> ref);
202:
203: private void process() {
204: TMapModel tMapModel = getTMapModel();
205: try {
206: populateProviderConsumer(tMapModel);
207: } catch (Exception ex) {
208: logger
209: .log(Level.SEVERE,
210: "Error encountered while processing transformmap model");
211: throw new RuntimeException(ex);
212: }
213: ////
214: ////
215: ////
216: ////
217: //// Document document = null;
218: //// if (transformmapFile != null) {
219: //// document = XmlUtil.getDocument(transformmapFile);
220: //// }
221: ////
222: //// if (document != null) {
223: //// NodeList operationNodeList = document.getElementsByTagName(TMapComponents.OPERATION.getTagName());
224: //// System.out.println("operationNodeList: "+operationNodeList);
225: ////
226: //// if (operationNodeList != null && operationNodeList.getLength() > 0) {
227: //// System.out.println("inside operationNodeList");
228: //// populateProviderServices(transformmapFile.getParentFile(), operationNodeList);
229: //// }
230: ////
231: //// NodeList invokesNodeList = document.getElementsByTagName(TMapComponents.INVOKES.getTagName());
232: //// if (invokesNodeList != null && invokesNodeList.getLength() > 0) {
233: //// populateConsumerServices(transformmapFile.getParentFile(), invokesNodeList);
234: //// }
235: //// }
236: }
237:
238: private ServiceEntry createServiceEntry(
239: PartnerLinkTypeReference pltRefComponent) {
240: if (pltRefComponent == null) {
241: return null;
242: }
243:
244: WSDLReference<PartnerLinkType> pltRef = pltRefComponent
245: .getPartnerLinkType();
246: WSDLReference<Role> roleRef = pltRefComponent.getRole();
247:
248: if (pltRef == null || roleRef == null) {
249: return null;
250: }
251: ServiceEntry entry = null;
252:
253: // TODO m
254: QName pltQname = pltRef.getQName();
255: if (pltQname == null) {
256: return null;
257: }
258:
259: String pltNS = pltQname.getNamespaceURI();
260: String pltName = pltQname.getLocalPart();
261: String pltNSPrefix = populateNamespace(pltNS);
262:
263: // PartnerLinkType plt = pltRef.get();
264: // if (plt == null) {
265: // logger.log(Level.SEVERE, "Problem encountered while processing partnerLinkType of \""+pltName+"\"");
266: // throw new RuntimeException("PartnerLink Type is Null!");
267: // }
268:
269: String portName = null;
270: String portNameNS = null;
271: String portNameNSPrefix = null;
272: QName portNameQname = null;
273:
274: String roleName = null;
275:
276: Role role = resolveReference(roleRef);
277: if (role == null) {
278: return null;
279: }
280:
281: roleName = role.getName();
282: NamedComponentReference<PortType> portTypeRef = role
283: .getPortType();
284:
285: if (portTypeRef != null) {
286: PortType pt = resolveReference(portTypeRef);
287: if (pt != null) {
288: portName = pt.getName();
289: portNameNS = pt.getModel().getDefinitions()
290: .getTargetNamespace();
291: portNameNSPrefix = populateNamespace(portNameNS);
292: portNameQname = portTypeRef.getQName();
293: }
294: }
295:
296: if (portName == null) {
297: logger.log(Level.SEVERE,
298: "Problem encountered while processing portType PartnerLink = \""
299: + pltName + "\"");
300: throw new RuntimeException(
301: "Problem encountered while processing portType !");
302: }
303:
304: entry = new ServiceEntry(pltName, portName, pltNS, portNameNS,
305: roleName, pltNSPrefix, portNameNSPrefix, pltQname,
306: portNameQname);
307:
308: return entry;
309: }
310:
311: /**
312: * Populate providers/consumers from transformmap model
313: */
314: private void populateProviderConsumer(TMapModel tMapModel) {
315: ServiceEntry provider = null;
316: ServiceEntry consumer = null;
317:
318: List<Service> services = tMapModel.getTransformMap()
319: .getServices();
320: if (services == null) {
321: return;
322: }
323:
324: for (Service service : services) {
325: provider = createServiceEntry(service);
326: if (provider != null && !mProviders.contains(provider)) {
327: mProviders.add(provider);
328: }
329:
330: List<Operation> operations = service.getOperations();
331: if (operations == null) {
332: continue;
333: }
334: for (Operation operation : operations) {
335: List<Invoke> invokes = operation.getInvokes();
336: System.out.println("invokes: " + invokes);
337: if (invokes == null) {
338: continue;
339: }
340: System.out.println("invokes.size(): " + invokes.size());
341: for (Invoke invoke : invokes) {
342: if (invoke != null) {
343: consumer = createServiceEntry(invoke);
344: System.out.println("created consumer "
345: + consumer + " for invoke: " + invoke);
346: if (consumer != null
347: && !mConsumers.contains(consumer)) {
348: mConsumers.add(consumer);
349: }
350: }
351: }
352: }
353: }
354: }
355:
356: private void generateJBIDescriptor() throws IOException {
357:
358: FileOutputStream fos = null;
359: try {
360: StringBuffer sb = new StringBuffer();
361: sb
362: .append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
363: sb.append("<jbi version=\"1.0\"\n");
364: sb
365: .append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
366: sb
367: .append(" xmlns=\"http://java.sun.com/xml/ns/jbi\"\n");
368: sb
369: .append(" xsi:schemaLocation=\"http://java.sun.com/xml/ns/jbi jbi.xsd\"\n");
370:
371: int nss = mNameSpacePrefix.size();
372: int i = 0;
373: Set<String> nsUris = mNameSpacePrefix.keySet();
374: for (String nsUri : nsUris) {
375: sb.append(" xmlns:"
376: + mNameSpacePrefix.get(nsUri) + "=\"" + nsUri
377: + "\"");
378: if (i < nss - 1) {
379: sb.append("\n");
380: }
381: i++;
382: }
383:
384: sb.append(">\n");
385: sb.append(" <services binding-component=\"false\">\n");
386:
387: if (mProviders != null) {
388: for (int j = 0; j < mProviders.size(); j++) {
389: ServiceEntry tmpService = mProviders.get(j);
390: sb.append(" <provides interface-name=\""
391: + getColonedQName(tmpService
392: .getPortNameQname(),
393: mNameSpacePrefix));
394: sb.append("\" service-name=\""
395: + getColonedQName(tmpService
396: .getPartnerLinkNameQname(),
397: mNameSpacePrefix));
398: sb.append("\" endpoint-name=\""
399: + tmpService.getRoleName());
400: sb.append("\"/>\n");
401:
402: }
403: }
404:
405: if (mConsumers != null) {
406: for (int j = 0; j < mConsumers.size(); j++) {
407: ServiceEntry tmpService = mConsumers.get(j);
408: sb.append(" <consumes interface-name=\""
409: + getColonedQName(tmpService
410: .getPortNameQname(),
411: mNameSpacePrefix));
412: sb.append("\" service-name=\""
413: + getColonedQName(tmpService
414: .getPartnerLinkNameQname(),
415: mNameSpacePrefix));
416: sb.append("\" endpoint-name=\""
417: + tmpService.getRoleName());
418: // sb.append("\" link-type=\"standard\"/>\n");
419: sb.append("\" />\n");
420: }
421: }
422:
423: sb.append(" </services>\n");
424: sb.append(" </jbi>\n");
425: String content = sb.toString();
426: fos = new FileOutputStream(getJbiFile());
427: store(content.getBytes("UTF-8"), fos);
428: } finally {
429: if (fos != null) {
430: fos.close();
431: }
432: }
433: }
434:
435: // TODO a
436: // private void generateTransformmapFromXsltMap() {
437: // File xsltMapFile = getFileInSrc(XsltproConstants.XSLTMAP_XML);
438: //
439: // if (xsltMapFile != null && xsltMapFile.isFile()) {
440: //
441: // }
442: // }
443:
444: private File getFileInSrc(String fileName) {
445: if (fileName == null) {
446: return null;
447: }
448:
449: String srcDir = getSourceDirectory();
450: if (srcDir == null || "".equals(srcDir)) {
451: throw new BuildException(
452: "source directory shouldn't be null or empty");
453: }
454:
455: File file = new File(srcDir + "/" + fileName);
456:
457: return file;
458: }
459:
460: protected File getTransformmapFile() {
461: File transformmapFile = getFileInSrc(XsltproConstants.TRANSFORMMAP_XML);
462: if (transformmapFile == null || !transformmapFile.isFile()) {
463: //TODO a transformmapFile = generateTransformmapFromXsltMap();
464: }
465:
466: return transformmapFile;
467: }
468:
469: private File getJbiFile() {
470: String buildDir = getBuildDirectory();
471: if (buildDir == null || "".equals(buildDir)) {
472: throw new BuildException(
473: "build directory shouldn't be null or empty");
474: }
475:
476: File jbiFile = new File(buildDir + "/META-INF/jbi.xml");
477: return jbiFile;
478: }
479:
480: /**
481: * Collect the namespaces and generate Prefix
482: * @param namespaceURI
483: * @return namespace prefix
484: */
485: private String populateNamespace(String namespaceURI) {
486: if (namespaceURI == null || "".equals(namespaceURI)) {
487: return null;
488: }
489:
490: String namespacePrefix = null;
491: namespacePrefix = (String) mNameSpacePrefix.get(namespaceURI);
492:
493: if (namespacePrefix == null) {
494: namespacePrefix = NAMESPACE_PREFIX
495: + mNameSpacePrefix.size();
496: mNameSpacePrefix.put(namespaceURI, namespacePrefix);
497: }
498: return namespacePrefix;
499: }
500:
501: private static String getColonedQName(QName qn, Map nsTable) {
502: String ns = qn.getNamespaceURI();
503: String prefix = (String) nsTable.get(ns);
504: if (prefix == null)
505: return qn.getLocalPart();
506: else
507: return prefix + COLON_SEPARATOR + qn.getLocalPart();
508: }
509:
510: private static QName getQName(String qname) {
511: return QName.valueOf(qname);
512: }
513:
514: private static void store(byte input[], OutputStream output)
515: throws IOException {
516:
517: ByteArrayInputStream in = new ByteArrayInputStream(input);
518: byte buf[] = new byte[4096];
519: for (int n = 0; (n = in.read(buf)) != -1;) {
520: output.write(buf, 0, n);
521: }
522:
523: output.flush();
524: }
525: }
|