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: package com.sun.jbi.jsf.handlers;
038:
039: import com.sun.appserv.management.config.ClusteredServerConfig;
040: import com.sun.appserv.management.j2ee.J2EEServer;
041: import com.sun.enterprise.tools.admingui.handlers.ConfigurationHandlers;
042: import com.sun.enterprise.tools.admingui.util.AMXUtil;
043: import com.sun.enterprise.tools.admingui.util.GuiUtil;
044: import com.sun.jbi.jsf.bean.AlertBean;
045: import com.sun.jbi.jsf.util.BeanUtilities;
046: import com.sun.jbi.jsf.util.ClusterUtilities;
047: import com.sun.jbi.jsf.util.I18nUtilities;
048: import com.sun.jbi.jsf.util.JBIConstants;
049: import com.sun.jbi.jsf.util.JBILogger;
050: import com.sun.jbi.jsf.util.SharedConstants;
051: import com.sun.jsftemplating.annotation.Handler;
052: import com.sun.jsftemplating.annotation.HandlerInput;
053: import com.sun.jsftemplating.annotation.HandlerOutput;
054: import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
055: import java.util.ArrayList;
056: import java.util.Collections;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.Map;
060: import java.util.Properties;
061: import java.util.logging.Logger;
062: import javax.faces.model.SelectItem;
063:
064: /**
065: * Provides jsftemplating handlers for instance selection.
066: */
067: public class InstanceSelectionHandlers {
068:
069: private static Logger sLog = JBILogger.getInstance();
070:
071: /**
072: * <p> Returns the values for runtime instances list
073: * <p> Output value: "InstancesList" -- Type: <code>SelectItem[]</code></p>
074: * @param context The HandlerContext.
075: */
076: @Handler(id="jbiGetRuntimeInstances",input={@HandlerInput(name="addDomainFlag",type=Boolean.class,required=false)},output={@HandlerOutput(name="instancesList",type=SelectItem[].class)})
077: public static void jbiGetRuntimeInstances(HandlerContext handlerCtx) {
078: List targetsList = ClusterUtilities.findAllNonDomainTargets();
079:
080: List allInstancesList = getAllInstances(targetsList);
081:
082: Collections.sort(allInstancesList);
083:
084: String[] instancesArray = new String[0];
085: instancesArray = (String[]) allInstancesList
086: .toArray(instancesArray);
087: SelectItem[] options = ConfigurationHandlers
088: .getOptions(instancesArray);
089:
090: handlerCtx.setOutputValue("instancesList", options);
091: sLog
092: .fine("InstanceSelectionHandlers.jbiGetRuntimeInstances(), options.length="
093: + options.length);
094: }
095:
096: /**
097: * <p> Returns the values for runtime configuration targets list
098: * <p> Input value: "addDomainFlag" <code>Boolean</code> true if "domain" should
099: * appear in the returned list of targets
100: * <p> Output value: "TargetsList" -- Type: <code>SelectItem[]</code></p>
101: * @param context The HandlerContext.
102: */
103: @Handler(id="jbiGetRuntimeTargets",input={@HandlerInput(name="addDomainFlag",type=Boolean.class,required=false)},output={@HandlerOutput(name="targetsList",type=SelectItem[].class)})
104: public static void jbiGetRuntimeTargets(HandlerContext handlerCtx) {
105: boolean addDomain = true;
106: Boolean addDomainFlag = (Boolean) handlerCtx
107: .getInputValue("addDomainFlag");
108: if (addDomainFlag != null) {
109: addDomain = addDomainFlag;
110: }
111:
112: List targetsList = ClusterUtilities.findAllNonDomainTargets();
113: Collections.sort(targetsList);
114: if (addDomain) {
115: targetsList.add(0, "domain");
116: }
117:
118: String[] targetsArray = new String[0];
119: targetsArray = (String[]) targetsList.toArray(targetsArray);
120: SelectItem[] options = ConfigurationHandlers
121: .getOptions(targetsArray);
122:
123: handlerCtx.setOutputValue("targetsList", options);
124: sLog
125: .fine("InstanceSelectionHandlers.jbiGetRuntimeTargets(), options.length="
126: + options.length);
127: }
128:
129: /**
130: * <p> Returns the values for instances in configureComponent page
131: * <p> Input value: "type" -- Type: <code>String</code></p> one of:
132: * <code>binding-component</code>, <code>service-engine</code>, or
133: * <code>service-assembly</code>
134: * <p> Input value: "compOrSaName" -- Type: <code>String</code></p>
135: * <p> Output value: "InstanceList" -- Type: <code>SelectItem[]</code></p>
136: * <p> Output value: "isAlertNeeded" -- Type: <code>Boolean</code></p>
137: * <p> Output value: "alertSummary" -- Type: <code>String</code></p>
138: * <p> Output value: "alertDetails" -- Type: <code>String</code></p>
139: * @param handlerCtx the context used for inputs and outputs
140: */
141: @Handler(id="jbiGetInstances",input={@HandlerInput(name="type",type=String.class,required=true),@HandlerInput(name="compOrSaName",type=String.class,required=true)},output={@HandlerOutput(name="instancesList",type=SelectItem[].class),@HandlerOutput(name="isAlertNeeded",type=Boolean.class),@HandlerOutput(name="alertSummary",type=String.class),@HandlerOutput(name="alertDetails",type=String.class)})
142: public static void jbiGetInstances(HandlerContext handlerCtx) {
143: String type = (String) handlerCtx.getInputValue("type");
144: String compOrSaName = (String) handlerCtx
145: .getInputValue("compOrSaName");
146: sLog.fine("InstanceSelectionHandlers.jbiGetInstances(), type="
147: + type + ", compOrSaName=" + compOrSaName);
148:
149: List installedTargetsList = new ArrayList();
150: List installedList = ClusterUtilities.findTargetsForNameByType(
151: compOrSaName, type);
152: // Construct the installedTargetsList
153: Iterator targetsIterator = installedList.iterator();
154: while (targetsIterator.hasNext()) {
155: Properties targetProp = (Properties) targetsIterator.next();
156: String target = targetProp
157: .getProperty(SharedConstants.KEY_NAME);
158: installedTargetsList.add(target);
159: }
160:
161: List installedInstancesList = getAllInstances(installedTargetsList);
162: Collections.sort(installedInstancesList);
163:
164: // Construct the warning message if component is not installed on any target
165: String alertType = "";
166: String alertDetails = "";
167: String alertSummary = "";
168: boolean isAlertNeeded = false;
169: if (installedInstancesList.size() == 0) {
170: isAlertNeeded = true;
171: alertType = "warning";
172: if ("service-assembly".equals(type)) {
173: alertSummary = I18nUtilities
174: .getResourceString("jbi.sa.not.installed.to.target.summary");
175: } else {
176: alertSummary = I18nUtilities
177: .getResourceString("jbi.component.not.installed.to.target.summary");
178: }
179: if (null == compOrSaName) {
180: compOrSaName = ""; // prevent NPE in GuiUtil
181: }
182: Object[] args = { compOrSaName };
183: alertDetails = GuiUtil
184: .getMessage(
185: I18nUtilities
186: .getResourceString("jbi.component.not.installed.to.target.details"),
187: args);
188: AlertBean alertBean = BeanUtilities.getAlertBean();
189: alertBean.setAlertType(alertType);
190: }
191:
192: // Return the options list
193: String[] instances = new String[0];
194: instances = (String[]) installedInstancesList
195: .toArray(instances);
196: SelectItem[] options = ConfigurationHandlers
197: .getOptions(instances);
198:
199: handlerCtx.setOutputValue("instancesList", options);
200: handlerCtx.setOutputValue("isAlertNeeded", isAlertNeeded);
201: handlerCtx.setOutputValue("alertSummary", alertSummary);
202: handlerCtx.setOutputValue("alertDetails", alertDetails);
203: sLog
204: .fine("InstanceSelectionHandlers.jbiGetInstances(), options.length="
205: + options.length);
206: }
207:
208: /**
209: * <p> This handler sets the values for instances to be updated in configureComponent page
210: * <p> Input value: "fromInstance" -- Type: <code>java.lang.String</code></p>
211: * <p> Output value: "toInstances" -- Type: <code>java.lang.String[]</code></p>
212: * @param context The HandlerContext.
213: */
214: @Handler(id="jbiSetDefaultSelectedInstances",input={@HandlerInput(name="fromInstance",type=String.class,required=true)},output={@HandlerOutput(name="toInstances",type=String[].class)})
215: public static void jbiSetDefaultSelectedInstances(
216: HandlerContext handlerCtx) {
217: String[] result = new String[0];
218: String fromInstance = (String) handlerCtx
219: .getInputValue("fromInstance");
220: result[0] = fromInstance;
221: sLog
222: .fine("InstanceSelectionHandlers.jbiSetDefaultSelectedInstances("
223: + fromInstance
224: + "), result.length="
225: + result.length);
226: handlerCtx.setOutputValue("toInstances", result);
227: }
228:
229: /**
230: * <p> This converts an object array (containing strings) to a string array
231: * <p> Input value: "fromObjectArray" -- Type: <code>java.lang.Object[]</code></p>
232: * <p> Output value: "toStringArray" -- Type: <code>java.lang.String[]</code></p>
233: * @param context The HandlerContext.
234: */
235: @Handler(id="jbiObjectArrayToStringArray",input={@HandlerInput(name="fromObjectArray",type=Object[].class,required=true)},output={@HandlerOutput(name="toStringArray",type=String[].class)})
236: public static void jbiObjectArrayToStringArray(
237: HandlerContext handlerCtx) {
238: String[] result;
239: Object[] fromObjectArray = (Object[]) handlerCtx
240: .getInputValue("fromObjectArray");
241: if (null != fromObjectArray) {
242: result = new String[fromObjectArray.length];
243: for (int i = 0; i < result.length; ++i) {
244: result[i] = (String) fromObjectArray[i];
245: }
246: } else {
247: result = new String[0];
248: }
249: sLog
250: .fine("InstanceSelectionHandlers.jbiObjectArrayToStringArray("
251: + fromObjectArray
252: + "), result.length="
253: + result.length);
254: handlerCtx.setOutputValue("toStringArray", result);
255: }
256:
257: /**
258: * <p> This handler converts an array of strings to a printable comma separated list in one string
259: * <p> Input value: "fromStringArray" -- Type: <code>java.lang.String[]</code></p>
260: * <p> Output value: "toStringWithCommas" -- Type: <code>java.lang.String</code></p>
261: * @param context The HandlerContext.
262: */
263: @Handler(id="jbiListArrayStrings",input={@HandlerInput(name="fromStringArray",type=Object[].class,required=true)},output={@HandlerOutput(name="toStringWithCommas",type=String.class)})
264: public static void jbiListArrayStrings(HandlerContext handlerCtx) {
265: String result = "list: [";
266: Object[] fromStringArray = (Object[]) handlerCtx
267: .getInputValue("fromStringArray");
268: for (int i = 0; i < fromStringArray.length; ++i) {
269: result += (String) fromStringArray[i];
270: result += ", ";
271: }
272: result += " ]";
273:
274: sLog.fine("InstanceSelectionHandlers.jbiListArrayStrings("
275: + fromStringArray + "), result=" + result);
276: handlerCtx.setOutputValue("toStringWithCommas", result);
277: }
278:
279: /**
280: * Gets a list of all instances from the specified list of targets
281: * @param aTargetsArray contains cluster target names and standalone
282: * server target names
283: * @return all instances in each cluster specified and all standalone
284: * instances that were in the original target list
285: */
286: private static List getAllInstances(List aTargetsArray) {
287: ArrayList result = new ArrayList();
288:
289: Iterator targetsIterator = aTargetsArray.iterator();
290:
291: Iterator targetIt = aTargetsArray.iterator();
292: while (targetIt.hasNext()) {
293: String target = (String) targetIt.next();
294:
295: // for each cluster target, add all of its instances (if any)
296: if (ClusterUtilities.isCluster(target)) {
297: sLog
298: .fine("InstanceSelectionHandlers.jbiGetInstances(), cluster target="
299: + target);
300:
301: Map<String, ClusteredServerConfig> serverMap = AMXUtil
302: .getDomainConfig().getClusterConfigMap().get(
303: target).getClusteredServerConfigMap();
304:
305: List instancesForOneCluster = new ArrayList();
306:
307: if (null != serverMap) {
308: for (String key : serverMap.keySet()) {
309: String name = serverMap.get(key).getName();
310:
311: J2EEServer server = AMXUtil.getJ2EEDomain()
312: .getJ2EEServerMap().get(name);
313:
314: int state = server.getstate();
315: if (state == 1) {
316: instancesForOneCluster.add(name);
317: }
318: sLog
319: .fine("InstanceSelectionHandlers.jbiGetInstances(), cluster target="
320: + target
321: + ", instanceName="
322: + name + ", state=" + state);
323: }
324: }
325:
326: Iterator instanceIt = instancesForOneCluster.iterator();
327: while (instanceIt.hasNext()) {
328: String instance = (String) instanceIt.next();
329: result.add(instance);
330: }
331: }
332: // for each stand-alone server target, add it as an instance
333: else {
334: J2EEServer server = AMXUtil.getJ2EEDomain()
335: .getJ2EEServerMap().get(target);
336:
337: int state = server.getstate();
338:
339: if (state == 1) {
340: result.add(target);
341: }
342: sLog
343: .fine("InstanceSelectionHandlers.jbiGetInstances(), stand-alone server target="
344: + target + ", state=" + state);
345: }
346: }
347:
348: return result;
349: }
350: }
|