[jarti-commits] SF.net SVN: jarti: [636] commons/trunk/codebase/src/com/lbf/commons
Status: Beta
Brought to you by:
tpokorny
From: <tpo...@us...> - 2007-04-13 19:04:14
|
Revision: 636 http://svn.sourceforge.net/jarti/?rev=636&view=rev Author: tpokorny Date: 2007-04-13 12:04:12 -0700 (Fri, 13 Apr 2007) Log Message: ----------- COM-??: Added a number of minor omissions thrown up from the integration of commons-2.0 with jaRTI (also fixed a nasty Bag bug) Modified Paths: -------------- commons/trunk/codebase/src/com/lbf/commons/component/ComponentGroup.java commons/trunk/codebase/src/com/lbf/commons/logging/Log4jConfigurator.java commons/trunk/codebase/src/com/lbf/commons/messaging/MessageContext.java commons/trunk/codebase/src/com/lbf/commons/messaging/MessageSink.java commons/trunk/codebase/src/com/lbf/commons/messaging/Module.java commons/trunk/codebase/src/com/lbf/commons/messaging/ModuleConfigurator.java commons/trunk/codebase/src/com/lbf/commons/messaging/RequestMessage.java commons/trunk/codebase/src/com/lbf/commons/utils/Bag.java commons/trunk/codebase/src/com/lbf/commons/utils/ResourceLocator.java Modified: commons/trunk/codebase/src/com/lbf/commons/component/ComponentGroup.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/component/ComponentGroup.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/component/ComponentGroup.java 2007-04-13 19:04:12 UTC (rev 636) @@ -234,6 +234,25 @@ } /** + * Removes the component of the given name from this group and returns it. If there is no + * component of that name, null is returned. + */ + public T removeComponent( String name ) + { + T found = null; + for( T component : components.keySet() ) + { + if( name.equals(component.getName()) ) + { + found = component; + } + } + + this.components.remove( found ); + return found; + } + + /** * Returns the number of components currently contained within this group */ public int size() Modified: commons/trunk/codebase/src/com/lbf/commons/logging/Log4jConfigurator.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/logging/Log4jConfigurator.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/logging/Log4jConfigurator.java 2007-04-13 19:04:12 UTC (rev 636) @@ -73,7 +73,7 @@ * classpath. The file will be looked for firstly on the file system, and then, if not found, * as a system resource. */ - public void bootstrap() + public static void bootstrap() { bootstrap( DEFAULT_CONFIG_FILE ); } @@ -84,7 +84,7 @@ * * @param withFile The location to look for a valid log4j configuration file under. */ - public void bootstrap( String withFile ) + public static void bootstrap( String withFile ) { ///////////////////////////////////////// // 1. attempt to find the file locally // Modified: commons/trunk/codebase/src/com/lbf/commons/messaging/MessageContext.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/messaging/MessageContext.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/messaging/MessageContext.java 2007-04-13 19:04:12 UTC (rev 636) @@ -179,7 +179,7 @@ /** * Puts an empty {@link SuccessMessage} into the given context. */ - protected void success() + public void success() { this.response = new SuccessMessage(); } @@ -187,7 +187,7 @@ /** * Puts a {@link ExtendedSuccessMessage} into the context, including the given properties. */ - protected void success( Bag<String,Object> properties ) + public void success( Bag<String,Object> properties ) { this.response = new ExtendedSuccessMessage( properties ); } @@ -196,7 +196,7 @@ * Puts a {@link ExtendedSuccessMessage} into the context. The given object is bound to the * properties for the message under the key "result". */ - protected void success( Object result ) + public void success( Object result ) { Bag<String,Object> properties = new Bag<String,Object>(); properties.put( "result", result ); @@ -207,7 +207,7 @@ * Sets an error response for the context using the given message. The message is wrapped up * inside an {@link Exception}. */ - protected void error( String message ) + public void error( String message ) { this.response = new ExceptionMessage( new Exception(message) ); } @@ -215,7 +215,7 @@ /** * Sets an error response for the context embedding the given cause */ - protected void error( Throwable cause ) + public void error( Throwable cause ) { this.response = new ExceptionMessage( cause ); } @@ -235,7 +235,6 @@ output.writeObject( response ); } - //---------------------------------------------------------- // STATIC METHODS //---------------------------------------------------------- Modified: commons/trunk/codebase/src/com/lbf/commons/messaging/MessageSink.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/messaging/MessageSink.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/messaging/MessageSink.java 2007-04-13 19:04:12 UTC (rev 636) @@ -14,6 +14,9 @@ */ package com.lbf.commons.messaging; +import com.lbf.commons.config.ConfigurationException; +import com.lbf.commons.utils.Bag; + import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -85,7 +88,7 @@ * is augmentable). This is because the new handler has been declared as not augmentable, and to * associated with the message id would require augmentation (due to the existing handler). */ -public class MessageSink +public class MessageSink implements IMessageHandler { //---------------------------------------------------------- // STATIC VARIABLES @@ -129,6 +132,14 @@ //---------------------------------------------------------- // INSTANCE METHODS //---------------------------------------------------------- + /** + * Currently ignored. Although this may change in the future + */ + public void initialize( Bag<String,?> properties ) throws ConfigurationException + { + // ignore + } + //////////////////////////////////////////////////////////// /////////////////// Processing Methods ///////////////////// //////////////////////////////////////////////////////////// @@ -492,6 +503,27 @@ return this.name; } + public void setName( String name ) + { + this.name = name; + } + + /** + * Any calls to this method are ignored + */ + public void setAugmentable( boolean augmentable ) + { + // ignored + } + + /** + * Always returns false + */ + public boolean isAugmentable() + { + return false; + } + //////////////////////////////////////////////////////////// ///////////////////// Utility Methods ////////////////////// //////////////////////////////////////////////////////////// Modified: commons/trunk/codebase/src/com/lbf/commons/messaging/Module.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/messaging/Module.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/messaging/Module.java 2007-04-13 19:04:12 UTC (rev 636) @@ -88,7 +88,7 @@ this.notes = ""; this.location = ""; - this.packageAlias = null; + this.packageAlias = ""; this.aliases = new HashMap<String,String>(); } @@ -167,7 +167,10 @@ public void setPackageAlias( String alias ) { - this.packageAlias = alias; + if( alias == null ) + this.packageAlias = ""; + else + this.packageAlias = alias.trim(); } public Map<String,String> getAliases() Modified: commons/trunk/codebase/src/com/lbf/commons/messaging/ModuleConfigurator.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/messaging/ModuleConfigurator.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/messaging/ModuleConfigurator.java 2007-04-13 19:04:12 UTC (rev 636) @@ -278,7 +278,7 @@ ///////////////////////////// // this is a PACKAGE ALIAS // ///////////////////////////// - if( module.getPackageAlias() == null ) + if( module.getPackageAlias() == null || module.getPackageAlias().equals("") ) { // we're not overwriting an existing package alias, it is safe to store it module.setPackageAlias( element.getAttribute(ALIAS_PACKAGE) ); Modified: commons/trunk/codebase/src/com/lbf/commons/messaging/RequestMessage.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/messaging/RequestMessage.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/messaging/RequestMessage.java 2007-04-13 19:04:12 UTC (rev 636) @@ -53,11 +53,11 @@ //---------------------------------------------------------- /** - * Returns the result of <code>this.getClass().getCanonicalName()</code> + * Returns the result of <code>this.getClass().getSimpleName()</code> */ public String getIdentifier() { - return this.getClass().getCanonicalName(); + return this.getClass().getSimpleName(); } /** Modified: commons/trunk/codebase/src/com/lbf/commons/utils/Bag.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/utils/Bag.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/utils/Bag.java 2007-04-13 19:04:12 UTC (rev 636) @@ -135,7 +135,7 @@ for( K key : otherBag.getKeys() ) { // rely on our other put method to take care of business - this.put( key, otherBag.get(key) ); + this.put( key, otherBag.values.get(key) ); } } @@ -149,7 +149,9 @@ public void replace( K key, V withValue ) { // remove any existing value - values.remove( key ); + if( values.containsKey(key) ) + this.remove( key ); + // put the new value in this.put( key, withValue ); } Modified: commons/trunk/codebase/src/com/lbf/commons/utils/ResourceLocator.java =================================================================== --- commons/trunk/codebase/src/com/lbf/commons/utils/ResourceLocator.java 2007-04-13 17:14:25 UTC (rev 635) +++ commons/trunk/codebase/src/com/lbf/commons/utils/ResourceLocator.java 2007-04-13 19:04:12 UTC (rev 636) @@ -14,7 +14,17 @@ */ package com.lbf.commons.utils; +import com.lbf.commons.config.ConfigurationException; +import com.lbf.commons.config.xml.XMLPreprocessor; +import com.lbf.commons.config.xml.XMLSource; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; /** * Provides static helper methods for locating and loading system resources. @@ -67,4 +77,144 @@ return loader.getResource( resource ); } + + /** + * Locate the XML resource of the given name (using the ClassLoader that loaded this class). + * If the resource can not be found, or it is not a valid XML document, a + * ConfigurationException will be thrown. Otherwise, the document will be returned in an + * {@link com.lbf.commons.config.xml.XMLSource XMLSource} instance. + */ + public static XMLSource locateXMLResource( String resource ) throws ConfigurationException + { + return locateXMLResource( resource, ResourceLocator.class.getClassLoader() ); + } + + /** + * The same as {@link #locateXMLResource(String) locateXMLResource(String)} except that the + * ClassLoader to be used is defined. + */ + public static XMLSource locateXMLResource( String resource, ClassLoader loader ) + throws ConfigurationException + { + // check the parameters // + if( loader == null ) + { + throw new ConfigurationException( "Can't locate XML resource [" + resource + + "]: ClassLoader was null" ); + } + + if( resource == null || resource.trim().equals("") ) + { + throw new ConfigurationException( + "Can't locate XML resource: Resource parameter was null" ); + } + + /////////////////////// + // find the resource // + /////////////////////// + URL url = locateResource( resource, loader ); + if( url == null ) + { + throw new ConfigurationException( "Can't locate XML resource [" + resource + + "]: Resource not found" ); + } + + /////////////////////////////////////////////// + // load the XML resource and resolve imports // + /////////////////////////////////////////////// + return XMLPreprocessor.preprocess( url ); + } + + /** + * This method will attempt to take all the properties identified in the given file and load + * them as system properties. If the file cannot be open or read, or an error occurs during + * the process, a {@link ConfigurationException} will be thrown. + * + * @param filename The name of the properties file to load + */ + public static Map<String,String> loadPropertiesFile( String filename ) + throws ConfigurationException + { + // check to see if the file exists // + File file = new File( filename ); + if( file.canRead() == false ) + { + throw new ConfigurationException( "The properties file [" + filename + + "] either does not exist or cannot be read" ); + } + + // open the file and turn it into a properties instance // + Properties properties = new Properties(); + try + { + properties.load( new FileInputStream(file) ); + } + catch( IOException io ) + { + throw new ConfigurationException( "Error opening properties file [" + + file.getAbsolutePath() + "]: "+ io.getMessage(), io ); + } + + // load the properties as system properties // + return loadProperties( properties ); + } + + /** + * This method will attempt to take all the properties identified in the given resource and + * load them as system properties. If the resource cannot be open or read, or an error occurs + * during the process, a {@link ConfigurationException} will be thrown. + * + * @param resource The name of the resource representing the properties to load + */ + public static Map<String,String> loadPropertiesResource( String resource ) + throws ConfigurationException + { + // find the resource // + URL url = ResourceLocator.locateResource( resource ); + if( url == null ) + { + throw new ConfigurationException( "The resource ["+resource+"] could not be located" ); + } + + // load the resource into a set of properties // + Properties properties = new Properties(); + try + { + properties.load( url.openStream() ); + } + catch( IOException io ) + { + throw new ConfigurationException( "Error opening properties resource [" + url + "]: " + + io.getMessage(), io ); + } + + // load the properties as system properties // + return loadProperties( properties ); + } + + /** + * Set all the properties contained in the given argument as system properties. If the + * parameter is null, the requesst is ignored. Only properties with Strings and values as + * keys will be stored. + */ + public static Map<String,String> loadProperties( Properties properties ) + { + Map<String,String> stored = new HashMap<String,String>(); + if( properties == null ) + { + return stored; + } + + for( Object key : properties.keySet() ) + { + Object value = properties.get( key ); + if( key instanceof String && value instanceof String ) + { + System.setProperty( (String)key, (String)value ); + stored.put( (String)key, (String)value ); + } + } + + return stored; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |