001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: * The contents of this file are subject to the terms of either the GNU
006: * General Public License Version 2 only ("GPL") or the Common Development
007: * and Distribution License("CDDL") (collectively, the "License"). You
008: * may not use this file except in compliance with the License. You can obtain
009: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
010: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
011: * language governing permissions and limitations under the License.
012: *
013: * When distributing the software, include this License Header Notice in each
014: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
015: * Sun designates this particular file as subject to the "Classpath" exception
016: * as provided by Sun in the GPL Version 2 section of the License file that
017: * accompanied this code. If applicable, add the following below the License
018: * Header, with the fields enclosed by brackets [] replaced by your own
019: * identifying information: "Portions Copyrighted [year]
020: * [name of copyright owner]"
021: *
022: * Contributor(s):
023: *
024: * If you wish your version of this file to be governed by only the CDDL or
025: * only the GPL Version 2, indicate your decision by adding "[Contributor]
026: * elects to include this software in this distribution under the [CDDL or GPL
027: * Version 2] license." If you don't indicate a single choice of license, a
028: * recipient has the option to distribute your version of this file under
029: * either the CDDL, the GPL Version 2 or to extend the choice of license to
030: * its licensees as provided above. However, if you add GPL Version 2 code
031: * and therefore, elected the GPL Version 2 license, then the option applies
032: * only if the new code is made subject to such option by the copyright
033: * holder.
034: */
035:
036: package com.sun.jbi.jsf.util;
037:
038: import com.sun.appserv.management.config.ConfigConfig;
039: import com.sun.appserv.management.config.ModuleLogLevelsConfig;
040: import com.sun.enterprise.tools.admingui.util.GuiUtil;
041: import com.sun.jbi.jsf.util.BeanUtilities;
042: import com.sun.jbi.jsf.util.SharedConstants;
043: import com.sun.jbi.jsf.util.SystemLoggerUtilities;
044: import com.sun.jbi.jsf.bean.LoggingBean;
045: import com.sun.jbi.jsf.factory.PropertySheetAdaptorBase;
046: import com.sun.jbi.ui.common.JBIAdminCommands;
047: import com.sun.jsftemplating.layout.descriptors.LayoutComponent;
048: import com.sun.webui.jsf.component.Property;
049: import com.sun.webui.jsf.component.StaticText;
050: import com.sun.webui.jsf.component.DropDown;
051: import com.sun.webui.jsf.component.HiddenField;
052: import com.sun.webui.jsf.model.Option;
053: import java.util.TreeMap;
054: import java.util.HashMap;
055: import java.util.logging.Logger;
056: import java.util.logging.Level;
057: import java.util.Map;
058: import java.util.Set;
059: import java.util.Iterator;
060: import java.io.IOException;
061: import java.io.File;
062: import java.io.InputStream;
063: import java.io.PrintStream;
064: import javax.faces.component.UIComponent;
065: import javax.faces.context.FacesContext;
066:
067: /**
068: * <p> The <code>JBILogLevelsPropertySheetAdaptor</code> implementation must have a
069: * <code>public static JBILogLevelsPropertySheetAdaptor getInstance(FacesContext,
070: * LayoutComponent, UIComponent)</code> method in order to get access to
071: * an instance of the <code>JBILogLevelsPropertySheetAdaptor</code> instance.</p>
072: *
073: * <p> This class is used by <code>DynamicPropertySheetNodeFactory</code>.</p>
074: *
075: *
076: */
077: public class JBILogLevelsPropertySheetAdaptor extends
078: PropertySheetAdaptorBase {
079:
080: private static Logger sLog;
081: private JBIAdminCommands mJac = BeanUtilities.getClient();
082:
083: private static final Option[] mLogLevelOptions = {
084: new Option((Level.parse("FINEST")).getName(), GuiUtil
085: .getMessage(I18nUtilities
086: .getResourceString("loglevel.FINEST"))),
087: new Option((Level.parse("FINER")).getName(), GuiUtil
088: .getMessage(I18nUtilities
089: .getResourceString("loglevel.FINER"))),
090: new Option((Level.parse("FINE")).getName(), GuiUtil
091: .getMessage(I18nUtilities
092: .getResourceString("loglevel.FINE"))),
093: new Option((Level.parse("CONFIG")).getName(), GuiUtil
094: .getMessage(I18nUtilities
095: .getResourceString("loglevel.CONFIG"))),
096: new Option((Level.parse("INFO")).getName(), GuiUtil
097: .getMessage(I18nUtilities
098: .getResourceString("loglevel.INFO"))),
099: new Option((Level.parse("WARNING")).getName(), GuiUtil
100: .getMessage(I18nUtilities
101: .getResourceString("loglevel.WARNING"))),
102: new Option((Level.parse("SEVERE")).getName(), GuiUtil
103: .getMessage(I18nUtilities
104: .getResourceString("loglevel.SEVERE"))),
105: new Option((Level.parse("OFF")).getName(), GuiUtil
106: .getMessage(I18nUtilities
107: .getResourceString("loglevel.OFF"))),
108: new Option(GuiUtil.getMessage(I18nUtilities
109: .getResourceString("loglevel.DEFAULT")), GuiUtil
110: .getMessage(I18nUtilities
111: .getResourceString("loglevel.DEFAULT"))), };
112:
113: /**
114: * <p> This constructor is not used.</p>
115: */
116: private JBILogLevelsPropertySheetAdaptor() {
117: }
118:
119: /**
120: * <p> This constructor saves the <code>LayoutComponent</code> descriptor
121: * and the <code>UIComponent</code> associated with this
122: * <code>PropertySheetAdaptor</code>. This constructor is used by the
123: * getInstance() method.</p>
124: */
125: protected JBILogLevelsPropertySheetAdaptor(LayoutComponent desc,
126: UIComponent parent) {
127: super (desc, parent);
128: }
129:
130: /**
131: * <p> This method provides access to an <code>JBILogLevelsPropertySheetAdaptor</code>
132: * instance. Each time it is invoked, it returns a new instance.</p>
133: */
134: public static JBILogLevelsPropertySheetAdaptor getInstance(
135: FacesContext ctx, LayoutComponent desc, UIComponent parent) {
136: return new JBILogLevelsPropertySheetAdaptor(desc, parent);
137: }
138:
139: /**
140: * <p> Method that is called to initialize the PropertySheet component.</p>
141: */
142: public void init() {
143: // Initialise the logger
144: sLog = JBILogger.getInstance();
145:
146: // The parent UIComponent
147: UIComponent parent = getParentUIComponent();
148:
149: // Retrieve the required option values
150: mPropertySheetId = getRequiredOptionValue("propertySheetId",
151: parent);
152: mPropertySheetSectionIdTag = getRequiredOptionValue(
153: "propertySheetSectionIdTag", parent);
154: mPropertyIdTag = getRequiredOptionValue("propertyIdTag", parent);
155: mStaticTextIdTag = getRequiredOptionValue("staticTextIdTag",
156: parent);
157: mDropDownIdTag = getRequiredOptionValue("dropDownIdTag", parent);
158: mHiddenFieldIdTag = getRequiredOptionValue("hiddenFieldIdTag",
159: parent);
160: }
161:
162: /**
163: * <p> Method that is called to retrieve the property sheet object.
164: *
165: * @param parent - The parent component
166: * @return PropertySheet component
167: */
168: public UIComponent getPropertySheet(UIComponent parent) {
169: constructPropertySheet(parent);
170: return parent;
171: }
172:
173: /**
174: * Helper class that will create the property sheet components.
175: */
176: private UIComponent constructPropertySheet(UIComponent parent) {
177: String componentName = getRequiredOptionValue("componentName",
178: parent);
179: String instanceName = getRequiredOptionValue("instanceName",
180: parent);
181: String targetName = (String) getOptionValue("targetName",
182: parent);
183: String additionalFile = (String) getOptionValue(
184: "additionalLoggerFile", parent);
185:
186: // Initialize the Save button to be enabled
187: LoggingBean loggingBean = BeanUtilities.getLoggingBean();
188: loggingBean.setSaveButtonDisabled(false);
189:
190: // Retrieve any default value. This would be used instead in the dropdown
191: // instead of the level value read from the MBean.
192: String defaultLevel = (String) getOptionValue(
193: "dropDownDefaultLevel", parent);
194:
195: boolean PE_Flag = false;
196: if (targetName == null) {
197: PE_Flag = true;
198: targetName = instanceName;
199: }
200:
201: TreeMap logLevels = getLoggerLevels(componentName, targetName,
202: instanceName);
203: if (logLevels == null) {
204: logLevels = new TreeMap();
205: }
206:
207: // Add any additional appserver loggers. Note, this uses the xml file
208: // AdditionalLoggers.xml file located in the com/sun/jbi/config folder.
209: if (additionalFile != null) {
210: logLevels = SystemLoggerUtilities
211: .addAdditionalSystemLoggers(logLevels,
212: componentName, targetName, additionalFile);
213: }
214:
215: UIComponent propertySheetSection = getPropertySheetSectionComponent(parent);
216:
217: if (logLevels.size() == 0) {
218: UIComponent propertyComponent = getPropertyComponent(propertySheetSection);
219: if (PE_Flag) {
220: Object[] args = { componentName };
221: String msg = GuiUtil
222: .getMessage(
223: I18nUtilities
224: .getResourceString("jbi.configure.loggers.pe.page.no.loggers"),
225: args);
226: ((Property) propertyComponent).setLabel(msg);
227: } else {
228: Object[] args = { componentName, instanceName };
229: String msg = GuiUtil
230: .getMessage(
231: I18nUtilities
232: .getResourceString("jbi.configure.loggers.page.no.loggers"),
233: args);
234: ((Property) propertyComponent).setLabel(msg);
235: }
236: loggingBean.setSaveButtonDisabled(true);
237: } else {
238: Set logLevelSet = null;
239: Level level = null;
240: logLevelSet = logLevels.keySet();
241: Iterator iter = logLevelSet.iterator();
242: Map displayNameMap = null;
243: while (iter.hasNext()) {
244: String name = (String) iter.next();
245: String label = null;
246: String loggerTagName = (String) SystemLoggerUtilities.loggerNames
247: .get(name);
248:
249: if (loggerTagName != null) {
250: label = (String) SystemLoggerUtilities.loggerLabels
251: .get(loggerTagName.toLowerCase());
252: } else {
253: if (displayNameMap == null) {
254: displayNameMap = getComponentLoggerDisplayNames(
255: componentName, targetName, instanceName);
256: }
257: label = (String) displayNameMap.get((String) name);
258: if (label == null) {
259: label = extractLoggerDisplayName(name);
260: }
261: }
262: level = (Level) logLevels.get(name);
263: String displayName = "(" + name + ")";
264:
265: // If a default value was given, then we want to use that as the level
266: if (defaultLevel != null) {
267: level = Level.parse(defaultLevel);
268: }
269:
270: UIComponent propertyComponent = getPropertyComponent(propertySheetSection);
271: UIComponent dropDownComponent = getDropDownComponent(propertyComponent);
272: UIComponent staticTextComponent = getStaticTextComponent(propertyComponent);
273: UIComponent hiddenFieldComponent = getHiddenFieldComponent(propertyComponent);
274:
275: ((Property) propertyComponent).setLabelAlign("left");
276: ((Property) propertyComponent).setNoWrap(true);
277: ((Property) propertyComponent).setOverlapLabel(false);
278: ((Property) propertyComponent).setLabel(label);
279: ((DropDown) dropDownComponent)
280: .setItems(mLogLevelOptions);
281: ((DropDown) dropDownComponent).setSelected(level
282: .getName());
283: ((StaticText) staticTextComponent).setText(displayName);
284: ((HiddenField) hiddenFieldComponent).setText(name);
285: }
286: }
287: return parent;
288: }
289:
290: /**
291: * Helper class that will extract the logger display from the logger name.
292: */
293: private String extractLoggerDisplayName(String aFullname) {
294: String displayName = aFullname;
295: int index = displayName.lastIndexOf(".");
296: if (index > 0) {
297: displayName = aFullname.substring(index + 1);
298: if (displayName.length() > 0) {
299: displayName = capitalize(displayName);
300: }
301: }
302: return displayName;
303: }
304:
305: /**
306: * Helper class that is used to capitalize the first letter in a string.
307: */
308: private static String capitalize(String s) {
309: char chars[] = s.toCharArray();
310: chars[0] = Character.toUpperCase(chars[0]);
311: return new String(chars);
312: }
313:
314: /**
315: * <p> This method returns the "options" that should be supplied to the
316: * factory that creates the <code>PropertySheet</code>.</p>
317: *
318: * <p> Some useful options for the standard <code>PropertySheet</code>
319: * component include:<p>
320: *
321: * <ul><li>propertySheetId</li>
322: * <li>propertySheetSectionIdTag</li>
323: * <li>propertyIdTag</li>
324: * <li>staticTextIdTag</li>
325: * <li>dropDownIdTag</li>
326: * <li>dropDownDefaultLevel</li>
327: * <li>hiddenFieldIdTag</li>
328: * <li>componentName</li>
329: * <li>targetName</li>
330: * <li>instanceName</li>
331: * <li>propertySheetAdaptorClass</li></ul>
332: *
333: * <p> See PropertySheet component documentation for more details.</p>
334: */
335: public Map<String, Object> getFactoryOptions() {
336: Map<String, Object> result = null;
337: LayoutComponent desc = getLayoutComponent();
338: result = new HashMap<String, Object>();
339: setProperty(result, "propertySheetId", desc
340: .getOption("propertySheetId"));
341: setProperty(result, "propertySheetSectionIdTag", desc
342: .getOption("propertySheetSectionIdTag"));
343: setProperty(result, "propertyIdTag", desc
344: .getOption("propertyIdTag"));
345: setProperty(result, "staticTextIdTag", desc
346: .getOption("staticTextIdTag"));
347: setProperty(result, "dropDownIdTag", desc
348: .getOption("dropDownIdTag"));
349: setProperty(result, "dropDownDefaultLevel", desc
350: .getOption("dropDownDefaultLevel"));
351: setProperty(result, "hiddenFieldIdTag", desc
352: .getOption("hiddenFieldIdTag"));
353: setProperty(result, "componentName", desc
354: .getOption("componentName"));
355: setProperty(result, "targetName", desc.getOption("targetName"));
356: setProperty(result, "instanceName", desc
357: .getOption("instanceName"));
358: setProperty(result, "propertySheetAdaptorClass", desc
359: .getOption("propertySheetAdaptorClass"));
360: return result;
361: }
362:
363: /**
364: * <p> Helper method for setting Properties while avoiding NPE's.</p>
365: */
366: private void setProperty(Map props, String key, Object value) {
367: if (value != null) {
368: props.put(key, value);
369: }
370: }
371:
372: /**
373: * <p> This method returns any facets that should be applied to the
374: * <code>PropertySheetNode (comp)</code>. Useful facets for the sun
375: * <code>PropertySheetNode</code> component are: "content" and "image".</p>
376: *
377: * <p> Facets that already exist on <code>comp</code>, or facets that
378: * are directly added to <code>comp</code> do not need to be returned
379: * from this method.</p>
380: *
381: * <p> This implementation directly adds a "content" facet and returns
382: * <code>null</code> from this method.</p>
383: *
384: * @param comp The PropertySheet node <code>UIComponent</code>.
385: * @param nodeObject The (model) object representing the PropertySheet node.
386: */
387: public Map<String, UIComponent> getFacets(UIComponent comp,
388: Object nodeObject) {
389: return null;
390: }
391:
392: /**
393: * <p> Advanced framework feature which provides better handling for
394: * things such as expanding PropertySheetNodes, beforeEncode, and other
395: * events.</p>
396: *
397: * <p> This method should return a <code>Map</code> of <code>List</code>
398: * of <code>Handler</code> objects. Each <code>List</code> in the
399: * <code>Map</code> should be registered under a key that cooresponds
400: * to to the "event" in which the <code>Handler</code>s should be
401: * invoked.</p>
402: */
403: public Map getHandlersByType(UIComponent comp, Object nodeObject) {
404: return null;
405: }
406:
407: /**
408: * <p> Given the component, target and instance name, this routine will call
409: * jbi api method to retrieve the logger names and log level.
410: *
411: * @param componentName - The name of the component
412: * @param targetName - The name of the target
413: * @param instanceName - The name of the instance
414: * @return Map containing the logger names and log levels
415: */
416: private TreeMap getLoggerLevels(String componentName,
417: String targetName, String instanceName) {
418: Map result = null;
419: TreeMap treeMap = null;
420:
421: try {
422: if (null != mJac) {
423: sLog
424: .fine("JBILogLevelsPropertySheetAdaptor - getLoggerLevels: "
425: + "componentName="
426: + componentName
427: + ", targetName="
428: + targetName
429: + ", instanceName=" + instanceName);
430: result = mJac.getComponentLoggerLevels(componentName,
431: targetName, instanceName);
432: treeMap = new TreeMap(result);
433: }
434: } catch (com.sun.jbi.ui.common.JBIRemoteException jbiRemoteEx) {
435: sLog
436: .fine("JBILogLevelsPropertySheetAdaptor(): caught jbiRemoteEx="
437: + jbiRemoteEx);
438: }
439: return treeMap;
440: }
441:
442: /**
443: * <p> Given the component, target and instance name, this routine will call
444: * jbi api method to retrieve the component logger names and display names.
445: *
446: * @param componentName - The name of the component
447: * @param targetName - The name of the target
448: * @param instanceName - The name of the instance
449: * @return Map containing the component logger names and display names
450: */
451: private TreeMap getComponentLoggerDisplayNames(
452: String componentName, String targetName, String instanceName) {
453: Map result = null;
454: TreeMap treeMap = null;
455: try {
456: if (null != mJac) {
457: sLog
458: .fine("JBILogLevelsPropertySheetAdaptor - getComponentLoggerDisplayNames: "
459: + "componentName="
460: + componentName
461: + ", targetName="
462: + targetName
463: + ", instanceName=" + instanceName);
464: result = mJac.getComponentLoggerDisplayNames(
465: componentName, targetName, instanceName);
466: treeMap = new TreeMap(result);
467: }
468: } catch (com.sun.jbi.ui.common.JBIRemoteException jbiRemoteEx) {
469: sLog
470: .fine("JBILogLevelsPropertySheetAdaptor(): caught jbiRemoteEx="
471: + jbiRemoteEx);
472: }
473: return treeMap;
474: }
475:
476: }
|