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

📄 nullconfigurator.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
字号:
/* *  Copyright (c) 2001-2003 Sun Microsystems, Inc.  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 *  Sun Microsystems, Inc. for Project JXTA." *  Alternately, this acknowledgment may appear in the software itself, *  if and wherever such third-party acknowledgments normally appear. * *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" *  must not be used to endorse or promote products derived from this *  software without prior written permission. For written *  permission, please contact Project JXTA at http://www.jxta.org. * *  5. Products derived from this software may not be called "JXTA", *  nor may "JXTA" appear in their name, without prior written *  permission of Sun. * *  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 SUN MICROSYSTEMS 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 Project JXTA.  For more *  information on Project JXTA, please see *  <http://www.jxta.org/>. * *  This license is based on the BSD license adopted by the Apache Foundation. * *  $Id: NullConfigurator.java,v 1.10 2006/02/22 01:29:42 bondolo Exp $ */package net.jxta.impl.peergroup;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Reader;import java.net.URI;import java.util.StringTokenizer;import java.io.FileNotFoundException;import java.io.IOException;import org.apache.log4j.Level;import org.apache.log4j.Logger;import net.jxta.document.AdvertisementFactory;import net.jxta.document.XMLDocument;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.protocol.ConfigParams;import net.jxta.exception.ConfiguratorException;import net.jxta.impl.protocol.PlatformConfig;/** * A minimal Platform Configurator. This implementation can load a * configuration from an existing PlatformConfig file and also save a * configuration to the PlatformConfig file. * * <p/>This configurator provides no explict validation of the PlatformConfig * as it is read from the file (Some is done by the PlatformConfig class) and * provides no mechanism for reconfiguration. The NullConfigurator provides a * useful base implementation for extending your own Configurator and also * provides the minimal implementation needed for applications which perform * their own configuration. **/public class NullConfigurator implements PlatformConfigurator {        /**     *  Log4J logger     **/    private final static transient Logger LOG = Logger.getLogger(NullConfigurator.class.getName());        /**     *  The location in which the configuration files will reside.     **/    protected final URI jxtaHome;        /**     *  The file in which contains the platform configurtation.     **/    protected final URI configFile;        /**     *  The platform config     **/    protected PlatformConfig advertisement = null;        /**     *  Constructor for the NullConfigurator     *     *  @param homeRoot The location in which the configuration files will reside.     *  @throws ConfiguratorException If there is a problem accessing the configuration information.     **/    public NullConfigurator( URI homeRoot ) throws ConfiguratorException {        if( !homeRoot.isAbsolute() ) {            throw new IllegalArgumentException( "homeRoot must be an absoluteURI" );        }                jxtaHome = homeRoot;                if (LOG.isEnabledFor(Level.INFO)) {            LOG.info("JXTA_HOME = " + jxtaHome.toASCIIString() );        }                if( "file".equalsIgnoreCase( jxtaHome.getScheme() ) ) {            File jxtaHomeDir = new File( jxtaHome );                        if( jxtaHomeDir.exists() && !jxtaHomeDir.isDirectory() ) {                throw new IllegalArgumentException( "'" + jxtaHomeDir + "' is not a directory." );            }                    if( !jxtaHomeDir.exists() ) {                if( !jxtaHomeDir.mkdirs() ) {                    throw new IllegalStateException( "Could not create '" + jxtaHomeDir + "'." );                }            }                        configFile = new File( jxtaHomeDir, "PlatformConfig" ).toURI();        } else {                configFile = jxtaHome.resolve( "PlatformConfig" );        }    }        /**     * @inheritDoc     **/    public PlatformConfig getPlatformConfig() throws ConfiguratorException {        advertisement = (PlatformConfig) load();                // Set the global debug level.        adjustLog4JPriority();                return advertisement;    }        /**     * @inheritDoc     **/    public final void setPlatformConfig(PlatformConfig config) {        advertisement = (PlatformConfig) config;                // Set the global debug level.        adjustLog4JPriority();    }        /**     * @inheritDoc     **/    public ConfigParams getConfigParams() throws ConfiguratorException {        return getPlatformConfig();    }        /**     * @inheritDoc     **/    public void setConfigParams(ConfigParams cp) {        setPlatformConfig( (PlatformConfig) cp );    }        /**     * @inheritDoc     **/    public void setReconfigure(boolean reconfigure) {        // This implementation doesn't do configuration so ignores this operation.    }        /**     * @inheritDoc     **/    public boolean isReconfigure() {        return false;    }        /**     *  {@inheritDoc}     **/    public ConfigParams load()  throws ConfiguratorException {        return load(configFile);    }        /**     *  {@inheritDoc}     **/    protected PlatformConfig load( URI loadFile ) throws ConfiguratorException {        if( LOG.isEnabledFor(Level.DEBUG) ) {            LOG.debug( "Reading Platform Config from : " + loadFile );        }                InputStream advStream = null;        try {            advStream = loadFile.toURL().openStream();                        XMLDocument xmlDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument( MimeMediaType.XMLUTF8, advStream );            PlatformConfig result = (PlatformConfig) AdvertisementFactory.newAdvertisement( xmlDoc );                        if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("Recovered Platform Config from : " + loadFile);            }                        return result;        } catch (FileNotFoundException e) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("Platform Config not found : " + loadFile );            }                        return null;        } catch (Exception e) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("Failed to Recover '" + loadFile + "' due to : ", e);            }                        throw new ConfiguratorException( "Failed to recover PlatformConfig", e );        } finally {            try {                if (advStream != null) {                    advStream.close();                }                advStream = null;            } catch (Exception ignored ) {                ;            }        }    }        /**     *  {@inheritDoc}     **/    public boolean save() throws ConfiguratorException {        return save(configFile);    }        /**     *  {@inheritDoc}     **/    protected boolean save(URI saveFile ) throws ConfiguratorException {                // Save the adv as input for future reconfiguration        OutputStream out = null;        try {            XMLDocument aDoc = (XMLDocument) advertisement.getDocument(MimeMediaType.XMLUTF8);                        if( "file".equalsIgnoreCase( saveFile.getScheme() ) ){                out = new FileOutputStream( new File( saveFile ) );            } else {                out = saveFile.toURL().openConnection().getOutputStream();            }                        OutputStreamWriter os = new OutputStreamWriter(out, "UTF-8");                        aDoc.sendToWriter( os );            os.flush();        } catch (IOException e) {            if ( LOG.isEnabledFor(Level.WARN) ) {                LOG.warn("Could not save to : " + saveFile, e );            }                        throw new ConfiguratorException( "Could not save to : " + saveFile, e );        } finally {            try {                if( null != out ) {                    out.close();                }            } catch (Exception ignored ) {;}            out = null;        }                return true;    }        /**     * Adjust the log4j priority based on the user's configuration file.     * If the configuration is not set or the value is "user default",     * then don't change it.     **/    protected void adjustLog4JPriority() {        if (advertisement == null || advertisement.getDebugLevel() == null) {            if (LOG.isEnabledFor(Level.INFO)) {                LOG.info("Log4J logging preference not set, using defaults");            }            return;        }                String requestedLevel = advertisement.getDebugLevel();        if ("user default".equals(requestedLevel)) {            if (LOG.isEnabledFor(Level.INFO)) {                LOG.info("Log4J [user default] requested, not adjusting logging priority");            }            return;        }                if (LOG.isEnabledFor(Level.INFO)) {            LOG.info("Setting Log4J priority to [" + requestedLevel + "] based on the user's configuration");        }        Logger jxtaLogger = Logger.getLogger("net.jxta");        jxtaLogger.setLevel(Level.toLevel(requestedLevel));    }}

⌨️ 快捷键说明

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