dobbs 02/03/12 07:40:04
Modified: src/test-conf log4j.properties
Added: src/java/org/apache/stratum/component ComponentLoader.java
src/test/org/apache/stratum/component MockComponent.java
TestComponentLoader.java
src/test-conf ComponentLoader.properties
TestComponentLoader.properties
Log:
Adding a ComponentLoader and some tests
Revision Changes Path
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/component/ComponentLoader.java
Index: ComponentLoader.java
===================================================================
package org.apache.stratum.component;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-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 "Apache" and "Apache Software Foundation" and
* "Apache Turbine" 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",
* "Apache Turbine", 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/>.
*/
import java.io.IOException;
import java.util.Vector;
import org.apache.log4j.Category;
import org.apache.stratum.configuration.Configuration;
import org.apache.stratum.configuration.PropertiesConfiguration;
import org.apache.stratum.lifecycle.Configurable;
import org.apache.stratum.lifecycle.Initializable;
/**
* @author <a href="mailto:eric NOSPAM dobbse.net">Eric Dobbs</a>
* @version $Id: ComponentLoader.java,v 1.1 2002/03/12 15:40:04 dobbs Exp $
*/
public class ComponentLoader
{
private static Category log = Category.getInstance(ComponentLoader.class);
private static String COMPONENT = "component";
private static String CLASSNAME = "classname";
private static String CONFIG = "config";
private static String NAME = "name";
private Configuration configuration;
public ComponentLoader(Configuration configuration)
{
this.configuration = configuration;
}
/**
* Support method for testing the constructor
*/
protected Configuration getConfiguration()
{
return configuration;
}
/**
* Load all the components listed in the ComponentLoader's configuration.
* Log any errors, but throw no exceptions if components cannot be loaded.
*/
public void load()
{
Vector components = configuration.getVector(COMPONENT + '.' + NAME);
String componentName,componentClassName,componentConfig;
for (int i = 0; i < components.size(); i++)
{
componentName = (String) components.get(i);
componentClassName = getComponentClassname(componentName);
componentConfig = getComponentConfigFile(componentName);
log.info("loading component " + componentName + " - class: "
+ componentClassName + " - config: " + componentConfig);
}
}
/**
* <p>Get the component's classname as defined in the ComponentLoader
* configuration.</p>
*
* <p>Example property:<br/>
* component.NAME.classname=com.mycompany.components.MyComponent</p>
*
* @param name the String NAME of the component in the classfile
*/
private String getComponentClassname(String name)
{
return configuration.getString(COMPONENT + '.' + name + '.' + CLASSNAME);
}
/**
* <p>Get the component's config file as defined in the ComponentLoader
* configuration.</p>
*
* <p>Example property:<br/>
* component.NAME.config=/path/to/your/config</p>
*
* @param name the String NAME of the component in the classfile
*/
private String getComponentConfigFile(String name)
{
return configuration.getString(COMPONENT + '.' + name + '.' + CONFIG);
}
/**
* load the given component, configure it with the given config
* file, and initialize it.
*
* @param className the String class name of the component to load
* @param configFile the String path name of the component's
* config file */
public void loadComponent(String className, String configFile)
{
if (log.isDebugEnabled())
{
log.debug("attempting to load '" + className
+ "' with the config file '" + configFile);
}
try
{
Object component = Class.forName(className).newInstance();
// configure component using the given config file
((Configurable) component)
.configure(new PropertiesConfiguration(configFile));
// initialize component
((Initializable) component).initialize();
if (log.isDebugEnabled())
{
log.debug("good news! " + className
+ " successfully configured and initialized");
}
}
catch (IOException ioe)
{
log.error(className + " could not be configured with file '"
+ configFile + "'.", ioe);
}
catch (Exception e)
{
log.error(className + " could not be initialized!", e);
}
}
}
1.1
jakarta-turbine-stratum/src/test/org/apache/stratum/component/MockComponent.java
Index: MockComponent.java
===================================================================
package org.apache.stratum.component;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-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 "Apache" and "Apache Software Foundation" and
* "Apache Turbine" 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",
* "Apache Turbine", 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/>.
*/
import junit.framework.Assert;
import org.apache.commons.lang.exception.NestableException;
import org.apache.log4j.Category;
import org.apache.stratum.configuration.Configuration;
import org.apache.stratum.lifecycle.Configurable;
import org.apache.stratum.lifecycle.Initializable;
/**
* This class is used by TestComponentLoader to test some assertions
* about the behvior of ComponentLoader.
*
* @author <a href="mailto:eric NOSPAM dobbse.net">Eric Dobbs</a>
* @version $Id: MockComponent.java,v 1.1 2002/03/12 15:40:04 dobbs Exp $
*/
public class MockComponent
extends Assert
implements Configurable, Initializable
{
private static Category log = Category.getInstance(MockComponent.class);
private int callsToConfigure = 0;
private int callsToInitialize = 0;
private int expectedCallsToConfigure = 1;
private int expectedCallsToInitialize = 1;
/**
* Verify that the given Configuration is not null
* and that configure() is called only once.
*/
public void configure( Configuration configuration )
throws NestableException
{
log.debug("MockComponent.configure() called");
assertNotNull(configuration);
log.debug("good news! configuration not null");
callsToConfigure++;
if (callsToConfigure > expectedCallsToConfigure)
{
log.debug("bad news! configure() called too many times");
fail("configure() called too many times");
}
log.debug("good news! configure() called successfully");
}
/**
* Verify that initialize() is called only once.
*/
public void initialize()
throws Exception
{
log.debug("MockComponent.initialize() called");
callsToInitialize++;
if (callsToInitialize > expectedCallsToInitialize)
{
log.debug("bad news! initialize() called too many times");
fail("initialize() called too many times");
}
log.debug("good news! initialize() called successfully");
}
}
1.1
jakarta-turbine-stratum/src/test/org/apache/stratum/component/TestComponentLoader.java
Index: TestComponentLoader.java
===================================================================
package org.apache.stratum.component;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-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 "Apache" and "Apache Software Foundation" and
* "Apache Turbine" 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",
* "Apache Turbine", 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/>.
*/
import java.io.IOException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.log4j.Category;
import org.apache.stratum.configuration.Configuration;
import org.apache.stratum.configuration.PropertiesConfiguration;
import org.apache.stratum.component.ComponentLoader;
public class TestComponentLoader extends TestCase
{
private static Category log = Category.getInstance(TestComponentLoader.class);
private static String CONFIG = "bin/classes/ComponentLoader.properties";
private static boolean isNotInitialized = true;
private static Configuration config;
static
{
try
{
config = new PropertiesConfiguration(CONFIG);
}
catch (IOException ioe)
{
log.error("unable to configure ComponentLoader from '" + CONFIG + "'.");
}
}
private ComponentLoader cl = new ComponentLoader(config);
public TestComponentLoader(String name)
{
super(name);
}
public void testConstructor()
{
ComponentLoader loader = new ComponentLoader(config);
Configuration loaderConfig = loader.getConfiguration();
assertNotNull(loaderConfig);
assertEquals("org.apache.stratum.component.MockComponent",
loaderConfig.getString("component.testing.classname"));
}
public void testLoadComponent()
{
String goodClassName = "org.apache.stratum.component.MockComponent";
String goodConfigFile = "bin/classes/TestComponentLoader.properties";
String badClassName = "org.apache.stratum.component.MissingMockComponent";
String badConfigFile = "bin/classes/MissingTestComponentLoader.properties";
// test loading with a mock component
cl.loadComponent(goodClassName,goodConfigFile);
// test that loadComponent() catches the IOException
cl.loadComponent(badClassName,goodConfigFile);
// test that loadComponent() catches the ClassNotFoundException
cl.loadComponent(goodClassName,badConfigFile);
}
public void testLoad()
{
assertNotNull(cl);
cl.load();
}
}
1.2 +7 -0 jakarta-turbine-stratum/src/test-conf/log4j.properties
Index: log4j.properties
===================================================================
RCS file: /home/cvs/jakarta-turbine-stratum/src/test-conf/log4j.properties,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- log4j.properties 14 Feb 2002 00:57:41 -0000 1.1
+++ log4j.properties 12 Mar 2002 15:40:04 -0000 1.2
@@ -6,3 +6,10 @@
log4j.appender.jcs.layout = org.apache.log4j.PatternLayout
log4j.appender.jcs.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.jcs.append = false
+
+log4j.category.org.apache.stratum.component = DEBUG,
org.apache.stratum.component
+log4j.appender.org.apache.stratum.component = org.apache.log4j.FileAppender
+log4j.appender.org.apache.stratum.component.file = bin/test/logs/loader.log
+log4j.appender.org.apache.stratum.component.layout = org.apache.log4j.PatternLayout
+log4j.appender.org.apache.stratum.component.layout.conversionPattern = %d [%t] %-5p
%c - %m%n
+log4j.appender.org.apache.stratum.component.append = true
1.1 jakarta-turbine-stratum/src/test-conf/ComponentLoader.properties
Index: ComponentLoader.properties
===================================================================
# -------------------------------------------------------------------
#
# C O M P O N E N T S
#
# -------------------------------------------------------------------
# Components implementing the lifecycle interfaces can be loaded,
# configured and initialized by stratum
# -------------------------------------------------------------------
#component.name = torque
#component.torque.classname = org.apache.torque.Torque
#component.torque.config = /WEB-INF/conf/Torque.properties
#component.name = fulcrum
#component.fulcrum.classname = org.apache.fulcrum.TurbineServices
#component.fulcrum.config = /WEB-INF/conf/Fulcrum.properties
component.name = testing
component.testing.classname = org.apache.stratum.component.MockComponent
component.testing.config = bin/classes/TestComponentLoader.properties
component.name = missing
component.missing.classname = org.apache.stratum.component.MissingMockComponent
component.missing.config = bin/classes/MissingTestComponentLoader.properties
1.1
jakarta-turbine-stratum/src/test-conf/TestComponentLoader.properties
Index: TestComponentLoader.properties
===================================================================
foo=bar
plugh=xyzzy
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>