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: * AppVarUtils.java
039: *
040: */
041:
042: package com.sun.jbi.jsf.util;
043:
044: import com.sun.jbi.jsf.util.BeanUtilities;
045: import com.sun.jbi.jsf.util.SharedConstants;
046: import com.sun.jbi.ui.common.JBIAdminCommands;
047: import com.sun.jbi.ui.common.JBIManagementMessage;
048: import java.util.Properties;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051:
052: /**
053: *
054: * This class is used to provide utilitiies for the Component
055: * Application Variable use-cases
056: *
057: **/
058:
059: public final class AppVarUtils {
060:
061: /**
062: * Controls printing of diagnostic messages to the log
063: */
064: private static Logger sLog = JBILogger.getInstance();
065:
066: /**
067: * deletes one or more application variables for the specified component
068: * on the specified target
069: * @param aComponentName String JBI Component name
070: * @param anInstanceName String instance with target MBean for the component
071: * @param anAppVarNamesArray String[] with one or more names to delete
072: */
073: public static Properties deleteCompAppVars(String aComponentName,
074: String anInstanceName, String[] anAppVarNamesArray) {
075: Properties result = new Properties();
076:
077: JBIManagementMessage mgmtMsg = null;
078:
079: try {
080: JBIAdminCommands jac = BeanUtilities.getClient();
081:
082: String jacResult = jac.deleteApplicationVariables(
083: aComponentName, anInstanceName, anAppVarNamesArray);
084:
085: sLog.fine("AppVarUtils.deleteCompAppVars(" + aComponentName
086: + ", " + anInstanceName + ", " + anAppVarNamesArray
087: + "), jacResult=" + jacResult);
088:
089: } catch (com.sun.jbi.ui.common.JBIRemoteException jrEx) {
090: sLog.log(Level.FINE, ("AppVarUtils.deleteCompAppVars("
091: + aComponentName + ", " + anInstanceName + ", "
092: + anAppVarNamesArray + "), caught jrEx=" + jrEx),
093: jrEx);
094: }
095:
096: if (null != mgmtMsg) {
097: if (mgmtMsg.isSuccessMsg()) {
098: result.put("success-result", aComponentName);
099: } else {
100: String details = mgmtMsg.getMessage();
101: if (null == details) {
102: details = I18nUtilities
103: .getResourceString("jbi.internal.error.invalid.remote.exception");
104: }
105: result.put("failure-result", details);
106: }
107: }
108:
109: sLog.fine("AppVarUtils.deleteCompAppVars(...), result="
110: + result);
111:
112: return result;
113: }
114:
115: /**
116: * Gets the application variables for the specified component
117: * on the specified target
118: * @param aComponentName String JBI Component name
119: * @param anInstanceName String instance with target MBean for the component
120: * @returns Properties that contain a the name->[type]value properties
121: */
122: public static Properties getCompAppVars(String aComponentName,
123: String anInstanceName) {
124:
125: Properties result = null;
126:
127: JBIManagementMessage mgmtMsg = null;
128:
129: try {
130: JBIAdminCommands jac = BeanUtilities.getClient();
131:
132: result = jac.getApplicationVariables(aComponentName,
133: anInstanceName);
134:
135: sLog.fine("AppVarUtils.getCompAppVars(" + aComponentName
136: + ", " + anInstanceName + "), result=" + result);
137: } catch (com.sun.jbi.ui.common.JBIRemoteException jrEx) {
138: sLog.log(Level.FINE, ("AppVarUtils.getCompAppVars("
139: + aComponentName + ", " + anInstanceName
140: + "), caught jrEx=" + jrEx), jrEx);
141: }
142:
143: return result;
144: }
145:
146: /**
147: * Saves a new application variable for the specified component
148: * on the specified target
149: * @param aComponentName String JBI Component name
150: * @param anInstanceName String instance with target MBean for the component
151: * @param anAppVarName String the new Application Variable name
152: * @param anAppVarType String the new Application Variable type
153: * @param anAppVarValue String the new Application Variable value or null
154: * @returns Properties that contain a success-result key mapped to the
155: * Application Variable, or a failure-result key mapped to an
156: * I18n alert details message
157: */
158: public static Properties saveNewCompAppVar(String aComponentName,
159: String anInstanceName, String anAppVarName,
160: String anAppVarType, String anAppVarValue) {
161: sLog.fine("AppVarUtils.saveNewCompAppVar(" + aComponentName
162: + ", " + anInstanceName + ", " + anAppVarName + ", "
163: + anAppVarType + ", " + anAppVarValue + ")");
164:
165: Properties result = new Properties();
166:
167: String jacResult = null;
168:
169: JBIManagementMessage mgmtMsg = null;
170:
171: // ensure variables have some default value
172: if (null == anAppVarValue) {
173: if (SharedConstants.APP_VAR_TYPE_BOOLEAN
174: .equals(anAppVarType)) {
175: anAppVarValue = "false";
176: } else if (SharedConstants.APP_VAR_TYPE_NUMBER
177: .equals(anAppVarType)) {
178: anAppVarValue = "0";
179: } else {
180: anAppVarValue = "";
181: }
182: }
183:
184: // ensure TRUE or FALSE when needed
185: if (SharedConstants.APP_VAR_TYPE_BOOLEAN.equals(anAppVarType)) {
186: if ("true".equalsIgnoreCase(anAppVarValue)) {
187: anAppVarValue = "TRUE";
188: } else {
189: anAppVarValue = "FALSE";
190: }
191: }
192:
193: Properties aNewAppVarProps = new Properties();
194: aNewAppVarProps.put(anAppVarName,
195: (anAppVarType + anAppVarValue));
196: try {
197: JBIAdminCommands jac = BeanUtilities.getClient();
198:
199: jacResult = jac.addApplicationVariables(aComponentName,
200: anInstanceName, aNewAppVarProps);
201:
202: sLog.fine("AppVarUtils.saveNewCompAppVar(...), jacResult="
203: + jacResult);
204:
205: mgmtMsg = BeanUtilities
206: .extractJBIManagementMessage(jacResult);
207: } catch (com.sun.jbi.ui.common.JBIRemoteException jrEx) {
208: sLog
209: .log(
210: Level.FINE,
211: ("AppVarUtils.saveNewCompAppVar(...) caught jrEx=" + jrEx),
212: jrEx);
213: mgmtMsg = BeanUtilities.extractJBIManagementMessage(jrEx);
214: }
215:
216: if (null != mgmtMsg) {
217: if (mgmtMsg.isSuccessMsg()) {
218: result.put("success-result", anAppVarName);
219: } else {
220: String details = mgmtMsg.getMessage();
221: if (null == details) {
222: details = I18nUtilities
223: .getResourceString("jbi.internal.error.invalid.remote.exception");
224: }
225: result.put("failure-result", details);
226: }
227: }
228:
229: sLog.fine("AppVarUtils.saveNewCompAppVar(...), result="
230: + result);
231:
232: return result;
233: }
234:
235: /**
236: * Saves a new application variable for the specified component
237: * on the specified target
238: * @param aComponentName String JBI Component name
239: * @param anInstanceName String instance with target MBean for the component
240: * @param aNewAppVarsProps Properties with new name->[type]value properties
241: * @returns Properties that contain a success-result key mapped to the
242: * Application Variable, or a failure-result key mapped to an
243: * I18n alert details message
244: */
245: public static Properties update(String aComponentName,
246: String anInstanceName, Properties aNewAppVarsProps) {
247: sLog.fine("AppVarUtils.update(" + aComponentName + ", "
248: + anInstanceName + ", " + aNewAppVarsProps + ")");
249:
250: Properties result = new Properties();
251:
252: sLog.fine("AppVarUtils.update(), result=" + result);
253:
254: JBIManagementMessage mgmtMsg = null;
255:
256: String jacResult = null;
257:
258: try {
259: JBIAdminCommands jac = BeanUtilities.getClient();
260:
261: jacResult = jac.setApplicationVariables(aComponentName,
262: anInstanceName, aNewAppVarsProps);
263:
264: sLog.fine("AppVarUtils.saveNewCompAppVar(...), jacResult="
265: + jacResult);
266:
267: mgmtMsg = BeanUtilities
268: .extractJBIManagementMessage(jacResult);
269: } catch (com.sun.jbi.ui.common.JBIRemoteException jrEx) {
270: sLog
271: .log(
272: Level.FINE,
273: ("AppVarUtils.saveNewCompAppVar(...) caught jrEx=" + jrEx),
274: jrEx);
275: mgmtMsg = BeanUtilities.extractJBIManagementMessage(jrEx);
276: }
277:
278: if (null != mgmtMsg) {
279: if (mgmtMsg.isSuccessMsg()) {
280: result.put("success-result", aComponentName);
281: } else {
282: String details = mgmtMsg.getMessage();
283: if (null == details) {
284: details = I18nUtilities
285: .getResourceString("jbi.internal.error.invalid.remote.exception");
286: }
287: result.put("failure-result", details);
288: }
289: }
290:
291: sLog.fine("AppVarUtils.update(...), result=" + result);
292:
293: return result;
294: }
295:
296: }
|