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: * EjbRefMaintainer.java
043: *
044: * Created on June 16, 2004, 12:15 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb;
048:
049: import org.netbeans.modules.visualweb.ejb.datamodel.EjbGroup;
050: import org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlCreator;
051: import org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlParser;
052: import org.netbeans.modules.visualweb.ejb.util.Util;
053: import java.io.BufferedWriter;
054: import java.io.File;
055: import java.io.FileOutputStream;
056: import java.io.OutputStreamWriter;
057: import java.util.*;
058: import org.openide.ErrorManager;
059:
060: /**
061: * This class is to maintain the ejb ref xml file the project-data/ejb-sources directory
062: *
063: * @author cao
064: */
065: public class EjbRefMaintainer {
066: // The ejb-ref.xml
067: private String xmlFile;
068:
069: public EjbRefMaintainer(String ejbRefXmlFile) {
070: this .xmlFile = ejbRefXmlFile;
071: }
072:
073: /**
074: * Add the given ejb group to the ejb-refs.xml if not there yet
075: *
076: * @param ejbGroup the ejb group to be added to the xml file
077: */
078: public void addToEjbRefXml(EjbGroup ejbGroup) {
079: try {
080: // It is possible that this is the very first group we are adding
081: Collection referredEjbGrps = null;
082: if ((new File(xmlFile).exists())) {
083: EjbDataSourceXmlParser parser = new EjbDataSourceXmlParser(
084: xmlFile);
085: referredEjbGrps = parser.parse();
086: } else
087: referredEjbGrps = new HashSet();
088:
089: // Check whether this group is already added. We can check the existence based on the client wrapper bean jar
090: // name since it does not change
091: for (Iterator iter = referredEjbGrps.iterator(); iter
092: .hasNext();) {
093: if (Util.getFileName(
094: ((EjbGroup) iter.next())
095: .getClientWrapperBeanJar()).equals(
096: Util.getFileName(ejbGroup
097: .getClientWrapperBeanJar())))
098: // Found it
099: return;
100: }
101:
102: referredEjbGrps.add(ejbGroup);
103:
104: writeToFile(referredEjbGrps);
105: } catch (Exception e) {
106: ErrorManager
107: .getDefault()
108: .getInstance(
109: "org.netbeans.modules.visualweb.ejb.EjbRefMaintainer")
110: .log(ErrorManager.ERROR, e.getMessage());
111: e.printStackTrace();
112: }
113: }
114:
115: /**
116: * Remove the ejb groups from the ejb-refs.xml if all the client jars in the group
117: * are in given removed jars
118: *
119: * @param jars a list of jar files. The jar files in the list should be
120: * only the filename (not the whole path)
121: */
122: public void removeFromEjbRefXml(ArrayList removedJars) {
123: try {
124: EjbDataSourceXmlParser parser = new EjbDataSourceXmlParser(
125: xmlFile);
126: Collection referredEjbGrps = parser.parse();
127:
128: for (Iterator iter = referredEjbGrps.iterator(); iter
129: .hasNext();) {
130: EjbGroup grp = (EjbGroup) iter.next();
131:
132: // Only if all the client jars in this group got removed, then this
133: // group will get removed from the ejb-refs.xml
134: boolean derefIt = true;
135: for (Iterator jarIter = grp.getClientJarFiles()
136: .iterator(); jarIter.hasNext();) {
137: String jarFileName = org.netbeans.modules.visualweb.ejb.util.Util
138: .getFileName((String) jarIter.next());
139: if (!removedJars.contains(jarFileName)) {
140: derefIt = false;
141: break;
142: }
143: }
144:
145: if (derefIt)
146: iter.remove();
147: }
148:
149: // Write the remaining groups back the file
150: writeToFile(referredEjbGrps);
151: } catch (Exception e) {
152: ErrorManager
153: .getDefault()
154: .getInstance(
155: "org.netbeans.modules.visualweb.ejb.EjbRefMaintainer")
156: .log(ErrorManager.ERROR, e.getMessage());
157: e.printStackTrace();
158: }
159: }
160:
161: public Collection getRefferedEjbGroups() {
162: try {
163: EjbDataSourceXmlParser parser = new EjbDataSourceXmlParser(
164: xmlFile);
165: return parser.parse();
166: } catch (Exception e) {
167: ErrorManager
168: .getDefault()
169: .getInstance(
170: "org.netbeans.modules.visualweb.ejb.EjbRefMaintainer")
171: .log(ErrorManager.ERROR, e.getMessage());
172: e.printStackTrace();
173: return null;
174: }
175: }
176:
177: /**
178: * Updates the given ejb group
179: *
180: * @param group the ejb group to be updated
181: */
182: public void updateEjbRefXml(EjbGroup oldGrp, EjbGroup modGrp) {
183: try {
184: // Get the ejb groups in the ejb-refs.xml
185: EjbDataSourceXmlParser parser = new EjbDataSourceXmlParser(
186: xmlFile);
187: Collection referredEjbGrps = parser.parse();
188:
189: // Remove the old one; then add the modified one
190:
191: for (Iterator iter = referredEjbGrps.iterator(); iter
192: .hasNext();) {
193: EjbGroup grp = (EjbGroup) iter.next();
194:
195: if (grp.getName().equals(oldGrp.getName())) {
196: // Remove the out of date one
197: iter.remove();
198: break;
199: }
200: }
201:
202: // Add the modified one
203: referredEjbGrps.add(modGrp);
204:
205: // Wirte the updated groups back to file
206: writeToFile(referredEjbGrps);
207: } catch (Exception e) {
208: ErrorManager
209: .getDefault()
210: .getInstance(
211: "org.netbeans.modules.visualweb.ejb.EjbRefMaintainer")
212: .log(ErrorManager.WARNING, e.getMessage());
213: e.printStackTrace();
214: }
215: }
216:
217: public void updateEjbRefXml(Collection ejbGroups) {
218: writeToFile(ejbGroups);
219: }
220:
221: /**
222: * Wirte the ejb groups to the xml file
223: *
224: * @param ejbGroups a collection of ejb groups to be written to the xml file
225: */
226: private void writeToFile(Collection ejbGroups) {
227: try {
228: File file = new File(this .xmlFile);
229: if (!file.exists())
230: file.createNewFile();
231:
232: OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
233: new FileOutputStream(file), "UTF-8");
234: BufferedWriter bufferWriter = new BufferedWriter(
235: outputStreamWriter);
236:
237: EjbDataSourceXmlCreator xmlCreator = new EjbDataSourceXmlCreator(
238: ejbGroups, bufferWriter);
239: xmlCreator.toXml();
240: bufferWriter.flush();
241: bufferWriter.close();
242: } catch (java.io.IOException ex) {
243: // What to do
244: ex.printStackTrace();
245: }
246: }
247:
248: /**
249: * Finds the ejb group in the ejb-refs.xml for the given jar file
250: *
251: * @param jar should be only the jar filename, not the whole path
252: * @return returns the ejb group which contains the given jar file
253: */
254: public EjbGroup findReferredEjbGroup(String jar) {
255: try {
256: EjbDataSourceXmlParser parser = new EjbDataSourceXmlParser(
257: xmlFile);
258: Collection referredEjbGrps = parser.parse();
259:
260: for (Iterator iter = referredEjbGrps.iterator(); iter
261: .hasNext();) {
262: EjbGroup grp = (EjbGroup) iter.next();
263:
264: for (Iterator jarIter = grp.getClientJarFiles()
265: .iterator(); jarIter.hasNext();) {
266: String jarFileName = org.netbeans.modules.visualweb.ejb.util.Util
267: .getFileName((String) jarIter.next());
268:
269: if (jarFileName.equals(jar))
270: return grp;
271: }
272: }
273:
274: return null;
275: } catch (Exception e) {
276: // Having problem find the ejb group. It's ok. Return null;
277: e.printStackTrace();
278: return null;
279: }
280: }
281: }
|