You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2009/04/02 14:50:37 UTC

svn commit: r761274 - in /commons/sandbox/monitoring/branches/modules/core/src: main/java/org/apache/commons/monitoring/repositories/ main/java/org/apache/commons/monitoring/stopwatches/ test/java/org/apache/commons/monitoring/repositories/

Author: nicolas
Date: Thu Apr  2 12:50:36 2009
New Revision: 761274

URL: http://svn.apache.org/viewvc?rev=761274&view=rev
Log:
HistoryOfMyThread

Added:
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java
    commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java
Modified:
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java

Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java?rev=761274&view=auto
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java (added)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java Thu Apr  2 12:50:36 2009
@@ -0,0 +1,67 @@
+/*
+ * 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.commons.monitoring.repositories;
+
+import java.util.Collection;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.Repository;
+import org.apache.commons.monitoring.StopWatch;
+
+
+/**
+ * @author ndeloof
+ *
+ */
+public class HOMTRepositoryDecorator
+    extends RepositoryDecorator
+    implements Repository
+{
+    private ThreadLocal<HistoryOfMyThread> history = new ThreadLocal<HistoryOfMyThread>();
+
+    private Collection<HistoryOfMyThread.Listener> listeners = new CopyOnWriteArrayList<HistoryOfMyThread.Listener>();
+
+    public void addListener( HistoryOfMyThread.Listener listener )
+    {
+        listeners.add( listener );
+    }
+
+    public void removeListener( HistoryOfMyThread.Listener listener )
+    {
+        listeners.remove( listener );
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.apache.commons.monitoring.repositories.RepositoryDecorator#start(org.apache.commons.monitoring.Monitor)
+     */
+    @Override
+    public StopWatch start( Monitor monitor )
+    {
+        StopWatch stopWatch = super.start( monitor );
+        HistoryOfMyThread myThread = history.get();
+        if ( myThread == null )
+        {
+            myThread = new HistoryOfMyThread( listeners );
+            history.set( myThread );
+        }
+        return myThread.add( stopWatch );
+    }
+}

Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java?rev=761274&view=auto
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java (added)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java Thu Apr  2 12:50:36 2009
@@ -0,0 +1,97 @@
+/*
+ * 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.commons.monitoring.repositories;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.monitoring.StopWatch;
+import org.apache.commons.monitoring.stopwatches.StopWatchDecorator;
+
+
+/**
+ * @author ndeloof
+ *
+ */
+public class HistoryOfMyThread
+{
+    private List<StopWatch> history = new LinkedList<StopWatch>();
+
+    private Collection<HistoryOfMyThread.Listener> listeners;
+
+    /**
+     * @param listeners
+     */
+    public HistoryOfMyThread( Collection<Listener> listeners )
+    {
+        super();
+        this.listeners = listeners;
+    }
+
+    protected StopWatch add( StopWatch stopWatch )
+    {
+        if ( history.size() == 0 )
+        {
+            stopWatch = new StopWatchDecorator( stopWatch )
+            {
+                public StopWatch stop()
+                {
+                    super.stop();
+                    historyEnd();
+                    return getDecorated();
+                }
+
+                public StopWatch stop( boolean canceled )
+                {
+                    super.stop( canceled );
+                    historyEnd();
+                    return getDecorated();
+                }
+
+                public StopWatch cancel()
+                {
+                    super.cancel();
+                    historyEnd();
+                    return getDecorated();
+                }
+            };
+        }
+        history.add( stopWatch );
+        return stopWatch;
+    }
+
+    private void historyEnd()
+    {
+        for ( Listener listener : listeners )
+        {
+            listener.onHistoryEnd( this );
+        }
+    }
+
+    public Iterator<StopWatch> history()
+    {
+        return history.iterator();
+    }
+
+    public interface Listener
+    {
+        void onHistoryEnd( HistoryOfMyThread history );
+    }
+}

Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java?rev=761274&view=auto
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java (added)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java Thu Apr  2 12:50:36 2009
@@ -0,0 +1,102 @@
+/*
+ * 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.commons.monitoring.repositories;
+
+import java.util.Collection;
+import java.util.Set;
+
+import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.Repository;
+import org.apache.commons.monitoring.StopWatch;
+import org.apache.commons.monitoring.Monitor.Key;
+
+/**
+ * @author ndeloof
+ *
+ */
+public abstract class RepositoryDecorator
+    implements Repository
+{
+    private Repository decorated;
+
+    public Repository decorate( Repository decorated )
+    {
+        this.decorated = decorated;
+        return this;
+    }
+
+    public void clear()
+    {
+        decorated.clear();
+    }
+
+    public Set<String> getCategories()
+    {
+        return decorated.getCategories();
+    }
+
+    public Monitor getMonitor( Key key )
+    {
+        return decorated.getMonitor( key );
+    }
+
+    public Monitor getMonitor( String name, String category, String subsystem )
+    {
+        return decorated.getMonitor( name, category, subsystem );
+    }
+
+    public Monitor getMonitor( String name, String category )
+    {
+        return decorated.getMonitor( name, category );
+    }
+
+    public Monitor getMonitor( String name )
+    {
+        return decorated.getMonitor( name );
+    }
+
+    public Collection<Monitor> getMonitors()
+    {
+        return decorated.getMonitors();
+    }
+
+    public Collection<Monitor> getMonitorsFromCategory( String category )
+    {
+        return decorated.getMonitorsFromCategory( category );
+    }
+
+    public Collection<Monitor> getMonitorsFromSubSystem( String subsystem )
+    {
+        return decorated.getMonitorsFromSubSystem( subsystem );
+    }
+
+    public Set<String> getSubSystems()
+    {
+        return decorated.getSubSystems();
+    }
+
+    public void reset()
+    {
+        decorated.reset();
+    }
+
+    public StopWatch start( Monitor monitor )
+    {
+        return decorated.start( monitor );
+    }
+}

Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java?rev=761274&r1=761273&r2=761274&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java (original)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java Thu Apr  2 12:50:36 2009
@@ -1,3 +1,20 @@
+/*
+ * 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.commons.monitoring.stopwatches;
 
 import org.apache.commons.monitoring.Monitor;

Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java?rev=761274&view=auto
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java (added)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java Thu Apr  2 12:50:36 2009
@@ -0,0 +1,90 @@
+/*
+ * 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.commons.monitoring.stopwatches;
+
+import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.StopWatch;
+
+/**
+ * @author ndeloof
+ *
+ */
+public abstract class StopWatchDecorator
+    implements StopWatch
+{
+    private StopWatch decorated;
+
+    /**
+     * @param decorated
+     */
+    public StopWatchDecorator( StopWatch decorated )
+    {
+        super();
+        this.decorated = decorated;
+    }
+
+    public StopWatch cancel()
+    {
+        return decorated.cancel();
+    }
+
+    public long getElapsedTime()
+    {
+        return decorated.getElapsedTime();
+    }
+
+    public Monitor getMonitor()
+    {
+        return decorated.getMonitor();
+    }
+
+    public boolean isPaused()
+    {
+        return decorated.isPaused();
+    }
+
+    public boolean isStoped()
+    {
+        return decorated.isStoped();
+    }
+
+    public StopWatch pause()
+    {
+        return decorated.pause();
+    }
+
+    public StopWatch resume()
+    {
+        return decorated.resume();
+    }
+
+    public StopWatch stop()
+    {
+        return decorated.stop();
+    }
+
+    public StopWatch stop( boolean canceled )
+    {
+        return decorated.stop( canceled );
+    }
+
+    public StopWatch getDecorated()
+    {
+        return decorated;
+    }
+}

Added: commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java?rev=761274&view=auto
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java (added)
+++ commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java Thu Apr  2 12:50:36 2009
@@ -0,0 +1,50 @@
+package org.apache.commons.monitoring.repositories;
+
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.monitoring.StopWatch;
+
+/**
+ * @author ndeloof
+ *
+ */
+public class HistoryOfMyThreadTest
+    extends TestCase
+    implements HistoryOfMyThread.Listener
+{
+    private HistoryOfMyThread historyOfMyThread;
+
+    public void testHistoryEnd()
+        throws Exception
+    {
+        HOMTRepositoryDecorator repository = new HOMTRepositoryDecorator();
+        repository.decorate( new DefaultRepository() );
+        repository.addListener( this );
+
+        StopWatch s1 = repository.start( repository.getMonitor( "test0" ) );
+        StopWatch s2 = repository.start( repository.getMonitor( "test1" ) );
+        s2.stop();
+        StopWatch s3 = repository.start( repository.getMonitor( "test2" ) );
+        s3.stop();
+        s1.stop();
+
+        assertNotNull( historyOfMyThread );
+        Iterator<StopWatch> history = historyOfMyThread.history();
+        assertEquals( s1, history.next() );
+        assertEquals( s2, history.next() );
+        assertEquals( s3, history.next() );
+        assertFalse( history.hasNext() );
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.apache.commons.monitoring.repositories.HistoryOfMyThread.Listener#onHistoryEnd(org.apache.commons.monitoring.repositories.HistoryOfMyThread)
+     */
+    public void onHistoryEnd( HistoryOfMyThread history )
+    {
+        historyOfMyThread = history;
+    }
+}