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