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.getConstructor().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       * Gets 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      /**
97       * TODO.
98       *
99       * @return TODO.
100      */
101     public static boolean isUriStyle() {
102         if (uriStyle == null) {
103             uriStyle = Boolean.FALSE;
104         }
105         return uriStyle.booleanValue();
106     }
107 
108     /**
109      * Resets the FileSystemManager to the default.
110      *
111      * @return the new FileSystemManager.
112      * @throws FileSystemException if an error occurs creating the manager.
113      * @since 2.5.0
114      */
115     public static synchronized FileSystemManager reset() throws FileSystemException {
116         close();
117         return instance = createFileSystemManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
118     }
119 
120     /**
121      * Sets the file system manager.
122      *
123      * @param manager the file system manager
124      * @since 2.2
125      */
126     public static synchronized void setManager(final FileSystemManager manager) {
127         instance = manager;
128     }
129 
130     /**
131      * TODO.
132      *
133      * @param uriStyle TODO.
134      */
135     public static void setUriStyle(final boolean uriStyle) {
136         if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle) {
137             throw new IllegalStateException("VFS.uriStyle was already set differently.");
138         }
139         VFS.uriStyle = Boolean.valueOf(uriStyle);
140     }
141 
142     private VFS() {
143         // no public instantiation.
144     }
145 }