KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > config > ConfigBeanStorage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.share.config;
20 import java.util.*;
21 import java.beans.PropertyChangeListener JavaDoc;
22 import java.beans.PropertyChangeEvent JavaDoc;
23
24 import javax.enterprise.deploy.model.*;
25 import javax.enterprise.deploy.spi.*;
26 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
27
28 import org.openide.*;
29 import org.openide.nodes.*;
30
31 import org.netbeans.modules.j2ee.sun.share.config.ui.ConfigBeanNode;
32 import org.netbeans.modules.j2ee.sun.share.configbean.Base;
33 import org.netbeans.modules.j2ee.sun.share.configbean.DConfigBeanProperties;
34 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration;
35
36 /**
37  */

38 public class ConfigBeanStorage implements PropertyChangeListener JavaDoc, Comparable JavaDoc {
39     
40     private ConfigurationStorage storage;
41     DConfigBean bean;
42     private ConfigBeanStorage parent;
43     private ConfigBeanNode node = null;
44     private Map childMap = new HashMap();
45     
46     public ConfigBeanStorage(DConfigBean bean, ConfigBeanStorage parent, ConfigurationStorage storage) throws ConfigurationException JavaDoc{
47         this.bean = bean;
48         this.parent = parent;
49         this.storage = storage;
50         // need to ensure that the basebean events are caught?
51
// synchronize new CBS and event handling
52
initChildren();
53         // store itself in its BaseBean.
54
StandardDDImpl dd = (StandardDDImpl) bean.getDDBean();
55         dd.proxy.addConfigBean(this);
56         // PENDING set up listener on dd to implement notify()
57

58         bean.addPropertyChangeListener(this);
59     }
60     
61     public Map getChildMap() {
62         return childMap;
63     }
64     
65     public ConfigurationStorage getStorage() {
66         return storage;
67     }
68     
69     public synchronized Node getNode() {
70         //if (node == null)
71
node = new ConfigBeanNode(this);
72         return node;
73     }
74     
75     public void propertyChange(PropertyChangeEvent JavaDoc pce) {
76         if (storage != null) {
77             // Only set changed on true change events (as opposed to DISPLAY_NAME, which can
78
// be changed by a validation event and is not associated with a change of persistable data.)
79
if(!DConfigBeanProperties.PROP_DISPLAY_NAME.equalsIgnoreCase(pce.getPropertyName())) {
80                 storage.setChanged();
81             }
82         }
83         if (DConfigBeanProperties.PROP_DISPLAY_NAME.equalsIgnoreCase(pce.getPropertyName())) {
84             if(node != null) {
85                 node.setDisplayName((String JavaDoc) pce.getNewValue());
86             }
87         }
88     }
89     
90     public void remove() {
91         DDCommon dd = (DDCommon) ((StandardDDImpl)bean.getDDBean()).proxy;
92         dd.removeConfigBean(this);
93         if(parent != null) {
94             try {
95                 parent.bean.removeDConfigBean(bean);
96                 storage.setChanged();
97             } catch (Exception JavaDoc e) {
98                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
99             }
100         }
101     }
102     
103     private void initChildren() throws ConfigurationException JavaDoc {
104         String JavaDoc[] xpaths = bean.getXpaths();
105         if(xpaths == null) return;
106         for(int i = 0; i < xpaths.length; i++) {
107             DDBean[] beans = bean.getDDBean().getChildBean(xpaths[i]);
108             for(int j = 0; j < beans.length; j++) {
109                 addChild((StandardDDImpl) beans[j]);
110             }
111         }
112     }
113     
114     void fireEvent(String JavaDoc relPath, XpathEvent xe) throws ConfigurationException JavaDoc {
115         String JavaDoc[] xpaths = bean.getXpaths();
116         if(xpaths == null) return;
117         StandardDDImpl eventDD = (StandardDDImpl) xe.getBean();
118         StandardDDImpl[] targetDDs = null;
119         for(int i = 0 ; i < xpaths.length; i++) {
120             if(xpaths[i].equals(relPath) || xpaths[i].equals(xe.getBean().getXpath())) {
121                 targetDDs = new StandardDDImpl[] { eventDD };
122                 break;
123             }
124         }
125         HashSet targetSet = new HashSet();
126         for (int i=0; targetDDs == null && i < xpaths.length; i++) {
127             // IZ 73849 - Make sure that relative path is a true xpath prefix which means
128
// that if xpath[i] starts with, but is longer than the relative path, then the
129
// character immediately following the prefix must be a slash. i.e.
130
//
131
// xpath == relpath OR
132
// xpath == relpath + "/" + remainder of xpath
133
//
134
if(xpaths[i].startsWith(relPath) &&
135                     (xpaths[i].length() == relPath.length() || xpaths[i].charAt(relPath.length()) == '/')) {
136                 String JavaDoc targetPath = DDCommon.getRelativePath(xpaths[i], relPath);
137                 DDBean[] dds = eventDD.getChildBean(targetPath);
138                 if (dds == null)
139                     continue;
140                 for (int j=0; j<dds.length; j++) {
141                     if (!(dds[j] instanceof StandardDDImpl)) {
142                         continue;
143                     }
144                     targetSet.add(dds[j]);
145                 }
146                 if (targetSet.size() > 0) {
147                     targetDDs = (StandardDDImpl[]) targetSet.toArray(new StandardDDImpl[targetSet.size()]);
148                 }
149             }
150         }
151         if (targetDDs != null) {
152             for (int i=0; i<targetDDs.length; i++) {
153                 if (xe.isAddEvent()) {
154                     addChild(targetDDs[i]);
155                 } else {
156                     removeChild(targetDDs[i]);
157                 }
158             }
159         }
160     }
161     
162     public static final String JavaDoc RESOURCE_REF = "resource-ref"; //NOI18N
163

164     private void addChild(StandardDDImpl dd) throws ConfigurationException JavaDoc {
165         DConfigBean cb = bean.getDConfigBean(dd);
166         if(cb == null) {
167             return;
168         }
169         if (RESOURCE_REF.equals(dd.proxy.dtdname)) {
170             DeploymentConfiguration dc = storage.getDeploymentConfiguration();
171             if(dc instanceof SunONEDeploymentConfiguration) {
172                 SunONEDeploymentConfiguration config = (SunONEDeploymentConfiguration) dc;
173                 config.ensureResourceDefined(dd);
174             }
175         }
176         ConfigBeanStorage cbs = new ConfigBeanStorage(cb, this, storage);
177         Collection c = (Collection) childMap.get(dd.getXpath());
178         if(c == null) {
179             c = new TreeSet();
180             childMap.put(dd.getXpath(), c);
181         }
182         c.add(cbs);
183         fireChildBeanAddedEvent(cbs);
184     }
185     
186     private void removeChild(DDBean remBean) {
187         Collection c = (Collection) childMap.get(remBean.getXpath());
188         if(c == null) {
189             return;
190         }
191         for(Iterator i = c.iterator(); i.hasNext(); ) {
192             ConfigBeanStorage cbs = (ConfigBeanStorage) i.next();
193             if (cbs.bean.getDDBean().equals(remBean)) {
194                 cbs.remove();
195                 i.remove();
196                 fireChildBeanRemovedEvent(cbs);
197             }
198         }
199     }
200
201     /** Ordering for ConfigBeanStorage intances. This is so we can use TreeSet or
202      * some other ordered set to store these objects and offer a reasonable ordering
203      * in the Configuration Editor UI.
204      */

205     public int compareTo(Object JavaDoc o) {
206         if(o == null) {
207             throw new NullPointerException JavaDoc("Null argument passed to ConfigBeanStorage.compareTo()"); // NOI18N
208
}
209         
210         ConfigBeanStorage target = (ConfigBeanStorage) o;
211         int result = -1;
212         if(this == o) {
213             result = 0;
214         } else if(bean instanceof Base && target.bean instanceof Base) {
215             Base sourceBean = (Base) bean;
216             Base targetBean = (Base) target.bean;
217             
218             result = sourceBean.getDisplayName().compareTo(targetBean.getDisplayName());
219
220             if(result == 0) {
221                 result = sourceBean.getIdentity().compareTo(targetBean.getIdentity());
222             }
223         } else {
224             throw new IllegalStateException JavaDoc("Source or target bean is null or not derived from Base."); // NOI18N
225
}
226         
227         return result;
228     }
229     
230     public DConfigBean getConfigBean() {
231         return bean;
232     }
233     
234     public static interface ChildrenChangeListener {
235         public void childBeanAdded(ConfigBeanStorage childBeanStorage);
236         public void childBeanRemoved(ConfigBeanStorage childBeanStorage);
237     }
238     
239     private List childrenChangeListeners = new Vector();
240     public void addChildrenChangeListener(ChildrenChangeListener l) {
241         childrenChangeListeners.add(l);
242     }
243     public void removeChildrenChangeListener(ChildrenChangeListener l) {
244         childrenChangeListeners.remove(l);
245     }
246     private void fireChildBeanAddedEvent(ConfigBeanStorage childBeanStorage) {
247         for (Iterator i=childrenChangeListeners.iterator(); i.hasNext();) {
248             ChildrenChangeListener l = (ChildrenChangeListener) i.next();
249             l.childBeanAdded(childBeanStorage);
250         }
251     }
252     private void fireChildBeanRemovedEvent(ConfigBeanStorage childBeanStorage) {
253         for (Iterator i=childrenChangeListeners.iterator(); i.hasNext();) {
254             ChildrenChangeListener l = (ChildrenChangeListener) i.next();
255             l.childBeanRemoved(childBeanStorage);
256         }
257     }
258
259 }
260
Popular Tags