]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/internal/osgi/CmsOsgiLogger.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / internal / osgi / CmsOsgiLogger.java
1 package org.argeo.cms.internal.osgi;
2
3 import java.security.SignatureException;
4 import java.util.Enumeration;
5
6 import org.argeo.api.cms.CmsConstants;
7 import org.argeo.api.cms.CmsLog;
8 import org.argeo.cms.runtime.DirectoryConf;
9 import org.osgi.framework.Bundle;
10 import org.osgi.framework.Constants;
11 import org.osgi.framework.ServiceReference;
12 import org.osgi.service.cm.ConfigurationAdmin;
13 import org.osgi.service.log.LogEntry;
14 import org.osgi.service.log.LogLevel;
15 import org.osgi.service.log.LogListener;
16 import org.osgi.service.log.LogReaderService;
17
18 /** Logs OSGi events. */
19 public class CmsOsgiLogger implements LogListener {
20 private final static String WHITEBOARD_PATTERN_PROP = "osgi.http.whiteboard.servlet.pattern";
21 private final static String CONTEXT_NAME_PROP = "contextName";
22
23 private LogReaderService logReaderService;
24
25 public void start() {
26 if (logReaderService != null) {
27 Enumeration<LogEntry> logEntries = logReaderService.getLog();
28 while (logEntries.hasMoreElements())
29 logged(logEntries.nextElement());
30 logReaderService.addLogListener(this);
31 }
32 }
33
34 public void stop() throws Exception {
35 logReaderService.removeLogListener(this);
36 }
37
38 public String toString() {
39 return "Node Logger";
40 }
41
42 //
43 // OSGi LOGGER
44 //
45 @Override
46 public void logged(LogEntry status) {
47 String loggerName = status.getBundle().getSymbolicName();
48 if (loggerName == null)
49 loggerName = "org.argeo.ext.osgi";
50 CmsLog pluginLog = CmsLog.getLog(loggerName);
51 LogLevel severity = status.getLogLevel();
52 if (severity.equals(LogLevel.ERROR) && pluginLog.isErrorEnabled()) {
53 // FIXME Fix Argeo TP
54 if (status.getException() instanceof SignatureException)
55 return;
56 pluginLog.error(msg(status), status.getException());
57 } else if (severity.equals(LogLevel.WARN) && pluginLog.isWarnEnabled()) {
58 if ("org.apache.felix.scr".equals(status.getBundle().getSymbolicName())
59 && (status.getException() != null && status.getException() instanceof InterruptedException)) {
60 // do not print stacktraces by Felix SCR shutdown
61 pluginLog.warn(msg(status));
62 } else {
63 pluginLog.warn(msg(status), status.getException());
64 }
65 } else if (severity.equals(LogLevel.INFO) && pluginLog.isDebugEnabled())
66 pluginLog.debug(msg(status), status.getException());
67 else if (severity.equals(LogLevel.DEBUG) && pluginLog.isTraceEnabled())
68 pluginLog.trace(msg(status), status.getException());
69 else if (severity.equals(LogLevel.TRACE) && pluginLog.isTraceEnabled())
70 pluginLog.trace(msg(status), status.getException());
71 }
72
73 private String msg(LogEntry status) {
74 StringBuilder sb = new StringBuilder();
75 sb.append(status.getMessage());
76 Bundle bundle = status.getBundle();
77 if (bundle != null) {
78 sb.append(" '" + bundle.getSymbolicName() + "'");
79 }
80 ServiceReference<?> sr = status.getServiceReference();
81 if (sr != null) {
82 sb.append(' ');
83 String[] objectClasses = (String[]) sr.getProperty(Constants.OBJECTCLASS);
84 if (isSpringApplicationContext(objectClasses)) {
85 sb.append("{org.springframework.context.ApplicationContext}");
86 Object symbolicName = sr.getProperty(Constants.BUNDLE_SYMBOLICNAME);
87 if (symbolicName != null)
88 sb.append(" " + Constants.BUNDLE_SYMBOLICNAME + ": " + symbolicName);
89 } else {
90 sb.append(arrayToString(objectClasses));
91 }
92 Object cn = sr.getProperty(CmsConstants.CN);
93 if (cn != null)
94 sb.append(" " + CmsConstants.CN + ": " + cn);
95 Object factoryPid = sr.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID);
96 if (factoryPid != null)
97 sb.append(" " + ConfigurationAdmin.SERVICE_FACTORYPID + ": " + factoryPid);
98 // else {
99 // Object servicePid = sr.getProperty(Constants.SERVICE_PID);
100 // if (servicePid != null)
101 // sb.append(" " + Constants.SERVICE_PID + ": " + servicePid);
102 // }
103 // servlets
104 Object whiteBoardPattern = sr.getProperty(WHITEBOARD_PATTERN_PROP);
105 if (whiteBoardPattern != null) {
106 if (whiteBoardPattern instanceof String) {
107 sb.append(" " + WHITEBOARD_PATTERN_PROP + ": " + whiteBoardPattern);
108 } else {
109 sb.append(" " + WHITEBOARD_PATTERN_PROP + ": " + arrayToString((String[]) whiteBoardPattern));
110 }
111 }
112 // RWT
113 Object contextName = sr.getProperty(CONTEXT_NAME_PROP);
114 if (contextName != null)
115 sb.append(" " + CONTEXT_NAME_PROP + ": " + contextName);
116
117 // user directories
118 Object baseDn = sr.getProperty(DirectoryConf.baseDn.name());
119 if (baseDn != null)
120 sb.append(" " + DirectoryConf.baseDn.name() + ": " + baseDn);
121
122 }
123 return sb.toString();
124 }
125
126 private String arrayToString(Object[] arr) {
127 StringBuilder sb = new StringBuilder();
128 sb.append('[');
129 for (int i = 0; i < arr.length; i++) {
130 if (i != 0)
131 sb.append(',');
132 sb.append(arr[i]);
133 }
134 sb.append(']');
135 return sb.toString();
136 }
137
138 private boolean isSpringApplicationContext(String[] objectClasses) {
139 for (String clss : objectClasses) {
140 if (clss.equals("org.eclipse.gemini.blueprint.context.DelegatedExecutionOsgiBundleApplicationContext")) {
141 return true;
142 }
143 }
144 return false;
145 }
146
147 public void setLogReaderService(LogReaderService logReaderService) {
148 this.logReaderService = logReaderService;
149 }
150
151 }