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:
020: package org.netbeans.modules.iep.project.anttasks;
021:
022: import org.netbeans.modules.iep.model.IEPModel;
023: import org.netbeans.modules.iep.model.IEPModelFactory;
024: import org.netbeans.modules.iep.model.lib.IOUtil;
025: import org.netbeans.modules.xml.xam.ModelSource;
026: import org.openide.filesystems.FileUtil;
027:
028: import java.io.File;
029: import java.io.FileOutputStream;
030: import java.util.ArrayList;
031: import java.util.List;
032:
033: import org.apache.tools.ant.BuildException;
034: import org.apache.tools.ant.Task;
035:
036: /**
037: *
038: * @author blu
039: */
040: public class GenerateAsaArtifacts extends Task {
041: private String mSrcDirectoryLocation;
042: private String mBuildDirectoryLocation;
043: private String mJbiDescriptorFileLocation;
044:
045: /** Creates a new instance of GenerateIEPASAArtifacts */
046: public GenerateAsaArtifacts() {
047: }
048:
049: /**
050: * @return Returns the srcDirectoryLocation.
051: */
052: public String getSrcDirectoryLocation() {
053: return mSrcDirectoryLocation;
054: }
055:
056: /**
057: * @param srcDirectoryLocation The srcDirectoryLocation to set.
058: */
059: public void setSrcDirectoryLocation(String srcDirectoryLocation) {
060: mSrcDirectoryLocation = srcDirectoryLocation;
061: }
062:
063: /**
064: * @return Returns the srcDirectoryLocation.
065: */
066: public String getBuildDirectoryLocation() {
067: return mBuildDirectoryLocation;
068: }
069:
070: /**
071: * @param srcDirectoryLocation The srcDirectoryLocation to set.
072: */
073: public void setBuildDirectoryLocation(String buildDirectoryLocation) {
074: mBuildDirectoryLocation = buildDirectoryLocation;
075: }
076:
077: /**
078: * @return Returns the portMapFileLocation.
079: */
080: public String getJbiDescriptorFileLocation() {
081: return mJbiDescriptorFileLocation;
082: }
083:
084: /**
085: * @param portMapFileLocation The portMapFileLocation to set.
086: */
087: public void setJbiDescriptorFileLocation(
088: String jbiDescriptorFileLocation) {
089: mJbiDescriptorFileLocation = jbiDescriptorFileLocation;
090: }
091:
092: public void execute() throws BuildException {
093: File srcDir = new File(mSrcDirectoryLocation);
094: if (!srcDir.exists()) {
095: throw new BuildException("Directory "
096: + mSrcDirectoryLocation + " does not exit.");
097: }
098: String srcDirPath = srcDir.getAbsolutePath();
099: //1: for appending '/'
100: int srcDirPathLen = srcDir.getPath().length() + 1;
101: String[] ext = new String[] { ".iep" };
102: List iepFiles = DirectoryUtil.getFilesRecursively(srcDir, ext);
103: List portMapEntryList = new ArrayList();
104: List nsList = new ArrayList();
105: FileOutputStream fos = null;
106: try {
107: for (int i = 0, I = iepFiles.size(); i < I; i++) {
108: File f = (File) iepFiles.get(i);
109: String fPath = f.getPath();
110:
111: ModelSource modelSource = org.netbeans.modules.xml.retriever.catalog.Utilities
112: .getModelSource(FileUtil.toFileObject(f), true);
113: if (modelSource != null) {
114: IEPModel model = IEPModelFactory.getDefault()
115: .getModel(modelSource);
116: // see com.sun.jbi.engine.iep.jbiadapter.IEPSEServiceUnitManager
117: String ns = NameUtil.makeJavaId(fPath
118: .substring(srcDirPathLen));
119: nsList.add(ns);
120: String tns = "ns" + (i + 1);
121: List<PortMapEntry> portMaps = Util
122: .generatePortMapEntryList(model, tns);
123: // Generate PortMapEntries and append to portMapEntryList
124: portMapEntryList.addAll(portMaps);
125: }
126:
127: }
128: // Generate jbi.xml
129: // <?xml version='1.0'?>
130: // <jbi version="1.0"
131: // xmlns="http://java.sun.com/xml/ns/jbi"
132: // xmlns:ns0=${ns1} ... xmlns:nsN=${nsN}
133: // version="1.0">
134: // <services binding-component="false">
135: // <provides interface-name=port-type service-name=partner-link endpoint-name=role-name/>
136: // <consumes interface-name=port-type service-name=partner-link endpoint-name=role-name link-type="standard"/>
137: // </services>
138: // </jbi>
139: try {
140: StringBuffer sb = new StringBuffer();
141: sb.append("<!--start of generated code -->\n");
142: sb.append("<jbi version=\"1.0\"\n");
143: sb
144: .append(" xmlns=\"http://java.sun.com/xml/ns/jbi\"\n");
145: for (int i = 0, I = nsList.size(); i < I; i++) {
146: String ns = (String) nsList.get(i);
147: sb.append(" xmlns:ns" + (i + 1) + "=\"" + ns
148: + "\"");
149: if (i < I - 1) {
150: sb.append("\n");
151: }
152: }
153: sb.append(">\n");
154: sb
155: .append(" <services binding-component=\"false\">\n");
156: // Generate all <provides> first
157: for (int i = 0, I = portMapEntryList.size(); i < I; i++) {
158: PortMapEntry pme = (PortMapEntry) portMapEntryList
159: .get(i);
160: if (pme.getRole().equals(PortMapEntry.MY_ROLE)) {
161: sb.append(" <provides interface-name=\""
162: + pme.getPortType());
163: sb.append("\" service-name=\""
164: + pme.getPartnerLink());
165: sb.append("\" endpoint-name=\""
166: + pme.getRoleName());
167: sb.append("\"/>\n");
168: }
169: }
170: // Generate all <consumes> second
171: for (int i = 0, I = portMapEntryList.size(); i < I; i++) {
172: PortMapEntry pme = (PortMapEntry) portMapEntryList
173: .get(i);
174: if (pme.getRole().equals(PortMapEntry.PARTNER_ROLE)) {
175: sb.append(" <consumes interface-name=\""
176: + pme.getPortType());
177: sb.append("\" service-name=\""
178: + pme.getPartnerLink());
179: sb.append("\" endpoint-name=\""
180: + pme.getRoleName());
181: sb.append("\" link-type=\"standard\"/>\n");
182: }
183: }
184: sb.append(" </services>\n");
185: sb.append(" </jbi>\n");
186: sb.append("<!--end of generated code -->\n");
187:
188: String content = sb.toString();
189: fos = new FileOutputStream(mJbiDescriptorFileLocation);
190: IOUtil.copy(content.getBytes("UTF-8"), fos);
191: } catch (Exception e) {
192: throw e;
193: } finally {
194: if (fos != null) {
195: try {
196: fos.close();
197: } catch (Exception e1) {
198: e1.printStackTrace();
199: }
200: }
201: }
202: } catch (Exception e) {
203: throw new BuildException(e.getMessage());
204: }
205: }
206:
207: public static void main(String[] args) {
208: GenerateAsaArtifacts tsk = new GenerateAsaArtifacts();
209: tsk.setJbiDescriptorFileLocation("c:/temp/portMap.xml");
210: tsk.setSrcDirectoryLocation("c:/temp");
211: tsk.execute();
212: }
213:
214: }
|