View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  
22  import org.apache.commons.lang3.reflect.MethodUtils;
23  import org.apache.commons.vfs2.provider.AbstractFileSystem;
24  
25  /**
26   * The main entry point for the VFS. Used to create {@link FileSystemManager} instances.
27   */
28  public final class VFS {
29  
30      /** The FileSystemManager */
31      private static FileSystemManager instance;
32  
33      /** The URI style */
34      private static Boolean uriStyle;
35  
36      /**
37       * Closes the default {@link FileSystemManager} instance.
38       * <p>
39       * Warning, if you close the default instance, a new one will be created by {@link #getManager()}.
40       * </p>
41       *
42       * @since 2.8.0
43       */
44      public static synchronized void close() {
45          if (instance != null) {
46              instance.close();
47              instance = null;
48          }
49      }
50  
51      /**
52       * Creates a file system manager instance.
53       *
54       * @param managerClassName The specific manager implementation class name.
55       * @return The FileSystemManager.
56       * @throws FileSystemException if an error occurs creating the manager.
57       */
58      private static FileSystemManager createFileSystemManager(final String managerClassName) throws FileSystemException {
59          try {
60              // Create instance
61              final Class<FileSystemManager> clazz = (Class<FileSystemManager>) Class.forName(managerClassName);
62              final FileSystemManager manager = clazz.newInstance();
63              // Initialize
64              if (manager instanceof AbstractFileSystem) {
65                  ((AbstractFileSystem) manager).init();
66              } else {
67                  final Method method = MethodUtils.getMatchingMethod(clazz, "init");
68                  if (method != null) {
69                      method.invoke(manager, (Object[]) null);
70                  }
71              }
72              return manager;
73          } catch (final InvocationTargetException e) {
74              throw new FileSystemException("vfs/create-manager.error", managerClassName, e.getTargetException());
75          } catch (final Exception e) {
76              throw new FileSystemException("vfs/create-manager.error", managerClassName, e);
77          }
78      }
79  
80      /**
81       * Returns the default {@link FileSystemManager} instance.
82       * <p>
83       * Warning, if you close this instance you may affect all current and future users of this manager singleton.
84       * </p>
85       *
86       * @return The FileSystemManager.
87       * @throws FileSystemException if an error occurs creating the manager.
88       */
89      public static synchronized FileSystemManager getManager() throws FileSystemException {
90          if (instance == null) {
91              instance = reset();
92          }
93          return instance;
94      }
95  
96      public static boolean isUriStyle() {
97          if (uriStyle == null) {
98              uriStyle = Boolean.FALSE;
99          }
100         return uriStyle.booleanValue();
101     }
102 
103     /**
104      * Resets the FileSystemManager to the default.
105      *
106      * @return the new FileSystemManager.
107      * @throws FileSystemException if an error occurs creating the manager.
108      * @since 2.5.0
109      */
110     public static synchronized FileSystemManager reset() throws FileSystemException {
111         close();
112         return instance = createFileSystemManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
113     }
114 
115     /**
116      * Sets the file system manager
117      *
118      * @param manager the file system manager
119      * @since 2.2
120      */
121     public static synchronized void setManager(final FileSystemManager manager) {
122         VFS.instance = manager;
123     }
124 
125     public static void setUriStyle(final boolean uriStyle) {
126         if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle) {
127             throw new IllegalStateException("VFS.uriStyle was already set differently.");
128         }
129         VFS.uriStyle = Boolean.valueOf(uriStyle);
130     }
131 
132     private VFS() {
133         // no public instantiation.
134     }
135 }