You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mb...@apache.org on 2007/03/04 10:28:10 UTC

svn commit: r514380 - in /myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace: ./ Closure.java TracingIterator.java TracingSupport.java

Author: mbr
Date: Sun Mar  4 01:28:09 2007
New Revision: 514380

URL: http://svn.apache.org/viewvc?view=rev&rev=514380
Log:
add support classes for tracing

Added:
    myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/
    myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/Closure.java   (with props)
    myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingIterator.java   (with props)
    myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingSupport.java   (with props)

Added: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/Closure.java
URL: http://svn.apache.org/viewvc/myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/Closure.java?view=auto&rev=514380
==============================================================================
--- myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/Closure.java (added)
+++ myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/Closure.java Sun Mar  4 01:28:09 2007
@@ -0,0 +1,24 @@
+/*
+ * 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.myfaces.shared.trace;
+
+public interface Closure<T>
+{
+    T call();
+}
\ No newline at end of file

Propchange: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/Closure.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/Closure.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingIterator.java
URL: http://svn.apache.org/viewvc/myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingIterator.java?view=auto&rev=514380
==============================================================================
--- myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingIterator.java (added)
+++ myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingIterator.java Sun Mar  4 01:28:09 2007
@@ -0,0 +1,143 @@
+/*
+ * 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.myfaces.shared.trace;
+
+import java.util.Iterator;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Trace method calls to an iterator
+ * 
+ * @author Mathias Broekelmann (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class TracingIterator<E> implements Iterator<E>
+{
+    private final Iterator<E> _iterator;
+
+    private final TracingSupport _tracer;
+
+    public TracingIterator(Iterator<E> iterator)
+    {
+        _iterator = iterator;
+        _tracer = new TracingSupport(iterator.getClass());
+    }
+
+    public static <E> TracingIterator<E> create(Iterator<E> iterator)
+    {
+        return new TracingIterator<E>(iterator);
+    }
+
+    /**
+     * @return the iterator
+     */
+    public Iterator<E> getIterator()
+    {
+        return _iterator;
+    }
+
+    public boolean hasNext()
+    {
+        return _tracer.trace("hasNext", new Closure<Boolean>()
+        {
+            public Boolean call()
+            {
+                return _iterator.hasNext();
+            }
+        });
+    }
+
+    public E next()
+    {
+        return _tracer.trace("next", new Closure<E>()
+        {
+            public E call()
+            {
+                return _iterator.next();
+            }
+        });
+    }
+
+    public void remove()
+    {
+        _tracer.trace("remove", new Closure<Object>()
+        {
+            public Object call()
+            {
+                _iterator.remove();
+                return Void.class;
+            }
+        });
+    }
+
+    /**
+     * @return
+     * @see org.apache.myfaces.shared.trace.TracingSupport#getLevel()
+     */
+    public Level getLevel()
+    {
+        return _tracer.getLevel();
+    }
+
+    /**
+     * @return
+     * @see org.apache.myfaces.shared.trace.TracingSupport#getLogger()
+     */
+    public Logger getLogger()
+    {
+        return _tracer.getLogger();
+    }
+
+    /**
+     * @return
+     * @see org.apache.myfaces.shared.trace.TracingSupport#getSourceClass()
+     */
+    public String getSourceClass()
+    {
+        return _tracer.getSourceClass();
+    }
+
+    /**
+     * @param level
+     * @see org.apache.myfaces.shared.trace.TracingSupport#setLevel(java.util.logging.Level)
+     */
+    public void setLevel(Level level)
+    {
+        _tracer.setLevel(level);
+    }
+
+    /**
+     * @param logger
+     * @see org.apache.myfaces.shared.trace.TracingSupport#setLogger(java.util.logging.Logger)
+     */
+    public void setLogger(Logger logger)
+    {
+        _tracer.setLogger(logger);
+    }
+
+    /**
+     * @param className
+     * @see org.apache.myfaces.shared.trace.TracingSupport#setSourceClass(java.lang.String)
+     */
+    public void setSourceClass(String className)
+    {
+        _tracer.setSourceClass(className);
+    }
+}

