1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28 public final class VFS {
29
30
31 private static FileSystemManager instance;
32
33
34 private static Boolean uriStyle;
35
36
37
38
39
40
41
42
43
44 public static synchronized void close() {
45 if (instance != null) {
46 instance.close();
47 instance = null;
48 }
49 }
50
51
52
53
54
55
56
57
58 private static FileSystemManager createFileSystemManager(final String managerClassName) throws FileSystemException {
59 try {
60
61 final Class<FileSystemManager> clazz = (Class<FileSystemManager>) Class.forName(managerClassName);
62 final FileSystemManager manager = clazz.newInstance();
63
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
82
83
84
85
86
87
88
89 public static synchronized FileSystemManager getManager() throws FileSystemException {
90 if (instance == null) {
91 instance = reset();
92 }
93 return instance;
94 }
95
96 public static boolean isUriStyle() {
97 if (uriStyle == null) {
98 uriStyle = Boolean.FALSE;
99 }
100 return uriStyle.booleanValue();
101 }
102
103
104
105
106
107
108
109
110 public static synchronized FileSystemManager reset() throws FileSystemException {
111 close();
112 return instance = createFileSystemManager("org.apache.commons.vfs2.impl.StandardFileSystemManager");
113 }
114
115
116
117
118
119
120
121 public static synchronized void setManager(final FileSystemManager manager) {
122 VFS.instance = manager;
123 }
124
125 public static void setUriStyle(final boolean uriStyle) {
126 if (VFS.uriStyle != null && VFS.uriStyle.booleanValue() != uriStyle) {
127 throw new IllegalStateException("VFS.uriStyle was already set differently.");
128 }
129 VFS.uriStyle = Boolean.valueOf(uriStyle);
130 }
131
132 private VFS() {
133
134 }
135 }