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: * EjbDataSourceXmlParser.java
043: *
044: * Created on May 2, 2004, 2:45 AM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import org.netbeans.modules.visualweb.ejb.datamodel.EjbInfo;
050: import org.netbeans.modules.visualweb.ejb.datamodel.EjbGroup;
051: import org.netbeans.modules.visualweb.ejb.datamodel.MethodInfo;
052: import org.netbeans.modules.visualweb.ejb.datamodel.MethodParam;
053: import org.netbeans.modules.visualweb.ejb.datamodel.MethodReturn;
054: import java.io.File;
055: import java.io.FileInputStream;
056: import java.util.*;
057: import javax.xml.parsers.ParserConfigurationException;
058: import javax.xml.parsers.SAXParser;
059: import javax.xml.parsers.SAXParserFactory;
060: import org.openide.ErrorManager;
061: import org.xml.sax.Attributes;
062: import org.xml.sax.SAXException;
063: import org.xml.sax.helpers.DefaultHandler;
064: import org.openide.modules.InstalledFileLocator;
065:
066: /**
067: *
068: * @author cao
069: */
070: public class EjbDataSourceXmlParser extends DefaultHandler {
071: private String xmlFileName;
072:
073: private EjbGroup curEjbGroup;
074: private MethodInfo curMethodInfo;
075: private MethodParam curMethodParam;
076: private EjbInfo curEjbInfo;
077: private String currentTag;
078: private String data;
079:
080: private Collection allEjbGroups;
081:
082: private static String bundledPeRmiIiopPort;
083: private static File sampleEjbDir;
084: static {
085: bundledPeRmiIiopPort = getBundledPeRmiIiopPort();
086: sampleEjbDir = InstalledFileLocator.getDefault().locate(
087: "samples/ejb", null, false); // NOI18N
088: }
089:
090: /** Creates a new instance of EjbDataSourceXmlParser */
091: public EjbDataSourceXmlParser(String xmlFile) {
092: xmlFileName = xmlFile;
093: allEjbGroups = new HashSet();
094: }
095:
096: // Return a Collection of EjbGroups parsed from the given xml file
097: public Collection parse() throws EjbLoadException {
098: try {
099: SAXParserFactory factory = SAXParserFactory.newInstance();
100: factory.setNamespaceAware(true);
101: factory.setValidating(false);
102: SAXParser parser = factory.newSAXParser();
103: File file = new File(xmlFileName);
104: parser.parse(file, this );
105: } catch (java.io.IOException e) {
106: // Log error
107: String logMsg = "Error occured when trying to parse the standard deployment descriptor file. Cannot read file "
108: + xmlFileName;
109: ErrorManager
110: .getDefault()
111: .getInstance(
112: "org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlParser")
113: .log(ErrorManager.ERROR, logMsg);
114: e.printStackTrace();
115:
116: // Throw up as SYSTEM_ERROR
117: throw new EjbLoadException(e.getMessage());
118: } catch (ParserConfigurationException e) {
119: // Log error
120: String logMsg = "Error occured when trying to parse the ejb data source file "
121: + xmlFileName;
122: ErrorManager
123: .getDefault()
124: .getInstance(
125: "org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlParser")
126: .log(ErrorManager.ERROR, logMsg);
127: e.printStackTrace();
128:
129: // Throw up as SYSTEM_ERROR
130: throw new EjbLoadException(e.getMessage());
131: } catch (SAXException e) {
132: // Log error
133: String logMsg = "Error occured when trying to parse the ejb data source file "
134: + xmlFileName;
135: ErrorManager
136: .getDefault()
137: .getInstance(
138: "org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlParser")
139: .log(ErrorManager.ERROR, logMsg);
140: e.printStackTrace();
141:
142: // Throw up as SYSTEM_ERROR
143: throw new EjbLoadException(e.getMessage());
144: }
145:
146: return allEjbGroups;
147: }
148:
149: public void startElement(String uri, String localName,
150: String qName, Attributes attributes) throws SAXException {
151: currentTag = qName;
152:
153: if (currentTag.equalsIgnoreCase(XmlTagConstants.EJB_GROUP_TAG))
154: curEjbGroup = new EjbGroup();
155: else if (currentTag
156: .equalsIgnoreCase(XmlTagConstants.STATELESS_SESSION_TAG)) {
157: curEjbInfo = new EjbInfo();
158: curEjbInfo.setBeanType(EjbInfo.STATELESS_SESSION_BEAN);
159: } else if (currentTag
160: .equalsIgnoreCase(XmlTagConstants.STATEFUL_SESSION_TAG)) {
161: curEjbInfo = new EjbInfo();
162: curEjbInfo.setBeanType(EjbInfo.STATEFUL_SESSION_BEAN);
163: } else if (currentTag
164: .equalsIgnoreCase(XmlTagConstants.METHOD_TAG)) {
165: curMethodInfo = new MethodInfo();
166: curMethodInfo.setIsBusinessMethod(true);
167: } else if (currentTag
168: .equalsIgnoreCase(XmlTagConstants.CREATE_METHOD_TAG)) {
169: curMethodInfo = new MethodInfo();
170: curMethodInfo.setIsBusinessMethod(false);
171: } else if (currentTag
172: .equalsIgnoreCase(XmlTagConstants.RETURN_TYPE_TAG)) {
173: // Get the element class type
174: String elemType = (String) attributes
175: .getValue(XmlTagConstants.RETURN_ELEM_TYPE_ATTR);
176: String isReturnCol = (String) attributes
177: .getValue(XmlTagConstants.RETURN_IS_COLLECTION_ATTR);
178:
179: MethodReturn mr = new MethodReturn();
180:
181: if (isReturnCol != null)
182: mr.setIsCollection(new Boolean(isReturnCol)
183: .booleanValue());
184:
185: mr.setElemClassName(elemType);
186:
187: curMethodInfo.setReturnType(mr);
188: } else if (currentTag
189: .equalsIgnoreCase(XmlTagConstants.PARAMETER_TAG)) {
190: curMethodParam = new MethodParam();
191:
192: // Parameter name is from the "name" attribute
193: String paramName = (String) attributes
194: .getValue(XmlTagConstants.PARAM_NAME_ATTR);
195:
196: if (paramName == null) {
197: // Not specified. Default to arg#
198: int numParams = 0;
199: if (curMethodInfo.getParameters() != null)
200: numParams = curMethodInfo.getParameters().size() - 1;
201:
202: paramName = "arg" + numParams; // NOI8N
203: }
204:
205: curMethodParam.setName(paramName);
206: }
207: }
208:
209: public void endElement(String uri, String localName, String qName)
210: throws SAXException {
211: if (currentTag != null) {
212: // Set the data to the ejb group
213: setData();
214: }
215:
216: if (qName.equals(XmlTagConstants.STATELESS_SESSION_TAG)
217: || qName.equals(XmlTagConstants.STATEFUL_SESSION_TAG)) {
218: curEjbGroup.addSessionBean(curEjbInfo);
219: } else if (qName.equalsIgnoreCase(XmlTagConstants.METHOD_TAG)
220: || qName
221: .equalsIgnoreCase(XmlTagConstants.CREATE_METHOD_TAG)) {
222: curEjbInfo.addMethod(curMethodInfo);
223: } else if (qName
224: .equalsIgnoreCase(XmlTagConstants.PARAMETER_TAG)) {
225: curMethodInfo.addParameter(curMethodParam);
226: } else if (qName
227: .equalsIgnoreCase(XmlTagConstants.EJB_GROUP_TAG)) {
228: // End of this ejb group
229: allEjbGroups.add(curEjbGroup);
230: curEjbGroup = null;
231: }
232:
233: currentTag = null;
234: data = null;
235: }
236:
237: public void characters(char buf[], int offset, int len)
238: throws SAXException {
239: if (data == null)
240: data = new String(buf, offset, len);
241: else
242: data = data + new String(buf, offset, len);
243: }
244:
245: private void setData() {
246: // Trim the data first
247: if (data != null)
248: data = data.trim();
249:
250: if (currentTag.equalsIgnoreCase(XmlTagConstants.GROUP_NAME_TAG))
251: curEjbGroup.setName(data);
252: else if (currentTag
253: .equalsIgnoreCase(XmlTagConstants.CONTAINER_VENDOR_TAG))
254: curEjbGroup.setAppServerVendor(data);
255: else if (currentTag
256: .equalsIgnoreCase(XmlTagConstants.SERVER_HOST_TAG))
257: curEjbGroup.setServerHost(data);
258: else if (currentTag
259: .equalsIgnoreCase(XmlTagConstants.IIOP_PORT_TAG))
260: curEjbGroup.setIIOPPort(resolveIiopPort(data));
261: else if (currentTag
262: .equalsIgnoreCase(XmlTagConstants.CONTAINER_VENDOR_TAG))
263: curEjbGroup.setAppServerVendor(data);
264: else if (currentTag
265: .equalsIgnoreCase(XmlTagConstants.CLIENT_JAR_TAG))
266: curEjbGroup.addClientJarFile(resolveJarFileName(data));
267: else if (currentTag
268: .equalsIgnoreCase(XmlTagConstants.BEAN_WRAPPER_JAR_TAG))
269: curEjbGroup
270: .setClientWrapperBeanJar(resolveJarFileName(data));
271: else if (currentTag
272: .equalsIgnoreCase(XmlTagConstants.DESIGN_TIME_TAG))
273: curEjbGroup.setDesignInfoJar(resolveJarFileName(data));
274: else if (currentTag
275: .equalsIgnoreCase(XmlTagConstants.ADDITIONAL_DD_JAR_TAG))
276: curEjbGroup.setDDLocationFile(resolveJarFileName(data));
277: else if (currentTag
278: .equalsIgnoreCase(XmlTagConstants.JNDI_NAME_TAG))
279: curEjbInfo.setJNDIName(data);
280: else if (currentTag
281: .equalsIgnoreCase(XmlTagConstants.EJB_NAME_TAG))
282: curEjbInfo.setEjbName(data);
283: else if (currentTag
284: .equalsIgnoreCase(XmlTagConstants.HOME_TAG_TAG))
285: curEjbInfo.setHomeInterfaceName(data);
286: else if (currentTag
287: .equalsIgnoreCase(XmlTagConstants.REMOTE_TAG))
288: curEjbInfo.setCompInterfaceName(data);
289: else if (currentTag
290: .equalsIgnoreCase(XmlTagConstants.WEB_EJB_REF_TAG))
291: curEjbInfo.setWebEjbRef(data);
292: else if (currentTag
293: .equalsIgnoreCase(XmlTagConstants.WRAPPER_BEAN_TAG))
294: curEjbInfo.setBeanWrapperName(data);
295: else if (currentTag
296: .equalsIgnoreCase(XmlTagConstants.WRAPPER_BEAN_INFO_TAG))
297: curEjbInfo.setBeanInfoWrapperName(data);
298: else if (currentTag
299: .equalsIgnoreCase(XmlTagConstants.METHOD_NAME_TAG))
300: curMethodInfo.setName(data);
301: else if (currentTag
302: .equalsIgnoreCase(XmlTagConstants.RETURN_TYPE_TAG))
303: curMethodInfo.getReturnType().setClassName(data);
304: else if (currentTag
305: .equalsIgnoreCase(XmlTagConstants.PARAMETER_TAG))
306: curMethodParam.setType(data);
307: else if (currentTag
308: .equalsIgnoreCase(XmlTagConstants.EXCEPTION_TAG))
309: curMethodInfo.addException(data);
310: else if (currentTag
311: .equalsIgnoreCase(XmlTagConstants.DATAPROVIDER_TAG))
312: curMethodInfo.setDataProvider(data);
313: }
314:
315: private int resolveIiopPort(String port) {
316: int iiopPort = 23700;
317:
318: if (port.indexOf("{bundle_pe_iiop_port}") != -1) {
319: String bundleiiopPort = getBundledPeRmiIiopPort();
320:
321: if (bundleiiopPort != null)
322: return Integer.parseInt(bundleiiopPort);
323: } else
324: iiopPort = Integer.parseInt(port);
325:
326: return iiopPort;
327: }
328:
329: private static String getBundledPeRmiIiopPort() {
330: /**
331: * Get the installer properties to find out what port PE was installed on.
332: */
333: File installPropertiesFile = InstalledFileLocator.getDefault()
334: .locate("config/com-sun-rave-install.properties", null,
335: true);
336:
337: try {
338: /**
339: * If we couldn't find the installer properties, don't try to get
340: * the port from them.
341: */
342: if (installPropertiesFile != null
343: || installPropertiesFile.exists()) {
344: Properties installProps = new Properties();
345: installProps.load(new FileInputStream(
346: installPropertiesFile));
347: return installProps.getProperty("iiop1Port");
348: }
349:
350: return null;
351: } catch (Exception e) {
352: // No install.properties found. It is OK. Just return null so that default IIOP port will be used
353: ErrorManager
354: .getDefault()
355: .getInstance(
356: "org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlParser")
357: .log(ErrorManager.INFORMATIONAL,
358: "Error accessing com-sun-rave-install.properties");
359: return null;
360: }
361: }
362:
363: private String resolveJarFileName(String fileName) {
364: // If there is a {tools.home} in the file name, resolove it to
365: // the real directory location
366: if (fileName.indexOf("{samples/ejb}") != -1) // NOI18N
367: {
368: fileName = sampleEjbDir.getAbsolutePath()
369: + fileName.substring(fileName.indexOf('}') + 1);
370: }
371:
372: // Use the OS file separator
373: fileName = fileName.replace('/', File.separatorChar);
374: fileName = fileName.replace('\\', File.separatorChar);
375:
376: return fileName;
377: }
378: }
|