001: /*
002: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.wso2.esb.services;
018:
019: import org.apache.axis2.AxisFault;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.log4j.Appender;
023: import org.apache.log4j.Layout;
024: import org.apache.log4j.Logger;
025: import org.apache.log4j.spi.LoggingEvent;
026: import org.apache.synapse.SynapseConstants;
027: import org.wso2.esb.ServiceBusException;
028: import org.wso2.esb.services.tos.ServerData;
029: import org.wso2.esb.services.tos.ServerStatus;
030: import org.wso2.utils.MemoryAppender;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /*
036: * Returns ServerStatus for the ESB home page
037: * Detailed ServerData for the Manage->System page
038: * Tracing and Log entry views off the MemoryAppender
039: */
040:
041: public class ServerAdmin extends AbstractESBAdmin {
042:
043: private static Log log = LogFactory.getLog(ServerAdmin.class);
044:
045: public ServerStatus getStatus() throws AxisFault {
046: return new ServerStatus(getAxisConfig());
047: }
048:
049: public ServerData getServerData() throws AxisFault {
050: return new ServerData(
051: org.wso2.esb.ServiceBusConstants.ESB_INSTANCE,
052: getAxisConfig().getRepository().getPath());
053: }
054:
055: public String[] getTraceLogs() throws ServiceBusException {
056: int amount;
057: int DEFAULT_NO_OF_LOGS = 100;
058: Logger logger = Logger.getLogger(SynapseConstants.TRACE_LOGGER);
059: Appender appender = logger.getAppender("TRACE_MEMORYAPPENDER");
060: if (appender instanceof MemoryAppender) {
061: MemoryAppender memoryAppender = (MemoryAppender) appender;
062: if ((memoryAppender.getCircularQueue() != null)) {
063: amount = memoryAppender.getBufferSize();
064: } else {
065: return new String[] { "--- No trace entries found. "
066: + "You need to enable tracing on the configuration elements ---" };
067: }
068: if ((memoryAppender.getCircularQueue().getObjects(amount) == null)
069: || (memoryAppender.getCircularQueue().getObjects(
070: amount).length == 0)) {
071: return new String[] { "--- No trace entries found. "
072: + "You need to enable tracing on the configuration elements ---" };
073: }
074: Object[] objects;
075: if (amount < 1) {
076: objects = memoryAppender.getCircularQueue().getObjects(
077: DEFAULT_NO_OF_LOGS);
078: } else {
079: objects = memoryAppender.getCircularQueue().getObjects(
080: amount);
081: }
082: String[] resp = new String[objects.length];
083: Layout layout = memoryAppender.getLayout();
084: for (int i = 0; i < objects.length; i++) {
085: LoggingEvent logEvt = (LoggingEvent) objects[i];
086: if (logEvt != null) {
087: resp[i] = layout.format(logEvt);
088: }
089: }
090: return resp;
091: } else {
092: return new String[] { "The trace log must be configured to use the "
093: + "org.wso2.utils.MemoryAppender to view entries through the admin console" };
094: }
095: }
096:
097: public boolean clearTraceLogs() {
098: Logger logger = Logger.getLogger(SynapseConstants.TRACE_LOGGER);
099: Appender appender = logger.getAppender("TRACE_MEMORYAPPENDER");
100: if (appender instanceof MemoryAppender) {
101: try {
102: MemoryAppender memoryAppender = (MemoryAppender) appender;
103: if (memoryAppender.getCircularQueue() != null) {
104: memoryAppender.getCircularQueue().clear();
105: }
106: return true;
107: } catch (Exception e) {
108: return false;
109: }
110: } else {
111: return false;
112: }
113: }
114:
115: public String[] getLogs() throws AxisFault, ServiceBusException {
116: int DEFAULT_NO_OF_LOGS = 100;
117: int definedamount;
118: Appender appender = Logger.getRootLogger().getAppender(
119: "LOG_MEMORYAPPENDER");
120: if (appender instanceof MemoryAppender) {
121: MemoryAppender memoryAppender = (MemoryAppender) appender;
122: if ((memoryAppender.getCircularQueue() != null)) {
123: definedamount = memoryAppender.getBufferSize();
124: } else {
125: return new String[] { "--- No log entries found. "
126: + "You may try increasing the log level ---" };
127: }
128: if ((memoryAppender.getCircularQueue().getObjects(
129: definedamount) == null)
130: || (memoryAppender.getCircularQueue().getObjects(
131: definedamount).length == 0)) {
132: return new String[] { "--- No log entries found. "
133: + "You may try increasing the log level ---" };
134: }
135: Object[] objects;
136: if (definedamount < 1) {
137: objects = memoryAppender.getCircularQueue().getObjects(
138: DEFAULT_NO_OF_LOGS);
139: } else {
140: objects = memoryAppender.getCircularQueue().getObjects(
141: definedamount);
142: }
143: String[] resp = new String[objects.length];
144: Layout layout = memoryAppender.getLayout();
145: for (int i = 0; i < objects.length; i++) {
146: LoggingEvent logEvt = (LoggingEvent) objects[i];
147: if (logEvt != null) {
148: resp[i] = layout.format(logEvt);
149: }
150: }
151: return resp;
152: } else {
153: return new String[] { "The log must be configured to use the "
154: + "org.wso2.utils.MemoryAppender to view entries on the admin console" };
155: }
156: }
157:
158: public String[] getLogsForSingleLogLevel(String type)
159: throws AxisFault, ServiceBusException {
160: if (type == null) {
161: handleFault(log, "Type can not be null", null);
162: }
163: if ("ALL".equals(type)) {
164: return getLogs();
165: } else {
166: return getLogsForType(type);
167: }
168: }
169:
170: public String[] searchLog(String type, String keyword,
171: boolean ignoreCase) throws AxisFault, ServiceBusException {
172: if (keyword == null) {
173: handleFault(log, "Key word can not be null", null);
174: }
175: if (type == null) {
176: handleFault(log, "Type can not be null", null);
177: }
178: if ("ALL".equals(type)) {
179: if ("".equals(keyword)) {
180: return getLogs();
181: } else {
182: return getLogsForKey(keyword, ignoreCase);
183: }
184: } else {
185: String[] filerByType = getLogsForType(type);
186: List resultList = new ArrayList();
187: for (int i = 0; i < filerByType.length; i++) {
188: String result = filerByType[i];
189: if (result != null) {
190: if (!ignoreCase) {
191: if (result.indexOf(keyword) > -1) {
192: resultList.add(result);
193: }
194: } else {
195: if (keyword != null
196: && result.toLowerCase().indexOf(
197: keyword.toLowerCase()) > -1) {
198: resultList.add(result);
199: }
200: }
201: }
202: }
203: if (resultList.isEmpty()) {
204: return new String[] { "--- No log entries found for "
205: + "the keyword " + keyword
206: + " and the log level " + type + "---" };
207: }
208: return (String[]) resultList.toArray(new String[resultList
209: .size()]);
210: }
211: }
212:
213: private String[] getLogsForKey(String keyword, boolean ignoreCase) {
214: int DEFAULT_NO_OF_LOGS = 100;
215: int definedamount;
216: Appender appender = Logger.getRootLogger().getAppender(
217: "LOG_MEMORYAPPENDER");
218: if (appender instanceof MemoryAppender) {
219: MemoryAppender memoryAppender = (MemoryAppender) appender;
220: if ((memoryAppender.getCircularQueue() != null)) {
221: definedamount = memoryAppender.getBufferSize();
222: } else {
223: return new String[] { "--- No log entries found for "
224: + "the " + keyword + " ---" };
225: }
226: if ((memoryAppender.getCircularQueue().getObjects(
227: definedamount) == null)
228: || (memoryAppender.getCircularQueue().getObjects(
229: definedamount).length == 0)) {
230: return new String[] { "--- No log entries found for "
231: + "the " + keyword + " ---" };
232: }
233: Object[] objects;
234: if (definedamount < 1) {
235: objects = memoryAppender.getCircularQueue().getObjects(
236: DEFAULT_NO_OF_LOGS);
237: } else {
238: objects = memoryAppender.getCircularQueue().getObjects(
239: definedamount);
240: }
241: Layout layout = memoryAppender.getLayout();
242: List resultList = new ArrayList();
243: for (int i = 0; i < objects.length; i++) {
244: LoggingEvent logEvt = (LoggingEvent) objects[i];
245: if (logEvt != null) {
246: String result = layout.format(logEvt);
247: if (result != null) {
248: if (!ignoreCase) {
249: if (result.indexOf(keyword) > -1) {
250: resultList.add(result);
251: }
252: } else {
253: if (keyword != null
254: && result.toLowerCase().indexOf(
255: keyword.toLowerCase()) > -1) {
256: resultList.add(result);
257: }
258: }
259: }
260: }
261: }
262: if (resultList.isEmpty()) {
263: return new String[] { "--- No log entries found for "
264: + "the " + keyword + " ---" };
265: }
266: return (String[]) resultList.toArray(new String[resultList
267: .size()]);
268: } else {
269: return new String[] { "The log must be configured to use the "
270: + "org.wso2.utils.MemoryAppender to view entries on the admin console" };
271: }
272: }
273:
274: private String[] getLogsForType(String type) {
275: int DEFAULT_NO_OF_LOGS = 100;
276: int definedamount;
277: Appender appender = Logger.getRootLogger().getAppender(
278: "LOG_MEMORYAPPENDER");
279: if (appender instanceof MemoryAppender) {
280: MemoryAppender memoryAppender = (MemoryAppender) appender;
281: if ((memoryAppender.getCircularQueue() != null)) {
282: definedamount = memoryAppender.getBufferSize();
283: } else {
284: return new String[] { "--- No log entries found for "
285: + "the " + type + " ---" };
286: }
287: if ((memoryAppender.getCircularQueue().getObjects(
288: definedamount) == null)
289: || (memoryAppender.getCircularQueue().getObjects(
290: definedamount).length == 0)) {
291: return new String[] { "--- No log entries found for "
292: + "the " + type + " ---" };
293: }
294: Object[] objects;
295: if (definedamount < 1) {
296: objects = memoryAppender.getCircularQueue().getObjects(
297: DEFAULT_NO_OF_LOGS);
298: } else {
299: objects = memoryAppender.getCircularQueue().getObjects(
300: definedamount);
301: }
302: Layout layout = memoryAppender.getLayout();
303: List resultList = new ArrayList();
304: for (int i = 0; i < objects.length; i++) {
305: LoggingEvent logEvt = (LoggingEvent) objects[i];
306: if (logEvt != null) {
307: String result = layout.format(logEvt);
308: if (result != null && result.indexOf(type) > -1
309: && result.indexOf(type) < 4) {
310: resultList.add(result);
311: }
312: }
313: }
314: if (resultList.isEmpty()) {
315: return new String[] { "--- No log entries found for "
316: + "the " + type + " ---" };
317: }
318: return (String[]) resultList.toArray(new String[resultList
319: .size()]);
320: } else {
321: return new String[] { "The log must be configured to use the "
322: + "org.wso2.utils.MemoryAppender to view entries through the admin console" };
323: }
324: }
325:
326: public String[] searchTraceLog(String keyword, boolean ignoreCase)
327: throws ServiceBusException {
328: int DEFAULT_NO_OF_LOGS = 100;
329: int definedamonut;
330: if (keyword == null) {
331: throw new ServiceBusException("Key word can not be null");
332: }
333: if ("ALL".equals(keyword) || "".equals(keyword)) {
334: return getTraceLogs();
335: }
336: Logger logger = Logger.getLogger(SynapseConstants.TRACE_LOGGER);
337: Appender appender = logger.getAppender("TRACE_MEMORYAPPENDER");
338: if (appender instanceof MemoryAppender) {
339: MemoryAppender memoryAppender = (MemoryAppender) appender;
340: if ((memoryAppender.getCircularQueue() != null)) {
341: definedamonut = memoryAppender.getBufferSize();
342: } else {
343: return new String[] { "--- No trace entries found for "
344: + "the " + keyword + " ---" };
345: }
346: if ((memoryAppender.getCircularQueue().getObjects(
347: definedamonut) == null)
348: || (memoryAppender.getCircularQueue().getObjects(
349: definedamonut).length == 0)) {
350: return new String[] { "--- No trace entries found for "
351: + "the " + keyword + " ---" };
352: }
353: Object[] objects;
354: if (definedamonut < 1) {
355: objects = memoryAppender.getCircularQueue().getObjects(
356: DEFAULT_NO_OF_LOGS);
357: } else {
358: objects = memoryAppender.getCircularQueue().getObjects(
359: definedamonut);
360: }
361: Layout layout = memoryAppender.getLayout();
362: List resultList = new ArrayList();
363: for (int i = 0; i < objects.length; i++) {
364: LoggingEvent logEvt = (LoggingEvent) objects[i];
365: if (logEvt != null) {
366: String result = layout.format(logEvt);
367: if (result != null) {
368: if (!ignoreCase) {
369: if (result.indexOf(keyword) > -1) {
370: resultList.add(result);
371: }
372: } else {
373: if (result.toLowerCase().indexOf(
374: keyword.toLowerCase()) > -1) {
375: resultList.add(result);
376: }
377: }
378: }
379: }
380: }
381: if (resultList.isEmpty()) {
382: return new String[] { "--- No trace entries found for "
383: + "the " + keyword + " ---" };
384: }
385: return (String[]) resultList.toArray(new String[resultList
386: .size()]);
387: } else {
388: return new String[] { "The trace log must be configured to use the "
389: + "org.wso2.utils.MemoryAppender to view entries through the admin console" };
390: }
391:
392: }
393:
394: public boolean clearLogs() {
395: Appender appender = Logger.getRootLogger().getAppender(
396: "LOG_MEMORYAPPENDER");
397: if (appender instanceof MemoryAppender) {
398: try {
399: MemoryAppender memoryAppender = (MemoryAppender) appender;
400: if (memoryAppender.getCircularQueue() != null) {
401: memoryAppender.getCircularQueue().clear();
402: }
403: return true;
404: } catch (Exception e) {
405: return false;
406: }
407: } else {
408: return false;
409: }
410: }
411:
412: }
|