KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ws7 > nodes > WS70Resource


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
20 /*
21  * WS70Resource.java
22  */

23
24 package org.netbeans.modules.j2ee.sun.ws7.nodes;
25
26 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
27 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
28 import org.netbeans.modules.j2ee.sun.ws7.Constants;
29 import org.openide.util.Lookup;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.Node;
32 import org.openide.nodes.Children;
33 import org.openide.util.NbBundle;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Arrays JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import org.openide.nodes.Sheet;
42 import org.openide.nodes.PropertySupport;
43 import java.beans.PropertyEditor JavaDoc;
44
45 import org.netbeans.modules.j2ee.sun.ws7.ide.editors.TaggedValue;
46 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePairsPropertyEditor;
47 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePair;
48 import org.netbeans.modules.j2ee.sun.ws7.dm.WS70SunDeploymentManager;
49 import org.netbeans.modules.j2ee.sun.ws7.j2ee.ResourceType;
50 import org.netbeans.modules.j2ee.sun.ws7.ui.Util;
51
52
53 /**
54  *
55  * @author Administrator
56  */

57 public class WS70Resource extends WS70ManagedObjectBase {
58     private String JavaDoc jndiName;
59     private ResourceType resType;
60     private String JavaDoc configName;
61     private HashMap JavaDoc attributes;
62     private WS70SunDeploymentManager manager;
63     private Map JavaDoc properties;
64     
65     /**
66      * Creates a new instance of WS70Resource
67      */

68     public WS70Resource(DeploymentManager JavaDoc manager, String JavaDoc config,
69                         HashMap JavaDoc attributes, ResourceType type) {
70         Object JavaDoc name = attributes.get("jndi-name");
71         if(name==null){
72             this.jndiName = "jndiname";
73         }else{
74             this.jndiName = (String JavaDoc)name;
75         }
76         
77         this.attributes = this.constructAttributes(attributes);
78         this.manager = (WS70SunDeploymentManager)manager;
79         configName = config;
80         resType = type;
81     }
82     
83     public String JavaDoc getJndiName(){
84         return jndiName;
85     }
86     public String JavaDoc getDisplayName(){
87         return getJndiName();
88     }
89     public void setProperties(String JavaDoc propType, Map JavaDoc props){
90         if(properties==null){
91             properties = new HashMap JavaDoc();
92         }
93         properties.put(propType, props);
94     }
95     public void deleteResource()throws Exception JavaDoc{
96         try{
97             manager.deleteResource(resType, configName, jndiName);
98         }catch(Exception JavaDoc ex){
99             throw ex;
100         }
101     }
102     public Attribute setAttribute(String JavaDoc attribute, Object JavaDoc value) throws Exception JavaDoc{
103         try{
104             HashMap JavaDoc map = new HashMap JavaDoc();
105             map.put(attribute, value.toString());
106             manager.setResource(resType, configName, getJndiName(), map, true);
107             if(attributes.containsKey(attribute)){
108                 attributes.put(attribute, value);
109             }
110         }catch(Exception JavaDoc ex){
111             throw ex;
112         }
113         return new Attribute(attribute, value);
114     }
115     private void setResourceProperty(String JavaDoc type, HashMap JavaDoc props, ArrayList JavaDoc list) throws Exception JavaDoc{
116         try{
117             manager.setUserResourceProp(configName, resType.toString(),
118                     getJndiName(), type, list, true);
119  
120         }catch(Exception JavaDoc ex){
121             throw ex;
122         }
123         properties.put(type, props);
124     }
125     
126
127     public Sheet updateSheet(Sheet sheet) {
128         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
129         try {
130             Set JavaDoc readOnlyResource = new HashSet JavaDoc(Arrays.asList(READ_ONLY_PROPS_RESOURCES));
131             for(Iterator JavaDoc itr = attributes.keySet().iterator(); itr.hasNext(); ) {
132                 Attribute a = (Attribute) itr.next();
133                 AttributeInfo attr = (AttributeInfo)attributes.get(a);
134                 String JavaDoc shortDescription = getShortDescription(attr);
135
136                 if (attr == null || ! attr.isReadable()) {
137                     continue;
138                 }
139                
140                 Class JavaDoc type = getSupportedType(attr.getType());
141                                 // make jndi-name/name of resource
142
// read only
143
if (readOnlyResource.contains(attr.getName())) {
144                     ps.put(createReadOnlyProperty(a, attr, shortDescription));
145                 } else {
146                     if (attr.isWritable()) {
147                         if (a.getValue() instanceof String JavaDoc[]) {
148                             String JavaDoc[] props = (String JavaDoc[])a.getValue();
149                             if (props != null) {
150                                 ps.put(getStringArrayEditor(a, attr,
151                                                             shortDescription,
152                                                             props.getClass(),
153                                                             true));
154                             } else {
155                                 ps.put(createReadOnlyProperty(a, attr,
156                                                               shortDescription));
157                             }
158                         } else if (type != null) {
159                             if (a.getValue() instanceof TaggedValue) {
160                                 ps.put(createTaggedProperty(a, attr, shortDescription, type));
161                             }else {
162                                 ps.put(createWritableProperty(a, attr, shortDescription, type));
163                             } // end of else
164

165                         } else {
166                             ps.put(createReadOnlyProperty(a, attr, shortDescription));
167                         }
168                     }else {
169                         if (a.getValue() instanceof String JavaDoc[]) {
170                             String JavaDoc[] props = (String JavaDoc[])a.getValue();
171
172                             if (props != null) {
173                                 ps.put(getStringArrayEditor(a, attr, shortDescription,
174                                                             props.getClass(), false));
175                             } else {
176                                 ps.put(createReadOnlyProperty(a, attr,
177                                                               shortDescription));
178                             }
179                         } else {
180                             ps.put(createReadOnlyProperty(a, attr,
181                                                           shortDescription));
182                         }
183                     }//attr is writable
184
} //else of resource name or jndi-name
185
} //for loop ends
186

187             // Get all user properties if properties is not null
188
if(properties!=null){
189                 HashMap JavaDoc props = (HashMap JavaDoc)properties.get(Constants.RES_PROPERTY);
190                 if(props!=null){
191                     ps.put(this.createExtraProperties(Constants.RES_PROPERTY, props));
192                 }
193                 props = (HashMap JavaDoc)properties.get(Constants.JDBC_RES_CONN_LEASE_PROPERTY);
194                 if(props!=null){
195                     ps.put(this.createExtraProperties(Constants.JDBC_RES_CONN_LEASE_PROPERTY, props));
196                 }
197                 props = (HashMap JavaDoc)properties.get(Constants.JDBC_RES_CONN_CREATION_PROPERTY);
198                 if(props!=null){
199                     ps.put(this.createExtraProperties(Constants.JDBC_RES_CONN_CREATION_PROPERTY, props));
200                 }
201             }
202             
203         } catch (Exception JavaDoc t) {
204             t.printStackTrace();
205         }
206
207         return sheet;
208     }
209     PropertySupport createExtraProperties(final String JavaDoc name, final Map JavaDoc props) {
210         return new PropertySupport.ReadWrite(
211             name,
212             NameValuePairsPropertyEditor.class, name,
213                 NbBundle.getMessage(WS70Resource.class, "DSC_ExtParams")){//NOI18N
214
Map JavaDoc values = props;
215             String JavaDoc type = name;
216             public Object JavaDoc getValue() {
217                 return values;
218             }
219               
220             public void setValue(Object JavaDoc obj) {
221                 if(obj instanceof Object JavaDoc[]){
222                     //Create a an array of updated properties
223
Object JavaDoc[] currentVal = (Object JavaDoc[])obj;
224                     
225                     HashMap JavaDoc propertyList = new HashMap JavaDoc();
226                     ArrayList JavaDoc list = new ArrayList JavaDoc();
227                     for(int i=0; i<currentVal.length; i++){
228                         NameValuePair pair = (NameValuePair)currentVal[i];
229                         propertyList.put(pair.getParamName(), pair.getParamValue());
230                         list.add(pair.getParamName()+"="+pair.getParamValue());
231                     }
232                     try{
233                         setResourceProperty(name, propertyList, list);
234                         values = propertyList;
235                     }catch(Exception JavaDoc ex){
236                         Util.showError(ex.getLocalizedMessage());
237                     }
238                 }
239             }
240             public PropertyEditor JavaDoc getPropertyEditor(){
241                 return new NameValuePairsPropertyEditor(values);
242             }
243         };
244     }//createExtraProperties
245
private HashMap JavaDoc constructAttributes(HashMap JavaDoc attrMap){
246         HashMap JavaDoc attributes = new HashMap JavaDoc();
247         for(Iterator JavaDoc itr = attrMap.keySet().iterator(); itr.hasNext(); ) {
248             String JavaDoc attrName = (String JavaDoc) itr.next();
249             Object JavaDoc attrValue = attrMap.get(attrName);
250             Attribute attr = null;
251             AttributeInfo attrInfo = null;
252             Set JavaDoc booleans = new HashSet JavaDoc(Arrays.asList(RESOURCE_BOOLEAN_VALS));
253             if(attrName.equals("jndi-name")){
254                 attr = new Attribute(attrName, attrValue);
255                 attrInfo = new AttributeInfo(attrName, "java.lang.String", null,
256                                                        true, false, false);
257             }else if(attrName.equals("isolation-level")){
258                 Object JavaDoc obj = TransactionIsolation.getValue((String JavaDoc)attrValue);
259                 attr = new Attribute(attrName, obj);
260                 attrInfo = new AttributeInfo(attrName, obj.getClass().getName(),
261                                                 null, true, true, false);
262                 
263             }/*else if(attrName.equals("isolation-level-guaranteed") ||
264                         attrName.equals("fail-all-connections") ||
265                         attrName.equals("enabled")){*/

266             else if(booleans.contains(attrName)){
267                 
268                 Object JavaDoc obj = Boolean.getValue((java.lang.Boolean JavaDoc)attrValue);
269                 attr = new Attribute(attrName, obj);
270                 attrInfo = new AttributeInfo(attrName, obj.getClass().getName(),
271                                                 null, true, true, true);
272                 
273             }else if(attrName.equals("connection-validation")){
274                 Object JavaDoc obj = ValidationMethod.getValue((String JavaDoc)attrValue);
275                 attr = new Attribute(attrName, obj);
276                 attrInfo = new AttributeInfo(attrName, obj.getClass().getName(),
277                                                 null, true, true, false);
278                 
279             }else{
280                 attr = new Attribute(attrName, attrValue);
281                 attrInfo = new AttributeInfo(attrName, "java.lang.String",
282                                                 null, true, true, false);
283                 
284             }
285             attributes.put(attr, attrInfo);
286         }
287         return attributes;
288     }
289     private NameValuePair[] getNameValuePairs(Object JavaDoc attrVal){
290         java.util.Map JavaDoc attributeMap = (java.util.Map JavaDoc)attrVal;
291         Set JavaDoc attributeKeys = attributeMap.keySet();
292         java.util.Iterator JavaDoc it = attributeKeys.iterator();
293         NameValuePair[] pairs = new NameValuePair[attributeKeys.size()];
294         int i=0;
295         while(it.hasNext()){
296             NameValuePair pair = new NameValuePair();
297             Object JavaDoc key = it.next();
298             pair.setParamName(key.toString());
299             pair.setParamValue(attributeMap.get(key).toString());
300             pairs[i] = pair;
301             i++;
302         }
303         return pairs;
304     }
305  
306     public static class ValidationMethod extends TaggedValue {
307         private String JavaDoc id;
308         
309         private ValidationMethod(String JavaDoc id) {
310             this.id = id;
311         }
312         private static final ValidationMethod FALSE =
313             new ValidationMethod("false"); // NOI18N
314
private static final ValidationMethod AUTO_COMMIT =
315             new ValidationMethod("auto-commit"); // NOI18N
316
private static final ValidationMethod META_DATA =
317             new ValidationMethod("meta-data"); // NOI18N
318
private static final ValidationMethod TABLE =
319             new ValidationMethod("table"); // NOI18N
320

321         private static final ValidationMethod[] values =
322             new ValidationMethod[]{ FALSE, AUTO_COMMIT, META_DATA, TABLE };
323         
324         public static TaggedValue getValue(String JavaDoc s) {
325             for (int i = 0; i < values.length; i++) {
326                 if (values[i].getId().equalsIgnoreCase(s)) {
327                     return values[i];
328                 } // end of if (values[i].getId().equalsIgnoreCase(s))
329
} // end of for (int i = 0; i < values.length; i++)
330

331             return null;
332         }
333
334         public static TaggedValue[] getChoices() {
335             return values;
336         }
337         
338         public String JavaDoc getId() {
339             return id;
340         }
341
342         public String JavaDoc toString() {
343             return id;
344         }
345     }
346     
347     public static class TransactionIsolation extends TaggedValue {
348         private String JavaDoc id;
349         
350         private TransactionIsolation(String JavaDoc id) {
351             this.id = id;
352         }
353         private static final TransactionIsolation DAFAULT =
354             new TransactionIsolation("default"); // NOI18N
355
private static final TransactionIsolation READ_UNCOMMITTED =
356             new TransactionIsolation("read-uncommitted"); // NOI18N
357
private static final TransactionIsolation READ_COMMITTED =
358             new TransactionIsolation("read-committed"); // NOI18N
359
private static final TransactionIsolation REPEATABLE_READ =
360             new TransactionIsolation("repeatable-read"); // NOI18N
361
private static final TransactionIsolation SERIALIZABLE =
362             new TransactionIsolation("serializable"); // NOI18N
363

364         private static final TransactionIsolation[] values =
365             new TransactionIsolation[]{ DAFAULT, READ_UNCOMMITTED,
366                                         READ_COMMITTED,
367                                         REPEATABLE_READ,
368                                         SERIALIZABLE };
369         
370         public static TaggedValue getValue(String JavaDoc s) {
371             for (int i = 0; i < values.length; i++) {
372                 if (values[i].getId().equalsIgnoreCase(s)) {
373                     return values[i];
374                 } // end of if (values[i].getId().equalsIgnoreCase(s))
375
} // end of for (int i = 0; i < values.length; i++)
376

377             return null;
378         }
379
380         public static TaggedValue[] getChoices() {
381             return values;
382         }
383         
384         public String JavaDoc getId() {
385             return id;
386         }
387
388         public String JavaDoc toString() {
389             return id;
390         }
391     }
392 }
393
Popular Tags