mcconnell 2003/04/05 03:53:42
Added: merlin/merlin-smp/src/tutorial/008/src/config block.xml
merlin/merlin-smp/src/tutorial/008/src/java/tutorial
HelloComponent.java HelloComponent.xinfo
RandomGenerator.java RandomGeneratorProvider.java
RandomGeneratorProvider.xinfo
RandomGeneratorProvider.xprofile
RandomProfileSelector.java
Log:
Pluggable Profile selection policy tutorial.
Revision Changes Path
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/config/block.xml
Index: block.xml
===================================================================
<block name="tutorial">
<container>
<component name="hello" class="tutorial.HelloComponent" activation="startup"/>
</container>
</block>
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/java/tutorial/HelloComponent.java
Index: HelloComponent.java
===================================================================
package tutorial;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceException;
public class HelloComponent extends AbstractLogEnabled
implements Serviceable
{
/**
* Servicing of the component by the container during
* which service dependencies declared under the component
* can be resolved using the supplied service manager.
*/
public void service( ServiceManager manager )
throws ServiceException
{
RandomGenerator random = (RandomGenerator) manager.lookup( "random" );
getLogger().info( "random: " + random.getRandom() );
}
}
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/java/tutorial/HelloComponent.xinfo
Index: HelloComponent.xinfo
===================================================================
<?xml version="1.0"?>
<!DOCTYPE type
PUBLIC "-//AVALON/Type DTD Version 1.0//EN"
"http://avalon.apache.org/dtds/meta/type_1_1.dtd" >
<type>
<info>
<name>hello</name>
<version>1.0</version>
</info>
<dependencies>
<dependency key="random" type="tutorial.RandomGenerator">
<attributes>
<attribute key="urn:avalon:profile.selector"
value="tutorial.RandomProfileSelector"/>
</attributes>
</dependency>
</dependencies>
</type>
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/java/tutorial/RandomGenerator.java
Index: RandomGenerator.java
===================================================================
package tutorial;
/**
* A service that provides access to a random number.
*/
public interface RandomGenerator
{
/**
* Return a random integer
* @return the random number
*/
int getRandom();
}
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/java/tutorial/RandomGeneratorProvider.java
Index: RandomGeneratorProvider.java
===================================================================
package tutorial;
import java.util.Random;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
/**
* An implementation of a random number generator.
*/
public class RandomGeneratorProvider extends AbstractLogEnabled
implements Configurable, RandomGenerator
{
private Random m_random = null;
/**
* Configuration of the component by the container. The
* implementation get a child element named 'source' and
* assigns the value of the element to a local variable.
*
* @param config the component configuration
* @exception ConfigurationException if a configuration error occurs
*/
public void configure( Configuration config ) throws ConfigurationException
{
getLogger().info( "configuration stage" );
long seed = config.getChild( "seed" ).getValueAsLong( 0 );
getLogger().info( "seed: " + seed );
m_random = new Random( System.currentTimeMillis() * seed );
}
/**
* Return a random integer
* @return the random number
*/
public int getRandom()
{
return m_random.nextInt();
}
}
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/java/tutorial/RandomGeneratorProvider.xinfo
Index: RandomGeneratorProvider.xinfo
===================================================================
<?xml version="1.0"?>
<!DOCTYPE type
PUBLIC "-//AVALON/Type DTD Version 1.0//EN"
"http://avalon.apache.org/dtds/meta/type_1_1.dtd" >
<type>
<info>
<name>random-provider</name>
<version>1.0</version>
</info>
<services>
<service type="tutorial.RandomGenerator"/>
</services>
</type>
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/java/tutorial/RandomGeneratorProvider.xprofile
Index: RandomGeneratorProvider.xprofile
===================================================================
<?xml version="1.0"?>
<profiles>
<profile name="primary">
<configuration>
<seed>1024</seed>
</configuration>
</profile>
<profile name="secondary">
<configuration>
<seed>2048</seed>
</configuration>
</profile>
</profiles>
1.1
avalon-sandbox/merlin/merlin-smp/src/tutorial/008/src/java/tutorial/RandomProfileSelector.java
Index: RandomProfileSelector.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software
* itself, if and wherever such third-party acknowledgments
* normally appear.
*
* 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package tutorial;
import org.apache.avalon.assembly.engine.profile.ProfileSelector;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.meta.info.StageDescriptor;
import org.apache.avalon.meta.info.DependencyDescriptor;
import org.apache.avalon.meta.model.Profile;
import org.apache.avalon.meta.model.Mode;
/**
* Select one profile from the multiple profile provided.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
* @version $Revision: 1.1 $ $Date: 2003/04/05 11:53:42 $
*/
public class RandomProfileSelector extends AbstractLogEnabled
implements ProfileSelector
{
/**
* Returns the preferred profile form an available selection of
* candidate profiles.
* @param profiles the set of candidate profiles
* @param dependency the service dependency
* @return the preferred profile or null if no satisfactory provider can be
established
*/
public Profile select( Profile[] profiles, DependencyDescriptor dependency )
{
if( profiles.length == 0 )
{
return null;
}
for( int i=0; i<profiles.length; i++ )
{
Profile profile = profiles[i];
getLogger().info( "available profile: " + profile.getName() );
}
//
// select the profile witgh the highest seed value
//
long seed = 0;
Profile selection = profiles[0];
for( int i=0; i<profiles.length; i++ )
{
Profile profile = profiles[i];
long value = profile.getConfiguration().getChild( "seed"
).getValueAsLong( 0 );
if( value > seed )
{
seed = value;
selection = profile;
}
}
return selection;
}
/**
* Returns the preferred profile form an available selection of
* candidate profiles.
* @param profiles the set of candidate profiles
* @param stage the service stage depedency
* @return the prefered profile or null if no satisfactory provider can be
established
*/
public Profile select( Profile[] profiles, StageDescriptor stage )
{
return null;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]