KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > tree > DynamicTreeNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.admingui.tree;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37
38 import com.iplanet.jato.RequestContext;
39 import com.iplanet.jato.util.NonSyncStringBuffer;
40
41 import com.sun.enterprise.tools.admingui.util.Util;
42 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
43 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
44
45 public class DynamicTreeNode extends IndexTreeNode implements Serializable JavaDoc {
46     
47     public DynamicTreeNode(IndexTreeNode parent, String JavaDoc type, String JavaDoc name, IndexTreeModel model) {
48     super(parent, type, name, model);
49     }
50     
51     protected void ensureChildren() {
52         if (dynamicChild == null)
53             return;
54         if (getRefresh() == false)
55             return;
56         try {
57             HashSet JavaDoc newNames = new HashSet JavaDoc();
58             updateKids(dynamicChild, newNames);
59             removeDeletedNodes(newNames);
60         } catch (Exception JavaDoc ex) {
61         if (Util.isLoggableINFO()) {
62         Util.logINFO("ERROR in IndexTreeNode.ensureChildren.", ex);
63         }
64         }
65     setRefresh(false);
66     }
67     
68     private void updateKids(Element JavaDoc element, HashSet JavaDoc newNames) {
69         HashMap JavaDoc params = new HashMap JavaDoc();
70         loadParams(element, params);
71                 
72         Object JavaDoc[] objs = getChildObjectNames(params);
73         if (objs == null)
74             return;
75         
76         if (objs instanceof ObjectName JavaDoc[]) {
77             ObjectName JavaDoc[] objectNames = (ObjectName JavaDoc[]) objs;
78
79             for (int i = 0; i < objectNames.length; i++) {
80                 if (!isChildValid(objectNames[i]))
81                     continue;
82
83                 String JavaDoc name = null;
84                 String JavaDoc attributeName = (String JavaDoc)params.get("attributeName");
85                 String JavaDoc attrNameMethod = (String JavaDoc)params.get("attrNameMethod");
86                 if (attributeName != null && attributeName.length() > 0) {
87                     name = (String JavaDoc)MBeanUtil.getAttribute(objectNames[i],
88                                 attributeName);
89                 } else if (attrNameMethod != null && attrNameMethod.length() > 0) {
90                     // This is for monitoring MBeans, they don't have a name attr,
91
// but perhaps a getName() method.
92
name = (String JavaDoc)MBeanUtil.invoke(objectNames[i],
93                         attrNameMethod, null, null);
94                 }
95                 if (name == null) {
96                     // fallback to the object name
97
name = (String JavaDoc) objectNames[i].toString();
98                 }
99                 newNames.add(name);
100                 createNode(element, name, objectNames[i]);
101             }
102         } else if (objs instanceof String JavaDoc[]) {
103             String JavaDoc[] objectNames = (String JavaDoc[]) objs;
104             String JavaDoc isWebService = (String JavaDoc)params.get("isWebService");
105             for (int i = 0; i < objectNames.length; i++) {
106                 String JavaDoc name = objectNames[i];
107                 newNames.add(name);
108                 IndexTreeNode node = createNode(element, name, objectNames[i]);
109                 if (node != null && isWebService != null && isWebService.equals("true")) {
110                     node.setAttribute("webServiceKey", webServiceKeyMap.get(name));
111                     node.setAttribute("webServiceName", name);
112                 }
113             }
114         } else {
115             if (Util.isLoggableINFO()) {
116                 NonSyncStringBuffer buf = new NonSyncStringBuffer();
117                 buf.append("Illegal type for tree processing:\n");
118                 buf.append(" TYPE: "+objs.getClass().getName()+"\n");
119                 buf.append(" OBJECT NAME: "+params.get("objectName")+"\n");
120                 buf.append(" METHOD NAME: "+params.get("methodName")+"\n");
121                 Util.logINFO(buf.toString());
122             }
123             throw new RuntimeException JavaDoc("Illegal type for tree processing");
124         }
125     }
126     
127     private Object JavaDoc[] getChildObjectNames(HashMap JavaDoc params) {
128         String JavaDoc objectName = (String JavaDoc)params.get("objectName");
129         String JavaDoc isWebService = (String JavaDoc)params.get("isWebService");
130         Object JavaDoc[] o = null;
131         if (isWebService != null && isWebService.equals("true")) {
132             webServiceKeyMap.clear();
133             String JavaDoc attributeName = (String JavaDoc)params.get("attributeName");
134             Map JavaDoc m = (Map JavaDoc)MBeanUtil.getAttribute(objectName, attributeName);
135             HashSet JavaDoc names = new HashSet JavaDoc();
136             // If a web service has the same name, append the application name
137
// to make unique. ie, "HelloIF (jaxrpc-simple)"
138
HashSet JavaDoc duplicateNames = new HashSet JavaDoc();
139             for (Iterator JavaDoc iter = m.entrySet().iterator();iter.hasNext();) {
140                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
141                 String JavaDoc name = (String JavaDoc)entry.getValue();
142                 webServiceKeyMap.put(name, (String JavaDoc)entry.getKey());
143                 if (!names.add(name)) {
144                     duplicateNames.add(name);
145                     String JavaDoc fullName = (String JavaDoc)entry.getKey();
146                     String JavaDoc appName = fullName.substring(0, fullName.indexOf('#'));
147                     names.add(name + " ("+ appName +")");
148                     webServiceKeyMap.put(name + " ("+ appName +")", (String JavaDoc)entry.getKey());
149                 }
150             }
151             for (Iterator JavaDoc iter = m.entrySet().iterator();iter.hasNext();) {
152                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
153                 String JavaDoc name = (String JavaDoc)entry.getValue();
154                 if (duplicateNames.contains(name)) {
155                     names.remove(name);
156                     String JavaDoc fullName = (String JavaDoc)entry.getKey();
157                     String JavaDoc appName = fullName.substring(0, fullName.indexOf('#'));
158                     names.add(name + " ("+ appName +")");
159                     webServiceKeyMap.put(name + " ("+ appName +")", (String JavaDoc)entry.getKey());
160                 }
161             }
162             o = (String JavaDoc[])names.toArray(new String JavaDoc[m.size()]);
163         } else {
164         
165             if (objectName == null || objectName.length() == 0) {
166                 throw new RuntimeException JavaDoc(
167                 "Object name parameter is required for tree node: "+getName());
168             }
169             objectName = replaceTokens(objectName);
170             String JavaDoc methodName = (String JavaDoc)params.get("methodName");
171             if (methodName == null || methodName.length() == 0) {
172                 throw new RuntimeException JavaDoc(
173                 "Method name parameter is required for tree node: "+getName());
174             }
175             String JavaDoc passParams = (String JavaDoc)params.get("passParams");
176             Object JavaDoc[] paramsArray = null;
177             String JavaDoc[] typesArray = null;
178             if (passParams != null && passParams.equals("false")) {
179                 paramsArray = null;
180                 typesArray = null;
181             } else {
182                 paramsArray = new Object JavaDoc[1];
183                 typesArray = new String JavaDoc[1];
184                 String JavaDoc paramData = (String JavaDoc)params.get("paramData");
185                 paramsArray[0] = replaceTokens(paramData);
186                 typesArray[0] = (String JavaDoc)params.get("paramType");
187                 if (typesArray[0] == null) {
188                     typesArray[0] = "java.lang.String";
189                 }
190                 if (typesArray[0].equals("boolean") || typesArray[0].equals("java.lang.Boolean")) {
191                     paramsArray[0] = new Boolean JavaDoc(paramsArray[0].toString());
192                 } else if(typesArray[0].equals("int") || typesArray[0].equals("java.lang.Integer")) {
193                     paramsArray[0] = new Integer JavaDoc(paramsArray[0].toString());
194                 }
195             }
196             o = (Object JavaDoc[])MBeanUtil.invoke(objectName, methodName, paramsArray, typesArray);
197         }
198         return o;
199     }
200     
201     private Map JavaDoc webServiceKeyMap = new HashMap JavaDoc();
202 }
203
Popular Tags