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: /*
043: * EjbDataModel.java
044: *
045: * Created on May 3, 2004, 11:38 PM
046: */
047:
048: package org.netbeans.modules.visualweb.ejb.datamodel;
049:
050: import java.util.*;
051:
052: /**
053: * The data model for all the ejbs loaded in the rave
054: *
055: * @author dongmei cao
056: */
057: public class EjbDataModel {
058: private static EjbDataModel instance = new EjbDataModel();
059:
060: // A flag to indicate whether the data model has been modified
061: // during the session.
062: private boolean modified = false;
063:
064: private Set ejbGroups = new HashSet();
065:
066: // The EjbDataModelListeners
067: private Set listeners;
068:
069: public static EjbDataModel getInstance() {
070: return instance;
071: }
072:
073: private EjbDataModel() {
074: }
075:
076: public EjbGroup findEjbGroupForJar(String jar) {
077: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
078: EjbGroup grp = (EjbGroup) iter.next();
079:
080: for (Iterator jarIter = grp.getClientJarFiles().iterator(); jarIter
081: .hasNext();) {
082: String jarFileName = org.netbeans.modules.visualweb.ejb.util.Util
083: .getFileName((String) jarIter.next());
084:
085: if (jarFileName.equals(jar))
086: return grp;
087: }
088: }
089:
090: return null;
091: }
092:
093: public Collection findEjbGroupsForJar(String jar) {
094: Collection grps = new ArrayList();
095: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
096: EjbGroup grp = (EjbGroup) iter.next();
097:
098: for (Iterator jarIter = grp.getClientJarFiles().iterator(); jarIter
099: .hasNext();) {
100: String jarFileName = org.netbeans.modules.visualweb.ejb.util.Util
101: .getFileName((String) jarIter.next());
102:
103: if (jarFileName.equals(jar))
104: grps.add(grp);
105: }
106: }
107:
108: return grps;
109: }
110:
111: /**
112: * Finds the EJB Group containing the given client jars
113: *
114: * @param jars a list of jar names (not path)
115: */
116: public EjbGroup findEjbGroupForJars(ArrayList jars) {
117: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
118: EjbGroup grp = (EjbGroup) iter.next();
119:
120: if (grp.getClientJarFiles().size() == jars.size()) {
121: // Lets first assume we found it
122: boolean foundIt = true;
123:
124: for (Iterator jarIter = grp.getClientJarFiles()
125: .iterator(); jarIter.hasNext();) {
126: String jarFileName = org.netbeans.modules.visualweb.ejb.util.Util
127: .getFileName((String) jarIter.next());
128:
129: if (!jars.contains(jarFileName)) {
130: foundIt = false;
131: break;
132: }
133: }
134:
135: if (foundIt)
136: return grp;
137: }
138: }
139:
140: return null;
141: }
142:
143: /**
144: * Finds the EJB Group containing the given client jars
145: *
146: * @param jars a list of jar names (not path)
147: */
148: public EjbGroup findEjbGroupForClientWrapperJar(
149: String wrapperJarName) {
150: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
151: EjbGroup grp = (EjbGroup) iter.next();
152:
153: String jarFileName = org.netbeans.modules.visualweb.ejb.util.Util
154: .getFileName(grp.getClientWrapperBeanJar());
155:
156: if (jarFileName.equals(wrapperJarName))
157: return grp;
158: }
159:
160: return null;
161: }
162:
163: /**
164: * Find the EJB with the given remote interface name
165: */
166: public EjbInfo findEjbInfo(String remote) {
167: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
168: EjbGroup grp = (EjbGroup) iter.next();
169: EjbInfo ejb = grp.getEjbInfo(remote);
170:
171: if (ejb != null)
172: return ejb;
173: }
174:
175: return null;
176: }
177:
178: /**
179: * Find the EJB group the EJB with the given remote interface
180: */
181: public EjbGroup findEjbGroup(String remote) {
182: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
183: EjbGroup grp = (EjbGroup) iter.next();
184: EjbInfo ejb = grp.getEjbInfo(remote);
185:
186: if (ejb != null)
187: return grp;
188: }
189:
190: return null;
191: }
192:
193: public EjbGroup getEjbGroup(String name) {
194: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
195: EjbGroup grp = (EjbGroup) iter.next();
196: if (grp.getName().equals(name))
197: return grp;
198: }
199:
200: return null;
201: }
202:
203: public Set getEjbGroups() {
204: return this .ejbGroups;
205: }
206:
207: public Collection getEjbGroupNames() {
208: ArrayList names = new ArrayList();
209:
210: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
211: EjbGroup grp = (EjbGroup) iter.next();
212: names.add(grp.getName());
213: }
214:
215: // Sort the names; then return
216: Collections.sort(names);
217: return names;
218: }
219:
220: /**
221: * Modify the ejb group
222: */
223: public void modifyEjbGroup(EjbGroup oldValue, EjbGroup newValue) {
224: EjbGroup group = getEjbGroup(oldValue.getName());
225: group.setName(newValue.getName());
226: group.setAppServerVendor(newValue.getAppServerVendor());
227: group.setClientJarFiles(newValue.getClientJarFiles());
228: group.setIIOPPort(newValue.getIIOPPort());
229: group.setServerHost(newValue.getServerHost());
230: group.setClientWrapperBeanJar(newValue
231: .getClientWrapperBeanJar());
232: group.setDesignInfoJar(newValue.getDesignInfoJar());
233:
234: // Notify the listeners about this new group
235: if (listeners != null && !listeners.isEmpty()) {
236: EjbDataModelEvent event = new EjbDataModelEvent(newValue);
237:
238: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
239: EjbDataModelListener listener = (EjbDataModelListener) iter
240: .next();
241: listener.groupChanged(event);
242: }
243: }
244:
245: touchModifiedFlag();
246: }
247:
248: public void refreshEjbGroup(EjbGroup group) {
249: // Notify the listeners aobut the possible changes
250: if (listeners != null && !listeners.isEmpty()) {
251: EjbDataModelEvent event = new EjbDataModelEvent(group);
252:
253: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
254: EjbDataModelListener listener = (EjbDataModelListener) iter
255: .next();
256: listener.groupChanged(event);
257: }
258: }
259:
260: touchModifiedFlag();
261: }
262:
263: public void addEjbGroups(Collection groups) {
264: if (ejbGroups == null)
265: ejbGroups = new HashSet(groups);
266: else {
267: for (Iterator iter = groups.iterator(); iter.hasNext();) {
268: addEjbGroup((EjbGroup) iter.next());
269: }
270: }
271:
272: touchModifiedFlag();
273: }
274:
275: // Add a new EjbGroup
276: public void addEjbGroup(EjbGroup newGroup) {
277: if (ejbGroups == null)
278: ejbGroups = new HashSet();
279:
280: // Make sure the name is unique
281: // Fix it if not
282: checkNameUniqueness(newGroup);
283:
284: ejbGroups.add(newGroup);
285:
286: // Notify the listeners about this new group
287: if (listeners != null && !listeners.isEmpty()) {
288: EjbDataModelEvent event = new EjbDataModelEvent(newGroup);
289:
290: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
291: EjbDataModelListener listener = (EjbDataModelListener) iter
292: .next();
293: listener.groupAdded(event);
294: }
295: }
296:
297: touchModifiedFlag();
298: }
299:
300: public void removeEjbGroup(String name) {
301: EjbGroup grp = getEjbGroup(name);
302: removeEjbGroup(grp);
303: }
304:
305: public void removeEjbGroup(EjbGroup group) {
306: ejbGroups.remove(group);
307:
308: // Notify the listeners about this remove
309: if (listeners != null && !listeners.isEmpty()) {
310: EjbDataModelEvent event = new EjbDataModelEvent(group);
311:
312: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
313: EjbDataModelListener listener = (EjbDataModelListener) iter
314: .next();
315: listener.groupDeleted(event);
316: }
317: }
318:
319: touchModifiedFlag();
320: }
321:
322: public void removeEjbGroups(Collection groups) {
323: ejbGroups.removeAll(groups);
324:
325: // Notify the listeners about this remove
326: if (listeners != null && !listeners.isEmpty()) {
327: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
328: EjbDataModelListener listener = (EjbDataModelListener) iter
329: .next();
330: listener.groupsDeleted();
331: }
332: }
333:
334: touchModifiedFlag();
335: }
336:
337: public void addListener(EjbDataModelListener listener) {
338: if (!(listener instanceof EjbDataModelListener))
339: throw new java.lang.IllegalArgumentException(
340: "Incorrect listener");
341:
342: if (listeners == null)
343: listeners = new HashSet();
344:
345: listeners.add(listener);
346: }
347:
348: public void removeListener(EjbDataModelListener listener) {
349: listeners.remove(listener);
350: }
351:
352: private void checkNameUniqueness(EjbGroup ejbGroup) {
353: Collection existingNames = getEjbGroupNames();
354: String name = ejbGroup.getName();
355:
356: if (!existingNames.contains(name))
357: return;
358:
359: // Name not unique. Then get a unique one
360: ejbGroup.setName(getAUniqueName(name));
361: }
362:
363: /**
364: * Append a number after the given name till the name is unique
365: *
366: * @param baseName The name the number will be appended on
367: */
368: public String getAUniqueName(String baseName) {
369: Collection existingNames = getEjbGroupNames();
370:
371: int count = 1;
372: String newName = baseName + count;
373: while (existingNames.contains(newName)) {
374: count++;
375: newName = baseName + count;
376: }
377:
378: return newName;
379: }
380:
381: public void resetModifiedFlag() {
382: this .modified = false;
383: }
384:
385: public void touchModifiedFlag() {
386: this .modified = true;
387: }
388:
389: public boolean isModified() {
390: return this.modified;
391: }
392: }
|