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 */
017 package org.apache.commons.vfs2;
018
019 import java.lang.reflect.InvocationTargetException;
020 import java.lang.reflect.Method;
021
022 /**
023 * The main entry point for the VFS. Used to create {@link FileSystemManager}
024 * instances.
025 *
026 * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
027 */
028 public final class VFS
029 {
030 /** The URI style */
031 private static Boolean uriStyle;
032
033 /** The FileSystemManager */
034 private static FileSystemManager instance;
035
036 private VFS()
037 {
038 }
039
040 /**
041 * Returns the default {@link FileSystemManager} instance.
042 * @return The FileSystemManager.
043 * @throws FileSystemException if an error occurs creating the manager.
044 */
045 public static synchronized FileSystemManager getManager()
046 throws FileSystemException
047 {
048 if (instance == null)
049 {
050 instance = createManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
051 }
052 return instance;
053 }
054
055 /**
056 * Creates a file system manager instance.
057 * @param managerClassName The specific manager impelmentation class name.
058 * @return The FileSystemManager.
059 * @throws FileSystemException if an error occurs creating the manager.
060 */
061 private static FileSystemManager createManager(final String managerClassName)
062 throws FileSystemException
063 {
064 try
065 {
066 // Create instance
067 final Class<?> mgrClass = Class.forName(managerClassName);
068 final FileSystemManager mgr = (FileSystemManager) mgrClass.newInstance();
069
070 /*
071 try
072 {
073 // Set the logger
074 final Method setLogMethod = mgrClass.getMethod("setLogger", new Class[]{Log.class});
075 final Log logger = LogFactory.getLog(VFS.class);
076 setLogMethod.invoke(mgr, new Object[]{logger});
077 }
078 catch (final NoSuchMethodException e)
079 {
080 // Ignore; don't set the logger
081 }
082 */
083
084 try
085 {
086 // Initialise
087 final Method initMethod = mgrClass.getMethod("init", (Class[]) null);
088 initMethod.invoke(mgr, (Object[]) null);
089 }
090 catch (final NoSuchMethodException e)
091 {
092 // Ignore; don't initialize
093 }
094
095 return mgr;
096 }
097 catch (final InvocationTargetException e)
098 {
099 throw new FileSystemException("vfs/create-manager.error",
100 managerClassName,
101 e.getTargetException());
102 }
103 catch (final Exception e)
104 {
105 throw new FileSystemException("vfs/create-manager.error",
106 managerClassName,
107 e);
108 }
109 }
110
111 public static boolean isUriStyle()
112 {
113 if (uriStyle == null)
114 {
115 uriStyle = Boolean.FALSE;
116 }
117 return uriStyle.booleanValue();
118 }
119
120 public static void setUriStyle(boolean uriStyle)
121 {
122 if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle)
123 {
124 throw new IllegalStateException("URI STYLE ALREADY SET TO");
125 }
126 VFS.uriStyle = uriStyle ? Boolean.TRUE : Boolean.FALSE;
127 }
128 }