001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * EjbDataSourceXmlCreator.java
043: *
044: * Created on May 2, 2004, 4:17 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import org.netbeans.modules.visualweb.ejb.datamodel.EjbDataModel;
050: import org.netbeans.modules.visualweb.ejb.datamodel.EjbInfo;
051: import org.netbeans.modules.visualweb.ejb.datamodel.EjbGroup;
052: import org.netbeans.modules.visualweb.ejb.datamodel.MethodInfo;
053: import org.netbeans.modules.visualweb.ejb.datamodel.MethodParam;
054: import java.util.*;
055:
056: /**
057: * This class saves the EJBDataSource into xml file
058: *
059: * @author cao
060: */
061: public class EjbDataSourceXmlCreator {
062:
063: public static final String XML_BEGIN_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
064: public static final String XML_BEGIN_2 = "<ejb-data-source xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
065: public static final String XML_END = "</ejb-data-source>";
066:
067: private static final int INDENT_SPACES = 4;
068:
069: private Collection ejbGroups;
070:
071: // Where the xml will output/write to
072: private java.io.Writer out;
073:
074: public EjbDataSourceXmlCreator(EjbDataModel ejbData,
075: java.io.Writer out) {
076: this .ejbGroups = ejbData.getEjbGroups();
077: this .out = out;
078: }
079:
080: public EjbDataSourceXmlCreator(Collection ejbGroups,
081: java.io.Writer out) {
082: this .ejbGroups = ejbGroups;
083: this .out = out;
084: }
085:
086: public void toXml() throws java.io.IOException {
087: out.write(XML_BEGIN_1);
088: out.write(XML_BEGIN_2);
089:
090: if (this .ejbGroups == null || this .ejbGroups.isEmpty()) {
091: out.write(XML_END);
092: return;
093: }
094:
095: for (Iterator iter = this .ejbGroups.iterator(); iter.hasNext();) {
096: EjbGroup ejbGrp = (EjbGroup) iter.next();
097:
098: out.write(openTag(XmlTagConstants.EJB_GROUP_TAG, true, 1));
099:
100: out
101: .write(openTag(XmlTagConstants.GROUP_NAME_TAG,
102: false, 2));
103: out.write(ejbGrp.getName());
104: out.write(closeTag(XmlTagConstants.GROUP_NAME_TAG, 0));
105:
106: out.write(openTag(XmlTagConstants.CONTAINER_VENDOR_TAG,
107: false, 2));
108: out.write(ejbGrp.getAppServerVendor());
109: out
110: .write(closeTag(
111: XmlTagConstants.CONTAINER_VENDOR_TAG, 0));
112:
113: out
114: .write(openTag(XmlTagConstants.SERVER_HOST_TAG,
115: false, 2));
116: out.write(ejbGrp.getServerHost());
117: out.write(closeTag(XmlTagConstants.SERVER_HOST_TAG, 0));
118:
119: out.write(openTag(XmlTagConstants.IIOP_PORT_TAG, false, 2));
120: out.write(Integer.toString(ejbGrp.getIIOPPort()));
121: out.write(closeTag(XmlTagConstants.IIOP_PORT_TAG, 0));
122:
123: for (Iterator jarIter = ejbGrp.getClientJarFiles()
124: .iterator(); jarIter.hasNext();) {
125: String clientJar = (String) jarIter.next();
126: out.write(openTag(XmlTagConstants.CLIENT_JAR_TAG,
127: false, 2));
128: out.write(clientJar);
129: out.write(closeTag(XmlTagConstants.CLIENT_JAR_TAG, 0));
130: }
131:
132: // Client wrapper jar
133: out.write(openTag(XmlTagConstants.BEAN_WRAPPER_JAR_TAG,
134: false, 2));
135: out.write(ejbGrp.getClientWrapperBeanJar());
136: out
137: .write(closeTag(
138: XmlTagConstants.BEAN_WRAPPER_JAR_TAG, 0));
139:
140: // DesignInfo jar - new in Thresher
141: if (ejbGrp.getDesignInfoJar() != null) {
142: out.write(openTag(XmlTagConstants.DESIGN_TIME_TAG,
143: false, 2));
144: out.write(ejbGrp.getDesignInfoJar());
145: out.write(closeTag(XmlTagConstants.DESIGN_TIME_TAG, 0));
146: }
147:
148: if (ejbGrp.getDDLocationFile() != null) {
149: out
150: .write(openTag(
151: XmlTagConstants.ADDITIONAL_DD_JAR_TAG,
152: false, 2));
153: out.write(ejbGrp.getDDLocationFile());
154: out.write(closeTag(
155: XmlTagConstants.ADDITIONAL_DD_JAR_TAG, 0));
156: }
157:
158: // All the enterprise beans
159: out.write(openTag(XmlTagConstants.ENTERPRISE_BEANS_TAG,
160: true, 2));
161:
162: // Session beans;
163: if (ejbGrp.getSessionBeans() != null
164: && !ejbGrp.getSessionBeans().isEmpty())
165: writeOutSessionBeans(ejbGrp.getSessionBeans());
166:
167: // entity beans;
168:
169: // message driven beans
170:
171: out
172: .write(closeTag(
173: XmlTagConstants.ENTERPRISE_BEANS_TAG, 2));
174:
175: // end of this group
176: out.write(closeTag(XmlTagConstants.EJB_GROUP_TAG, 1));
177: }
178:
179: out.write(XML_END);
180: }
181:
182: private void writeOutSessionBeans(Collection sessionBeans)
183: throws java.io.IOException {
184: for (Iterator iter = sessionBeans.iterator(); iter.hasNext();) {
185: EjbInfo info = (EjbInfo) iter.next();
186:
187: String sessionTag = XmlTagConstants.STATELESS_SESSION_TAG;
188: if (info.getBeanType() == EjbInfo.STATEFUL_SESSION_BEAN)
189: sessionTag = XmlTagConstants.STATEFUL_SESSION_TAG;
190:
191: out.write(openTag(sessionTag, true, 3));
192:
193: out.write(openTag(XmlTagConstants.JNDI_NAME_TAG, false, 4));
194: out.write(info.getJNDIName());
195: out.write(closeTag(XmlTagConstants.JNDI_NAME_TAG, 0));
196:
197: out.write(openTag(XmlTagConstants.EJB_NAME_TAG, false, 4));
198: out.write(info.getEjbName());
199: out.write(closeTag(XmlTagConstants.EJB_NAME_TAG, 0));
200:
201: out.write(openTag(XmlTagConstants.HOME_TAG_TAG, false, 4));
202: out.write(info.getHomeInterfaceName());
203: out.write(closeTag(XmlTagConstants.HOME_TAG_TAG, 0));
204:
205: out.write(openTag(XmlTagConstants.REMOTE_TAG, false, 4));
206: out.write(info.getCompInterfaceName());
207: out.write(closeTag(XmlTagConstants.REMOTE_TAG, 0));
208:
209: out
210: .write(openTag(XmlTagConstants.WEB_EJB_REF_TAG,
211: false, 4));
212: out.write(info.getWebEjbRef());
213: out.write(closeTag(XmlTagConstants.WEB_EJB_REF_TAG, 0));
214:
215: out.write(openTag(XmlTagConstants.WRAPPER_BEAN_TAG, false,
216: 4));
217: out.write(info.getBeanWrapperName());
218: out.write(closeTag(XmlTagConstants.WRAPPER_BEAN_TAG, 0));
219:
220: out.write(openTag(XmlTagConstants.WRAPPER_BEAN_INFO_TAG,
221: false, 4));
222: out.write(info.getBeanInfoWrapperName());
223: out
224: .write(closeTag(
225: XmlTagConstants.WRAPPER_BEAN_INFO_TAG, 0));
226:
227: if (info.getMethods() != null
228: && !info.getMethods().isEmpty())
229: writeOutMethods(info.getMethods());
230: ;
231:
232: out.write(closeTag(sessionTag, 3));
233: }
234: }
235:
236: private void writeOutMethods(Collection methodInfos)
237: throws java.io.IOException {
238: for (Iterator iter = methodInfos.iterator(); iter.hasNext();) {
239: MethodInfo mInfo = (MethodInfo) iter.next();
240:
241: String methodTag = XmlTagConstants.METHOD_TAG;
242: if (!mInfo.isBusinessMethod())
243: methodTag = XmlTagConstants.CREATE_METHOD_TAG;
244:
245: out.write(openTag(methodTag, true, 4));
246:
247: out
248: .write(openTag(XmlTagConstants.METHOD_NAME_TAG,
249: false, 5));
250: out.write(mInfo.getName());
251: out.write(closeTag(XmlTagConstants.METHOD_NAME_TAG, 0));
252:
253: // Return-type
254: Map returnTypeAttrMap = new HashMap();
255:
256: // RETURN_IS_COLLECTION_ATTR and RETURN_ELEM_TYPE_ATTR are new in Thresher.
257: // In Thresher, because of the data provider support, we need to remember whether
258: // the turn type is a collection. If it is, then the element class name needs to be remembered too
259: if (mInfo.getReturnType().isCollection()) {
260: returnTypeAttrMap.put(
261: XmlTagConstants.RETURN_IS_COLLECTION_ATTR,
262: "true");
263:
264: if (mInfo.getReturnType().getElemClassName() != null)
265: returnTypeAttrMap.put(
266: XmlTagConstants.RETURN_ELEM_TYPE_ATTR,
267: mInfo.getReturnType().getElemClassName());
268: } else
269: returnTypeAttrMap.put(
270: XmlTagConstants.RETURN_IS_COLLECTION_ATTR,
271: "false");
272:
273: out.write(openTag(XmlTagConstants.RETURN_TYPE_TAG,
274: returnTypeAttrMap, false, 5));
275: out.write(mInfo.getReturnType().getClassName());
276: out.write(closeTag(XmlTagConstants.RETURN_TYPE_TAG, 0));
277:
278: // Parameters
279: // PARAM_NAME_ATTR is new in Thresher
280: // In Thresher, we allow the user to enter/modify the parameter method names
281: ArrayList parameters = mInfo.getParameters();
282: if (parameters != null && !parameters.isEmpty()) {
283: for (int i = 0; i < parameters.size(); i++) {
284: MethodParam p = (MethodParam) parameters.get(i);
285:
286: Map nameAttr = new HashMap();
287: nameAttr.put(XmlTagConstants.PARAM_NAME_ATTR, p
288: .getName());
289:
290: out.write(openTag(XmlTagConstants.PARAMETER_TAG,
291: nameAttr, false, 5));
292: out.write(p.getType());
293: out
294: .write(closeTag(
295: XmlTagConstants.PARAMETER_TAG, 0));
296: }
297: }
298:
299: // Exceptions
300: ArrayList exceptions = mInfo.getExceptions();
301: if (exceptions != null && !exceptions.isEmpty()) {
302: for (int i = 0; i < exceptions.size(); i++) {
303: out.write(openTag(XmlTagConstants.EXCEPTION_TAG,
304: false, 5));
305: out.write((String) exceptions.get(i));
306: out
307: .write(closeTag(
308: XmlTagConstants.EXCEPTION_TAG, 0));
309: }
310: }
311:
312: // Data privider is new in Thresher
313: if (mInfo.getDataProvider() != null) {
314: out.write(openTag(XmlTagConstants.DATAPROVIDER_TAG,
315: false, 5));
316: out.write(mInfo.getDataProvider());
317: out
318: .write(closeTag(
319: XmlTagConstants.DATAPROVIDER_TAG, 0));
320: }
321:
322: out.write(closeTag(methodTag, 4));
323: }
324: }
325:
326: private String openTag(String tagName, boolean newLine,
327: int indentLevel) {
328: return openTag(tagName, null, newLine, indentLevel);
329: }
330:
331: private String openTag(String tagName, Map attributes,
332: boolean newLine, int indentLevel) {
333: StringBuffer buf = new StringBuffer();
334:
335: // Indent INDENT_SPACES for each level
336: for (int i = 0; i < indentLevel * INDENT_SPACES; i++)
337: buf.append(" ");
338:
339: buf.append("<");
340: buf.append(tagName);
341:
342: // Attributes
343: if (attributes != null) {
344: for (Iterator iter = attributes.entrySet().iterator(); iter
345: .hasNext();) {
346: Map.Entry entry = (Map.Entry) iter.next();
347:
348: buf.append(" ");
349: buf.append((String) entry.getKey());
350: buf.append("=\"");
351: buf.append((String) entry.getValue());
352: buf.append("\"");
353: }
354: }
355:
356: buf.append(">");
357:
358: if (newLine)
359: buf.append("\n");
360:
361: return buf.toString();
362: }
363:
364: private String closeTag(String tagName, int indentLevel) {
365: StringBuffer buf = new StringBuffer();
366:
367: for (int i = 0; i < indentLevel * INDENT_SPACES; i++)
368: buf.append(" ");
369:
370: buf.append("</");
371: buf.append(tagName);
372: buf.append(">");
373: buf.append("\n");
374:
375: return buf.toString();
376: }
377:
378: }
|