Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-11-29 16:37:12 -0500
committerpelder2007-11-29 16:37:12 -0500
commit8d55f06a9489ca0ce2e88ed0a83554e1ed996215 (patch)
tree2ad6e18935a3ce20218c340ca0edb1fa4a29ad24
parenta4c9493d7975f62c685030e045647914f641f57b (diff)
downloadorg.eclipse.jet-8d55f06a9489ca0ce2e88ed0a83554e1ed996215.tar.gz
org.eclipse.jet-8d55f06a9489ca0ce2e88ed0a83554e1ed996215.tar.xz
org.eclipse.jet-8d55f06a9489ca0ce2e88ed0a83554e1ed996215.zip
[211510,211513] Performance: Logging of runtime messages has hotspots
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/RuntimeLoggerContextExtender.java190
1 files changed, 117 insertions, 73 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/RuntimeLoggerContextExtender.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/RuntimeLoggerContextExtender.java
index edbb425..c0fd4c8 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/RuntimeLoggerContextExtender.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/RuntimeLoggerContextExtender.java
@@ -20,45 +20,95 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
-import org.eclipse.jet.AbstractContextExtender;
import org.eclipse.jet.ContextLogEntry;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.taglib.TagInfo;
-import org.eclipse.jet.transform.TransformContextExtender;
/**
* A JET2Context extender that may be installed for logging runtime tag activity.
*/
-public class RuntimeLoggerContextExtender extends AbstractContextExtender
+public final class RuntimeLoggerContextExtender
{
+ private static String PRIVATE_CONTEXT_DATA_KEY = RuntimeLoggerContextExtender.class.getName();
+
+ private static interface LogState {
+ void log(String message, TagInfo td, String templatePath, int level);
+ }
+
+ private static final LogState NO_LISTENERS = new LogState() {
+
+ public void log(String message, TagInfo td, String templatePath, int level)
+ {
+ }
+
+ };
+
+ private static final class OneListenerState implements LogState {
+
+ private final RuntimeTagLogger runtimeTagLogger;
+
+ public OneListenerState(RuntimeTagLogger runtimeTagLogger) {
+ this.runtimeTagLogger = runtimeTagLogger;
+
+ }
+
+ public void log(String message, TagInfo td, String templatePath, int level)
+ {
+ runtimeTagLogger.log(message, td, templatePath, level);
+ }
+ }
+
+ private static final class MultipleListenerState implements LogState {
+
+ private final RuntimeTagLogger[] listeners;
+ private int listenersLength;
+ public MultipleListenerState(List listeners) {
+ this.listeners = (RuntimeTagLogger[])listeners.toArray(new RuntimeTagLogger[listeners.size()]);
+ this.listenersLength = this.listeners.length;
+ }
+
+ public void log(String message, TagInfo td, String templatePath, int level)
+ {
+ for (int i = 0; i < listenersLength; i++)
+ {
+ listeners[i].log(message, td, templatePath, level);
+ }
+ }
+
+ }
public static final int ERROR_LEVEL = 5;
+
public static final int WARNING_LEVEL = 4;
+
public static final int INFO_LEVEL = 3;
+
public static final int TRACE_LEVEL = 2;
+
public static final int DEBUG_LEVEL = 1;
-
- private static final class RuntimeLogger implements JET2Context.LogListener
+
+ private final ContextData contextData;
+
+ private static final class ContextData implements JET2Context.LogListener
{
private final List listeners = new ArrayList();
+ private LogState state = NO_LISTENERS;
public void log(String message, TagInfo td, String templatePath, int level)
{
- for (Iterator i = listeners.iterator(); i.hasNext();)
- {
- RuntimeTagLogger logger = (RuntimeTagLogger)i.next();
- logger.log(message, td, templatePath, level);
- }
+ state.log(message, td, templatePath, level);
}
+
public void log(ContextLogEntry entry)
{
- log(entry.getMessage(), entry.getTagInfo(), entry.getTemplatePath(), getLevel(entry.getSeverity()));
+ state.log(entry.getMessage(), entry.getTagInfo(), entry.getTemplatePath(), getLevel(entry.getSeverity()));
}
+
private int getLevel(int severity)
{
- switch(severity)
+ switch (severity)
{
case ContextLogEntry.ERROR:
case ContextLogEntry.CANCEL:
@@ -70,92 +120,86 @@ public class RuntimeLoggerContextExtender extends AbstractContextExtender
return RuntimeLoggerContextExtender.INFO_LEVEL;
default:
return RuntimeLoggerContextExtender.ERROR_LEVEL;
-
+
+ }
+
+ }
+
+ public void addListener(RuntimeTagLogger tagLogger)
+ {
+ listeners.add(tagLogger);
+ updateState();
+ }
+
+ public void removeListener(RuntimeTagLogger tagLogger)
+ {
+ listeners.remove(tagLogger);
+ updateState();
+ }
+
+
+
+ /**
+ *
+ */
+ public void updateState()
+ {
+ switch(listeners.size()) {
+ case 0:
+ state = NO_LISTENERS;
+ break;
+ case 1:
+ state = new OneListenerState((RuntimeTagLogger)listeners.get(0));
+ break;
+ default:
+ state = new MultipleListenerState(listeners);
+ break;
}
}
}
/**
- * Get the runtime logger instance. This method
- * will not install this context extender. Use {@link #install(JET2Context)}
- * to install the context extender
+ * Get the runtime contextData instance.
* @param context the context
* @return the installed context extender or <code>null</code>
*/
- public static RuntimeLoggerContextExtender getInstance(JET2Context context) {
- if(context.hasContextExtender(RuntimeLoggerContextExtender.class)) {
- return new RuntimeLoggerContextExtender(context);
- } else {
- return null;
- }
- }
-
- /**
- *
- * @param context
- */
- public static void install(JET2Context context) {
- new RuntimeLoggerContextExtender(context);
- }
-
- private RuntimeLoggerContextExtender(JET2Context context)
+ public static RuntimeLoggerContextExtender getInstance(JET2Context context)
{
- super(context);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.AbstractContextExtender#createExtendedData(org.eclipse.jet.JET2Context)
- */
- protected Object createExtendedData(JET2Context context)
- {
- final RuntimeLogger runtimeLogger = new RuntimeLogger();
- context.addLogListener(runtimeLogger);
- return runtimeLogger;
+ if(context == null) {
+ throw new NullPointerException();
+ }
+ RuntimeLoggerContextExtender ex = (RuntimeLoggerContextExtender)context.getPrivateData(PRIVATE_CONTEXT_DATA_KEY);
+ if (ex == null)
+ {
+ ex = new RuntimeLoggerContextExtender(context, new ContextData());
+ context.addPrivateData(PRIVATE_CONTEXT_DATA_KEY, ex);
+ }
+ return ex;
}
- private RuntimeLogger getRuntimeLogger()
+ private RuntimeLoggerContextExtender(JET2Context context, ContextData contextData)
{
- return (RuntimeLogger)getExtendedData();
+ context.addLogListener(contextData);
+ this.contextData = contextData;
}
- public void addListener(RuntimeTagLogger logger)
+ public void addListener(RuntimeTagLogger tagLogger)
{
- getRuntimeLogger().listeners.add(logger);
+ contextData.addListener(tagLogger);
}
- public void removeListener(RuntimeTagLogger logger)
+ public void removeListener(RuntimeTagLogger tagLogger)
{
- getRuntimeLogger().listeners.remove(logger);
+ contextData.removeListener(tagLogger);
}
public static void log(JET2Context context, String message, TagInfo td, int level)
{
- if (context.hasContextExtender(RuntimeLoggerContextExtender.class))
- {
- RuntimeLoggerContextExtender rl = getInstance(context);
- String templatePath = null;
- if(context.hasContextExtender(TransformContextExtender.class)) {
- TransformContextExtender tce = new TransformContextExtender(context);
- templatePath = tce.getTemplatePath();
- }
- rl.log(message, td, templatePath, level);
- }
- }
-
- public void log(String message, TagInfo td, String templatePath, int level)
- {
- for (Iterator i = getRuntimeLogger().listeners.iterator(); i.hasNext();)
- {
- RuntimeTagLogger logger = (RuntimeTagLogger)i.next();
- logger.log(message, td, templatePath, level);
- }
+ getInstance(context).contextData.log(message, td, context.getTemplatePath(), level);
}
public static void log(JET2Context context, String message, TagInfo tagInfo, String templatePath, int level)
{
- if (context.hasContextExtender(RuntimeLoggerContextExtender.class))
- {
- getInstance(context).getRuntimeLogger().log(message, tagInfo, templatePath, level);
- }
+ getInstance(context).contextData.log(message, tagInfo, templatePath, level);
}
}

Back to the top