001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.admin.cli.commands.logging;
006:
007: import com.sun.enterprise.cli.framework.CommandException;
008: import com.sun.enterprise.cli.framework.CommandValidationException;
009: import com.sun.portal.admin.cli.commands.AdminBaseCommand;
010: import com.sun.portal.log.common.LoggersList;
011:
012: import javax.management.MBeanException;
013: import java.util.LinkedList;
014:
015: public abstract class LoggingBaseCommand extends AdminBaseCommand {
016:
017: protected static final String LINE_SEPARATOR = System
018: .getProperty("line.separator");
019:
020: protected static final String LOGGING_PREFIX = "logging.";
021:
022: protected static final String SUCCESS = "success";
023: protected static final String ERROR = "error";
024: protected static final String WARNING = "warning";
025:
026: // Options common to logging
027: protected static final String OPT_NAME = "name";
028: protected static final String OPT_LEVEL = "level";
029: protected static final String OPT_COMPONENT = "component";
030:
031: // Options for "component"
032: // Options like portal, instance, searchserver is defined in AdminCLIConstants
033: private static final String OPT_SRA_INSTANCE = "sra-instance";
034:
035: // Error suffixes
036: protected static final String INSTANCE_NOT_FOUND_EXCEPTION = "instance.not.found.exception";
037: protected static final String INVALID_COMPONENT = "invalid.component";
038: protected static final String INVALID_LEVEL = "invalid.level";
039: protected static final String INVALID_LOGGER = "invalid.logger";
040: protected static final String REQUIRED_PORTAL_INSTANCE = "required.portal.instance";
041: protected static final String INVALID_PORTAL_INSTANCE = "invalid.portal.instance";
042: protected static final String REQUIRED_SEARCH_SERVER = "required.search.server";
043: protected static final String INVALID_SEARCH_SERVER = "invalid.search.server";
044: protected static final String REQUIRED_INSTANCE_NAME = "required.instance.name";
045: protected static final String INVALID_INSTANCE_NAME = "invalid.instance.name";
046:
047: // Object Name registered in Portal MBean server
048: protected static final String PORTAL_LOG_OBJECT_NAME = "PortalDomain.PortalLogConfigurator";
049:
050: protected LinkedList getPath() {
051: LinkedList path = new LinkedList();
052: path.addFirst(getDomainId());
053: path.addFirst("portalLogConfigurator");
054: return path;
055: }
056:
057: protected String getComponent() {
058: return getOption(OPT_COMPONENT);
059: }
060:
061: protected String getComponentId() {
062: // componentId is valid only for portal and search
063: String component = getComponent();
064: if (component.equals(LoggersList.COMPONENT_PORTAL_INSTANCE)) {
065: return getPortalId();
066:
067: } else if (component
068: .equals(LoggersList.COMPONENT_SEARCH_INSTANCE)) {
069: return getSearchServerId();
070:
071: } else {
072: return component;
073: }
074: }
075:
076: protected String getInstance() {
077: // instance is valid only for portal and sra
078: String component = getComponent();
079: if (component.equals(LoggersList.COMPONENT_PORTAL_INSTANCE)) {
080: return getInstanceId();
081:
082: } else if (component.equals(LoggersList.COMPONENT_GW_INSTANCE)
083: || component.equals(LoggersList.COMPONENT_NLP_INSTANCE)
084: || component.equals(LoggersList.COMPONENT_RWP_INSTANCE)) {
085: return getSRAInstance();
086:
087: } else {
088: return component;
089: }
090: }
091:
092: protected String getSRAInstance() {
093: return getOption(OPT_SRA_INSTANCE);
094: }
095:
096: protected String getLoggerName() {
097: return getOption(OPT_NAME);
098: }
099:
100: protected String getLevel() {
101: return getOption(OPT_LEVEL);
102: }
103:
104: protected boolean isVerbose() {
105: return getBooleanOption(OPT_VERBOSE);
106: }
107:
108: public boolean validateOptions() throws CommandValidationException {
109: if (super .validateOptions()) {
110: String component = getComponent();
111: if (component.equals(LoggersList.COMPONENT_PORTAL_INSTANCE)) {
112:
113: checkPortalInstance(component, true);
114: checkSearchServer(component, false);
115: checkSRAInstance(component, false);
116:
117: } else if (component
118: .equals(LoggersList.COMPONENT_SEARCH_INSTANCE)) {
119:
120: checkSearchServer(component, true);
121: checkPortalInstance(component, false);
122: checkSRAInstance(component, false);
123:
124: } else if (component
125: .equals(LoggersList.COMPONENT_GW_INSTANCE)
126: || component
127: .equals(LoggersList.COMPONENT_NLP_INSTANCE)
128: || component
129: .equals(LoggersList.COMPONENT_RWP_INSTANCE)) {
130:
131: checkSRAInstance(component, true);
132: checkPortalInstance(component, false);
133: checkSearchServer(component, false);
134:
135: } else if (component
136: .equals(LoggersList.COMPONENT_PAS_INSTANCE)) {
137:
138: checkPortalInstance(component, false);
139: checkSearchServer(component, false);
140: checkSRAInstance(component, false);
141: } else {
142: StringBuffer validComponentList = new StringBuffer(
143: " Valid component names are ");
144: validComponentList
145: .append(LoggersList.COMPONENT_PORTAL_INSTANCE);
146: validComponentList.append(",");
147: validComponentList
148: .append(LoggersList.COMPONENT_SEARCH_INSTANCE);
149: validComponentList.append(",");
150: validComponentList
151: .append(LoggersList.COMPONENT_PAS_INSTANCE);
152: validComponentList.append(",");
153: validComponentList
154: .append(LoggersList.COMPONENT_GW_INSTANCE);
155: validComponentList.append(",");
156: validComponentList
157: .append(LoggersList.COMPONENT_NLP_INSTANCE);
158: validComponentList.append(",");
159: validComponentList
160: .append(LoggersList.COMPONENT_RWP_INSTANCE);
161: throw new CommandValidationException(
162: getLocalizedString(LOGGING_PREFIX
163: + INVALID_COMPONENT, new Object[] {
164: component, validComponentList }));
165: }
166: }
167: return true;
168: }
169:
170: private void checkPortalInstance(String component, boolean required)
171: throws CommandValidationException {
172: String portalId = getPortalId(); // Returns "" if not present
173: String instanceId = getInstanceId(); // Returns "" if not present
174: if (portalId == null) // prudency check
175: portalId = "";
176: if (instanceId == null) // prudency check
177: instanceId = "";
178:
179: if (required) {
180: if (portalId.equals("") || instanceId.equals("")) {
181: throw new CommandValidationException(
182: getLocalizedString(LOGGING_PREFIX
183: + REQUIRED_PORTAL_INSTANCE,
184: new Object[] { component }));
185: }
186: } else {
187: if (!portalId.equals("") || !instanceId.equals("")) {
188: throw new CommandValidationException(
189: getLocalizedString(LOGGING_PREFIX
190: + INVALID_PORTAL_INSTANCE,
191: new Object[] { component }));
192: }
193: }
194: }
195:
196: private void checkSearchServer(String component, boolean required)
197: throws CommandValidationException {
198: String searchserverId = getSearchServerId(); // Returns "" if not present
199: if (searchserverId == null) // prudency check
200: searchserverId = "";
201:
202: if (required) {
203: if (searchserverId.equals("")) {
204: throw new CommandValidationException(
205: getLocalizedString(LOGGING_PREFIX
206: + REQUIRED_SEARCH_SERVER,
207: new Object[] { component }));
208: }
209: } else {
210: if (!searchserverId.equals("")) {
211: throw new CommandValidationException(
212: getLocalizedString(LOGGING_PREFIX
213: + INVALID_SEARCH_SERVER,
214: new Object[] { component }));
215: }
216: }
217: }
218:
219: private void checkSRAInstance(String component, boolean required)
220: throws CommandValidationException {
221: String instanceName = getSRAInstance(); // Returns "" if not present
222: if (instanceName == null) // prudency check
223: instanceName = "";
224:
225: if (required) {
226: if (instanceName.equals("")) {
227: throw new CommandValidationException(
228: getLocalizedString(LOGGING_PREFIX
229: + REQUIRED_INSTANCE_NAME,
230: new Object[] { component }));
231: }
232: } else {
233: if (!instanceName.equals("")) {
234: throw new CommandValidationException(
235: getLocalizedString(LOGGING_PREFIX
236: + INVALID_INSTANCE_NAME,
237: new Object[] { component }));
238: }
239: }
240: }
241:
242: protected String getCauseMessages(Exception e) {
243: StringBuffer sb = new StringBuffer(e.getLocalizedMessage());
244: Throwable t = e.getCause();
245: while (t != null) {
246: sb.append(LINE_SEPARATOR).append("\t").append(
247: t.getLocalizedMessage());
248: t = t.getCause();
249: }
250:
251: return sb.toString();
252: }
253:
254: protected void throwError(MBeanException me)
255: throws CommandException {
256: Exception e = (Exception) me.getTargetException();
257: throw new CommandException(getLocalizedString(LOGGING_PREFIX
258: + ERROR, new Object[] { getCauseMessages(e) }));
259: }
260: }
|