001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * AppConfigUtils.java
039: *
040: */
041:
042: package com.sun.jbi.jsf.util;
043:
044: import com.sun.jbi.jsf.bean.AppConfigField;
045: import com.sun.jbi.jsf.util.BeanUtilities;
046: import com.sun.jbi.ui.common.JBIAdminCommands;
047: import java.util.ArrayList;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Properties;
051: import java.util.logging.Level;
052: import java.util.logging.Logger; //import javax.management.openmbean.ArrayType;
053: import javax.management.openmbean.CompositeData;
054: import javax.management.openmbean.CompositeType;
055: import javax.management.openmbean.OpenType;
056:
057: /**
058: *
059: * This class is used to provide utilitiies for the Component
060: * Application Configuration use-cases
061: *
062: **/
063:
064: public final class AppConfigUtils {
065:
066: /**
067: * Controls printing of diagnostic messages to the log
068: */
069: private static Logger sLog = JBILogger.getInstance();
070:
071: /**
072: * Determines if the specified Application Configuration
073: * exists for the specified component on the specified
074: * instance.
075: * @param anInstanceName String instance with target MBean for the component
076: * @param aComponentName String JBI Component name
077: * @param anAppConfigName String the Application Configuration name
078: * @returns Properties that contain a success-result key mapped to the
079: * Application Configuration name, or a failure-result key mapped to an
080: * I18n alert details message
081: */
082: public static Properties checkAppConfig(String anInstanceName,
083: String aComponentName, String anAppConfigName) {
084: sLog.fine("AppConfigUtils.checkAppConfig(" + anInstanceName
085: + ", " + aComponentName + ", " + anAppConfigName + ")");
086:
087: Properties result = new Properties();
088:
089: result = checkComponent(anInstanceName, aComponentName);
090:
091: String successResult = (String) result.get("success-result");
092:
093: if (null != successResult) {
094: result.remove("success-result");
095:
096: Properties appConfigProps = getProperties(anInstanceName,
097: aComponentName, anAppConfigName);
098:
099: sLog
100: .fine("AppConfigUtils.checkAppConfig(...), appConfigProps="
101: + appConfigProps);
102:
103: if (null != appConfigProps) {
104: result.put("success-result", anAppConfigName);
105: } else {
106: result.put("failure-result",
107: "I18n-TBD Application Configuration \"{"
108: + anAppConfigName
109: + "}\" was not found for component \"{"
110: + aComponentName
111: + "}\" on instance \"{"
112: + anInstanceName + "}\"");
113: }
114: }
115: // else failure-result already set
116:
117: sLog.fine("AppConfigUtils.checkAppConfig(...), result="
118: + result);
119:
120: return result;
121: }
122:
123: /**
124: * Determines if the specified component, on the specified
125: * instance, supports Application Configurations.
126: * @param anInstanceName String instance with target MBean for the component
127: * @param aComponentName String JBI Component name
128: * @returns Properties that contain a success-result key mapped to the
129: * Component name, or a failure-result key mapped to an
130: * I18n alert details message
131: */
132: public static Properties checkComponent(String anInstanceName,
133: String aComponentName) {
134: Properties result = new Properties();
135:
136: String xml = getDescriptor(anInstanceName, aComponentName);
137: if (null != xml) {
138: result.put("success-result", aComponentName);
139: } else {
140: result.put("failure-result", "I18n-TBD component \"{"
141: + aComponentName + "}\" on instance \"{"
142: + anInstanceName
143: + "}\" does not support application configuration");
144: }
145:
146: sLog.fine("AppConfigUtils.checkComponent(...), result="
147: + result);
148:
149: return result;
150: }
151:
152: /**
153: * Gets the fields for the specified Application Configuration name if it exists,
154: * otherwise returns the default values for a new Application Configuration.
155: * @param anInstanceName String instance with target MBean for the component
156: * @param aComponentName String JBI Component name
157: * @param anAppConfigName String the Application Configuration name
158: * @returns List<AppConfigField> with a sorted (on Display Name) list of fields.
159: * If the name exists, the fields contain current values; if the name doesn't
160: * exist, the fields contain default values (if any); if the component
161: * doesn't support application configuration, returns an empty list.
162: */
163: public static List<AppConfigField> getAppConfigFields(
164: String aComponentName, String anInstanceName,
165: String anAppConfigName) {
166: List<AppConfigField> result = null;
167:
168: sLog.fine("AppConfigUtils.getAppConfigFields(" + aComponentName
169: + ", " + anInstanceName + ", " + anAppConfigName + ")");
170:
171: String displayDescriptor = getDescriptor(anInstanceName,
172: aComponentName);
173:
174: if (null != displayDescriptor) {
175: // populate list with sorted fields (with default values)
176: result = AppConfigXmlUtils.readFromXml(displayDescriptor);
177:
178: CompositeType appConfigType = getType(anInstanceName,
179: aComponentName);
180:
181: Properties appConfigData = getProperties(anInstanceName,
182: aComponentName, anAppConfigName);
183:
184: // populate fields with existing data (erasing defaults)
185: if (null != anAppConfigName) {
186: result = setValues(result, appConfigType, appConfigData);
187: }
188: }
189:
190: sLog.fine("AppConfigUtils.getAppConfigFields(...), result="
191: + result);
192:
193: return result;
194: }
195:
196: /**
197: * Gets the Properties for the specified Application Configuration
198: * on the specified component and instance.
199: * @param anInstanceName String instance with target MBean for the component
200: * @param aComponentName String JBI Component name
201: * @param anAppConfigName String the Application Configuration name
202: * @returns Properties that contain the fieldNames mapped to CompositeData
203: * items for each Application Configuration field, or null if the
204: * Application Configuration cannot be found.
205: */
206: private static Properties getProperties(String anInstanceName,
207: String aComponentName, String anAppConfigName) {
208: sLog.fine("AppConfigUtils.getProperties(" + anInstanceName
209: + ", " + aComponentName + ", " + anAppConfigName + ")");
210:
211: Properties result = null;
212:
213: try {
214: JBIAdminCommands jac = BeanUtilities.getClient();
215:
216: Map<String, Properties> compAppConfigMaps = jac
217: .getApplicationConfigurations(aComponentName,
218: anInstanceName);
219:
220: sLog
221: .fine("AppConfigUtils.getProperties(...), compAppConfigMaps="
222: + compAppConfigMaps);
223:
224: result = compAppConfigMaps.get(anAppConfigName);
225:
226: } catch (com.sun.jbi.ui.common.JBIRemoteException jrEx) {
227: sLog
228: .log(
229: Level.FINE,
230: ("AppConfigUtils.getProperties(...) caught jrEx=" + jrEx),
231: jrEx);
232: }
233:
234: sLog
235: .fine("AppConfigUtils.getProperties(...), result="
236: + result);
237:
238: return result;
239: }
240:
241: /**
242: * Gets the Display Descriptor XML String for all
243: * Application Configurations on the specified
244: * component and instance.
245: * @param anInstanceName String instance with target MBean for the component
246: * @param aComponentName String JBI Component name
247: * @returns String the XML Display Descriptor for all Application Configurations
248: * supported by the specified component, or null if the component does not
249: * support Application Configration use-cases.
250: */
251: private static String getDescriptor(String anInstanceName,
252: String aComponentName) {
253: sLog.fine("AppConfigUtils.getDescriptor(" + anInstanceName
254: + ", " + aComponentName + ")");
255:
256: String result = null;
257:
258: try {
259: JBIAdminCommands jac = BeanUtilities.getClient();
260:
261: result = jac.retrieveConfigurationDisplayData(
262: aComponentName, anInstanceName);
263: } catch (com.sun.jbi.ui.common.JBIRemoteException jrEx) {
264: sLog
265: .log(
266: Level.FINE,
267: ("AppConfigUtils.getDescriptor(...) caught jrEx=" + jrEx),
268: jrEx);
269: }
270:
271: sLog
272: .fine("AppConfigUtils.getDescriptor(...), result="
273: + result);
274:
275: return result;
276: }
277:
278: /**
279: * Gets the CompositeType for all Application Configurations
280: * on the specified component and instance.
281: * @param anInstanceName String instance with target MBean for the component
282: * @param aComponentName String JBI Component name
283: * @returns CompositeType the Types for all Application Configurations
284: * supported by the specified component, or null if the component does not
285: * support Application Configration use-cases.
286: */
287: private static CompositeType getType(String anInstanceName,
288: String aComponentName) {
289: sLog.fine("AppConfigUtils.getType(" + anInstanceName + ", "
290: + aComponentName + ")");
291: CompositeType result = null;
292:
293: try {
294: JBIAdminCommands jac = BeanUtilities.getClient();
295:
296: result = jac.queryApplicationConfigurationType(
297: aComponentName, anInstanceName);
298: } catch (com.sun.jbi.ui.common.JBIRemoteException jrEx) {
299: sLog
300: .log(
301: Level.FINE,
302: ("AppConfigUtils.getType(...) caught jrEx=" + jrEx),
303: jrEx);
304: }
305:
306: sLog.fine("AppConfigUtils.getType(...), result=" + result);
307: return result;
308: }
309:
310: private static List<AppConfigField> setValues(
311: List<AppConfigField> aListOfFields,
312: CompositeType aCompositeType, Properties anAppConfigValues) {
313: sLog.fine("AppConfigUtils.setValues(" + aListOfFields + ", "
314: + aCompositeType + ", " + anAppConfigValues + ")");
315:
316: List<AppConfigField> result = aListOfFields;
317:
318: if ((null != aCompositeType) && (null != anAppConfigValues)
319: && (null != result)) {
320: for (int i = 0; i < result.size(); ++i) {
321: AppConfigField nextField = result.get(i);
322:
323: String name = nextField.getName();
324:
325: nextField.setValue(null); // clear default value
326:
327: if (!"configurationName".equals(name)) {
328: String typeStr = "";
329: OpenType type = (OpenType) aCompositeType
330: .getType(name);
331: if ("java.lang.String".equals(type.getTypeName())) {
332: typeStr = "String";
333: String value = (String) anAppConfigValues
334: .get(name);
335: nextField.setValue(value);
336: sLog
337: .fine("AppConfigUtils.setValues(...), added value="
338: + value);
339: } else {
340: sLog
341: .fine("AppConfigUtils.setValues(...), skipping non-String value for name="
342: + name);
343: }
344: }
345: }
346: }
347:
348: sLog.fine("AppConfigUtils.setValues(...), result=" + result);
349:
350: return result;
351: }
352: }
|