bloritsch 02/03/12 13:36:57
Added: examples/container build.bat build.sh build.xml
examples/container/bin cpappend.bat run.bat run.sh
examples/container/conf components.xml logkit.xml
profiler.xml
examples/container/src/java/org/apache/avalon/example/container
DefaultExampleProfilable.java ExampleContainer.java
ExampleProfilable.java Main.java
Log:
first hack at container code. There was a bug introduced with the
ContextManager resolver
Revision Changes Path
1.1 jakarta-avalon-excalibur/examples/container/build.bat
Index: build.bat
===================================================================
@echo off
if exist "..\..\..\jakarta-avalon\tools\bin\ant.bat" set
LOCAL_AVALON_TOOLS=..\..\..\jakarta-avalon\tools
if not "%AVALON_TOOLS%"=="" set LOCAL_AVALON_TOOLS=%AVALON_TOOLS%
if not "%LOCAL_AVALON_TOOLS%"=="" goto runAnt
echo "Unable to locate tools directory at "
echo "../../../jakarta-avalon/tools/"
echo "Aborting."
goto end
:runAnt
set OLD_ANT_HOME=%ANT_HOME%
set ANT_HOME=%LOCAL_AVALON_TOOLS%
%LOCAL_AVALON_TOOLS%\bin\ant.bat -logger org.apache.tools.ant.NoBannerLogger
-emacs -Dtools.dir=%LOCAL_AVALON_TOOLS% %1 %2 %3 %4 %5 %6 %7 %8
set ANT_HOME=%OLD_ANT_HOME%
set OLD_ANT_HOME=
:end
set LOCAL_AVALON_TOOLS=
1.1 jakarta-avalon-excalibur/examples/container/build.sh
Index: build.sh
===================================================================
#!/bin/sh
LOCAL_AVALON_TOOLS=$AVALON_TOOLS
if [ "$AVALON_TOOLS" = "" ] ; then
if [ -d ../../../jakarta-avalon/tools ] ; then
LOCAL_AVALON_TOOLS=../../../jakarta-avalon/tools
else
echo "Unable to locate tools directory at "
echo "../../../jakarta-avalon/tools/"
echo "Aborting."
exit 1
fi
fi
chmod u+x $LOCAL_AVALON_TOOLS/bin/antRun
chmod u+x $LOCAL_AVALON_TOOLS/bin/ant
$LOCAL_AVALON_TOOLS/bin/ant -logger org.apache.tools.ant.NoBannerLogger
-emacs -Dtools.dir=$LOCAL_AVALON_TOOLS $@
1.1 jakarta-avalon-excalibur/examples/container/build.xml
Index: build.xml
===================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- =======================================================================
-->
<!-- Container Example build file!
-->
<!-- =======================================================================
-->
<project name="container" default="main" basedir=".">
<!-- ================================================================ -->
<!-- Local user-defined build properties. -->
<!-- ================================================================ -->
<!-- The file below should exist on your system. On Windows machines, -->
<!-- user.home is probably the root of the C drive, but you should -->
<!-- verify this. This file should be a normal Java properties file. -->
<property file="${user.home}/.ant.properties" />
<property name="name" value="container"/>
<property name="Name" value="Container"/>
<!-- Directories build by ant -->
<property name="build.dir" value="${basedir}/build"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="build.javadocs.dir" value="${build.dir}/javadocs"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="data.dir" value="${basedir}/data"/>
<!-- Existing directories -->
<property name="src.dir" value="${basedir}/src"/>
<property name="src.java.dir" value="${src.dir}/java"/>
<property name="commonlib.dir" value="${basedir}/../commonlib"/>
<!-- Define paths used by this project -->
<path id="project.class.path">
<fileset dir="${commonlib.dir}">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${build.classes.dir}" />
</path>
<!-- ===================================================================
-->
<!-- Help on usage
-->
<!-- ===================================================================
-->
<target name="usage">
<echo message="Use the -projecthelp option instead"/>
</target>
<target name="help" depends="usage"/>
<!-- ===================================================================
-->
<!-- Help on usage
-->
<!-- ===================================================================
-->
<target name="collect-jars">
<ant antfile="build.xml" dir="${basedir}/.." target="collect-jars"
inheritall="false"/>
</target>
<!-- ===================================================================
-->
<!-- Initialize custom tasks
-->
<!-- ===================================================================
-->
<target name="init" depends="collect-jars">
</target>
<!-- ===================================================================
-->
<!-- Compiles the source code
-->
<!-- ===================================================================
-->
<target name="compile" depends="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
<!-- compile everything under src/java -->
<javac srcdir="${src.java.dir}" destdir="${build.classes.dir}"
deprecation="on"
debug="on"
optimize="off">
<classpath refid="project.class.path" />
</javac>
<!-- copy image files to build directory -->
<copy todir="${build.classes.dir}" >
<fileset dir="${src.java.dir}" >
<include name="**/*.jpg" />
<include name="**/*.gif" />
</fileset>
</copy>
</target>
<!-- ===================================================================
-->
<!-- Creates the jar archive
-->
<!-- ===================================================================
-->
<target name="jar" depends="compile" description="Builds the jar
archive.">
<mkdir dir="${lib.dir}"/>
<jar jarfile="${lib.dir}/${name}.jar"
basedir="${build.classes.dir}"
includes="**/*.class,**/*.properties,**/*.xml,**/*.jpg,**/*.gif,**/*.roles"
/>
</target>
<!-- ===================================================================
-->
<!-- Main target
-->
<!-- ===================================================================
-->
<target name="main" depends="jar" description="build the runnable
application">
<mkdir dir="${data.dir}"/>
</target>
<!-- ===================================================================
-->
<!-- Creates the javadocs archive.
-->
<!-- ===================================================================
-->
<target name="javadocs" description="Generates javadoc documentation">
<mkdir dir="${build.javadocs.dir}"/>
<javadoc packagenames="org.apache.*"
sourcepath="${src.java.dir}"
destdir="${build.javadocs.dir}">
<classpath refid="project.class.path" />
<doclet name="com.sun.tools.doclets.standard.Standard">
<param name="-author"/>
<param name="-version"/>
<param name="-doctitle" value="${Name}"/>
<param name="-windowtitle" value="${Name} API"/>
<param name="-bottom"
value=""Copyright © 2001 Apache Jakarta
Project. All Rights Reserved.""/>
</doclet>
</javadoc>
</target>
<!-- ===================================================================
-->
<!-- Builds everything.
-->
<!-- ===================================================================
-->
<target name="all" depends="main, javadocs" description="Build
everything"/>
<!-- ===================================================================
-->
<!-- Cleans up compiled classes and generated XMLC source.
-->
<!-- ===================================================================
-->
<target name="clean" description="cleans up the directory">
<delete dir="${build.dir}"/>
</target>
</project>
1.1
jakarta-avalon-excalibur/examples/container/bin/cpappend.bat
Index: cpappend.bat
===================================================================
set _LIBJARS=%_LIBJARS%;%1
1.1 jakarta-avalon-excalibur/examples/container/bin/run.bat
Index: run.bat
===================================================================
@echo off
rem
rem Example start script.
rem
rem Author: Leif Mortenson [EMAIL PROTECTED]
rem Author: Berin Loritsch [EMAIL PROTECTED]
rem
rem Determine if JAVA_HOME is set and if so then use it
rem
if not "%JAVA_HOME%"=="" goto found_java
set EXAMPLE_JAVACMD=java
goto file_locate
:found_java
set EXAMPLE_JAVACMD=%JAVA_HOME%\bin\java
:file_locate
rem
rem Locate where the example is in filesystem
rem
if not "%OS%"=="Windows_NT" goto start
rem %~dp0 is name of current script under NT
set EXAMPLE_HOME=%~dp0
rem : operator works similar to make : operator
set EXAMPLE_HOME=%EXAMPLE_HOME:\bin\=%
:start
if not "%EXAMPLE_HOME%" == "" goto example_home
echo.
echo Warning: EXAMPLE_HOME environment variable is not set.
echo This needs to be set for Win9x as it's command prompt
echo scripting bites
echo.
goto end
:example_home
rem
rem build the runtime classpath
rem
set CP=%EXAMPLE_HOME%\lib\container.jar
set _LIBJARS=
for %%i in (%EXAMPLE_HOME%\..\commonlib\*.jar) do call
%EXAMPLE_HOME%\bin\cpappend.bat %%i
if not "%_LIBJARS%" == "" goto run
echo Unable to set CLASSPATH dynamically.
goto end
:run
set CP=%CP%%_LIBJARS%
rem Run the example application
%EXAMPLE_JAVACMD% -Djava.compiler="NONE" -classpath "%CP%"
org.apache.avalon.examples.container.Main %1 %2 %3 %4 %5 %6 %7 %8 %9
:end
1.1 jakarta-avalon-excalibur/examples/container/bin/run.sh
Index: run.sh
===================================================================
#!/bin/bash
#
# Startup script.
#
#
# Determine if JAVA_HOME is set and if so then use it
#
if [ -z "$JAVA_HOME" ] ; then
JAVA=`which java`
if [ -z "$JAVA" ] ; then
echo "Cannot find JAVA. Please set your PATH."
exit 1
fi
JAVA_BINDIR=`dirname $JAVA`
JAVA_HOME=$JAVA_BINDIR/..
fi
if [ "$JAVACMD" = "" ] ; then
# it may be defined in env - including flags!!
JAVACMD=$JAVA_HOME/bin/java
fi
# Main.java has hard coded config values so this script must be run from
# altprofile/bin (any better ideas ?)
EXAMPLE_HOME=../..
#
# Build the runtime classpath
#
for i in ${EXAMPLE_HOME}/commonlib/*.jar ; do
CP=${CP}:$i
done
CP=${CP}:../lib/container.jar
#echo $CP
# Run the example application
$JAVACMD -classpath $CP org.apache.avalon.examples.container.Main $@
1.1
jakarta-avalon-excalibur/examples/container/conf/components.xml
Index: components.xml
===================================================================
<components logger="example">
<!-- =============================================================== -->
<!-- Application components. -->
<!-- =============================================================== -->
<example-profilable logger="app" profilable="example-profilable"/>
</components>
1.1
jakarta-avalon-excalibur/examples/container/conf/logkit.xml
Index: logkit.xml
===================================================================
<logkit logger="logger" log-level="INFO">
<factories>
<factory type="stream"
class="org.apache.avalon.excalibur.logger.factory.StreamTargetFactory"/>
<factory type="file"
class="org.apache.avalon.excalibur.logger.factory.FileTargetFactory"/>
<factory type="priority-filter"
class="org.apache.avalon.excalibur.logger.factory.PriorityFilterTargetFactory"/>
</factories>
<targets>
<stream id="console">
<stream>System.out</stream>
<format type="avalon">
%19.19{time:yyyy-MM-dd'T'HH:mm:ss.SSS} %5.5{priority} [%15.15{category}]:
%{message}\n%{throwable}
</format>
</stream>
</targets>
<categories>
<!-- Profiler Manager Logger -->
<category name="instrument" log-level="INFO">
<log-target id-ref="console"/>
<!-- The values child logger can dump out a lot of data. -->
<category name="values"
log-level="INFO">
<log-target id-ref="console"/>
</category>
</category>
<!-- Role Manager Logger -->
<category name="system" log-level="INFO">
<log-target id-ref="console"/>
</category>
<!-- Component Manager Logger -->
<category name="example" log-level="INFO">
<log-target id-ref="console"/>
</category>
<!-- Application Loggers -->
<category name="app" log-level="INFO">
<log-target id-ref="console"/>
</category>
</categories>
</logkit>
1.1
jakarta-avalon-excalibur/examples/container/conf/profiler.xml
Index: profiler.xml
===================================================================
<profiler logger="pm">
<state-file interval="60000"
use-compact-samples="true">../data/profiler.sampledata</state-file>
<profilables>
<!-- The profiler Profilable is the ProfilerManager itself. -->
<profilable name="profiler" description="Profiler">
<profile-point name="total-memory" description="Total Memory">
<sample type="max" interval="1000" size="600"
description="Maximum each second."/>
</profile-point>
<profile-point name="free-memory" description="Free Memory">
<sample type="min" interval="1000" size="600"
description="Minimum each second."/>
</profile-point>
<profile-point name="memory" description="In-Use Memory">
<sample type="max" interval="1000" size="600"
description="Maximum each second."/>
</profile-point>
</profilable>
<!-- ExampleProfilable. -->
<profilable name="example-profilable" description="Example
Profilable">
<!-- Note that any number of samples are possible for each
profile point. -->
<profile-point name="random-quick" description="Random Values at
a quick rate.">
<sample type="maximum" interval="1000" size="600"
description="Maximum each second."/>
<sample type="minimum" interval="1000" size="600"
description="Minimum each second."/>
<sample type="average" interval="1000" size="600"
description="Average each second."/>
</profile-point>
<profile-point name="random-slow" description="Random Values at a
slow rate.">
<sample type="maximum" interval="1000" size="600"
description="Maximum each second."/>
<sample type="minimum" interval="1000" size="600"
description="Minimum each second."/>
<sample type="average" interval="1000" size="600"
description="Average each second."/>
</profile-point>
<profile-point name="random-random" description="Random Values at
a random rate.">
<sample type="maximum" interval="1000" size="600"
description="Maximum each second."/>
<sample type="minimum" interval="1000" size="600"
description="Minimum each second."/>
<sample type="average" interval="1000" size="600"
description="Average each second."/>
</profile-point>
<profile-point name="counter-quick" description="Count of actions
at a quick rate.">
<sample type="counter" interval="100" size="600"
description="Count per 100ms."/>
<sample type="counter" interval="1000" size="600"
description="Count per second."/>
<sample type="counter" interval="60000" size="60"
description="Count per minute."/>
</profile-point>
<profile-point name="counter-slow" description="Count of actions
at a slow rate.">
<sample type="counter" interval="1000" size="600"
description="Count per second."/>
<sample type="counter" interval="60000" size="60"
description="Count per minute."/>
</profile-point>
<profile-point name="counter-random" description="Count of
actions at a random rate.">
<sample type="counter" interval="1000" size="600"
description="Count per second."/>
<sample type="counter" interval="60000" size="60"
description="Count per minute."/>
</profile-point>
<profile-point name="doaction-counter" description="Number of
times doAction is called.">
<sample type="counter" interval="1000" size="600"
description="Count per second."/>
<sample type="counter" interval="60000" size="60"
description="Count per minute."/>
</profile-point>
</profilable>
</profilables>
</profiler>
1.1
jakarta-avalon-excalibur/examples/container/src/java/org/apache/avalon/example/container/DefaultExampleProfilable.java
Index: DefaultExampleProfilable.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.examples.container;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.apache.avalon.excalibur.altprofile.CounterProfilePoint;
import org.apache.avalon.excalibur.altprofile.Profilable;
import org.apache.avalon.excalibur.altprofile.ProfilePoint;
import org.apache.avalon.excalibur.altprofile.ValueProfilePoint;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
/**
* This example application creates a component which registers several
* ProfilePoints for the example.
*
* Note, this code ignores exceptions to keep the code simple.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/03/12 21:36:56 $
* @since 4.1
*/
public class DefaultExampleProfilable
extends AbstractLogEnabled
implements ExampleProfilable, Startable, Runnable, Profilable
{
public static final String PROFILE_POINT_RANDOM_QUICK_NAME =
"random-quick";
public static final String PROFILE_POINT_RANDOM_SLOW_NAME =
"random-slow";
public static final String PROFILE_POINT_RANDOM_RANDOM_NAME =
"random-random";
public static final String PROFILE_POINT_COUNTER_QUICK_NAME =
"counter-quick";
public static final String PROFILE_POINT_COUNTER_SLOW_NAME =
"counter-slow";
public static final String PROFILE_POINT_COUNTER_RANDOM_NAME =
"counter-random";
public static final String PROFILE_POINT_DOACTION_NAME =
"doaction-counter";
/** Profilable Name assigned to this Profilable */
private String m_profilableName;
/** ProfilePoint used to profile random values with lots of updates. */
private ValueProfilePoint m_randomQuickProfilePoint;
/** ProfilePoint used to profile random values with few of updates. */
private ValueProfilePoint m_randomSlowProfilePoint;
/** ProfilePoint used to profile random values with updates at a random
rate. */
private ValueProfilePoint m_randomRandomProfilePoint;
/** ProfilePoint used to profile random actions with lots of updates. */
private CounterProfilePoint m_counterQuickProfilePoint;
/** ProfilePoint used to profile random actions with few of updates. */
private CounterProfilePoint m_counterSlowProfilePoint;
/** ProfilePoint used to profile random actions with updates at a random
rate. */
private CounterProfilePoint m_counterRandomProfilePoint;
/** ProfilePoint used to count the number of times that doAction is
called. */
private CounterProfilePoint m_doActionProfilePoint;
/** Thread which is used to send profile data to the random profile
points. */
private Thread m_runner;
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
public DefaultExampleProfilable()
{
// Initialize the Profiler elements.
m_randomQuickProfilePoint = new ValueProfilePoint(
PROFILE_POINT_RANDOM_QUICK_NAME );
m_randomSlowProfilePoint = new ValueProfilePoint(
PROFILE_POINT_RANDOM_SLOW_NAME );
m_randomRandomProfilePoint = new ValueProfilePoint(
PROFILE_POINT_RANDOM_RANDOM_NAME );
m_counterQuickProfilePoint = new CounterProfilePoint(
PROFILE_POINT_COUNTER_QUICK_NAME );
m_counterSlowProfilePoint = new CounterProfilePoint(
PROFILE_POINT_COUNTER_SLOW_NAME );
m_counterRandomProfilePoint = new CounterProfilePoint(
PROFILE_POINT_COUNTER_RANDOM_NAME );
m_doActionProfilePoint = new CounterProfilePoint(
PROFILE_POINT_DOACTION_NAME );
}
/*---------------------------------------------------------------
* ExampleProfilable Methods
*-------------------------------------------------------------*/
/**
* Example action method.
*/
public void doAction()
{
getLogger().info( "ExampleProfilable.doAction() called." );
// Notify the profiler.
m_doActionProfilePoint.increment();
}
/*---------------------------------------------------------------
* Startable Methods
*-------------------------------------------------------------*/
/**
* Start the component.
*/
public void start()
{
if ( m_runner == null )
{
m_runner = new Thread( this, "ExampleProfilableRunner" );
m_runner.start();
}
}
/**
* Stop the component.
*/
public void stop()
{
if ( m_runner != null )
{
m_runner.interrupt();
m_runner = null;
}
}
/*---------------------------------------------------------------
* Runnable Methods
*-------------------------------------------------------------*/
/**
* Runner thread which is responsible for sending data to the Profiler via
* the various random Profile Points.
*/
public void run()
{
int counter = 0;
while ( m_runner != null )
{
// Add some delay to the loop.
try
{
Thread.sleep( 100 );
}
catch ( InterruptedException e )
{
if ( m_runner == null )
{
return;
}
}
// Handle the quick Profile Points
m_randomQuickProfilePoint.setValue( (int)(Math.random() * 100) );
m_counterQuickProfilePoint.increment();
// Handle the slow Profile Points
counter++;
if ( counter >= 20 )
{
m_randomSlowProfilePoint.setValue( (int)(Math.random() * 100)
);
m_counterSlowProfilePoint.increment();
counter = 0;
}
// Handle the random Profile Points. Fire 10% of the time.
if ( 100 * Math.random() < 10 )
{
m_randomRandomProfilePoint.setValue( (int)(Math.random() *
100) );
m_counterRandomProfilePoint.increment();
}
}
}
/*---------------------------------------------------------------
* Profilable Methods
*-------------------------------------------------------------*/
/**
* Sets the name for the Profilable. The Profilable Name is used to
* uniquely identify the Profilable during the configuration of the
* Profiler and to gain access to a ProfilableDescriptor through a
* ProfilerManager. The value should be a string which does not
* contain spaces or periods.
* <p>
* This value may be set by a parent Profilable, or by the ProfilerManager
* using the value of the 'profilable' attribute in the configuration
* of the component.
*
* @param name The name used to identify a Profilable.
*/
public void setProfilableName( String name )
{
m_profilableName = name;
}
/**
* Gets the name of the Profilable. The Profilable Name is used to
* uniquely identify the Profilable during the configuration of the
* Profiler and to gain access to a ProfilableDescriptor through a
* ProfilerManager. The value should be a string which does not
* contain spaces or periods.
*
* @return The name used to identify a Profilable.
*/
public String getProfilableName()
{
return m_profilableName;
}
/**
* Obtain a reference to all the ProfilePoints that the Profilable
* object wishes to expose. All sampling is done directly through
* the ProfilePoints as opposed to the Profilable interface.
*
* @return An array of the ProfilePoints available for profiling.
* Should never be null. If there are no ProfilePoints, then
* EMPTY_PROFILE_POINT_ARRAY can be returned. This should
* never be the case though unless there are child Profilables
* with ProfilePoints.
*/
public ProfilePoint[] getProfilePoints()
{
return new ProfilePoint[]
{
m_randomQuickProfilePoint,
m_randomSlowProfilePoint,
m_randomRandomProfilePoint,
m_counterQuickProfilePoint,
m_counterSlowProfilePoint,
m_counterRandomProfilePoint,
m_doActionProfilePoint
};
}
/**
* Any Object which implements Profilable can also make use of other
* Profilable child objects. This method is used to tell the Profiler
* about them.
*
* @return An array of child Profilables. This method should never
* return null. If there are no child Profilables, then
* EMPTY_PROFILABLE_ARRAY can be returned.
*/
public Profilable[] getChildProfilables()
{
// This profilable does not have any children.
return Profilable.EMPTY_PROFILABLE_ARRAY;
}
}
1.1
jakarta-avalon-excalibur/examples/container/src/java/org/apache/avalon/example/container/ExampleContainer.java
Index: ExampleContainer.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.examples.container;
import org.apache.avalon.excalibur.system.*;
import org.apache.avalon.excalibur.system.AbstractContainer;
/**
* This example application loads a component in which publishs a series
* of ProfilePoints. A ProfilerManager is created to collect and
* manage the Profiler data. And a ProfilerFrame is created and shown
* to allow the profiler data to be browsed.
* <p>
* WARNING - Feel free to make use of the classes shown in this example.
* but be aware that the API and package name are likely to change as
* the package is still experimental.
* <p>
* Note, this code ignores exceptions to keep the code simple.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/03/12 21:36:56 $
* @since 4.1
*/
public class ExampleContainer extends AbstractContainer
{
public final ExampleProfilable getExampleProfilable()
{
try
{
return (ExampleProfilable)
getComponentManager().lookup(ExampleProfilable.ROLE);
}
catch (Exception e)
{
getLogger().error("Could not return ExampleProfilable", e);
return null;
}
}
}
1.1
jakarta-avalon-excalibur/examples/container/src/java/org/apache/avalon/example/container/ExampleProfilable.java
Index: ExampleProfilable.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.examples.container;
import org.apache.avalon.framework.component.Component;
/**
* This example application creates a component which registers several
* ProfilePoints for the example.
*
* Note, this code ignores exceptions to keep the code simple.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/03/12 21:36:56 $
* @since 4.1
*/
public interface ExampleProfilable
extends Component
{
String ROLE = ExampleProfilable.class.getName();
/**
* Example action method.
*/
void doAction();
}
1.1
jakarta-avalon-excalibur/examples/container/src/java/org/apache/avalon/example/container/Main.java
Index: Main.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.examples.container;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import org.apache.avalon.excalibur.altprofile.profiler.DefaultProfilerManager;
import org.apache.avalon.excalibur.altprofile.profiler.ProfilerManager;
import org.apache.avalon.excalibur.altprofile.profiler.gui.ProfilerFrame;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.logger.LogKitLogger;
import org.apache.avalon.excalibur.concurrent.*;
import org.apache.avalon.excalibur.system.util.*;
import org.apache.avalon.excalibur.system.*;
/**
* This example application loads a component in which publishs a series
* of ProfilePoints. A ProfilerManager is created to collect and
* manage the Profiler data. And a ProfilerFrame is created and shown
* to allow the profiler data to be browsed.
* <p>
* WARNING - Feel free to make use of the classes shown in this example.
* but be aware that the API and package name are likely to change as
* the package is still experimental.
* <p>
* Note, this code ignores exceptions to keep the code simple.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/03/12 21:36:56 $
* @since 4.1
*/
public class Main
{
private static ProfilerFrame m_profilerFrame;
private static ContainerManager m_manager;
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
private Main() {}
/*---------------------------------------------------------------
* Methods
*-------------------------------------------------------------*/
private static void initialize() throws Exception
{
// Set up the ContextBuilder. This gives the container the initial
// values it needs to do its job.
ContextBuilder contextBuilder = new ContextBuilder ();
contextBuilder.setContainerClass( ExampleContainer.class );
contextBuilder.setContextDirectory( "../../" );
contextBuilder.setContainerConfiguration( "conf/components.xml" );
contextBuilder.setLoggerManagerConfiguration( "conf/logkit.xml" );
contextBuilder.setRoleManagerConfiguration(
"resource://org/apache/avalon/example/container/container.roles" );
contextBuilder.setInstrumentManagerConfiguration( "conf/profiler.xml"
);
// Initialize other managers as needed
ContextManager contextManager = new ContextManager( null,
contextBuilder.getContext(), null );
contextManager.initialize();
// Manage the Container
m_manager = new DefaultContainerManager(
contextManager.getChildContext() );
m_manager.initialize();
// Set up the ProfilerFrame
ProfilerManager pmanager = (ProfilerManager)
contextManager.getChildContext().get(ContainerConstants.INSTRUMENT_MANAGER);
m_profilerFrame = new ProfilerFrame(pmanager, "Example Profiler" );
m_profilerFrame.setVisible( true );
try
{
File desktopFile = new File( "../conf/altprofile.desktop" );
m_profilerFrame.loadDesktopStateFromFile( desktopFile, false );
}
catch ( Exception e )
{
m_manager.getLogger().error( "Unable to load desktop file", e );
}
}
/*---------------------------------------------------------------
* Main method
*-------------------------------------------------------------*/
/**
* All of the guts of this example exist in the main method.
*/
public static void main( String[] args )
throws Exception
{
System.out.println( "Running the Container Example Application" );
initialize();
ExampleContainer container = (ExampleContainer)
m_manager.getContainer();
// Get a reference to the example component.
ExampleProfilable profilable = container.getExampleProfilable();
try
{
boolean quit = false;
while ( !quit )
{
System.out.println( "Enter the number of times that
exampleAction should be called, or 'q' to quit." );
BufferedReader in = new BufferedReader( new
InputStreamReader( System.in ) );
System.out.print( " : " );
String cntStr = in.readLine();
// Can get a null if CTRL-C is hit.
if ( ( cntStr == null ) || ( cntStr.equalsIgnoreCase( "q" ) )
)
{
quit = true;
}
else if ( ( cntStr.equalsIgnoreCase( "gc" ) ) )
{
System.gc();
}
else
{
try
{
int concurrent = 100;
ThreadBarrier barrier = new ThreadBarrier( concurrent
);
int cnt = Integer.parseInt( cntStr );
int average = Math.max(cnt / concurrent, 1);
while (cnt > 0)
{
Thread t = new Thread( new ActionRunner(
profilable,
Math.min(average,
cnt),
barrier) );
t.start();
if (cnt > 0)
{
cnt -= average;
}
if (cnt < 0)
{
cnt = 0;
}
}
}
catch ( NumberFormatException e ) {}
}
}
}
finally
{
// Hide the frame
m_profilerFrame.setVisible( false );
m_profilerFrame.dispose();
m_profilerFrame = null;
m_manager.dispose();
}
System.out.println();
System.out.println( "Exiting..." );
System.exit(0);
}
private final static class ActionRunner implements Runnable
{
private final int m_numIterations;
private final ExampleProfilable m_profilable;
private final ThreadBarrier m_barrier;
protected ActionRunner( ExampleProfilable profilable, int
numIterations, ThreadBarrier barrier )
{
m_numIterations = numIterations;
m_profilable = profilable;
m_barrier = barrier;
}
public void run()
{
for ( int i = 0; i < m_numIterations; i++ )
{
m_profilable.doAction();
}
try
{
m_barrier.barrierSynchronize();
}
catch (Exception e) {}
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>