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     * @return The FileSystemManager.
041     * @throws FileSystemException if an error occurs creating the manager.
042     */
043    public static synchronized FileSystemManager getManager()
044        throws FileSystemException
045    {
046        if (instance == null)
047        {
048            instance = createManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
049        }
050        return instance;
051    }
052
053    /**
054     * Creates a file system manager instance.
055     * @param managerClassName The specific manager impelmentation class name.
056     * @return The FileSystemManager.
057     * @throws FileSystemException if an error occurs creating the manager.
058     */
059    private static FileSystemManager createManager(final String managerClassName)
060        throws FileSystemException
061    {
062        try
063        {
064            // Create instance
065            final Class<?> mgrClass = Class.forName(managerClassName);
066            final FileSystemManager mgr = (FileSystemManager) mgrClass.newInstance();
067
068            /*
069            try
070            {
071                // Set the logger
072                final Method setLogMethod = mgrClass.getMethod("setLogger", new Class[]{Log.class});
073                final Log logger = LogFactory.getLog(VFS.class);
074                setLogMethod.invoke(mgr, new Object[]{logger});
075            }
076            catch (final NoSuchMethodException e)
077            {
078                // Ignore; don't set the logger
079            }
080            */
081
082            try
083            {
084                // Initialize
085                final Method initMethod = mgrClass.getMethod("init", (Class[]) null);
086                initMethod.invoke(mgr, (Object[]) null);
087            }
088            catch (final NoSuchMethodException e)
089            {
090                // Ignore; don't initialize
091            }
092
093            return mgr;
094        }
095        catch (final InvocationTargetException e)
096        {
097            throw new FileSystemException("vfs/create-manager.error",
098                managerClassName,
099                e.getTargetException());
100        }
101        catch (final Exception e)
102        {
103            throw new FileSystemException("vfs/create-manager.error",
104                managerClassName,
105                e);
106        }
107    }
108
109    public static boolean isUriStyle()
110    {
111        if (uriStyle == null)
112        {
113            uriStyle = Boolean.FALSE;
114        }
115        return uriStyle.booleanValue();
116    }
117
118    public static void setUriStyle(final boolean uriStyle)
119    {
120        if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle)
121        {
122            throw new IllegalStateException("URI STYLE ALREADY SET TO");
123        }
124        VFS.uriStyle = uriStyle ? Boolean.TRUE : Boolean.FALSE;
125    }
126}