⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configtools.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Gereon Fassbender, Christopher Kohlhaas               |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.framework.internal;

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.rapla.components.util.Assert;
import org.rapla.components.util.JNLPUtil;
import org.rapla.components.util.xml.XMLReaderAdapter;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaContextException;
import org.rapla.framework.RaplaException;

/** Tools for configuring the rapla-system. */
public abstract class ConfigTools
{
    /** parse startup parameters. The parse format:
     <pre>
     [-?|-c PATH_TO_CONFIG_FILE] [ACTION]
     </pre>
     Possible map entries:
     <ul>
     <li>config: the config-file</li>
     <li>action: the start action</li>
     </ul>

     @return a map with the parameter-entries or  null if format is invalid or -? is used
     */
    public static Map parseParams( String[] args )
    {
        boolean bInvalid = false;
        Map map = new HashMap();
        String config = null;
        String action = null;

        // Investigate the passed arguments
        for ( int i = 0; i < args.length; i++ )
        {
            if ( args[i].toLowerCase().equals( "-c" ) )
            {
                if ( i + 1 == args.length )
                {
                    bInvalid = true;
                    break;
                }
                config = args[++i];
                continue;
            }
            if ( args[i].toLowerCase().equals( "-?" ) )
            {
                bInvalid = true;
                break;
            }
            if ( args[i].toLowerCase().substring( 0, 1 ).equals( "-" ) )
            {
                bInvalid = true;
                break;
            }
            action = args[i].toLowerCase();
        }

        if ( bInvalid )
        {
            return null;
        }

        if ( config != null )
            map.put( "config", config );
        if ( action != null )
            map.put( "action", action );
        return map;
    }

    /** Creates a configuration from a URL.*/
    public static Configuration createConfig( String configURL ) throws RaplaException
    {
        try
        {
            DefaultConfigurationBuilder configurationBuilder = new DefaultConfigurationBuilder(
                                                                                                XMLReaderAdapter
                                                                                                                .createXMLReader( false ) );
            Configuration config = configurationBuilder.build( configURL );
            Assert.notNull( config );
            return config;
        }
        catch ( org.xml.sax.SAXParseException ex )
        {
            throw new RaplaException( "Error parsing configuration "
                    + configURL
                    + " Line:"
                    + ex.getLineNumber()
                    + " Column:"
                    + ex.getColumnNumber()
                    + " "
                    + ex.getMessage() );
        }
        catch ( EOFException ex )
        {
            throw new RaplaException( "Can't load configuration-file at " + configURL );
        }
        catch ( Exception ex )
        {
            throw new RaplaException( ex );
        }
    }

    /** Creates an configuration URL from a configuration path.
     If path is null the URL of the defaultPropertiesFile
     will be returned.
     */
    public static URL configFileToURL( String path, String defaultPropertiesFile ) throws RaplaException
    {
        URL configURL = null;
        try
        {
            if ( path != null )
            {
                File file = new File( path );
                if ( file.exists() )
                {
                    configURL = ( file.getCanonicalFile() ).toURI().toURL();
                }
            }
            if ( configURL == null )
            {
                configURL = ConfigTools.class.getClassLoader().getResource( defaultPropertiesFile );
                if ( configURL == null )
                {
                    File file = new File( defaultPropertiesFile );
                    if ( !file.exists() )
                    {
                        file = new File( "webapp/WEB-INF/" + defaultPropertiesFile );
                    }
                    if ( file.exists() )
                    {
                        configURL = file.getCanonicalFile().toURI().toURL();
                    }
                }
            }
        }
        catch ( MalformedURLException ex )
        {
            throw new RaplaException( "malformed config path" + path );
        }
        catch ( IOException ex )
        {
            throw new RaplaException( "Can't resolve config path" + path );
        }

        if ( configURL == null )
        {
            throw new RaplaException( defaultPropertiesFile
                    + " not found on classpath and in working folder "
                    + " Path config file with -c argument. "
                    + " For more information start rapla -? or read the api-docs." );
        }
        return configURL;
    }

    /** Creates an configuration URL from a configuration filename and
     the webstart codebae.
     If filename is null the URL of the defaultPropertiesFile
     will be returned.
     */
    public static URL webstartConfigToURL( String config, String defaultPropertiesFilename ) throws RaplaException
    {
        try
        {
            URL base = JNLPUtil.getCodeBase();
            URL configURL;
            if ( config != null && config.trim().length() > 0 )
            {
                return new URL( base, config );
            }
            else
            {
                configURL = new URL( base, defaultPropertiesFilename );
            }
            return configURL;
        }
        catch ( Exception ex )
        {
            throw new RaplaException( "Can't get configuration file in webstart mode." );
        }
    }

    /** resolves a context value in the passed string. 
     If the string begins with <code>${</code> the method will lookup the String-Object in the context and returns it.
     If it doesn't, the method returns the unmodified string.
     Example:
     <code>resolveContext("${download-server}")</code> returns the same as
     context.get("download-server").toString();

     @throws ConfigurationException when no contex-object was found for the given variable.
     */
    public static String resolveContext( String s, RaplaContext sm ) throws ConfigurationException
    {
        int startToken = s.indexOf( "${" );
        if ( startToken < 0 )
            return s;
        int endToken = s.indexOf( "}", startToken );
        String token = s.substring( startToken + 2, endToken );
        StringBuffer value = new StringBuffer();
        value.append( s.substring( 0, startToken ) );
        try
        {
            Object contextObject = sm.lookup( token );
            String stringRep = contextObject.toString();
            value.append( stringRep );
        }
        catch ( RaplaContextException ex )
        {
            throw new ConfigurationException( ex.getMessage(), ex );
        }
        value.append( s.substring( endToken + 1 ) );
        return value.toString();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -