You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jb...@apache.org on 2007/02/11 21:12:38 UTC
svn commit: r506112 - in
/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor:
JavaLoggingMonitorFactory.java ProxyMonitorFactory.java
Author: jboynes
Date: Sun Feb 11 12:12:37 2007
New Revision: 506112
URL: http://svn.apache.org/viewvc?view=rev&rev=506112
Log:
refactor JavaLoggingMonitorFactory to extract a baseclass for proxy based MonitorFactory implementations
Added:
incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/ProxyMonitorFactory.java (with props)
Modified:
incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/JavaLoggingMonitorFactory.java
Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/JavaLoggingMonitorFactory.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/JavaLoggingMonitorFactory.java?view=diff&rev=506112&r1=506111&r2=506112
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/JavaLoggingMonitorFactory.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/JavaLoggingMonitorFactory.java Sun Feb 11 12:12:37 2007
@@ -20,26 +20,18 @@
import java.io.PrintWriter;
import java.io.StringWriter;
-import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
import java.util.Map;
-import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
-import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.osoa.sca.annotations.Service;
-import org.apache.tuscany.api.annotation.LogLevel;
import org.apache.tuscany.host.MonitorFactory;
import org.apache.tuscany.host.monitor.ExceptionFormatter;
import org.apache.tuscany.host.monitor.FormatterRegistry;
@@ -51,13 +43,7 @@
* @see java.util.logging
*/
@Service(interfaces = {MonitorFactory.class, FormatterRegistry.class})
-public class JavaLoggingMonitorFactory implements MonitorFactory, FormatterRegistry {
- private String bundleName;
- private Level defaultLevel;
- private Map<String, Level> levels;
- private List<ExceptionFormatter> formatters = new ArrayList<ExceptionFormatter>();
- private ExceptionFormatter defaultFormatter = new DefaultExceptionFormatter();
- private Map<Class<?>, WeakReference<?>> proxies = new WeakHashMap<Class<?>, WeakReference<?>>();
+public class JavaLoggingMonitorFactory extends ProxyMonitorFactory {
/**
* Construct a MonitorFactory that will monitor the specified methods at the specified levels and generate messages
@@ -86,134 +72,25 @@
public JavaLoggingMonitorFactory() {
}
- public void initialize(Map<String, Object> configProperties) {
- if (configProperties == null) {
- return;
- }
- initInternal(configProperties);
- }
-
- private void initInternal(Map<String, Object> configProperties) {
- try {
- this.defaultLevel = (Level) configProperties.get("defaultLevel");
- this.bundleName = (String) configProperties.get("bundleName");
- Properties levels = (Properties) configProperties.get("levels");
-
- this.levels = new HashMap<String, Level>();
- if (levels != null) {
- for (Map.Entry<Object, Object> entry : levels.entrySet()) {
- String method = (String) entry.getKey();
- String level = (String) entry.getValue();
- try {
- this.levels.put(method, Level.parse(level));
- } catch (IllegalArgumentException e) {
- throw new InvalidLevelException(method, level);
- }
- }
- }
- } catch (ClassCastException cce) {
- throw new IllegalArgumentException(cce.getLocalizedMessage());
- }
- }
-
- public synchronized <T> T getMonitor(Class<T> monitorInterface) {
- T proxy = getCachedMonitor(monitorInterface);
- if (proxy == null) {
- proxy = createMonitor(monitorInterface, bundleName);
- proxies.put(monitorInterface, new WeakReference<T>(proxy));
- }
- return proxy;
- }
-
- private <T> T getCachedMonitor(Class<T> monitorInterface) {
- WeakReference<?> ref = proxies.get(monitorInterface);
- return (ref != null) ? monitorInterface.cast(ref.get()) : null;
- }
-
- private <T> T createMonitor(Class<T> monitorInterface, String bundleName) {
- String className = monitorInterface.getName();
- Logger logger = Logger.getLogger(className);
- Method[] methods = monitorInterface.getMethods();
- Map<String, Level> levels = new HashMap<String, Level>(methods.length);
- for (Method method : methods) {
- String key = className + '#' + method.getName();
- Level level = null;
- if (this.levels != null) {
- this.levels.get(key);
- }
- // if not specified the in config properties, look for an annotation on the method
- if (level == null) {
- LogLevel annotation = method.getAnnotation(LogLevel.class);
- if (annotation != null && annotation.value() != null) {
- try {
- level = Level.parse(annotation.value());
- } catch (IllegalArgumentException e) {
- // bad value, just use the default
- level = defaultLevel;
- }
- }
- }
- if (level == null) {
- level = defaultLevel;
- }
- levels.put(method.getName(), level);
- }
-
+ protected <T> InvocationHandler createInvocationHandler(Class<T> monitorInterface,
+ Map<String, Level> levels) {
ResourceBundle bundle = locateBundle(monitorInterface, bundleName);
-
- InvocationHandler handler = new LoggingHandler(logger, levels, bundle, formatters, defaultFormatter);
- return monitorInterface
- .cast(Proxy.newProxyInstance(monitorInterface.getClassLoader(), new Class<?>[]{monitorInterface}, handler));
- }
-
- private static <T> ResourceBundle locateBundle(Class<T> monitorInterface, String bundleName) {
- Locale locale = Locale.getDefault();
- ClassLoader cl = monitorInterface.getClassLoader();
- String packageName = monitorInterface.getPackage().getName();
- while (true) {
- try {
- return ResourceBundle.getBundle(packageName + '.' + bundleName, locale, cl);
- } catch (MissingResourceException e) {
- //ok
- }
- int index = packageName.lastIndexOf('.');
- if (index == -1) {
- break;
- }
- packageName = packageName.substring(0, index);
- }
- try {
- return ResourceBundle.getBundle(bundleName, locale, cl);
- } catch (Exception e) {
- return null;
- }
- }
-
- public void register(ExceptionFormatter formatter) {
- formatters.add(formatter);
- }
-
- public void unregister(ExceptionFormatter formatter) {
- formatters.remove(formatter);
+ Logger logger = Logger.getLogger(monitorInterface.getName());
+ return new LoggingHandler(logger, levels, bundle);
}
- private static final class LoggingHandler implements InvocationHandler {
+ private class LoggingHandler implements InvocationHandler {
private final Logger logger;
private final Map<String, Level> methodLevels;
private final ResourceBundle bundle;
- private List<ExceptionFormatter> formatters;
- private ExceptionFormatter defaultFormatter;
public LoggingHandler(Logger logger,
Map<String, Level> methodLevels,
- ResourceBundle bundle,
- List<ExceptionFormatter> formatters,
- ExceptionFormatter defaultFormatter) {
+ ResourceBundle bundle
+ ) {
this.logger = logger;
this.methodLevels = methodLevels;
this.bundle = bundle;
- this.formatters = formatters;
- this.defaultFormatter = defaultFormatter;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
@@ -232,24 +109,7 @@
if (args != null) {
for (Object o : args) {
if (o instanceof Throwable) {
- Throwable e = (Throwable) o;
- ExceptionFormatter formatter = null;
- for (ExceptionFormatter candidate : formatters) {
- if (candidate.canFormat(e.getClass())) {
- formatter = candidate;
- break;
- }
- }
- StringWriter writer = new StringWriter();
- PrintWriter pw = new PrintWriter(writer);
- if (formatter != null) {
- formatter.write(pw, e);
- } else {
- defaultFormatter.write(pw, e);
- }
- format(pw, e);
- pw.close();
- logRecord.setMessage(writer.toString());
+ logRecord.setMessage(formatException((Throwable) o));
break;
}
}
@@ -259,63 +119,5 @@
}
return null;
}
-
- private void format(PrintWriter writer, Throwable throwable) {
- writer.println(throwable.getClass().getName());
- StackTraceElement[] trace = throwable.getStackTrace();
- for (StackTraceElement aTrace : trace) {
- writer.println("\tat " + aTrace);
- }
- Throwable ourCause = throwable.getCause();
-
- if (ourCause != null) {
- printStackTraceAsCause(writer, ourCause, trace);
- }
- }
-
- private void printStackTraceAsCause(PrintWriter pw,
- Throwable throwable,
- StackTraceElement[] causedTrace) {
-
- // Compute number of frames in common between this and caused
- StackTraceElement[] trace = throwable.getStackTrace();
- int m = trace.length - 1;
- int n = causedTrace.length - 1;
- while (m >= 0 && n >= 0 && trace[m].equals(causedTrace[n])) {
- m--;
- n--;
- }
- int framesInCommon = trace.length - 1 - m;
-
- pw.println("Caused by: " + throwable.getClass().getName());
-
- ExceptionFormatter formatter = null;
- for (ExceptionFormatter candidate : formatters) {
- if (candidate.canFormat(throwable.getClass())) {
- formatter = candidate;
- break;
- }
- }
- if (formatter != null) {
- formatter.write(pw, throwable);
- } else {
- defaultFormatter.write(pw, throwable);
- }
-
-
- for (int i = 0; i <= m; i++) {
- pw.println("\tat " + trace[i]);
- }
- if (framesInCommon != 0) {
- pw.println("\t... " + framesInCommon + " more");
- }
-
- // Recurse if we have a cause
- Throwable ourCause = throwable.getCause();
- if (ourCause != null) {
- printStackTraceAsCause(pw, ourCause, trace);
- }
- }
-
}
}
Added: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/ProxyMonitorFactory.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/ProxyMonitorFactory.java?view=auto&rev=506112
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/ProxyMonitorFactory.java (added)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/ProxyMonitorFactory.java Sun Feb 11 12:12:37 2007
@@ -0,0 +1,243 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.core.monitor;
+
+import java.util.Map;
+import java.util.Properties;
+import java.util.HashMap;
+import java.util.ResourceBundle;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.WeakHashMap;
+import java.util.logging.Level;
+import java.lang.ref.WeakReference;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.apache.tuscany.host.MonitorFactory;
+import org.apache.tuscany.host.monitor.FormatterRegistry;
+import org.apache.tuscany.host.monitor.ExceptionFormatter;
+import org.apache.tuscany.api.annotation.LogLevel;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public abstract class ProxyMonitorFactory implements MonitorFactory, FormatterRegistry {
+ protected String bundleName;
+ protected final List<ExceptionFormatter> formatters = new ArrayList<ExceptionFormatter>();
+ protected final ExceptionFormatter defaultFormatter = new DefaultExceptionFormatter();
+ protected Level defaultLevel;
+ protected Map<String, Level> levels;
+ private final Map<Class<?>, WeakReference<?>> proxies = new WeakHashMap<Class<?>, WeakReference<?>>();
+
+ public void initialize(Map<String, Object> configProperties) {
+ if (configProperties == null) {
+ return;
+ }
+ initInternal(configProperties);
+ }
+
+ protected void initInternal(Map<String, Object> configProperties) {
+ try {
+ this.defaultLevel = (Level) configProperties.get("defaultLevel");
+ this.bundleName = (String) configProperties.get("bundleName");
+ Properties levels = (Properties) configProperties.get("levels");
+
+ this.levels = new HashMap<String, Level>();
+ if (levels != null) {
+ for (Map.Entry<Object, Object> entry : levels.entrySet()) {
+ String method = (String) entry.getKey();
+ String level = (String) entry.getValue();
+ try {
+ this.levels.put(method, Level.parse(level));
+ } catch (IllegalArgumentException e) {
+ throw new InvalidLevelException(method, level);
+ }
+ }
+ }
+ } catch (ClassCastException cce) {
+ throw new IllegalArgumentException(cce.getLocalizedMessage());
+ }
+ }
+
+ public synchronized <T> T getMonitor(Class<T> monitorInterface) {
+ T proxy = getCachedMonitor(monitorInterface);
+ if (proxy == null) {
+ proxy = createMonitor(monitorInterface);
+ proxies.put(monitorInterface, new WeakReference<T>(proxy));
+ }
+ return proxy;
+ }
+
+ protected <T> T getCachedMonitor(Class<T> monitorInterface) {
+ WeakReference<?> ref = proxies.get(monitorInterface);
+ return (ref != null) ? monitorInterface.cast(ref.get()) : null;
+ }
+
+ protected <T> T createMonitor(Class<T> monitorInterface) {
+ String className = monitorInterface.getName();
+ Method[] methods = monitorInterface.getMethods();
+ Map<String, Level> levels = new HashMap<String, Level>(methods.length);
+ for (Method method : methods) {
+ String key = className + '#' + method.getName();
+ Level level = null;
+ if (this.levels != null) {
+ this.levels.get(key);
+ }
+ // if not specified the in config properties, look for an annotation on the method
+ if (level == null) {
+ LogLevel annotation = method.getAnnotation(LogLevel.class);
+ if (annotation != null && annotation.value() != null) {
+ try {
+ level = Level.parse(annotation.value());
+ } catch (IllegalArgumentException e) {
+ // bad value, just use the default
+ level = defaultLevel;
+ }
+ }
+ }
+ if (level == null) {
+ level = defaultLevel;
+ }
+ levels.put(method.getName(), level);
+ }
+
+ InvocationHandler handler = createInvocationHandler(monitorInterface, levels);
+ Object proxy = Proxy.newProxyInstance(monitorInterface.getClassLoader(),
+ new Class<?>[]{monitorInterface},
+ handler);
+ return monitorInterface.cast(proxy);
+ }
+
+ protected <T> ResourceBundle locateBundle(Class<T> monitorInterface, String bundleName) {
+ Locale locale = Locale.getDefault();
+ ClassLoader cl = monitorInterface.getClassLoader();
+ String packageName = monitorInterface.getPackage().getName();
+ while (true) {
+ try {
+ return ResourceBundle.getBundle(packageName + '.' + bundleName, locale, cl);
+ } catch (MissingResourceException e) {
+ //ok
+ }
+ int index = packageName.lastIndexOf('.');
+ if (index == -1) {
+ break;
+ }
+ packageName = packageName.substring(0, index);
+ }
+ try {
+ return ResourceBundle.getBundle(bundleName, locale, cl);
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ public void register(ExceptionFormatter formatter) {
+ formatters.add(formatter);
+ }
+
+ public void unregister(ExceptionFormatter formatter) {
+ formatters.remove(formatter);
+ }
+
+ protected abstract <T> InvocationHandler createInvocationHandler(Class<T> monitorInterface,
+ Map<String, Level> levels);
+
+ protected String formatException(Throwable e) {
+ ExceptionFormatter formatter = null;
+ for (ExceptionFormatter candidate : formatters) {
+ if (candidate.canFormat(e.getClass())) {
+ formatter = candidate;
+ break;
+ }
+ }
+ StringWriter writer = new StringWriter();
+ PrintWriter pw = new PrintWriter(writer);
+ if (formatter != null) {
+ formatter.write(pw, e);
+ } else {
+ defaultFormatter.write(pw, e);
+ }
+ format(pw, e);
+ pw.close();
+ return writer.toString();
+ }
+
+ protected void format(PrintWriter writer, Throwable throwable) {
+ writer.println(throwable.getClass().getName());
+ StackTraceElement[] trace = throwable.getStackTrace();
+ for (StackTraceElement aTrace : trace) {
+ writer.println("\tat " + aTrace);
+ }
+ Throwable ourCause = throwable.getCause();
+
+ if (ourCause != null) {
+ printStackTraceAsCause(writer, ourCause, trace);
+ }
+ }
+
+ protected void printStackTraceAsCause(PrintWriter pw,
+ Throwable throwable,
+ StackTraceElement[] causedTrace) {
+
+ // Compute number of frames in common between this and caused
+ StackTraceElement[] trace = throwable.getStackTrace();
+ int m = trace.length - 1;
+ int n = causedTrace.length - 1;
+ while (m >= 0 && n >= 0 && trace[m].equals(causedTrace[n])) {
+ m--;
+ n--;
+ }
+ int framesInCommon = trace.length - 1 - m;
+
+ pw.println("Caused by: " + throwable.getClass().getName());
+
+ ExceptionFormatter formatter = null;
+ for (ExceptionFormatter candidate : formatters) {
+ if (candidate.canFormat(throwable.getClass())) {
+ formatter = candidate;
+ break;
+ }
+ }
+ if (formatter != null) {
+ formatter.write(pw, throwable);
+ } else {
+ defaultFormatter.write(pw, throwable);
+ }
+
+
+ for (int i = 0; i <= m; i++) {
+ pw.println("\tat " + trace[i]);
+ }
+ if (framesInCommon != 0) {
+ pw.println("\t... " + framesInCommon + " more");
+ }
+
+ // Recurse if we have a cause
+ Throwable ourCause = throwable.getCause();
+ if (ourCause != null) {
+ printStackTraceAsCause(pw, ourCause, trace);
+ }
+ }
+}
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/ProxyMonitorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/monitor/ProxyMonitorFactory.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org