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: * EjbJarXmlParser.java
043: *
044: * Created on April 28, 2004, 11:04 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import java.io.StringReader;
050: import java.util.ArrayList;
051: import java.util.Collection;
052: import java.util.HashSet;
053: import java.util.Set;
054:
055: import javax.xml.parsers.ParserConfigurationException;
056: import javax.xml.parsers.SAXParser;
057: import javax.xml.parsers.SAXParserFactory;
058:
059: import org.netbeans.modules.visualweb.ejb.datamodel.EjbInfo;
060: import org.openide.ErrorManager;
061: import org.openide.util.NbBundle;
062: import org.xml.sax.Attributes;
063: import org.xml.sax.InputSource;
064: import org.xml.sax.SAXException;
065: import org.xml.sax.helpers.DefaultHandler;
066:
067: /**
068: * This class is used to parse the standard deployment descriptor - ejb-jar.xml.
069: * The class only extracts out the session beans. It can be enhanced to extract
070: * MDBs and entity beans later on as needed.
071: */
072: public class StdDeploymentDescriptorParser extends DefaultHandler {
073: private static final String HOME_TAG = "home";
074: private static final String REMOTE_TAG = "remote";
075: private static final String EJB_NAME_TAG = "ejb-name";
076: private static final String SESSION_TAG = "session";
077: private static final String EJB_SESSION_TYPE_TAG = "session-type";
078: private static final String BEAN_ID_ATTR = "id";
079:
080: private String xmlFileName;
081:
082: private String beanId;
083: private String homeName;
084: private String remoteName;
085: private String ejbName;
086: private String sessionType;
087:
088: private String currentTag;
089: private String data;
090:
091: private Collection sessionBeans = new ArrayList();
092:
093: // To remember to skipped non-package EJBs
094: private Set skippedEjbs = new HashSet();
095:
096: /** Creates a new instance of EjbJarXmlParser */
097: public StdDeploymentDescriptorParser(String ejbJarXml) {
098: this .xmlFileName = ejbJarXml;
099: }
100:
101: public Collection parse() throws EjbLoadException {
102: try {
103: SAXParserFactory factory = SAXParserFactory.newInstance();
104: factory.setNamespaceAware(true);
105: factory.setValidating(false);
106: SAXParser parser = factory.newSAXParser();
107: parser.parse(xmlFileName, this );
108:
109: return sessionBeans;
110: } catch (java.io.IOException e) {
111: // Log error
112: String logMsg = "Error occured when trying to parse the standard EJB deployment descriptor. Cannot read file "
113: + xmlFileName;
114: // TODO - EjbJarXmlParser is not a valid class name
115: ErrorManager
116: .getDefault()
117: .getInstance(
118: "org.netbeans.modules.visualweb.ejb.load.EjbJarXmlParser")
119: .log(ErrorManager.WARNING, logMsg);
120: e.printStackTrace();
121:
122: // Throw up as SYSTEM_ERROR. Should never happen
123: throw new EjbLoadException(e.getMessage());
124: } catch (ParserConfigurationException e) {
125: // Log error
126: String logMsg = "Error occured when trying to parse the standard EJB deployment descriptor file "
127: + xmlFileName;
128: ErrorManager
129: .getDefault()
130: .getInstance(
131: "org.netbeans.modules.visualweb.ejb.load.EjbJarXmlParser")
132: .log(ErrorManager.WARNING, logMsg);
133: e.printStackTrace();
134:
135: String errMsg = NbBundle.getMessage(
136: StdDeploymentDescriptorParser.class,
137: "CANNOT_PARSE_STD_DD");
138: throw new EjbLoadException(EjbLoadException.USER_ERROR,
139: errMsg);
140: } catch (SAXException e) {
141: // Log error
142: String logMsg = "Error occured when trying to parse the standard EJB deployment descriptor file "
143: + xmlFileName;
144: ErrorManager
145: .getDefault()
146: .getInstance(
147: "org.netbeans.modules.visualweb.ejb.load.EjbJarXmlParser")
148: .log(ErrorManager.WARNING, logMsg);
149: e.printStackTrace();
150:
151: String errMsg = NbBundle.getMessage(
152: StdDeploymentDescriptorParser.class,
153: "CANNOT_PARSE_STD_DD");
154: throw new EjbLoadException(EjbLoadException.USER_ERROR,
155: errMsg);
156: }
157: }
158:
159: public void startElement(String uri, String localName,
160: String qName, Attributes attributes) throws SAXException {
161: currentTag = qName;
162:
163: if (qName.equalsIgnoreCase(SESSION_TAG))
164: beanId = attributes.getValue(BEAN_ID_ATTR);
165: }
166:
167: public void endElement(String uri, String localName, String qName)
168: throws SAXException {
169:
170: // Set data
171: if (currentTag != null)
172: setData();
173:
174: if (qName.equals(SESSION_TAG)) {
175: if (homeName != null && remoteName != null
176: && ejbName != null && sessionType != null) {
177: EjbInfo ejbInfo = new EjbInfo();
178:
179: ejbInfo.setHomeInterfaceName(homeName);
180: ejbInfo.setCompInterfaceName(remoteName);
181: ejbInfo.setEjbName(ejbName);
182: ejbInfo.setBeanId(beanId);
183:
184: if (sessionType.equalsIgnoreCase("Stateless"))
185: ejbInfo.setBeanType(EjbInfo.STATELESS_SESSION_BEAN);
186: else
187: ejbInfo.setBeanType(EjbInfo.STATEFUL_SESSION_BEAN);
188:
189: // If either the home or the remote interface of the
190: // ejb does not have a package, then the ejb will be
191: // skipped with a log message
192: if (hasPackage(ejbInfo))
193: sessionBeans.add(ejbInfo);
194: else {
195: // EJB has been skipped because there is no package defined for its home or/and remote interface: {0}
196: ErrorManager
197: .getDefault()
198: .getInstance(
199: "org.netbeans.modules.visualweb.ejb.load")
200: .log(
201: ErrorManager.WARNING,
202: "EJB has been skipped because there is no package defined for its home or/and remote interface: "
203: + ejbInfo
204: .getCompInterfaceName());
205: skippedEjbs.add(ejbInfo.getCompInterfaceName());
206: }
207:
208: homeName = null;
209: remoteName = null;
210: ejbName = null;
211: }
212: }
213:
214: currentTag = null;
215: data = null;
216: }
217:
218: public void characters(char buf[], int offset, int len)
219: throws SAXException {
220: if (data == null)
221: data = new String(buf, offset, len);
222: else
223: data = data + new String(buf, offset, len);
224: }
225:
226: private void setData() {
227: if (data != null)
228: data = data.trim();
229:
230: if (currentTag.equalsIgnoreCase(HOME_TAG))
231: homeName = data;
232: else if (currentTag.equalsIgnoreCase(REMOTE_TAG))
233: remoteName = data;
234: else if (currentTag.equalsIgnoreCase(EJB_NAME_TAG))
235: ejbName = data;
236: else if (currentTag.equalsIgnoreCase(EJB_SESSION_TYPE_TAG))
237: sessionType = data;
238: }
239:
240: public InputSource resolveEntity(String publicId, String systemId) {
241: // Ignore any external entities
242: return new InputSource(new StringReader(""));
243: }
244:
245: public Collection getSkippedEjbs() {
246: return this .skippedEjbs;
247: }
248:
249: private boolean hasPackage(EjbInfo ejbInfo) {
250: // return false if either the home or the remote interface has no
251: // package defined
252: if (ejbInfo.getCompInterfaceName().indexOf('.') == -1
253: || ejbInfo.getHomeInterfaceName().indexOf('.') == -1)
254: return false;
255: else
256: return true;
257: }
258: }
|