001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JbiSetComponentLoggersTask.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.ant;
030:
031: import com.sun.jbi.ui.common.JBIManagementMessage;
032: import com.sun.jbi.ui.common.JBIResultXmlBuilder;
033: import java.io.File;
034: import java.io.FileInputStream;
035: import java.io.IOException;
036: import java.io.PrintWriter;
037: import java.io.StringWriter;
038: import java.util.ArrayList;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042: import java.util.TreeMap;
043: import java.util.Properties;
044: import org.apache.tools.ant.BuildException;
045:
046: /** This class is an ant task for updating service engine or binding component.
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public class JbiSetComponentLoggersTask extends JbiTargetTask {
051: /**
052: * logger success msg key
053: */
054: private static final String LOGGER_SUCCESS_STATUS_KEY = "jbi.ui.ant.set.logger.successful";
055:
056: /**
057: * logger failure msg key
058: */
059: private static final String LOGGER_FAILED_STATUS_KEY = "jbi.ui.ant.set.logger.failed";
060:
061: /**
062: * logger partial success msg key
063: */
064: private static final String LOGGER_PARTIAL_SUCCESS_STATUS_KEY = "jbi.ui.ant.set.logger.partial.success";
065:
066: /** Holds Nested element list of <logger> */
067: private List mLoggerList;
068:
069: /** Holds value of property componentName. */
070: private String mComponentName = null;
071:
072: /**
073: * Getter for property componentName.
074: * @return Value of property componentName.
075: */
076: public String getComponentName() {
077: return this .mComponentName;
078: }
079:
080: /**
081: * Setter for property componentName.
082: * @param componentName name of the component.
083: */
084: public void setComponentName(String componentName) {
085: this .mComponentName = componentName;
086: }
087:
088: private void debugPrintParams(Properties params) {
089: if (params == null) {
090: this .logDebug("Set Configuration params are NULL");
091: return;
092: }
093: StringWriter stringWriter = new StringWriter();
094: PrintWriter out = new PrintWriter(stringWriter);
095: params.list(out);
096: out.close();
097: this .logDebug(stringWriter.getBuffer().toString());
098: }
099:
100: private String createFormatedSuccessJbiResultMessage(
101: String i18nKey, Object[] args) {
102:
103: String msgCode = getI18NBundle().getMessage(i18nKey + ".ID");
104: String msg = getI18NBundle().getMessage(i18nKey, args);
105:
106: String jbiResultXml = JBIResultXmlBuilder.getInstance()
107: .createJbiResultXml("JBI_ANT_TASK_SET_CONFIG",
108: JBIResultXmlBuilder.SUCCESS_RESULT,
109: JBIResultXmlBuilder.INFO_MSG_TYPE, msgCode,
110: msg, args, null);
111:
112: JBIManagementMessage mgmtMsg = null;
113: mgmtMsg = JBIManagementMessage
114: .createJBIManagementMessage(jbiResultXml);
115: return (mgmtMsg != null) ? mgmtMsg.getMessage() : msg;
116: }
117:
118: private void executeSetComponentLoggers(String componentName)
119: throws BuildException {
120:
121: try {
122: // Go throught the static/constant configuration paramter elements
123: String target = getValidTarget();
124: List loggerList = this .getLoggerList();
125:
126: this .logDebug("Executing Set logger level ....");
127:
128: // Go throught the logger elements
129: Iterator it = loggerList.iterator();
130: boolean emptyFlag = true;
131:
132: while (it.hasNext()) {
133: Logger logger = (Logger) it.next();
134:
135: String theName = "" + logger.getName();
136: if (theName.compareTo("") != 0) {
137: if (("" + logger.getLevel()).toUpperCase()
138: .compareTo("DEFAULT") != 0) {
139: this .logDebug("component name: "
140: + componentName
141: + " logger name: "
142: + logger.getName()
143: + " logger level: "
144: + java.util.logging.Level.parse(logger
145: .getLevel()) + " target: "
146: + target);
147: this .getJBIAdminCommands()
148: .setComponentLoggerLevel(
149: componentName,
150: logger.getName(),
151: java.util.logging.Level
152: .parse(logger
153: .getLevel()),
154: target, target);
155: } else {
156: this .getJBIAdminCommands()
157: .setComponentLoggerLevel(componentName,
158: logger.getName(), null, target,
159: target);
160: }
161:
162: emptyFlag = false;
163: }
164: }
165:
166: if (emptyFlag == true) {
167: String msg = createFailedFormattedJbiAdminResult(
168: "jbi.ui.ant.task.error.no.input.component.logger.data.found",
169: null);
170: throw new BuildException(msg, getLocation());
171: }
172:
173: String i18nKey = "jbi.ui.ant.task.info.logger.comp.done.on.target";
174: Object[] args = { componentName, target };
175: this
176: .printTaskSuccess(createFormatedSuccessJbiResultMessage(
177: i18nKey, args));
178: } catch (Exception ex) {
179: processTaskException(ex);
180: }
181: }
182:
183: /** executes the install task. Ant Task framework calls this method to
184: * excute the task.
185: * @throws BuildException if error or exception occurs.
186: */
187: public void executeTask() throws BuildException {
188: this .logDebug("Executing Set Configuration Task....");
189: String compName = getComponentName();
190:
191: executeSetComponentLoggers(compName);
192: }
193:
194: /**
195: * returns i18n key. tasks implement this method.
196: * @return i18n key for the success status
197: */
198: protected String getTaskFailedStatusI18NKey() {
199: return LOGGER_FAILED_STATUS_KEY;
200: }
201:
202: /**
203: * returns i18n key. tasks implement this method.
204: * @return i18n key for the failed status
205: */
206: protected String getTaskSuccessStatusI18NKey() {
207: return LOGGER_SUCCESS_STATUS_KEY;
208: }
209:
210: /**
211: * return i18n key for the partial success
212: * @return i18n key for the partial success
213: */
214: protected String getTaskPartialSuccessStatusI18NKey() {
215: return LOGGER_PARTIAL_SUCCESS_STATUS_KEY;
216: }
217:
218: /**
219: * returns nested element list of <logger>
220: * @return Paramter List
221: */
222: protected List getLoggerList() {
223: if (this .mLoggerList == null) {
224: this .mLoggerList = new ArrayList();
225: }
226: return this .mLoggerList;
227: }
228:
229: /**
230: * factory method for creating the nested element <logger>
231: * @return Param Object
232: */
233: public Logger createLogger() {
234: Logger logger = new Logger();
235: this.getLoggerList().add(logger);
236: return logger;
237: }
238: }
|