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} instances. 024 */ 025public final class VFS { 026 /** The URI style */ 027 private static Boolean uriStyle; 028 029 /** The FileSystemManager */ 030 private static FileSystemManager instance; 031 032 private VFS() { 033 } 034 035 /** 036 * Returns the default {@link FileSystemManager} instance. 037 * <p> 038 * Warning, if you close this instance you may affect all current and future users of this manager singleton. 039 * 040 * @return The FileSystemManager. 041 * @throws FileSystemException if an error occurs creating the manager. 042 */ 043 public static synchronized FileSystemManager getManager() throws FileSystemException { 044 if (instance == null) { 045 instance = createManager("org.apache.commons.vfs2.impl.StandardFileSystemManager"); 046 } 047 return instance; 048 } 049 050 /** 051 * Creates a file system manager instance. 052 * 053 * @param managerClassName The specific manager impelmentation class name. 054 * @return The FileSystemManager. 055 * @throws FileSystemException if an error occurs creating the manager. 056 */ 057 private static FileSystemManager createManager(final String managerClassName) throws FileSystemException { 058 try { 059 // Create instance 060 final Class<?> mgrClass = Class.forName(managerClassName); 061 final FileSystemManager mgr = (FileSystemManager) mgrClass.newInstance(); 062 063 try { 064 // Initialize 065 final Method initMethod = mgrClass.getMethod("init", (Class[]) null); 066 initMethod.invoke(mgr, (Object[]) null); 067 } catch (final NoSuchMethodException ignored) { 068 /* Ignore; don't initialize. */ 069 } 070 071 return mgr; 072 } catch (final InvocationTargetException e) { 073 throw new FileSystemException("vfs/create-manager.error", managerClassName, e.getTargetException()); 074 } catch (final Exception e) { 075 throw new FileSystemException("vfs/create-manager.error", managerClassName, e); 076 } 077 } 078 079 public static boolean isUriStyle() { 080 if (uriStyle == null) { 081 uriStyle = Boolean.FALSE; 082 } 083 return uriStyle.booleanValue(); 084 } 085 086 public static void setUriStyle(final boolean uriStyle) { 087 if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle) { 088 throw new IllegalStateException("VFS.uriStyle was already set differently."); 089 } 090 VFS.uriStyle = Boolean.valueOf(uriStyle); 091 } 092 093 /** 094 * Sets the file system manager 095 * 096 * @param manager the file system manager 097 * @since 2.2 098 */ 099 public static void setManager(final FileSystemManager manager) { 100 VFS.instance = manager; 101 } 102}