All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jcifs.context.SingletonContext Maven / Gradle / Ivy

There is a newer version: 2.1.10
Show newest version
/*
 * © 2016 AgNO3 Gmbh & Co. KG
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package jcifs.context;


import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import jcifs.CIFSContext;
import jcifs.CIFSException;
import jcifs.RuntimeCIFSException;
import jcifs.config.PropertyConfiguration;


/**
 * Global singleton context
 * 
 * @author mbechler
 *
 */
public class SingletonContext extends BaseContext implements CIFSContext {

    private static final Logger log = Logger.getLogger(SingletonContext.class);
    private static SingletonContext INSTANCE;


    /**
     * Get singleton context
     * 
     * The singleton context will use system properties for configuration as well as values specified in a file
     * specified through this jcifs.properties system property.
     * 
     * @return a global context, initialized on first call
     */
    public static synchronized final SingletonContext getInstance () {
        if ( INSTANCE == null ) {
            try {
                log.debug("Initializing singleton context");
                Properties p = new Properties();
                try {
                    String filename = System.getProperty("jcifs.properties");
                    if ( filename != null && filename.length() > 1 ) {

                        try ( FileInputStream in = new FileInputStream(filename) ) {
                            p.load(in);
                        }
                    }

                }
                catch ( IOException ioe ) {
                    log.error("Failed to load config", ioe); //$NON-NLS-1$
                }

                p.putAll(System.getProperties());
                INSTANCE = new SingletonContext(p);
            }
            catch ( CIFSException e ) {
                log.error("Failed to create singleton JCIFS context", e);
            }
        }

        return INSTANCE;
    }


    /**
     * This static method registers the SMB URL protocol handler which is
     * required to use SMB URLs with the java.net.URL class. If this
     * method is not called before attempting to create an SMB URL with the
     * URL class the following exception will occur:
     * 
* *
     * Exception MalformedURLException: unknown protocol: smb
     *     at java.net.URL.<init>(URL.java:480)
     *     at java.net.URL.<init>(URL.java:376)
     *     at java.net.URL.<init>(URL.java:330)
     *     at jcifs.smb.SmbFile.<init>(SmbFile.java:355)
     *     ...
     * 
* *
* */ public static void registerSmbURLHandler () { String pkgs; float ver = Float.parseFloat(Runtime.class.getPackage().getSpecificationVersion()); if ( ver < 1.7f ) { throw new RuntimeCIFSException("jcifs-ng requires Java 1.7 or above. You are running " + ver); } SingletonContext.getInstance(); pkgs = System.getProperty("java.protocol.handler.pkgs"); if ( pkgs == null ) { System.setProperty("java.protocol.handler.pkgs", "jcifs"); } else if ( pkgs.indexOf("jcifs") == -1 ) { pkgs += "|jcifs"; System.setProperty("java.protocol.handler.pkgs", pkgs); } } /** * */ private SingletonContext ( Properties p ) throws CIFSException { super(new PropertyConfiguration(p)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy