001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2;
018
019import java.lang.reflect.InvocationTargetException;
020import java.lang.reflect.Method;
021
022/**
023 * The main entry point for the VFS.  Used to create {@link FileSystemManager}
024 * instances.
025 */
026public final class VFS
027{
028    /** The URI style */
029    private static Boolean uriStyle;
030
031    /** The FileSystemManager */
032    private static FileSystemManager instance;
033
034    private VFS()
035    {
036    }
037
038    /**
039     * Returns the default {@link FileSystemManager} instance.
040     * <p>
041     * Warning, if you close this instance you may affect all current
042     * and future users of this manager singleton.
043     *
044     * @return The FileSystemManager.
045     * @throws FileSystemException if an error occurs creating the manager.
046     */
047    public static synchronized FileSystemManager getManager()
048        throws FileSystemException
049    {
050        if (instance == null)
051        {
052            instance = createManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
053        }
054        return instance;
055    }
056
057    /**
058     * Creates a file system manager instance.
059     * @param managerClassName The specific manager impelmentation class name.
060     * @return The FileSystemManager.
061     * @throws FileSystemException if an error occurs creating the manager.
062     */
063    private static FileSystemManager createManager(final String managerClassName)
064        throws FileSystemException
065    {
066        try
067        {
068            // Create instance
069            final Class<?> mgrClass = Class.forName(managerClassName);
070            final FileSystemManager mgr = (FileSystemManager) mgrClass.newInstance();
071
072            try
073            {
074                // Initialize
075                final Method initMethod = mgrClass.getMethod("init", (Class[]) null);
076                initMethod.invoke(mgr, (Object[]) null);
077            }
078            catch (final NoSuchMethodException ignored)
079            {
080                /* Ignore; don't initialize. */
081            }
082
083            return mgr;
084        }
085        catch (final InvocationTargetException e)
086        {
087            throw new FileSystemException("vfs/create-manager.error",
088                managerClassName,
089                e.getTargetException());
090        }
091        catch (final Exception e)
092        {
093            throw new FileSystemException("vfs/create-manager.error",
094                managerClassName,
095                e);
096        }
097    }
098
099    public static boolean isUriStyle()
100    {
101        if (uriStyle == null)
102        {
103            uriStyle = Boolean.FALSE;
104        }
105        return uriStyle.booleanValue();
106    }
107
108    public static void setUriStyle(final boolean uriStyle)
109    {
110        if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle)
111        {
112            throw new IllegalStateException("VFS.uriStyle was already set differently.");
113        }
114        VFS.uriStyle = Boolean.valueOf(uriStyle);
115    }
116}