KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > SystemPropsHandlers


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 /*
25  * TableHandlers.java
26  *
27  * Created on March 16, 2004, 3:13 PM
28  */

29
30 package com.sun.enterprise.tools.admingui.handlers;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Enumeration JavaDoc;
35
36 import com.iplanet.jato.RequestContext;
37 import com.iplanet.jato.RequestContextImpl;
38 import com.iplanet.jato.RequestManager;
39 import com.iplanet.jato.ViewBeanManager;
40 import com.iplanet.jato.model.DefaultModel;
41 import com.iplanet.jato.model.Model;
42 import com.iplanet.jato.util.RootCauseException;
43 import com.iplanet.jato.view.ContainerView;
44 import com.iplanet.jato.view.ContainerViewBase;
45 import com.iplanet.jato.view.View;
46 import com.iplanet.jato.view.ViewBase;
47 import com.iplanet.jato.view.ViewBean;
48 import com.iplanet.jato.view.DisplayField;
49 import com.iplanet.jato.model.ModelControlException;
50 import com.iplanet.jato.view.event.ChildContentDisplayEvent;
51 import com.iplanet.jato.util.NonSyncStringBuffer;
52
53 import javax.management.MBeanException JavaDoc;
54 import javax.management.ObjectName JavaDoc;
55 import javax.management.AttributeList JavaDoc;
56 import javax.management.Attribute JavaDoc;
57
58 import javax.servlet.ServletRequest JavaDoc;
59
60 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
61 import com.sun.enterprise.tools.guiframework.model.ModelManager;
62 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
63 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
64 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager;
65 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
66 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
67 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent;
68 import com.sun.enterprise.tools.guiframework.view.event.ErrorEvent;
69
70 import com.sun.web.ui.model.CCActionTableModelInterface;
71 import com.sun.web.ui.view.html.CCButton;
72 import com.sun.web.ui.taglib.pagetitle.CCPageTitleTag;
73 import com.sun.web.ui.taglib.html.CCTextFieldTag;
74
75 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
76 import com.sun.enterprise.tools.admingui.util.Util;
77
78 import com.sun.enterprise.tools.admingui.ConfigProperties;
79
80
81 public class SystemPropsHandlers {
82     
83     static public String JavaDoc extractToken(String JavaDoc token) {
84         if (token == null)
85             return null;
86         int j = token.indexOf("${");
87         if (j >= 0) {
88             int i = token.indexOf('}');
89             if (i < 0) i = token.length();
90             return token.substring(j+2,i);
91         }
92         return token;
93     }
94     
95     static public String JavaDoc resolveToken(String JavaDoc token) {
96         String JavaDoc[] tokenTypes = new String JavaDoc[]{"java.lang.String", "java.lang.String"};
97         Object JavaDoc[] tokensParams = new Object JavaDoc[]{"${"+token+"}", "server"};
98         String JavaDoc value = (String JavaDoc) MBeanUtil.invoke(
99             "com.sun.appserv:type=domain,category=config", "resolveTokens",
100             tokensParams, tokenTypes);
101         return value;
102     }
103     
104     static public String JavaDoc resolveAndReplaceToken(String JavaDoc tokenString) {
105         String JavaDoc token = null;
106         if (tokenString != null && tokenString.indexOf("${") >= 0)
107             token = extractToken(tokenString);
108         if (token == null)
109             return tokenString;
110         return tokenString.replaceAll("\\$\\{"+token+"\\}", resolveToken(token));
111     }
112
113     public void isConfigProperty(RequestContext ctx, HandlerContext handlerCtx) {
114         String JavaDoc property = (String JavaDoc)handlerCtx.getInputValue("property");
115         boolean result = false;
116     //may be we've to check property.startsWith instead of indexOf.
117
//Leaving it for now, change this after HCF.
118
if (property != null)
119             result = property.indexOf("com.sun.aas") >= 0;
120     handlerCtx.setOutputValue("result", (result)?("true"):("false"));
121     }
122
123     public void loadConfigPropertyHelp(RequestContext ctx, HandlerContext handlerCtx) {
124         CCActionTableModelInterface model = (CCActionTableModelInterface)
125             handlerCtx.getInputValue(MODEL);
126         ((DefaultModel)model).clear();
127         String JavaDoc paramName = (String JavaDoc)handlerCtx.getInputValue("paramName");
128
129         if (paramName == null) {
130             return;
131         }
132         String JavaDoc baseName = "com.sun.enterprise.tools.admingui.resources.AsEnv";
133         String JavaDoc description = Util.getMessage(baseName, paramName, null);
134         model.appendRow();
135         model.setValue("propertyName", paramName);
136         model.setValue("description", description);
137     }
138     
139     public void loadTokenEditTable(RequestContext ctx, HandlerContext handlerCtx) {
140     CCActionTableModelInterface model = (CCActionTableModelInterface)
141             handlerCtx.getInputValue(MODEL);
142         ((DefaultModel)model).clear();
143
144         String JavaDoc configObjectName = (String JavaDoc)handlerCtx.getInputValue("configName");
145         String JavaDoc paramName = (String JavaDoc)handlerCtx.getInputValue("paramName");
146         if (paramName == null)
147             return;
148         // remove the ${}
149
paramName = extractToken(paramName);
150         
151         ObjectName JavaDoc[] refs = null;
152         try {
153             refs = (ObjectName JavaDoc[])MBeanUtil.invoke(configObjectName,
154                 "listReferencees", null, null);
155         } catch (Exception JavaDoc ex) {
156             // This special case handling is for when the $-paramters are outside
157
// a config and configName is not define, e.g. Domain attributes page.
158
model.appendRow();
159             model.setValue("clusterName", "");
160             model.setValue("instanceName", "");
161             model.setValue("value", resolveToken(paramName));
162             //model.setValue("objectName", reference.toString()); // hidden variable
163
return;
164         }
165         loadSystemProperties(model, refs, paramName, "");
166     }
167     
168     private void loadSystemProperties(CCActionTableModelInterface model,
169         ObjectName JavaDoc[] refs, String JavaDoc paramName, String JavaDoc clusterName) {
170             
171         if (refs == null)
172             return;
173         Object JavaDoc[] params = new Object JavaDoc[]{new Boolean JavaDoc(true)};
174         String JavaDoc[] types = new String JavaDoc[]{"boolean"};
175         String JavaDoc[] tokenTypes = new String JavaDoc[]{"java.lang.String", "java.lang.String"};
176
177         for (int i=0; i<refs.length; i++) {
178             ObjectName JavaDoc reference = refs[i];
179             //System.out.println("............target: "+reference);
180
//String referenceStr = reference.toString().replaceAll("null", "com.sun.appserv");
181
if (reference.getKeyProperty("type").equals("cluster")) {
182                 ObjectName JavaDoc[] instances = (ObjectName JavaDoc[]) MBeanUtil.invoke(reference,
183                     "listServerInstances", null, null);
184                 clusterName = reference.getKeyProperty("name");
185                 loadSystemProperties(model, instances, paramName, clusterName);
186                 continue;
187             }
188             Properties JavaDoc props = (Properties JavaDoc)MBeanUtil.invoke(reference,
189                 "listSystemProperties", params, types);
190             //System.out.println("............ "+props);
191
if (props == null) {
192                 continue;
193             }
194             String JavaDoc instanceName = reference.getKeyProperty("name");
195             String JavaDoc value = props.getProperty(paramName, null);
196             if (value == null) {
197                 // Value will be null if the property is a config property
198
// instead of a system property.
199
Object JavaDoc[] tokensParams = new Object JavaDoc[]{"${"+paramName+"}", instanceName};
200                 value = (String JavaDoc) MBeanUtil.invoke(
201                     "com.sun.appserv:type=domain,category=config", "resolveTokens",
202                     tokensParams, tokenTypes);
203             }
204             if (value == null) value = "";
205             //System.out.println("............ "+paramName+"="+value);
206
model.appendRow();
207             model.setValue("clusterName", clusterName);
208             model.setValue("instanceName", instanceName);
209             model.setValue("value", value);
210             model.setValue("objectName", reference.toString()); // hidden variable
211
}
212     }
213     
214     public void saveTokenEditTable(RequestContext ctx, HandlerContext handlerCtx)
215             throws ModelControlException {
216     View view = handlerCtx.getView();
217     DescriptorContainerView descView = (DescriptorContainerView)
218         (((ViewBase)view).getParentViewBean());
219     ViewDescriptor vd = descView.getViewDescriptor();
220         
221         String JavaDoc[] deleteTypes = new String JavaDoc[]{"java.lang.String"};
222         String JavaDoc[] createTypes = new String JavaDoc[]{"java.util.Properties"};
223         Object JavaDoc[] params = null;
224         
225         String JavaDoc tokenValue = (String JavaDoc) handlerCtx.getInputValue("tokenValue");
226         tokenValue = extractToken(tokenValue);
227         if (tokenValue == null)
228             return;
229         CCActionTableModelInterface model =
230             (CCActionTableModelInterface)handlerCtx.getInputValue(MODEL);
231         model.beforeFirst();
232         
233         while(model.next()) {
234             String JavaDoc objectName = (String JavaDoc) model.getValue("objectName");
235             String JavaDoc value = (String JavaDoc) model.getValue("value");
236             //System.out.print("saveTokens, on Object: "+objectName);
237
//System.out.println(" "+tokenValue+"="+value);
238

239             try {
240                 params = new Object JavaDoc[]{tokenValue};
241                 MBeanUtil.invoke(objectName, "deleteSystemProperty", params, deleteTypes);
242             } catch (Exception JavaDoc ex) {
243                 // ignore
244
}
245             if (value != null && value.length() > 0) {
246                 Properties JavaDoc newProp = new Properties JavaDoc();
247                 newProp.setProperty(tokenValue, value);
248                 params = new Object JavaDoc[]{newProp};
249                 MBeanUtil.invoke(objectName, "createSystemProperties", params, createTypes);
250             }
251         }
252     }
253     
254     public void beginTokensPageTitleDisplay(RequestContext ctx, HandlerContext handlerCtx) {
255         CCPageTitleTag tag = (CCPageTitleTag)handlerCtx.getEvent().getSource();
256         String JavaDoc titleKey = (String JavaDoc) handlerCtx.getInputValue("titleKey");
257         String JavaDoc params = (String JavaDoc) handlerCtx.getInputValue("params");
258
259         if(titleKey == null || titleKey.length() == 0) {
260             titleKey = "tokens.ConfigPageTitle";
261         }
262
263         String JavaDoc msg = Util.getMessage(titleKey, new Object JavaDoc[]{params});
264         tag.setPageTitleText(msg);
265     }
266     
267     public void removeDollarSign(RequestContext ctx, HandlerContext handlerCtx) {
268         String JavaDoc in = (String JavaDoc) handlerCtx.getInputValue("systemProp");
269         handlerCtx.setOutputValue("result", extractToken(in));
270     }
271     
272     public void loadConfigSystemProperties(RequestContext ctx, HandlerContext handlerCtx) {
273     CCActionTableModelInterface model = (CCActionTableModelInterface)
274             handlerCtx.getInputValue(MODEL);
275         ((DefaultModel)model).clear();
276         Properties JavaDoc props = (Properties JavaDoc) handlerCtx.getInputValue("properties");
277         String JavaDoc linkText = Util.getMessage("props.instanceValues");
278         try {
279             Enumeration JavaDoc ee = props.propertyNames();
280             while (ee.hasMoreElements()) {
281                 String JavaDoc name = (String JavaDoc)ee.nextElement();
282                 model.appendRow();
283                 model.setValue(PROPERTY_NAME, name);
284                 model.setValue(PROPERTY_VALUE, props.getProperty(name));
285                 model.setValue("instanceValuesHref", name);
286                 model.setValue("instanceValues", linkText);
287                 model.setRowSelected(false);
288             }
289         } catch (Exception JavaDoc ex) {
290             throw new FrameworkException("loadConfigSystemProperties: Loading error. ", ex);
291         }
292     }
293
294     public String JavaDoc endExtendedTextDisplay(RequestContext ctx, HandlerContext handlerCtx) {
295         ChildContentDisplayEvent ev = (ChildContentDisplayEvent)handlerCtx.getEvent();
296         String JavaDoc html = ev.getContent();
297
298         if ((ev.getSource() instanceof CCTextFieldTag) == false)
299             return html; // just in case.
300

301         if (html == null || html.length() == 0)
302             return html;
303         
304         String JavaDoc value = Util.extractHtmlProp(html, "value");
305         if (value == null || value.length() == 0)
306             return html;
307         value = value.trim();
308         
309         // check to see if the size of the text box need adjusting....
310
String JavaDoc sizeStr = Util.extractHtmlProp(html, "size");
311         if (sizeStr != null && sizeStr.length() > 0) {
312             int currentSize = 0;
313             try {
314                 currentSize = Integer.parseInt(sizeStr);
315                 int size = ((value.length() + 4) / 8) * 10 ;
316                 if (size > 100) size = 100;
317                 if (currentSize < size) {
318                     html = Util.removeHtmlProp(html, "size");
319                     html = Util.addHtmlProp(html, ("size="+size));
320                     //System.out.println("........> current/new size: "+currentSize+" "+size);
321
}
322             } catch (Exception JavaDoc ex) {
323                 // ignore
324
}
325         }
326         
327         int j = value.indexOf("${");
328         if (j >= 0) {
329             int i = value.indexOf('}');
330             if (i<0) i = value.length();
331             value = value.substring(j+2,i);
332         } else {
333             return html;
334         }
335         String JavaDoc name = Util.extractHtmlProp(html, "name");
336         int i = name.indexOf('.');
337         String JavaDoc listPage = name;
338         if (i>0)
339             listPage = name.substring(0,i);
340         NonSyncStringBuffer href = new NonSyncStringBuffer();
341         String JavaDoc editKeyValue = (String JavaDoc) ctx.getRequest().getAttribute("editKeyValue");
342         href.append("\n<a HREF=\"../admingui/tokensEdit?attrValue=")
343             .append(Util.URLencode(ctx, value))
344             .append("&listPage=").append(listPage);
345         if (editKeyValue != null && editKeyValue.length() > 0)
346             href.append("&editKeyValue=").append(Util.URLencode(ctx, editKeyValue));
347         if(value.startsWith("com.sun.aas")) {
348             //Display "Description" for href link
349
href.append("\">&nbsp;")
350                 .append(Util.getMessage("common.Description"))
351                 .append("</a>");
352             
353         }
354         else {
355             //Display "Actual Values" for href link
356
href.append("\">&nbsp;")
357                 .append(Util.getMessage("props.actualValues"))
358                 .append("</a>");
359         }
360         
361         return html + href;
362     }
363     
364 // Added this to the framework
365
// public void beginExtendedTextDisplay(RequestContext ctx, HandlerContext handlerCtx) {
366
// Object tag = handlerCtx.getEvent().getSource();
367
// if (tag instanceof CCTextFieldTag) {
368
// ((CCTextFieldTag)tag).setLocalizeDisplayFieldValue("false");
369
// }
370
// }
371

372     public static final String JavaDoc MODEL = "model";
373     public static final String JavaDoc PROPERTY_NAME = "propertyName";
374     public static final String JavaDoc PROPERTY_VALUE = "propertyValue";
375 }
376
Popular Tags