001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs;
018:
019: import java.lang.reflect.InvocationTargetException;
020: import java.lang.reflect.Method;
021:
022: /**
023: * The main entry point for the VFS. Used to create {@link FileSystemManager}
024: * instances.
025: *
026: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
027: * @version $Revision: 537839 $ $Date: 2007-05-14 06:44:20 -0700 (Mon, 14 May 2007) $
028: */
029: public class VFS {
030: private static Boolean URI_STYLE = null;
031:
032: private static FileSystemManager instance;
033:
034: private VFS() {
035: }
036:
037: /**
038: * Returns the default {@link FileSystemManager} instance
039: */
040: public static synchronized FileSystemManager getManager()
041: throws FileSystemException {
042: if (instance == null) {
043: instance = createManager("org.apache.commons.vfs.impl.StandardFileSystemManager");
044: }
045: return instance;
046: }
047:
048: /**
049: * Creates a file system manager instance.
050: */
051: private static FileSystemManager createManager(
052: final String managerClassName) throws FileSystemException {
053: try {
054: // Create instance
055: final Class mgrClass = Class.forName(managerClassName);
056: final FileSystemManager mgr = (FileSystemManager) mgrClass
057: .newInstance();
058:
059: /*
060: try
061: {
062: // Set the logger
063: final Method setLogMethod = mgrClass.getMethod("setLogger", new Class[]{Log.class});
064: final Log logger = LogFactory.getLog(VFS.class);
065: setLogMethod.invoke(mgr, new Object[]{logger});
066: }
067: catch (final NoSuchMethodException e)
068: {
069: // Ignore; don't set the logger
070: }
071: */
072:
073: try {
074: // Initialise
075: final Method initMethod = mgrClass.getMethod("init",
076: (Class[]) null);
077: initMethod.invoke(mgr, (Object[]) null);
078: } catch (final NoSuchMethodException e) {
079: // Ignore; don't initialize
080: }
081:
082: return mgr;
083: } catch (final InvocationTargetException e) {
084: throw new FileSystemException("vfs/create-manager.error",
085: managerClassName, e.getTargetException());
086: } catch (final Exception e) {
087: throw new FileSystemException("vfs/create-manager.error",
088: managerClassName, e);
089: }
090: }
091:
092: public static boolean isUriStyle() {
093: if (URI_STYLE == null) {
094: URI_STYLE = Boolean.FALSE;
095: }
096: return URI_STYLE.booleanValue();
097: }
098:
099: public static void setUriStyle(boolean uriStyle) {
100: if (URI_STYLE != null && URI_STYLE.booleanValue() != uriStyle) {
101: throw new IllegalStateException("URI STYLE ALREADY SET TO");
102: }
103: URI_STYLE = uriStyle ? Boolean.TRUE : Boolean.FALSE;
104: }
105: }
|