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: * OperationBean.java
039: */
040:
041: package com.sun.jbi.jsf.bean;
042:
043: import com.sun.enterprise.tools.admingui.util.GuiUtil;
044: import com.sun.jbi.jsf.util.BeanUtilities;
045: import com.sun.jbi.jsf.util.I18nUtilities;
046: import com.sun.jbi.jsf.util.JBIConstants;
047: import com.sun.jbi.jsf.util.JBILogger;
048: import com.sun.jbi.jsf.util.SharedConstants;
049: import com.sun.jbi.ui.common.JBIManagementMessage;
050: import com.sun.jbi.ui.common.JBIRemoteException;
051: import com.sun.jbi.ui.common.JBIAdminCommands;
052: import java.util.Iterator;
053: import java.util.List;
054: import java.util.Map;
055: import java.util.logging.Logger;
056: import java.util.Properties;
057:
058: import javax.faces.application.FacesMessage;
059: import javax.faces.component.UIComponent;
060: import javax.faces.context.FacesContext;
061: import javax.faces.validator.ValidatorException;
062:
063: public class OperationBean {
064:
065: public final static String OPERATION_SHUT_DOWN = "Shut Down"; // not i18n
066: public final static String OPERATION_START = "Start"; // not i18n
067: public final static String OPERATION_STOP = "Stop"; // not i18n
068:
069: //Get Logger to log fine mesages for debugging
070: private static Logger sLog = JBILogger.getInstance();
071:
072: public OperationBean() {
073: mJac = BeanUtilities.getClient();
074: }
075:
076: public Properties shutDown(Properties aRequestResponse,
077: List aTargetsList) {
078: sLog.fine("OperationBean.shutDown(" + aRequestResponse + ", "
079: + aTargetsList + ")");
080: Properties result = doCommand(OPERATION_SHUT_DOWN,
081: aRequestResponse, aTargetsList);
082: sLog.fine("OperationBean.shutDown(), result="
083: + aRequestResponse);
084: return result;
085: }
086:
087: public Properties start(Properties aRequestResponse,
088: List aTargetsList) {
089: sLog.fine("OperationBean.start(" + aRequestResponse + ", "
090: + aTargetsList + ")");
091:
092: Properties result = doCommand(OPERATION_START,
093: aRequestResponse, aTargetsList);
094: sLog.fine("OperationBean.start(), result=" + aRequestResponse);
095: return result;
096: }
097:
098: public Properties stop(Properties aRequestResponse,
099: List aTargetsList) {
100: sLog.fine("OperationBean.stop(" + aRequestResponse + ", "
101: + aTargetsList + ")");
102: Properties result = doCommand(OPERATION_STOP, aRequestResponse,
103: aTargetsList);
104: sLog.fine("OperationBean.stop(), result=" + aRequestResponse);
105: return result;
106: }
107:
108: public String getValueShutDown() {
109: return OPERATION_SHUT_DOWN;
110: }
111:
112: public String getValueStart() {
113: return OPERATION_START;
114: }
115:
116: public String getValueStop() {
117: return OPERATION_STOP;
118: }
119:
120: private Properties doCommand(String anOperation,
121: Properties aRequestResponse, List aTargetsList) {
122: int targetDownCount = 0;
123: String downTargetsList = "";
124:
125: sLog.fine("OperationBean.doCommand(" + anOperation + ", "
126: + aRequestResponse + ", " + aTargetsList + ")");
127: Properties result = aRequestResponse;
128:
129: String jbiName = (String) aRequestResponse
130: .getProperty(SharedConstants.KEY_NAME);
131: String jbiType = (String) aRequestResponse
132: .getProperty(SharedConstants.KEY_TYPE);
133:
134: String lifeCycleResult = null;
135:
136: if (null != mJac) {
137: Iterator targetsIterator = aTargetsList.iterator();
138: while (targetsIterator.hasNext()) {
139: Properties targetProp = (Properties) targetsIterator
140: .next();
141: String target = targetProp.getProperty(
142: SharedConstants.KEY_NAME,
143: JBIAdminCommands.SERVER_TARGET_KEY); // PE defaults to 'server'
144:
145: sLog.fine("OperationBean.doCommand(...), target="
146: + target);
147: try {
148: sLog.fine("OperationBean.doCommand(...), target="
149: + target);
150:
151: // Before processing the lifecycle operation, check to see if the target is up.
152: // If the target is down, then we need to inc the down count. This will cause
153: // us to create the warning message after the operation has completed.
154: if (!mJac.isTargetUp(target)) {
155: targetDownCount++;
156: if ("".equals(downTargetsList)) // first down target
157: {
158: downTargetsList = target;
159: } else // add subsequent targets
160: {
161: downTargetsList = downTargetsList + ", "
162: + target;
163: }
164: }
165: sLog.fine("OperationBean.doCommand(...), target="
166: + target + ", isTargetUp="
167: + mJac.isTargetUp(target)
168: + ", targetDownCount=" + targetDownCount
169: + ", downTargetsList=" + downTargetsList);
170:
171: if (JBIConstants.JBI_SERVICE_ASSEMBLY_TYPE
172: .equals(jbiType)) {
173: if (OPERATION_SHUT_DOWN.equals(anOperation)) {
174: lifeCycleResult = mJac
175: .shutdownServiceAssembly(jbiName,
176: target);
177: result.setProperty(
178: SharedConstants.SUCCESS_RESULT,
179: lifeCycleResult);
180: } else if (OPERATION_START.equals(anOperation)) {
181: lifeCycleResult = mJac
182: .startServiceAssembly(jbiName,
183: target);
184: result.setProperty(
185: SharedConstants.SUCCESS_RESULT,
186: lifeCycleResult);
187: } else if (OPERATION_STOP.equals(anOperation)) {
188: lifeCycleResult = mJac.stopServiceAssembly(
189: jbiName, target);
190: result.setProperty(
191: SharedConstants.SUCCESS_RESULT,
192: lifeCycleResult);
193: } else {
194: // error: unexpected or missing lifecycle state
195: result
196: .setProperty(
197: SharedConstants.FAILURE_RESULT,
198: I18nUtilities
199: .getResourceString("jbi.operate.missing.state")
200: + anOperation);
201: }
202: }
203:
204: else if ((JBIConstants.JBI_BINDING_COMPONENT_TYPE
205: .equals(jbiType))
206: || (JBIConstants.JBI_SERVICE_ENGINE_TYPE
207: .equals(jbiType))) {
208: if (OPERATION_SHUT_DOWN.equals(anOperation)) {
209: lifeCycleResult = mJac.shutdownComponent(
210: jbiName, target);
211: sLog
212: .fine("OperationBean.doCommand(...), shutdownComponent jbiName="
213: + jbiName
214: + ", lifeCycleResult="
215: + lifeCycleResult);
216: result.setProperty(
217: SharedConstants.SUCCESS_RESULT,
218: lifeCycleResult);
219: } else if (OPERATION_START.equals(anOperation)) {
220: lifeCycleResult = mJac.startComponent(
221: jbiName, target);
222: sLog
223: .fine("OperationBean.doCommand(...), startComponent jbiName="
224: + jbiName
225: + ", lifeCycleResult="
226: + lifeCycleResult);
227: result.setProperty(
228: SharedConstants.SUCCESS_RESULT,
229: lifeCycleResult);
230: } else if (OPERATION_STOP.equals(anOperation)) {
231: lifeCycleResult = mJac.stopComponent(
232: jbiName, target);
233: sLog
234: .fine("OperationBean.doCommand(...), stopComponent jbiName="
235: + jbiName
236: + ", lifeCycleResult="
237: + lifeCycleResult);
238: result.setProperty(
239: SharedConstants.SUCCESS_RESULT,
240: lifeCycleResult);
241: } else {
242: // error: unexpected or missing lifecycle state
243: result
244: .setProperty(
245: SharedConstants.FAILURE_RESULT,
246: I18nUtilities
247: .getResourceString("jbi.operate.missing.state")
248: + anOperation);
249: }
250: } else if (JBIConstants.JBI_SHARED_LIBRARY_TYPE
251: .equals(jbiType)) {
252: // error: shared-library has no lifecycle
253: result
254: .setProperty(
255: SharedConstants.FAILURE_RESULT,
256: I18nUtilities
257: .getResourceString("jbi.operate.lib.error"));
258: } else {
259: // error unexpected or missing jbiType
260: result
261: .setProperty(
262: SharedConstants.FAILURE_RESULT,
263: I18nUtilities
264: .getResourceString("jbi.operate.missing.type")
265: + jbiType);
266: }
267:
268: } catch (com.sun.jbi.ui.common.JBIRemoteException jbiRemoteEx) {
269: jbiRemoteEx.printStackTrace(System.err);
270: JBIManagementMessage mgmtMsg = BeanUtilities
271: .extractJBIManagementMessage(jbiRemoteEx);
272: if (mgmtMsg == null) {
273: String internalErrorMsg = I18nUtilities
274: .getResourceString("jbi.internal.error.invalid.remote.exception");
275: result.setProperty(
276: SharedConstants.INTERNAL_ERROR,
277: internalErrorMsg);
278: result.setProperty(
279: SharedConstants.FAILURE_RESULT,
280: internalErrorMsg);
281: sLog
282: .fine("OperationBean.doCommand(...), catch (empty) jbiRemoteEx; mgmtMsg=null, intnernalErrorMsg="
283: + internalErrorMsg);
284: } else {
285: String msg = mgmtMsg.getMessage();
286: msg = msg.replaceAll("\n", "<br />");
287: result.setProperty(
288: SharedConstants.FAILURE_RESULT, msg);
289: sLog
290: .fine("OperationBean.doCommand(...), catch (valid) jbiRemoteEx; mgmtMsg.getMessage="
291: + msg);
292: }
293: } catch (Exception e) {
294: String internalErrorMsg = I18nUtilities
295: .getResourceString("jbi.internal.error.invalid.remote.exception");
296: result.setProperty(SharedConstants.INTERNAL_ERROR,
297: internalErrorMsg);
298: sLog
299: .fine("OperationBean.doCommand(...), catch (empty) Exception; mgmtMsg=null, intnernalErrorMsg="
300: + internalErrorMsg);
301: }
302: }
303:
304: // If any target(s) are reported down, create the warning message and store it in the result properties
305: if (targetDownCount > 0) {
306: Object[] args = { downTargetsList, jbiName };
307: String resultString = "";
308: if (targetDownCount == 1) // use singular messages
309: {
310: result
311: .setProperty(
312: SharedConstants.WARNING_SUMMARY,
313: I18nUtilities
314: .getResourceString("jbi.operation.deferred.target.summary"));
315: resultString = GuiUtil
316: .getMessage(
317: I18nUtilities
318: .getResourceString("jbi.operation.deferred.target.down.msg"),
319: args);
320: } else // more than one target down, use plural messages
321: {
322: result
323: .setProperty(
324: SharedConstants.WARNING_SUMMARY,
325: I18nUtilities
326: .getResourceString("jbi.operation.deferred.targets.summary"));
327: resultString = GuiUtil
328: .getMessage(
329: I18nUtilities
330: .getResourceString("jbi.operation.deferred.targets.down.msg"),
331: args);
332: }
333: result.setProperty(SharedConstants.WARNING_RESULT,
334: resultString);
335: }
336:
337: } else {
338: // error: no JBI admin common client/connection
339: result
340: .setProperty(
341: SharedConstants.FAILURE_RESULT,
342: I18nUtilities
343: .getResourceString("jbi.missing.admin.client"));
344: }
345:
346: sLog.fine("OperationBean.doCommand(" + anOperation + ", "
347: + aRequestResponse + ", " + aTargetsList + "), result="
348: + result);
349:
350: return result;
351: }
352:
353: /**
354: * validates a string input field contains valid contents based on property metadata
355: * @param aContext with the Faces Context
356: * @param aComponent to be validated
357: * @param aValue String to be validated
358: * @throws ValidatorException if the value is invalid
359: */
360: public void validateField(FacesContext aContext,
361: UIComponent aComponent, Object aValue)
362: throws ValidatorException {
363: sLog.fine("OperationBean.validateField(" + aContext
364: + ", aComponent=" + aComponent + ", aValue=" + aValue);
365: String alertDetail = "";
366:
367: // look up propertyName based on UIComponent mapping
368: FacesContext facesContext = FacesContext.getCurrentInstance();
369: Map sessionMap = (Map) facesContext.getApplication()
370: .createValueBinding("#{sessionScope}").getValue(
371: facesContext);
372: String textFieldId = aComponent.getClientId(facesContext);
373: String propertyName = (String) sessionMap.get(textFieldId);
374:
375: sLog.finer("OperationBean.validateField before sessionMap="
376: + sessionMap);
377:
378: boolean isValid = false;
379: if (aValue instanceof String) {
380:
381: // if found, validate value against min/max
382: if (null != propertyName) {
383: isValid = validatePropertyNameValueInRange(
384: propertyName, (String) aValue);
385: }
386: }
387: sLog.fine("OperationBean.validateField(...), aValue=" + aValue
388: + ", isValid=" + isValid);
389: if (!isValid) {
390: // Replace the general anticipatory error for this invalid property
391: // with a more specific error.
392: Properties alertDetailProperties = (Properties) sessionMap
393: .get("jbiAlertDetailProperties");
394: Properties propertyLabels = (Properties) sessionMap
395: .get("jbiAlertPropertyLabels");
396: if (null != alertDetailProperties) {
397: Properties metadata = getJbiRtConfigMetadata(propertyName);
398: String failedValidationRangeMessage = getFailedValidationRangeMessage(
399: metadata, propertyLabels);
400:
401: alertDetailProperties.put(propertyName,
402: failedValidationRangeMessage);
403:
404: // replace alert detail message with now-updated set of messages.
405: for (Object object : alertDetailProperties.keySet()) {
406: String k = (String) object;
407: String v = (String) alertDetailProperties
408: .getProperty(k);
409: sLog
410: .finer("OperationBean.validateField(...), alertDetailProperties k="
411: + k + ", v=" + v);
412: if (!"".equals(v)) {
413: alertDetail = v + "<br />" + alertDetail;
414: }
415: }
416: }
417: AlertBean alertBean = BeanUtilities.getAlertBean();
418: alertBean.setAlertType("error");
419: alertBean
420: .setAlertSummary(I18nUtilities
421: .getResourceString("jbi.configure.component.alert.summary"));
422: alertBean.setAlertDetail(alertDetail);
423:
424: String msgString = textFieldId;
425: sLog.fine("OperationBean.validateField(" + aContext + ", "
426: + aComponent + ", " + aValue + "), msgString="
427: + msgString);
428: FacesMessage facesMessage = new FacesMessage(msgString);
429: throw new ValidatorException(facesMessage);
430: }
431: sLog.fine("OperationBean.validateField(" + aContext
432: + ", aComponent=" + aComponent + ", aValue=" + aValue
433: + ") validation passed");
434: }
435:
436: private void showAlert(String aType, String aSummaryMessage,
437: String aDetailsMessage) {
438: sLog.fine("OperationBean.showAlert(" + aType + ", "
439: + aSummaryMessage + ", " + aDetailsMessage + ")");
440:
441: AlertBean alertBean = BeanUtilities.getAlertBean();
442: alertBean.setAlertType(aType);
443: alertBean.setAlertSummary(aSummaryMessage);
444: alertBean.setAlertDetail(aDetailsMessage);
445: }
446:
447: private boolean validatePropertyNameValueInRange(
448: String aPropertyName, String aValue) {
449: sLog.finer("OperationBean.validatePropertyNameValueInRange("
450: + aPropertyName + "), " + aValue + ")");
451: boolean result = false;
452: Properties metadata = getJbiRtConfigMetadata(aPropertyName);
453:
454: try {
455: int value = Integer.parseInt(aValue);
456:
457: if (null != metadata) {
458: int minValue = getInteger(metadata, "minValue");
459: int maxValue = getInteger(metadata, "maxValue");
460:
461: if ((value >= minValue) && (value <= maxValue)) {
462: result = true;
463: }
464: }
465: } catch (NumberFormatException nfEx) {
466: sLog.fine("OperationBean.validatePropertyNameValueInRange("
467: + aPropertyName + ", " + aValue + "), caught nfEx="
468: + nfEx);
469: }
470:
471: sLog
472: .fine("OperationBean.validatePropertyNameValueInRange("
473: + aPropertyName + ", " + aValue + "), result="
474: + result);
475: return result;
476: }
477:
478: private String getFailedValidationRangeMessage(
479: Properties aMetadata, Properties aPropertyLabels) {
480: sLog.fine("OperationBean.getFailedValidationRangeMessage("
481: + aMetadata + ", " + aPropertyLabels + ")");
482: String result = I18nUtilities
483: .getResourceString("jbi.root.configuration.failed.validation.internal.error.message");
484:
485: if ((null != aMetadata) && (null != aPropertyLabels)) {
486: String name = (String) aMetadata.get("name");
487: if (null != name) {
488: String label = (String) aPropertyLabels.get(name); // convert name to use the displayed label instead
489: String minValue = (String) aMetadata.get("minValue");
490: String maxValue = (String) aMetadata.get("maxValue");
491: Object[] args = { label, minValue, maxValue };
492: result = GuiUtil
493: .getMessage(
494: I18nUtilities
495: .getResourceString("jbi.root.configuration.failed.validation.range.message"),
496: args);
497: }
498: }
499:
500: sLog.fine("OperationBean.getFailedValidationRangeMessage("
501: + aMetadata + "), result=" + result);
502: return result;
503: }
504:
505: private int getInteger(Properties aMetadata, String aKey)
506: throws NumberFormatException {
507: sLog.fine("OperationBean.getInteger(" + aMetadata + ", " + aKey
508: + ")");
509: int result = -1;
510: String intString = aMetadata.getProperty(aKey);
511: if (null != intString) {
512: int parsedInt = Integer.parseInt(intString);
513: result = parsedInt;
514: }
515: sLog.fine("OperationBean.getInteger(" + aMetadata + ", " + aKey
516: + "), result=" + result);
517: return result;
518: }
519:
520: private Properties getJbiRtConfigMetadata(String aPropertyName) {
521: Properties result = new Properties();
522: sLog.fine("OperationBean.getJbiRtConfigMetadata("
523: + aPropertyName + ")");
524: try {
525: Properties metadata = mJac
526: .getRuntimeConfigurationMetaData(aPropertyName);
527: sLog.fine("OperationBean.getMetadata(" + aPropertyName
528: + "), metadata=" + metadata);
529: result = metadata;
530: } catch (com.sun.jbi.ui.common.JBIRemoteException jbiRemoteEx) {
531: jbiRemoteEx.printStackTrace();
532: }
533: sLog.fine("OperationBean.getJbiRtConfigMetadata("
534: + aPropertyName + "), result=" + result);
535: return result;
536: }
537:
538: private JBIAdminCommands mJac;
539: private String mJbiAutoInstallDir;
540: private String mHeartBeatInterval;
541: }
|