Propchange: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingIterator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingSupport.java
URL: http://svn.apache.org/viewvc/myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingSupport.java?view=auto&rev=514380
==============================================================================
--- myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingSupport.java (added)
+++ myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingSupport.java Sun Mar  4 01:28:09 2007
@@ -0,0 +1,174 @@
+/*
+ * 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.myfaces.shared.trace;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Support class for tracing method calls providing params, exceptions and return types
+ * 
+ * @author Mathias Broekelmann (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class TracingSupport
+{
+    private static final Object[] EMPTY_PARAMS = new Object[0];
+
+    private Logger _logger;
+    private Level _level = Level.FINE;
+    private String _sourceClass;
+
+    public TracingSupport()
+    {
+        this(Logger.getAnonymousLogger());
+    }
+
+    public TracingSupport(Class clazz)
+    {
+        this(clazz.getName());
+    }
+
+    public TracingSupport(String className)
+    {
+        this(Logger.getLogger(className));
+    }
+
+    public TracingSupport(Logger logger)
+    {
+        _logger = logger;
+        _sourceClass = logger.getName();
+    }
+
+    /**
+     * @return the log level
+     */
+    public Level getLevel()
+    {
+        return _level;
+    }
+
+    /**
+     * 
+     * @param level
+     *            the log level to use
+     */
+    public void setLevel(Level level)
+    {
+        if (level == null)
+        {
+            throw new IllegalArgumentException("log level can not be null");
+        }
+        _level = level;
+    }
+
+    public <T> T trace(String methodName, Closure<T> closure)
+    {
+        return trace(methodName, closure, EMPTY_PARAMS);
+    }
+
+    public <T> T trace(String methodName, Closure<T> closure, Object... params)
+    {
+        if (_logger.isLoggable(_level))
+        {
+            _logger.logp(_level, _sourceClass, methodName, "ENTRY" + prepareParams(params), params);
+            try
+            {
+                T result = closure.call();
+                if (!Void.class.equals(result))
+                {
+                    _logger.logp(_level, _sourceClass, methodName, "RETURN {0}", result);
+                }
+                else
+                {
+                    _logger.logp(_level, _sourceClass, methodName, "RETURN");
+                }
+                return result;
+            }
+            catch (RuntimeException e)
+            {
+                _logger.logp(_level, _sourceClass, methodName, "THROW", e);
+                throw e;
+            }
+        }
+        else
+        {
+            return closure.call();
+        }
+    }
+
+    private String prepareParams(Object[] params)
+    {
+        if (params == null || params.length == 0)
+        {
+            return "";
+        }
+        StringBuilder builder = new StringBuilder(" ");
+        for (int i = 0, size = params.length; i < size; i++)
+        {
+            builder.append("{");
+            builder.append(i);
+            builder.append("}");
+            if (i + 1 < size)
+            {
+                builder.append(",");
+            }
+        }
+        return builder.toString();
+    }
+
+    public Logger getLogger()
+    {
+        return _logger;
+    }
+
+    /**
+     * @param logger
+     *            the logger to set
+     */
+    public void setLogger(Logger logger)
+    {
+        if (logger == null)
+        {
+            throw new IllegalArgumentException("logger must not be null");
+        }
+        _logger = logger;
+    }
+
+    /**
+     * @return the className
+     */
+    public String getSourceClass()
+    {
+        return _sourceClass;
+    }
+
+    /**
+     * @param className
+     *            the className to set
+     */
+    public void setSourceClass(String className)
+    {
+        if (className == null)
+        {
+            throw new IllegalArgumentException("className must not be null");
+        }
+        _sourceClass = className;
+    }
+}

Propchange: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/trace/TracingSupport.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL