0001: /*
0002: * BEGIN_HEADER - DO NOT EDIT
0003: *
0004: * The contents of this file are subject to the terms
0005: * of the Common Development and Distribution License
0006: * (the "License"). You may not use this file except
0007: * in compliance with the License.
0008: *
0009: * You can obtain a copy of the license at
0010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0011: * See the License for the specific language governing
0012: * permissions and limitations under the License.
0013: *
0014: * When distributing Covered Code, include this CDDL
0015: * HEADER in each file and include the License file at
0016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0017: * If applicable add the following below this CDDL HEADER,
0018: * with the fields enclosed by brackets "[]" replaced with
0019: * your own identifying information: Portions Copyright
0020: * [year] [name of copyright owner]
0021: */
0022:
0023: /*
0024: * @(#)JbiJmxTask.java
0025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
0026: *
0027: * END_HEADER - DO NOT EDIT
0028: */
0029: package com.sun.jbi.ui.ant;
0030:
0031: import com.sun.jbi.ui.client.ConnectionType;
0032: import com.sun.jbi.ui.client.JBIAdminCommandsClientFactory;
0033: import com.sun.jbi.ui.client.JMXConnectionProperties;
0034: import com.sun.jbi.ui.common.I18NBundle;
0035: import com.sun.jbi.ui.common.JBIAdminCommands;
0036: import com.sun.jbi.ui.common.JBIManagementMessage;
0037: import com.sun.jbi.ui.common.JBIRemoteException;
0038: import com.sun.jbi.ui.common.JBIResultXmlBuilder;
0039: import com.sun.jbi.ui.common.JMXConnectionException;
0040: import com.sun.jbi.ui.common.JBIManagementMessage.JBIManagementMessageException;
0041: import java.util.Properties;
0042: import java.util.Set;
0043: import java.util.Iterator;
0044: import java.io.IOException;
0045: import java.io.PrintWriter;
0046: import org.apache.tools.ant.BuildException;
0047: import org.apache.tools.ant.Project;
0048: import org.apache.tools.ant.Task;
0049: import javax.management.openmbean.CompositeData;
0050: import javax.management.openmbean.OpenType;
0051: import javax.management.openmbean.ArrayType;
0052: import javax.management.openmbean.CompositeType;
0053: import javax.management.openmbean.SimpleType;
0054: import javax.management.openmbean.TabularType;
0055: import javax.management.openmbean.TabularData;
0056:
0057: import java.io.FileInputStream;
0058: import java.io.File;
0059:
0060: /** This class is the base class for all the jbi admin tasks that uses JBIAdminCommands
0061: * interface implementation to execute the jbi admin tasks.
0062: * This class has a set of common properties for all the jbi admin tasks. These common properties
0063: * are related to jmx connection informaton ( host, port, username, passwrod )
0064: * This class also has helper methods to format the display message and handling the errors
0065: * and the management status message.
0066: *
0067: * @author Sun Microsystems, Inc.
0068: */
0069:
0070: public abstract class JbiJmxTask extends Task {
0071: /** default ant prop */
0072: public static final String DEF_USERNAME_PROP = "jbi.default.username";
0073: /** default ant prop */
0074: public static final String DEF_PASSWORD_PROP = "jbi.default.password";
0075: /** default ant prop */
0076: public static final String DEF_HOST_PROP = "jbi.default.host";
0077: /** default ant prop */
0078: public static final String DEF_PORT_PROP = "jbi.default.port";
0079: /** default ant prop */
0080: public static final String DEF_URL_PROP = "jbi.default.url";
0081: /**default ant prop**/
0082: public static final String DEF_JMX_PROPERTIES_FILE = "jmx.default.properties.file";
0083:
0084: /**
0085: * i18n bundle
0086: */
0087: private static I18NBundle sI18NBundle = null;
0088:
0089: /** Holds value of property Secure. */
0090: private Boolean mSecure = null;
0091:
0092: /** Holds value of property host. */
0093: private String mHost = null;
0094:
0095: /** Holds value of property port. */
0096: private String mPort = null;
0097:
0098: /** Holds value of property username. */
0099: private String mUsername = null;
0100:
0101: /** Holds value of property password. */
0102: private String mPassword = null;
0103:
0104: /** Holds value of property passwordFile. */
0105: private File mPasswordFile = null;
0106:
0107: /** Holds value of property failOnError. */
0108: private boolean mFailOnError = true;
0109:
0110: /** Jboss strings **/
0111: private String mPropertiesFile = null;
0112:
0113: /** jbi commands client interface */
0114: private JBIAdminCommands mJbiAdminCommands = null;
0115:
0116: /** Getter for property failOnError.
0117: * @return Value true if failOnError else false.
0118: */
0119: public boolean isFailOnError() {
0120: return this .mFailOnError;
0121: }
0122:
0123: /** Setter for property failOnError.
0124: * @param failOnError New value of property failOnError.
0125: */
0126: public void setFailOnError(boolean failOnError) {
0127: this .mFailOnError = failOnError;
0128: }
0129:
0130: /** Getter for property Secure.
0131: * @return Value true if Secure else false.
0132: */
0133: public Boolean getSecure() {
0134: return this .mSecure;
0135: }
0136:
0137: /** Setter for property Secure.
0138: * @param secure New value of property Secure.
0139: */
0140: public void setSecure(Boolean secure) {
0141: this .mSecure = secure;
0142: }
0143:
0144: /** Getter for Default Username.
0145: * @return Value of Default Username.
0146: *
0147: */
0148: private String getDefaultUsername() {
0149: return this .getProject().getProperty(this .DEF_USERNAME_PROP);
0150: }
0151:
0152: /** Getter for property Username.
0153: * @return Value of property Username.
0154: *
0155: */
0156: public String getUsername() {
0157: if (this .mUsername == null) {
0158: // try to get the default value from ant prop
0159: return getDefaultUsername();
0160: }
0161: return this .mUsername;
0162: }
0163:
0164: /**
0165: * Setter for property Username.
0166: * @param aUsername user name
0167: */
0168: public void setUsername(String aUsername) {
0169: // remove the old jbi client
0170: this .mJbiAdminCommands = null;
0171: this .mUsername = aUsername;
0172: }
0173:
0174: /** Getter for Default Password.
0175: * @return Value of Default Password.
0176: *
0177: */
0178: public String getDefaultPassword() {
0179: return this .getProject().getProperty(this .DEF_PASSWORD_PROP);
0180: }
0181:
0182: /** Getter for property Password.
0183: * @return Value of property Password.
0184: *
0185: */
0186: public String getPassword() {
0187: if (this .mPassword == null) {
0188: return getDefaultPassword();
0189: }
0190: return this .mPassword;
0191: }
0192:
0193: /**
0194: * Setter for property Password.
0195: * @param aPassword password
0196: */
0197: public void setPassword(String aPassword) {
0198: // remove the old jbi client
0199: this .mJbiAdminCommands = null;
0200: this .mPassword = aPassword;
0201: }
0202:
0203: /** Getter for property passwordFile.
0204: * @return Value of property passwordFile.
0205: *
0206: */
0207: public File getPasswordfile() {
0208: return this .mPasswordFile;
0209: }
0210:
0211: /** Setter for property passwordFile.
0212: * @param passwordFile New value of property passwordFile.
0213: *
0214: */
0215: public void setPasswordfile(File passwordFile) {
0216: this .mPasswordFile = passwordFile;
0217: }
0218:
0219: /** Getter for Default host.
0220: * @return Value of Default host.
0221: *
0222: */
0223: public String getDefaultHost() {
0224: return this .getProject().getProperty(this .DEF_HOST_PROP);
0225: }
0226:
0227: /** Getter for property host.
0228: * @return Value of property host.
0229: *
0230: */
0231: public String getHost() {
0232: if (this .mHost == null) {
0233: return getDefaultHost();
0234: }
0235: return this .mHost;
0236: }
0237:
0238: /** Setter for property host.
0239: * @param aHost New value of property host.
0240: *
0241: */
0242: public void setHost(String aHost) {
0243: // remove the old jbi client
0244: this .mJbiAdminCommands = null;
0245: this .mHost = aHost;
0246: }
0247:
0248: /** Setter for properties file.
0249: *
0250: * @param propertiesfile name of the properties file
0251: */
0252: public void setJmxpropertiesfile(String propertiesfile) {
0253: this .mPropertiesFile = propertiesfile;
0254: }
0255:
0256: /** Getter for properties file
0257: *
0258: * @return properties file name
0259: */
0260: public String getJmxpropertiesfile() {
0261: if ((mPropertiesFile == null)
0262: || (!(new File(mPropertiesFile)).exists())) {
0263: return getDefaultJmxpropertiesfile();
0264: }
0265: return mPropertiesFile;
0266: }
0267:
0268: /** Default Getter for properties file.
0269: *
0270: * @return default properties file name.
0271: */
0272: public String getDefaultJmxpropertiesfile() {
0273: return this .getProject().getProperty(
0274: this .DEF_JMX_PROPERTIES_FILE);
0275: }
0276:
0277: /**
0278: * Returns the properties from the file.
0279: *
0280: * @return properties
0281: */
0282:
0283: public Properties getProviderProperties() {
0284: Properties conprops = new Properties();
0285: if (mPropertiesFile != null) {
0286: try {
0287: File f = new File(mPropertiesFile);
0288: FileInputStream fio = new FileInputStream(f);
0289: conprops.load(fio);
0290: } catch (Exception ex) {
0291: ;
0292: }
0293: }
0294: return conprops;
0295: }
0296:
0297: /** Getter for property port.
0298: * @return Value of property port.
0299: *
0300: */
0301: public String getDefaultPort() {
0302: String port = this .getProject().getProperty(this .DEF_PORT_PROP);
0303: return port;
0304: }
0305:
0306: /** Getter for property port.
0307: * @return Value of property port.
0308: *
0309: */
0310: public String getPort() {
0311: if (this .mPort == null) {
0312: return getDefaultPort();
0313: }
0314: return this .mPort;
0315: }
0316:
0317: /** Setter for property port.
0318: * @param aPort New value of property port.
0319: */
0320: public void setPort(String aPort) {
0321: // remove the old jbi client
0322: this .mJbiAdminCommands = null;
0323: this .mPort = aPort;
0324: }
0325:
0326: /** Getter for Default host.
0327: * @return Value of Default host.
0328: *
0329: */
0330: private String getDefaultUrl() {
0331: return this .getProject().getProperty(this .DEF_URL_PROP);
0332: }
0333:
0334: /**
0335: * returns properties object contain the name/value pairs
0336: * for jmx connection properties
0337: * @return Properties object
0338: */
0339: protected Properties getJmxConnectionProperties() {
0340:
0341: String username = this .getUsername();
0342: String password = this .getPassword();
0343: String host = this .getHost();
0344: String port = this .getPort();
0345:
0346: String url = this .getDefaultUrl();
0347: Properties provprops = getProviderProperties();
0348:
0349: return JMXConnectionProperties.getJMXConnectionPropertyMap(
0350: provprops, url, host, port, username, password);
0351:
0352: }
0353:
0354: /**
0355: * reads the password from the file according to the platform password file definition
0356: * @param pwFile path to the password file
0357: * @return Value of the password from file
0358: */
0359: protected String getPasswordFromFile(File pwFile)
0360: throws IOException {
0361: final String AS_ADMIN_PASSWORD_PROP = "AS_ADMIN_PASSWORD";
0362: String password = null;
0363: FileInputStream in = null;
0364: try {
0365: in = new FileInputStream(pwFile);
0366: Properties props = new Properties();
0367: props.load(in);
0368: password = props.getProperty(AS_ADMIN_PASSWORD_PROP);
0369: if (password == null) {
0370: throw new IOException(
0371: createFailedFormattedJbiAdminResult(
0372: "jbi.ui.ant.task.error.password.not.found.in.file",
0373: new Object[] { pwFile.getName() }));
0374: } else {
0375: return password;
0376: }
0377: } finally {
0378: if (in != null) {
0379: try {
0380: in.close();
0381: } catch (IOException ex) {
0382: this .logDebug(ex.getMessage());
0383: }
0384: }
0385: }
0386: }
0387:
0388: /**
0389: * check if the password file is set. If set get the password from the password file, else
0390: * get the password from the task attribute "password".
0391: * @return Value of the password
0392: */
0393: protected String getPasswordFromFileOrAttribute()
0394: throws IOException {
0395: String password = null;
0396: File pwFile = this .getPasswordfile();
0397: if (pwFile == null) {
0398: // No password file, get it from attribute "password"
0399: return this .getPassword();
0400: }
0401: this .logDebug("Getting password from the password file "
0402: + pwFile);
0403: if (!pwFile.exists()) {
0404: this .logDebug("Password file path not found "
0405: + pwFile.getPath());
0406: throw new IOException(createFailedFormattedJbiAdminResult(
0407: "jbi.ui.ant.task.error.password.file.not.exist",
0408: new Object[] { pwFile.getName() }));
0409: }
0410: return password;
0411: }
0412:
0413: /**
0414: * returns JBIAdminCommands implementation
0415: * @return JBIAdminCommands interface
0416: * @throws JMXConnectionException on error
0417: */
0418: protected JBIAdminCommands getJBIAdminCommands() throws Exception {
0419: // lazzy initialize the mJbiAdminCommands
0420: if (this .mJbiAdminCommands == null) {
0421: // validate jmx connection properties ( e.g. host, port, username, password etc )
0422: this .validateJmxConnectionPortValue(this .getPort());
0423:
0424: String portValue = this .getPort();
0425: if (portValue == null) {
0426: portValue = "4848";
0427: }
0428: int port = Integer.parseInt(portValue);
0429:
0430: String host = this .getHost();
0431: if (host == null) {
0432: host = "localhost";
0433: }
0434:
0435: String username = this .getUsername();
0436: String password = this .getPassword();
0437:
0438: ConnectionType connType = null;
0439:
0440: Boolean secure = this .getSecure();
0441: if (secure != null && secure.booleanValue()) {
0442: connType = ConnectionType.HTTPS;
0443: this .mJbiAdminCommands = JBIAdminCommandsClientFactory
0444: .getInstance(host, port, username, password,
0445: connType);
0446: } else {
0447: //TODO: get the default connection type from the default settings from common client based on
0448: // platform ( PE or EE ) settings
0449: this .mJbiAdminCommands = JBIAdminCommandsClientFactory
0450: .getInstance(host, port, username, password);
0451: }
0452:
0453: }
0454: return this .mJbiAdminCommands;
0455: }
0456:
0457: /** gives the I18N bundle for standard ant tasks. It is possible to
0458: * have different i18n bundle for extended tasks such as filtered query tasks
0459: *@return I18NBundle object
0460: */
0461: private static I18NBundle getStandardI18NBundle() {
0462: // lazzy initialize the JBI Client
0463: if (sI18NBundle == null) {
0464: sI18NBundle = new I18NBundle("com.sun.jbi.ui.ant");
0465: }
0466: return sI18NBundle;
0467: }
0468:
0469: /** gives the I18N bundle for ant tasks
0470: * extended tasks override this if they want to provide different i18n bundle
0471: * for their messages.
0472: *@return I18NBundle object
0473: */
0474: protected I18NBundle getI18NBundle() {
0475: return getStandardI18NBundle();
0476: }
0477:
0478: /**
0479: * returns i18n key. tasks implement this method.
0480: * @return i18n key for the success status
0481: */
0482: protected abstract String getTaskSuccessStatusI18NKey();
0483:
0484: /**
0485: * returns i18n key. tasks implement this method.
0486: * @return i18n key for the failed status
0487: */
0488: /**
0489: * returns i18n key. tasks implement this method.
0490: * @return i18n key for the warning status
0491: */
0492: protected String getTaskWarningStatusI18NKey() {
0493: return null;
0494: }
0495:
0496: protected abstract String getTaskFailedStatusI18NKey();
0497:
0498: /**
0499: * returns i18n key. tasks implement this method to return the i18n key.
0500: * default implementation returns null indicating that the task
0501: * does not have partial success status message. If the tasks
0502: * such as service assembly tasks have to display main partial success
0503: * message, they have to override and return the i18n key that is in the
0504: * i18n bundle.
0505: *
0506: * use of this method should check for null return and then fall back to
0507: * the getTaskSuccessStatusI18NKey for the main status message for partial
0508: * success.
0509: * @return i18n key for the partial success
0510: */
0511: protected String getTaskPartialSuccessStatusI18NKey() {
0512: return null;
0513: }
0514:
0515: /**
0516: * logs as a INFO
0517: * @param anInfoMsg Message
0518: */
0519: protected void logInfo(String anInfoMsg) {
0520: log(anInfoMsg, Project.MSG_INFO);
0521: }
0522:
0523: /**
0524: * logs as a WARNING
0525: * @param aWarningMsg Message
0526: */
0527: protected void logWarning(String aWarningMsg) {
0528: log(aWarningMsg, Project.MSG_WARN);
0529: }
0530:
0531: /**
0532: * logs as a ERROR
0533: * @param aErrorMsg Message
0534: */
0535: protected void logError(String aErrorMsg) {
0536: log(aErrorMsg, Project.MSG_ERR);
0537: }
0538:
0539: /**
0540: * logs as a DEBUG
0541: * @param aDebugMsg Message
0542: */
0543: protected void logDebug(String aDebugMsg) {
0544: log(aDebugMsg, Project.MSG_DEBUG);
0545: }
0546:
0547: /**
0548: * logs as a VERBOSE
0549: * @param aVerboseMsg Message
0550: */
0551: protected void logVerbose(String aVerboseMsg) {
0552: log(aVerboseMsg, Project.MSG_VERBOSE);
0553: }
0554:
0555: /**
0556: * format message
0557: * @param statusMsg status message
0558: * @return formatted message
0559: */
0560: private String formatSuccessMessage(String statusMsg) {
0561: String key = "jbi.ui.ant.task.msg.success.no.args.format"; //NOI18N
0562: return getStandardI18NBundle().getMessage(key, statusMsg);
0563: }
0564:
0565: /**
0566: * returns formatted success message
0567: * @param statusMsg main message text
0568: * @param causeMsg cause message text
0569: * @return foramtted message
0570: */
0571: private String formatSuccessMessage(String statusMsg,
0572: String causeMsg) {
0573: if (causeMsg == null) {
0574: return formatSuccessMessage(statusMsg);
0575: }
0576: String key = "jbi.ui.ant.task.msg.success.format"; //NOI18N
0577: return getStandardI18NBundle().getMessage(key, statusMsg,
0578: causeMsg);
0579: }
0580:
0581: /**
0582: * returns formated failed message
0583: * @param statusMsg main message text
0584: * @param causeMsg cause message text
0585: * @return formatted message
0586: */
0587: private String formatFailedMessage(String statusMsg, String causeMsg) {
0588: String key = "jbi.ui.ant.task.msg.failed.format"; //NOI18N
0589: return getStandardI18NBundle().getMessage(key, statusMsg,
0590: causeMsg);
0591: }
0592:
0593: /**
0594: * task success message
0595: * @return formated message
0596: */
0597: protected String formatTaskSuccessMessage() {
0598: String statusMsg = getI18NBundle().getMessage(
0599: getTaskSuccessStatusI18NKey());
0600: return formatSuccessMessage(statusMsg);
0601: }
0602:
0603: /**
0604: * returns formatted success message
0605: * @param causeMsg cause message
0606: * @return formated message
0607: */
0608: protected String formatTaskSuccessMessage(String causeMsg) {
0609: String statusMsg = getI18NBundle().getMessage(
0610: getTaskSuccessStatusI18NKey());
0611: return formatSuccessMessage(statusMsg, causeMsg);
0612: }
0613:
0614: /**
0615: * returns formatted failed message
0616: * @param causeMsg cause message text
0617: * @return formatted message
0618: */
0619: protected String formatTaskFailedMessage(String causeMsg) {
0620: String statusMsg = getI18NBundle().getMessage(
0621: getTaskFailedStatusI18NKey());
0622: return formatFailedMessage(statusMsg, causeMsg);
0623: }
0624:
0625: /**
0626: * throws BuildException
0627: * @param causeMsg cuased message text
0628: * @throws BuildException with cause
0629: */
0630: protected void throwBuildException(String causeMsg)
0631: throws BuildException {
0632: String expMsg = formatTaskFailedMessage(causeMsg);
0633: throw new BuildException(expMsg, location);
0634: }
0635:
0636: /**
0637: * throws BuildException
0638: * @param aCause a exception to embed
0639: * @throws BuildException with cause
0640: */
0641: protected void throwBuildException(Throwable aCause)
0642: throws BuildException {
0643: String causeMsg = aCause.getMessage();
0644: if (causeMsg == null) {
0645: causeMsg = aCause.toString();
0646: }
0647: String expMsg = formatTaskFailedMessage(causeMsg);
0648: throw new BuildException(expMsg, aCause, location);
0649: }
0650:
0651: /**
0652: * throws exception
0653: * @param aCauseMgmtMsg message object
0654: * @throws BuildException on error
0655: */
0656: protected void throwBuildException(
0657: JBIManagementMessage aCauseMgmtMsg) throws BuildException {
0658: String causeMsg = aCauseMgmtMsg.getMessage();
0659: if (causeMsg == null) {
0660: causeMsg = aCauseMgmtMsg.toString();
0661: }
0662: String expMsg = formatTaskFailedMessage(causeMsg);
0663:
0664: // printMessage("*** throwing Failed Result Mgmt Message in print Result " +
0665: // formattedMsg );
0666: JBIManagementMessageException aCauseMgmtMsgEx = new JBIManagementMessageException(
0667: expMsg, aCauseMgmtMsg);
0668:
0669: throw new BuildException(expMsg, aCauseMgmtMsgEx, location);
0670:
0671: }
0672:
0673: /**
0674: * throws exception
0675: * @param aCauseKey i18n key of the cause
0676: * @throws BuildException build exception
0677: */
0678: protected void throwTaskBuildException(String aCauseKey)
0679: throws BuildException {
0680: // String aCauseMsg = this.getI18NBundle().getMessage(aCauseKey);
0681: String aCauseMsg = createFailedFormattedJbiAdminResult(
0682: aCauseKey, null);
0683: throwBuildException(aCauseMsg);
0684: }
0685:
0686: /**
0687: * throws exception
0688: * @param aCauseKey i18n key of the cause
0689: * @param aCauseArg1 root cuase
0690: * @throws BuildException build exception
0691: */
0692: protected void throwTaskBuildException(String aCauseKey,
0693: String aCauseArg1) throws BuildException {
0694: // String aCauseMsg = this.getI18NBundle().getMessage(aCauseKey, aCauseArg1);
0695: String aCauseMsg = createFailedFormattedJbiAdminResult(
0696: aCauseKey, new Object[] { aCauseArg1 });
0697: throwBuildException(aCauseMsg);
0698: }
0699:
0700: /**
0701: * prints result
0702: * @param result string to print
0703: */
0704: protected void printTaskSuccess(String result) {
0705: if (result == null || (result.trim().length() <= 0)) {
0706: logWarning(formatTaskSuccessMessage());
0707: } else {
0708: logWarning(formatTaskSuccessMessage(result));
0709: }
0710: }
0711:
0712: /**
0713: * prints result
0714: * @param result string to print
0715: */
0716: protected void printTaskPartialSuccess(String result) {
0717: String statusMsg = getI18NBundle().getMessage(
0718: getTaskPartialSuccessStatusI18NKey());
0719:
0720: String formatterKey = "jbi.ui.ant.task.msg.success.format"; //NOI18N
0721:
0722: String formattedResult = null;
0723:
0724: if (result == null || (result.trim().length() <= 0)) {
0725: formatterKey = "jbi.ui.ant.task.msg.success.no.args.format"; //NOI18N
0726: formattedResult = getStandardI18NBundle().getMessage(
0727: formatterKey, statusMsg);
0728: } else {
0729: formattedResult = getStandardI18NBundle().getMessage(
0730: formatterKey, statusMsg, result);
0731: }
0732:
0733: logWarning(formattedResult);
0734: }
0735:
0736: /**
0737: * prints result
0738: * @param mgmtMsg message object
0739: */
0740: protected void printTaskSuccess(JBIManagementMessage mgmtMsg) {
0741: String msg = mgmtMsg.getMessage();
0742:
0743: String partialSuccessI18NKey = this
0744: .getTaskPartialSuccessStatusI18NKey();
0745:
0746: if (partialSuccessI18NKey != null && mgmtMsg.isWarningMsg()) {
0747: // isWarningMsge = partial success ( success with warnings )
0748: // if partail success i18n key available and is a partial success message,
0749: // then print partail success msg
0750: printTaskPartialSuccess(msg);
0751: } else {
0752: // print regular success message.
0753: printTaskSuccess(msg);
0754: }
0755: }
0756:
0757: /** print message
0758: * @param aMsg a message string
0759: */
0760: protected void printMessage(String aMsg) {
0761: logWarning(aMsg);
0762: }
0763:
0764: /**
0765: * retrieves the exception message and try to construct the jbi mgmt message
0766: * @param ex Exception from which the message should be constructed
0767: * @return JBIManagementMessage object if the excpetion contains the xml text else return null.
0768: */
0769: protected JBIManagementMessage extractJBIManagementMessage(
0770: Exception ex) {
0771: JBIManagementMessage mgmtMsg = null;
0772: if (ex instanceof JBIRemoteException) {
0773: JBIRemoteException rEx = (JBIRemoteException) ex;
0774: mgmtMsg = rEx.extractJBIManagementMessage();
0775: // logDebug(rEx.getMessage());
0776: } else {
0777: String exMessage = ex.getMessage();
0778: mgmtMsg = JBIManagementMessage
0779: .createJBIManagementMessage(exMessage);
0780: // logDebug(exMessage);
0781: }
0782:
0783: return mgmtMsg;
0784: }
0785:
0786: /**
0787: * here we check for the management message in the exception and process
0788: * it for the error, warning or success information. If it is error throw
0789: * the build exception, for all other cases print exception results with warnings
0790: * @param ex Exception from the task execution
0791: * @throws BuildException if the exception status is error
0792: */
0793: protected void processTaskException(Exception ex)
0794: throws BuildException {
0795: JBIManagementMessage mgmtMsg = extractJBIManagementMessage(ex);
0796:
0797: if (mgmtMsg == null) {
0798: // not a management message
0799: throwBuildException(ex);
0800: } else {
0801: // check the mgmt msg is a success, warning or error
0802: if (mgmtMsg.isFailedMsg()) {
0803: throwBuildException(mgmtMsg);
0804: } else {
0805: // print success message
0806: printTaskSuccess(mgmtMsg);
0807: }
0808: }
0809: }
0810:
0811: /**
0812: * processes the result string as a management message for for the error, warning or
0813: * success information. If it is error throw the build exception, for all other cases
0814: * print success results with warnings
0815: * @param result results text
0816: * @throws BuildException if the exception status is error
0817: */
0818: protected void processTaskResult(String result)
0819: throws BuildException {
0820: JBIManagementMessage mgmtMsg = null;
0821:
0822: if (result != null) {
0823: mgmtMsg = JBIManagementMessage
0824: .createJBIManagementMessage(result);
0825: }
0826:
0827: if (mgmtMsg == null) {
0828: printTaskSuccess(result);
0829: return;
0830: }
0831:
0832: // check the mgmt msg is a success, warning or error
0833: if (mgmtMsg.isFailedMsg()) {
0834: throwBuildException(mgmtMsg);
0835: } else {
0836: // print success or partial success message
0837: printTaskSuccess(mgmtMsg);
0838: }
0839:
0840: // if ( mgmtMsg.isSuccessMsg())
0841: // {
0842: // printTaskSuccess(mgmtMsg);
0843: // }
0844: // else
0845: // {
0846: // throwBuildException(mgmtMsg);
0847: // }
0848:
0849: }
0850:
0851: /**
0852: * this is where the task exection code gets implemented.
0853: * this will be called from execute method that preprocesses the build exception
0854: * before throwing it to ant runtime to process.
0855: * @throws org.apache.tools.ant.BuildException on error
0856: */
0857: protected abstract void executeTask() throws BuildException;
0858:
0859: protected void logDebugConnectionAttributes() {
0860: logDebug("values of jbi task connection attributes: ("
0861: + " host=" + this .getHost() + ", port="
0862: + this .getPort() + ", secure=" + this .getSecure()
0863: + ", username=" + this .getUsername() + ")");
0864: }
0865:
0866: /** executes the ant task. Ant Task framework calls this method to
0867: * excute the task. this method intern calls the executeTask where the task execution
0868: * code gets implemented. This methods processes the failOnError before throwing back
0869: * the BuildException to the ant environment. If the failOnError is false, it just
0870: * prints the exception message and returns otherwise it rethrows the build exception.
0871: * @throws BuildException if error or exception occurs.
0872: */
0873: public void execute() throws BuildException {
0874: logDebugConnectionAttributes();
0875: try {
0876: executeTask();
0877: } catch (BuildException bEx) {
0878: if (this .isFailOnError()) {
0879: throw bEx;
0880: } else {
0881: logWarning(bEx.getMessage());
0882: }
0883: }
0884: }
0885:
0886: /**
0887: * converts the i18n error message to the jbi result display format.
0888: * @param i18nKey String represneting i18n key
0889: * @param args arguments to the i18n string
0890: * @return the string in the jbi result display format of jbi mgmt message.
0891: */
0892: protected String createFailedFormattedJbiAdminResult(
0893: String i18nKey, Object[] args) {
0894:
0895: String msgCode = getI18NBundle().getMessage(i18nKey + ".ID");
0896: String msg = getI18NBundle().getMessage(i18nKey, args);
0897:
0898: String jbiResultXml = JBIResultXmlBuilder.getInstance()
0899: .createJbiResultXml("JBI_ANT_TASKS_VALIDATION",
0900: JBIResultXmlBuilder.FAILED_RESULT,
0901: JBIResultXmlBuilder.ERROR_MSG_TYPE, msgCode,
0902: msg, args, null);
0903:
0904: JBIManagementMessage mgmtMsg = null;
0905: mgmtMsg = JBIManagementMessage
0906: .createJBIManagementMessage(jbiResultXml);
0907:
0908: if (mgmtMsg != null) {
0909: return mgmtMsg.getMessage();
0910: } else {
0911: return msg;
0912: }
0913: }
0914:
0915: /**
0916: * validate
0917: *
0918: * @param port port
0919: *
0920: * @throws JMXConnectionException on error
0921: */
0922: protected void validateJmxConnectionPortValue(String port)
0923: throws JMXConnectionException {
0924: if (port == null) {
0925: return;
0926: }
0927:
0928: int portValue = -1;
0929:
0930: try {
0931: portValue = Integer.parseInt(port);
0932: } catch (NumberFormatException ex) {
0933: // pass null to the cause as you have already serialized the cause to jbi mgmt xml
0934: throw new JMXConnectionException(
0935: createFailedFormattedJbiAdminResult(
0936: "jbi.ui.ant.task.error.invalid.jmx.port",
0937: new Object[] { port }), null);
0938: }
0939: }
0940:
0941: /**
0942: * return the trimed component name or throws build exception
0943: * @param name component name string as passed to the attribute
0944: * @return trimed component name
0945: * @throws BuildException if the compName is null or empty
0946: */
0947: protected String getValidName(String name, String errorMsgI18nKey)
0948: throws BuildException {
0949: String validName = name;
0950: if (validName != null) {
0951: validName = validName.trim();
0952: }
0953:
0954: if (validName == null || validName.length() == 0) {
0955: throwTaskBuildException(errorMsgI18nKey);
0956: }
0957:
0958: return name;
0959: }
0960:
0961: /**
0962: * return the trimed component name or throws build exception
0963: * @param compName component name string as passed to the attribute
0964: * @return trimed component name
0965: * @throws BuildException if the compName is null or empty
0966: */
0967: protected String getValidComponentName(String compName)
0968: throws BuildException {
0969: return getValidName(compName,
0970: "jbi.ui.ant.task.error.nullCompName");
0971: }
0972:
0973: /**
0974: * return the trimed component name or throws build exception
0975: * @param slibName shared library name string as passed to the attribute
0976: * @return trimed component name
0977: * @throws BuildException if the compName is null or empty
0978: */
0979: protected String getValidSharedLibraryName(String slibName)
0980: throws BuildException {
0981: return getValidName(slibName,
0982: "jbi.ui.ant.task.error.nullSLibName");
0983: }
0984:
0985: /**
0986: * return the trimed component name or throws build exception
0987: * @param saName service assembly name string as passed to the attribute
0988: * @return trimed component name
0989: * @throws BuildException if the compName is null or empty
0990: */
0991: protected String getValidServiceAssemblyName(String saName)
0992: throws BuildException {
0993: return getValidName(saName, "jbi.ui.ant.task.error.nullSAName");
0994: }
0995:
0996: /**
0997: * parse and print the tabular data
0998: */
0999: protected void printTabularData(TabularData statsReport,
1000: PrintWriter msgWriter, String tabStr) {
1001: Set names = statsReport.keySet();
1002: for (Object name : names) {
1003: Object[] key = ((java.util.List) name).toArray();
1004: CompositeData compData = statsReport.get(key);
1005: printMessage("");
1006: printCompositeDataRecursively(compData, msgWriter, tabStr);
1007: }
1008: }
1009:
1010: /**
1011: * @param compositeData
1012: * @param msgWriter
1013: */
1014: protected void printCompositeDataRecursively(
1015: CompositeData compositeData, PrintWriter msgWriter,
1016: String tabStr) {
1017: if (compositeData == null) {
1018: return;
1019: }
1020:
1021: CompositeType compType = compositeData.getCompositeType();
1022: Set openTypes = compType.keySet();
1023: Iterator itr = openTypes.iterator();
1024: while (itr.hasNext()) {
1025: String itemName = (String) itr.next();
1026: OpenType openType = (OpenType) compType.getType(itemName);
1027: if (openType != null) {
1028: if (openType instanceof SimpleType) {
1029: msgWriter.println(tabStr + itemName + ": "
1030: + (compositeData.get(itemName)));
1031: } else if (openType instanceof TabularType) {
1032: TabularData theTabularData = (TabularData) compositeData
1033: .get(itemName);
1034: printTabularData(theTabularData, msgWriter, tabStr
1035: + "\t");
1036: } else if (openType instanceof CompositeType) {
1037: msgWriter.println(tabStr + itemName + ":");
1038: printCompositeDataRecursively(
1039: (CompositeData) compositeData.get(itemName),
1040: msgWriter, tabStr + "\t");
1041: } else if (openType instanceof ArrayType) {
1042: msgWriter.println(tabStr + itemName + ":");
1043: OpenType arrayElementType = ((ArrayType) openType)
1044: .getElementOpenType();
1045: Object[] arrayData = (Object[]) compositeData
1046: .get(itemName);
1047: for (int i = 0; i < arrayData.length; ++i) {
1048: if (arrayElementType instanceof SimpleType) {
1049: msgWriter.println(tabStr + "\t"
1050: + arrayData[i]);
1051: } else if (arrayElementType instanceof TabularType) {
1052: TabularData theTbData = (TabularData) arrayData[i];
1053: printTabularData(theTbData, msgWriter,
1054: tabStr + "\t");
1055: } else if (arrayElementType instanceof CompositeType) {
1056: printCompositeDataRecursively(
1057: (CompositeData) arrayData[i],
1058: msgWriter, tabStr + "\t");
1059: } else if (arrayElementType instanceof ArrayType) {
1060: printArrayDataRecursively(
1061: (Object[]) arrayData[i],
1062: arrayElementType, msgWriter, tabStr
1063: + "\t");
1064: }
1065: }
1066: }
1067: }
1068: }
1069: }
1070:
1071: /**
1072: * @param arrayData
1073: * @param elementType
1074: * @param msgWriter
1075: */
1076: protected void printArrayDataRecursively(Object[] arrayData,
1077: OpenType elementType, PrintWriter msgWriter, String tabStr) {
1078: for (int i = 0; i < arrayData.length; ++i) {
1079: if (elementType instanceof SimpleType) {
1080: msgWriter.println(tabStr + arrayData[i].toString());
1081: } else if (elementType instanceof TabularType) {
1082: msgWriter.println(tabStr + arrayData[i].toString());
1083: } else if (elementType instanceof CompositeType) {
1084: printCompositeDataRecursively(
1085: (CompositeData) arrayData[i], msgWriter, tabStr);
1086: } else if (elementType instanceof ArrayType) {
1087: msgWriter
1088: .println(tabStr
1089: + ((ArrayType) elementType)
1090: .getTypeName() + ":");
1091: printArrayDataRecursively((Object[]) arrayData[i],
1092: elementType, msgWriter, tabStr + "\t");
1093: }
1094: }
1095: }
1096: }
|