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.vfs;
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}
24   * instances.
25   *
26   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
28   */
29  public class VFS
30  {
31      private static Boolean URI_STYLE = null;
32  
33      private static FileSystemManager instance;
34  
35      private VFS()
36      {
37      }
38  
39      /***
40       * Returns the default {@link FileSystemManager} instance
41       */
42      public static synchronized FileSystemManager getManager()
43          throws FileSystemException
44      {
45          if (instance == null)
46          {
47              instance = createManager("org.apache.commons.vfs.impl.StandardFileSystemManager");
48          }
49          return instance;
50      }
51  
52      /***
53       * Creates a file system manager instance.
54       */
55      private static FileSystemManager createManager(final String managerClassName)
56          throws FileSystemException
57      {
58          try
59          {
60              // Create instance
61              final Class mgrClass = Class.forName(managerClassName);
62              final FileSystemManager mgr = (FileSystemManager) mgrClass.newInstance();
63  
64              /*
65              try
66              {
67                  // Set the logger
68                  final Method setLogMethod = mgrClass.getMethod("setLogger", new Class[]{Log.class});
69                  final Log logger = LogFactory.getLog(VFS.class);
70                  setLogMethod.invoke(mgr, new Object[]{logger});
71              }
72              catch (final NoSuchMethodException e)
73              {
74                  // Ignore; don't set the logger
75              }
76              */
77  
78              try
79              {
80                  // Initialise
81                  final Method initMethod = mgrClass.getMethod("init", null);
82                  initMethod.invoke(mgr, null);
83              }
84              catch (final NoSuchMethodException e)
85              {
86                  // Ignore; don't initialize
87              }
88  
89              return mgr;
90          }
91          catch (final InvocationTargetException e)
92          {
93              throw new FileSystemException("vfs/create-manager.error",
94                  managerClassName,
95                  e.getTargetException());
96          }
97          catch (final Exception e)
98          {
99              throw new FileSystemException("vfs/create-manager.error",
100                 managerClassName,
101                 e);
102         }
103     }
104 
105     public static boolean isUriStyle()
106     {
107         if (URI_STYLE == null)
108         {
109             URI_STYLE = Boolean.FALSE;
110         }
111         return URI_STYLE.booleanValue();
112     }
113 
114     public static void setUriStyle(boolean uriStyle)
115     {
116         if (URI_STYLE != null && URI_STYLE.booleanValue() != uriStyle)
117         {
118             throw new IllegalStateException("URI STYLE ALREADY SET TO");
119         }
120         URI_STYLE = uriStyle ? Boolean.TRUE : Boolean.FALSE;
121     }
122 }