Steve, Really quick I wanted to say the Map idea may be better I just did not know if it was definitively. Might be the better route to go. I don't know about this KernelParameters or KernelBean thing myself - thought I could invoke a discussion.
> > Pre comments: > > 1. I think KernelParameters should go into a seperate package that other > packages (such as kernel) depend on. This will make it easier when > creating the bootstrap process. Absolutely I just used the package because it was better than the default package. Was hoping you would point out a better package for it. > > 2. Rename KernelParameters to KernelBean? Everything about it seems to > be beenish in nature. > KernelBean sounds too much like it is the kernel itself as a bean don't you think? And what about your Map approach? > > [EMAIL PROTECTED] wrote: > > >Ok I sat down to start implementing the search policy for kernel parameters and > >would like to run the default value discovery order by the Avalon folks: > > > >1). First check for parameter value in a merlin.properties file packaged within the > >same package as this KernelDefaults file. > > > > > +1 > > >2). If a merlin installation is found based on a non-null MERLIN_HOME use > >installation base relative paths to override jar file merlin.properties based > >values. > > > > > +1 > > >3). Check for overriding values in a merlin.properties file anywhere on the > >classpath. If there is more than one merlin.properties file with a value for the > >property, then the last path component with a value is used as the effective > >default property value. > > > > This one sound non-predictable due to the ordering issue. > I'm tempted to suggest we exclude this option. True it can be a tricky thing. > > > > >4). Check for overriding values in a dot prefixed (hidden) version of the > >merlin.properties file (.merlin.properties) within the user's home directory. > > > > > +1 > > >5). Check for default override values within the System properties. > > > +1 > > > > > > >These rules once followed may or may not explicitly set a value for each parameter. > > Obviously null and false will be returned if no value can be found in any property > >file or env parameter. When the default constructor is used to create a > >KernelParameters bean instance, the search for parameters occurs and sets the > >defaults. > > > >Also I can add a method to return a Map representation of the KernelParameters bean > >if we want that option as well. Should I do that? I will set the property keys > >(KernelDefaults constants) to reflect the ones used by Steve in previous emails > >where I can. > > > > I've committed the attched files to a new kernel subproject > kernel/loader with the package name org.apache.avalon.merlin.loader. > Groovey. I'll be hard to reach in the next couple of days but I want to bang this out asap. So forgive the blackout. Alex > Cheers, Steve. > > > > > >Alex > > > > > > > >>From: <[EMAIL PROTECTED]> > >>Date: 2003/10/23 Thu PM 12:17:36 EDT > >>To: <[EMAIL PROTECTED]> > >>Subject: Embedding and standardizing kernel parameter handling and discovery > >> > >>Steve, > >> > >>After doing some research on Merlin for the sake of embedding I've come to some > >>conclusions. > >> > >>First I see multiple places where kernel parameters are gathered and handled to > >>configure a kernel. Merlin.java, MerlinBean.java, CLIKernelLoader and even in the > >>DefaultEmbeddedKernel. Some kernel parameters are present as properties in beans > >>like the KernelContext and in other places some parameters are put into an > >>associative hash. > >> > >>Overall I think we need to find a generalized mechanism for providing these > >>parameters and standardize the mechanism everywhere in Merlin (as you already > >>stated). I think you started to do that with these map arguments. This is > >>probably the most flexible means to do so especially when the variables used are > >>in flux but it does not have strict type checking. Conversely, using a bean like > >>a KernelParameters object rather than a map would give you strict type checking. > >> > >>As I expressed before I think we need to leave parameter gathering up to the > >>embedding application. However there are some default kernel parameter discovery > >>mechanisms that should be provided like getting the MERLIN_HOME parameter from the > >>shell environment. > >> > >>I've followed your advice to create an Env class that gets operating system and > >>shell specific environment parameters. I'm trying to come up with a parameter > >>discovery process to default populate a KernelParameters bean's members taking the > >>following steps: > >> > >>1). Check for environment variables first > >>2). Check for properties files on the path with specific names or in the user's > >>home directory to see if we can get some defaults > >>3). Check the System properties for parameter overrides > >>4). et cetera. > >> > >>What do you and the others see for this discovery process? > >> > >>You already seemed to design the Kernel Loader using the factory or builder > >>pattern. We can stick to the Map approach which gives more flexibility but is > >>less type safe or we can use a bean like the KernelParameters bean which is less > >>flexible but more type safe. Or we could just do both providing alternative > >>create method overloads within the Loader: it's up to you on how to deal with > >>these trade offs. > >> > >>Now we need to centralize the code that implements the default kernel parameter > >>discovery process and agree on the search order and document it. If we go with > >>the Map approach we might create a default map in a Loader. The Map would > >>contain the defaults and then the API user provided Map argument to the create > >>call would have its properties overlaid onto the defaults Map. > >> > >>With the KernelParameters bean approach the default constructor can automatically > >>set member properties using the default kernel parameter value discovery policy > >>based class. Whooo that?s a mouth full. Anyway I have some mock classes ready to > >>just look at for the bean based approach. > >> > >>Personally I think we need to pick one approach and retrofit it to all the classes > >>that build a kernel. IMHO we should not differentiate between embedding the > >>kernel and the CLI based use of the kernel; everything is an embedding situation. > >> > >>So in conclusion which ever kernel parameter aggregation/passing mechanism we pick > >>(bean based or Map based) we need it to be standardized and well documented. The > >>default value discovery policy also must be well documented and the policy code > >>should be separate and isolated in one place. > >> > >>I have mocked up a KernelParameters bean and a utility class to start building a > >>policy around it. There is very little code here but I will add it once we agree > >>on a kernel default parameter discovery policy. Attached you'll find the Env > >>variable access utility class and other classes for managing kernel parameters. > >>Excuse the ugly code for the time being - I shall beautify once we decide to go a > >>particular route. > >> > >>Alex > >> > >>P.S. I made the KernelParameters an interface with the DefaultKernelParameters > >>being the implementation class just in case. > >> > >> > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >>/* > >> > >> ============================================================================ > >> The Apache Software License, Version 1.1 > >> ============================================================================ > >> > >> Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. > >> > >> Redistribution and use in source and binary forms, with or without modifica- > >> tion, 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", "Apache Avalon", "Avalon Framework" 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 (INCLU- > >> DING, 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 org.apache.avalon.merlin.kernel ; > >> > >>/** > >> * This class represents an exhaustive set of kernel parameters in their most > >> * rudimentary/common representation. > >> * > >> * Note I avoided the use of Files or URLs purposely so any value can be > >> * specified and interpreted in any fashion. I left this as general and > >> * flexible as possible. > >> * > >> * These methods have been gathered from other methods an members witnessed on > >> * the CLIKernelLoader, KernelContext[Default], and KernelBean classes/ > >> * interfaces. > >> * > >> * @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a> > >> * @author $Author$ > >> * @version $Revision$ > >> */ > >>public interface KernelParameters > >>{ > >> /** > >> * Gets whether or not the kernel should operate in server mode. > >> * > >> * @return true if it operates in server mode, false otherwise > >> */ > >> boolean isServer() ; > >> > >> /** > >> * Gets whether or not debugging is enabled in the kernel. > >> * > >> * @return true if debugging is enabled false otherwise > >> */ > >> boolean isDebugEnabled() ; > >> > >> /** > >> * Gets whether or not informative logging is enabled in the kernel. > >> * @todo determine whether or not this is absolutely necessary and if there > >> * is mutual exclusion between this method and the debug methods return. > >> * > >> * @return true if informative logging is enabled false otherwise > >> */ > >> boolean isInfoEnabled() ; > >> > >> /** > >> * Url of a potentially remote repository used to pull depenendencies into > >> * the other repositories on artifact misses. > >> * > >> * @return url to the remote repo from where we download jars > >> */ > >> String getRemoteRepositoryUrl() ; > >> > >> /** > >> * Path to the user repository base directory where the repository > >> * resides. > >> * > >> * @return the user artifact repository path > >> */ > >> String getUserRepositoryPath() ; > >> > >> /** > >> * Path to the system artifact repository used to bootstrap the kernel. > >> * > >> * @return the system artifact repo path/url > >> */ > >> String getSystemRepositoryPath() ; > >> > >> /** > >> * Gets the path to an optional directory that is the root anchor for > >> * optional-extension jar files. > >> * > >> * @return the optional extention jar directory path > >> */ > >> String getLibraryPath() ; > >> > >> /** > >> * Gets the working home directory path. > >> * > >> * @return the kernel's working directory path > >> */ > >> String getHomePath() ; > >> > >> /** > >> * Gets a temporary directory path. > >> * > >> * @return a temp space path > >> */ > >> String getTempPath() ; > >> > >> /** > >> * Gets the path/url to alternative configuration overrides: config.xml > >> * file. > >> * > >> * @return path/url to config overrides > >> */ > >> String getConfigUrl() ; > >> > >> /** > >> * Gets the paths or urls to block.xml files to load into the kernel. > >> * > >> * @return paths/urls to the block.xml file > >> */ > >> String [] getTargetUrls() ; > >> > >> /** > >> * Gets the path/url to the kernel configuration file. > >> * > >> * @return path/url to the kernel config file > >> */ > >> String getKernelUrl() ; > >>} > >> > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >>/* > >> > >> ============================================================================ > >> The Apache Software License, Version 1.1 > >> ============================================================================ > >> > >> Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. > >> > >> Redistribution and use in source and binary forms, with or without modifica- > >> tion, 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", "Apache Avalon", "Avalon Framework" 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 (INCLU- > >> DING, 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 org.apache.avalon.merlin.kernel ; > >> > >> > >>/** > >> * Maintains algorithms for determining default values to kernel parameters. > >> * This involves searching classpaths, finding variables in the environment in > >> * an operating system dependent fashion, and finding variables within the > >> * system properties. Basically the policy of finding the values to these > >> * kernel parameters are maintained here. > >> * > >> * @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a> > >> * @author $Author$ > >> * @version $Revision$ > >> */ > >>public class KernelDefaults > >>{ > >> public static final String IS_SERVER_KEY = > >> "merlin.kernel.isserver" ; > >> public static final String IS_INFO_KEY = > >> "merlin.kernel.isinfo" ; > >> public static final String IS_DEBUG_KEY = > >> "merlin.kernel.isdebug" ; > >> > >> public static final String REMOTE_REPO_KEY = > >> "merlin.kernel.remoterepo" ; > >> public static final String USER_REPO_KEY = > >> "merlin.kernel.userrepo" ; > >> public static final String SYSTEM_REPO_KEY = > >> "merlin.kernel.systemrepo" ; > >> public static final String HOME_PATH_KEY = > >> "merlin.kernel.homepath" ; > >> public static final String CONFIG_URL_KEY = > >> "merlin.kernel.configurl" ; > >> public static final String KERNEL_URL_KEY = > >> "merlin.kernel.kernelurl" ; > >> public static final String TARGET_URLS_KEY = > >> "merlin.kernel.targeturls" ; > >> public static final String TEMP_PATH_KEY = > >> "merlin.kernel.temppath" ; > >> public static final String LIBRARY_PATH_KEY = > >> "merlin.kernel.librarypath" ; > >> > >> > >> /** > >> * Gets a default value for the server flag. > >> * > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#isServer() > >> */ > >> public static boolean isServer() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * Gets a default value for the debug flag. > >> * > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#isDebugEnabled() > >> */ > >> public static boolean isDebugEnabled() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * Gets a default value for the info flag. > >> * > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#isInfoEnabled() > >> */ > >> public static boolean isInfoEnabled() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * Gets a default value for the remote repo. > >> * > >> * @see org.apache.avalon.merlin.kernel.KernelParameters# > >> * getRemoteRepositoryUrl() > >> */ > >> public static String getRemoteRepositoryUrl() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * > >> * @see org.apache.avalon.merlin.kernel.KernelParameters# > >> * getUserRepositoryPath() > >> */ > >> public static String getUserRepositoryPath() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * @see org.apache.avalon.merlin.kernel.KernelParameters# > >> * getSystemRepositoryPath() > >> */ > >> public static String getSystemRepositoryPath() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#getLibraryPath() > >> */ > >> public static String getLibraryPath() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#getHomePath() > >> */ > >> public static String getHomePath() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#getTempPath() > >> */ > >> public static String getTempPath() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#getConfigUrl() > >> */ > >> public static String getConfigUrl() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#getTargetUrls() > >> */ > >> public static String[] getTargetUrls() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >> > >> > >> /** > >> * @see org.apache.avalon.merlin.kernel.KernelParameters#getKernelUrl() > >> */ > >> public static String getKernelUrl() > >> { > >> throw new UnsupportedOperationException( > >> "N O T I M P L E M E N T E D Y E T !" ) ; > >> } > >>} > >> > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >>import java.io.BufferedReader ; > >>import java.io.IOException; > >>import java.io.InputStreamReader ; > >> > >>/* > >> > >> ============================================================================ > >> The Apache Software License, Version 1.1 > >> ============================================================================ > >> > >> Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. > >> > >> Redistribution and use in source and binary forms, with or without modifica- > >> tion, 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", "Apache Avalon", "Avalon Framework" 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 (INCLU- > >> DING, 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/>. > >> > >>*/ > >> > >>/** > >> * Encapsulates operating system specific access to environment variables. > >> * > >> * @todo Add more methods that allow access to path and library parameters in a > >> * platform neutral fashion. > >> * > >> * @see List of operating system specific System property values > >> * <a href="http://www.tolstoy.com/samizdat/sysprops.html">here</a>. > >> * @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a> > >> * @author $Author$ > >> * @version $Revision$ > >> */ > >>public class Env > >>{ > >> /** > >> * Gets the value of a shell environment variable. > >> * > >> * @param a_name the name of variable > >> * @return the String representation of an environment variable value > >> * @throws Exception if there is a problem accessing the environment > >> */ > >> public static String getVariable( String a_name ) > >> throws EnvAccessException > >> { > >> String l_osName = System.getProperty( "os.name" ) ; > >> > >> if ( -1 != l_osName.indexOf( "Linux" ) || > >> -1 != l_osName.indexOf( "Solaris" ) || > >> -1 != l_osName.indexOf( "MPE/iX" ) || > >> -1 != l_osName.indexOf( "AIX" ) || > >> -1 != l_osName.indexOf( "FreeBSD" ) || > >> -1 != l_osName.indexOf( "Irix" ) || > >> -1 != l_osName.indexOf( "Digital Unix" ) || > >> -1 != l_osName.indexOf( "HP-UX" ) ) > >> { > >> return getUnixVariable( a_name ) ; > >> } > >> else if ( -1 != l_osName.indexOf( "Windows" ) ) > >> { > >> return getWindowsVariable( a_name ) ; > >> } > >> > >> throw new EnvAccessException( a_name, "Unrecognized operating system: " > >> + l_osName ) ; > >> } > >> > >> > >> /** > >> * Gets a UNIX shell environment parameter by forking a call to echo. This > >> * should work on all UNIX shells like sh, ksh, csh, zsh and bash. > >> * > >> * @param a_name the name of the variable accessed > >> * @return the value of the variable > >> * @throws EnvAccessException if there is an error accessing the value > >> */ > >> private static String getUnixVariable( String a_name ) > >> throws EnvAccessException > >> { > >> String l_value = null ; > >> Process l_proc = null ; > >> BufferedReader l_in = null ; > >> StringBuffer l_cmd = new StringBuffer() ; > >> String l_osName = System.getProperty( "os.name" ) ; > >> > >> l_cmd.append( "echo $" ) ; > >> l_cmd.append( a_name ) ; > >> > >> // fire up the shell and get echo'd results on stdout > >> try > >> { > >> l_proc = Runtime.getRuntime().exec( l_cmd.toString() ) ; > >> l_proc.waitFor() ; > >> l_in = new BufferedReader( > >> new InputStreamReader( l_proc.getInputStream() ) ) ; > >> l_value = l_in.readLine() ; > >> l_in.close() ; > >> } > >> catch( Throwable t ) > >> { > >> throw new EnvAccessException( a_name, t ) ; > >> } > >> finally > >> { > >> l_proc.destroy() ; > >> > >> try > >> { > >> if ( null != l_in ) > >> { > >> l_in.close() ; > >> } > >> } > >> catch( IOException e ) > >> { > >> > >> } > >> } > >> > >> if ( 0 == l_proc.exitValue() ) > >> { > >> // Handle situations where the env property does not exist. > >> if ( l_value.trim().equals( "" ) ) > >> { > >> return null ; > >> } > >> > >> return l_value ; > >> } > >> > >> throw new EnvAccessException( a_name, "Environment process failed with " > >> + "non-zero exit code of " + l_proc.exitValue() ) ; > >> } > >> > >> > >> /** > >> * Gets the value for a windows command shell environment variable. > >> * > >> * @param a_name the name of the variable > >> * @return the value of the variable > >> * @throws EnvAccessException if there is an error accessing the value > >> */ > >> private static String getWindowsVariable( String a_name ) > >> throws EnvAccessException > >> { > >> String l_value = null ; > >> Process l_proc = null ; > >> BufferedReader l_in = null ; > >> StringBuffer l_cmd = new StringBuffer() ; > >> String l_osName = System.getProperty( "os.name" ) ; > >> > >> // build the the command based on the shell used: cmd.exe or command.exe > >> if ( -1 != l_osName.indexOf( "98" ) || -1 != l_osName.indexOf( "95" ) ) > >> { > >> l_cmd.append( "command.exe /C echo %" ) ; > >> } > >> else > >> { > >> l_cmd.append( "cmd.exe /C echo %" ) ; > >> } > >> > >> l_cmd.append( a_name ) ; > >> l_cmd.append( '%' ) ; > >> > >> // fire up the shell and get echo'd results on stdout > >> try > >> { > >> l_proc = Runtime.getRuntime().exec( l_cmd.toString() ) ; > >> l_proc.waitFor() ; > >> l_in = new BufferedReader( > >> new InputStreamReader( l_proc.getInputStream() ) ) ; > >> l_value = l_in.readLine() ; > >> l_in.close() ; > >> } > >> catch( Throwable t ) > >> { > >> throw new EnvAccessException( a_name, t ) ; > >> } > >> finally > >> { > >> l_proc.destroy() ; > >> > >> try > >> { > >> if ( null != l_in ) > >> { > >> l_in.close() ; > >> } > >> } > >> catch( IOException e ) > >> { > >> > >> } > >> } > >> > >> if ( 0 == l_proc.exitValue() ) > >> { > >> // Handle situations where the env property does not exist. > >> if ( l_value.startsWith( "%") && l_value.endsWith( "%" ) ) > >> { > >> return null ; > >> } > >> > >> return l_value ; > >> } > >> > >> throw new EnvAccessException( a_name, "Environment process failed with " > >> + "non-zero exit code of " + l_proc.exitValue() ) ; > >> } > >>} > >> > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >>/* > >> > >> ============================================================================ > >> The Apache Software License, Version 1.1 > >> ============================================================================ > >> > >> Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. > >> > >> Redistribution and use in source and binary forms, with or without modifica- > >> tion, 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", "Apache Avalon", "Avalon Framework" 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 (INCLU- > >> DING, 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/>. > >> > >>*/ > >> > >>/** > >> * A simple wrapper exception around exceptions that could occur while accessing > >> * environment parameters. > >> * > >> * @author <a href="mailto:[EMAIL PROTECTED]">Alex Karasulu</a> > >> * @author $Author$ > >> * @version $Revision$ > >> */ > >>public class EnvAccessException extends Exception > >>{ > >> /** the environment variable name if available */ > >> public final String m_variable ; > >> > >> > >> /** > >> * Creates an exception denoting a failure while attempting to access an > >> * environment variable within an operating system and shell specific > >> * environment that is caused by another exception. > >> * > >> * @param a_variable the variable whose value was to be accessed > >> * @param a_message the reason for the access failure > >> * @param a_cause the underlying exception that caused the failure > >> */ > >> EnvAccessException( final String a_variable, final Throwable a_cause ) > >> { > >> super( a_cause ) ; > >> > >> m_variable = a_variable ; > >> } > >> > >> > >> /** > >> * Creates an exception denoting a failure while attempting to access an > >> * environment variable within an operating system and shell specific > >> * environment. > >> * > >> * @param a_variable the variable whose value was to be accessed > >> * @param a_message the reason for the access failure > >> * @param a_cause the underlying exception that caused the failure > >> */ > >> EnvAccessException( final String a_variable, final String a_message ) > >> { > >> super( a_message ) ; > >> > >> m_variable = a_variable ; > >> } > >> > >> > >> /** > >> * Gets the variable that was to be accessed. > >> * > >> * @return the value of the variable > >> */ > >> public String getVariable() > >> { > >> return m_variable ; > >> } > >> > >> > >> /** > >> * Prepends variable name to the base message. > >> * > >> * @see java.lang.Throwable#getMessage() > >> */ > >> public String getMessage() > >> { > >> String l_base = super.getMessage() ; > >> > >> if ( null == l_base ) > >> { > >> return "Failed to access " + m_variable + " environment variable" ; > >> } > >> > >> return "Failed to access " + m_variable > >> + " environment variable - " + l_base ; > >> } > >>} > >> > >> > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >>--------------------------------------------------------------------- > >>To unsubscribe, e-mail: [EMAIL PROTECTED] > >>For additional commands, e-mail: [EMAIL PROTECTED] > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >>--------------------------------------------------------------------- > >>To unsubscribe, e-mail: [EMAIL PROTECTED] > >>For additional commands, e-mail: [EMAIL PROTECTED] > >> > > -- > > Stephen J. McConnell > mailto:[EMAIL PROTECTED] > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
