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  /**
23   * The main entry point for the VFS. Used to create {@link FileSystemManager} instances.
24   */
25  public final class VFS {
26  
27      /** The FileSystemManager */
28      private static FileSystemManager instance;
29  
30      /** The URI style */
31      private static Boolean uriStyle;
32  
33      /**
34       * Creates a file system manager instance.
35       *
36       * @param managerClassName The specific manager impelmentation class name.
37       * @return The FileSystemManager.
38       * @throws FileSystemException if an error occurs creating the manager.
39       */
40      private static FileSystemManager createFileSystemManager(final String managerClassName) throws FileSystemException {
41          try {
42              // Create instance
43              final Class<?> mgrClass = Class.forName(managerClassName);
44              final FileSystemManager/../org/apache/commons/vfs2/FileSystemManager.html#FileSystemManager">FileSystemManager mgr = (FileSystemManager) mgrClass.newInstance();
45  
46              try {
47                  // Initialize
48                  final Method initMethod = mgrClass.getMethod("init", (Class[]) null);
49                  initMethod.invoke(mgr, (Object[]) null);
50              } catch (final NoSuchMethodException ignored) {
51                  /* Ignore; don't initialize. */
52              }
53  
54              return mgr;
55          } catch (final InvocationTargetException e) {
56              throw new FileSystemException("vfs/create-manager.error", managerClassName, e.getTargetException());
57          } catch (final Exception e) {
58              throw new FileSystemException("vfs/create-manager.error", managerClassName, e);
59          }
60      }
61  
62      /**
63       * Returns the default {@link FileSystemManager} instance.
64       * <p>
65       * Warning, if you close this instance you may affect all current and future users of this manager singleton.
66       * </p>
67       *
68       * @return The FileSystemManager.
69       * @throws FileSystemException if an error occurs creating the manager.
70       */
71      public static synchronized FileSystemManager getManager() throws FileSystemException {
72          if (instance == null) {
73              instance = reset();
74          }
75          return instance;
76      }
77  
78      public static boolean isUriStyle() {
79          if (uriStyle == null) {
80              uriStyle = Boolean.FALSE;
81          }
82          return uriStyle.booleanValue();
83      }
84  
85      /**
86       * Resets the FileSystemManager to the default.
87       *
88       * @return the new FileSystemManager.
89       * @throws FileSystemException if an error occurs creating the manager.
90       * @since 2.5.0
91       */
92      public static FileSystemManager reset() throws FileSystemException {
93          if (instance != null) {
94              instance.close();
95          }
96          return instance = createFileSystemManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
97      }
98  
99      /**
100      * Sets the file system manager
101      *
102      * @param manager the file system manager
103      * @since 2.2
104      */
105     public static synchronized void setManager(final FileSystemManager manager) {
106         VFS.instance = manager;
107     }
108 
109     public static void setUriStyle(final boolean uriStyle) {
110         if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle) {
111             throw new IllegalStateException("VFS.uriStyle was already set differently.");
112         }
113         VFS.uriStyle = Boolean.valueOf(uriStyle);
114     }
115 
116     private VFS() {
117     }
118 }