private static final Log log = LogFactory.getLog(VFS.class);
/** 记录 MyBatis 供的两个 VFS 实现类. */
public static final Class<?>[] IMPLEMENTATIONS = { JBoss6VFS.class, DefaultVFS.class };
/** 记录 用户自定义 VFS 实现类 VFS addimplClass ()方法会将指定的 VFS 实现对应的 Class 对象添加到USER IMPLEMENTATIONS 集合中 */
public static final List<Class<? extends VFS>> USER_IMPLEMENTATIONS = new ArrayList<>();
/** Singleton instance holder. */
private static class VFSHolder {
// 最终指定的实现类,采用单例模式,记录了全局唯一FVS对象
static final VFS INSTANCE = createVFS();
/**
* 给出一个VFS实现。单例模式
* @return VFS实现
*/
static VFS createVFS() {
// 所有VFS实现类的列表。
List<Class<? extends VFS>> impls = new ArrayList<>();
// 列表中先加入用户自定义的实现类。因此,用户自定义的实现类优先级高
impls.addAll(USER_IMPLEMENTATIONS);
impls.addAll(Arrays.asList((Class<? extends VFS>[]) IMPLEMENTATIONS));
VFS vfs = null;
// 依次生成实例,找出第一个可用的
for (int i = 0; vfs == null || !vfs.isValid(); i++) {
Class<? extends VFS> impl = impls.get(i);
try {
// 生成一个实现类的对象
vfs = impl.newInstance();
// 判断对象是否生成成功并可用
if (vfs == null || !vfs.isValid()) {
if (log.isDebugEnabled()) {
log.debug("VFS implementation " + impl.getName() +
" is not valid in this environment.");
}
}
} catch (InstantiationException | IllegalAccessException e) {
log.error("Failed to instantiate " + impl, e);
return null;
}
}
if (log.isDebugEnabled()) {
log.debug("Using VFS adapter " + vfs.getClass().getName());
}
return vfs;
}
}
/**
* Get the singleton {@link VFS} instance. If no {@link VFS} implementation can be found for the
* current environment, then this method returns null.
*/
public static VFS getInstance() {
return VFSHolder.INSTANCE;
}
/**
* Adds the specified class to the list of {@link VFS} implementations. Classes added in this
* manner are tried in the order they are added and before any of the built-in implementations.
*
* @param clazz The {@link VFS} implementation class to add.
*/
public static void addImplClass(Class<? extends VFS> clazz) {
if (clazz != null) {
USER_IMPLEMENTATIONS.add(clazz);
}
}
/** Get a class by name. If the class is not found then return null. */
protected static Class<?> getClass(String className) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(className);
// return ReflectUtil.findClass(className);
} catch (ClassNotFoundException e) {
if (log.isDebugEnabled()) {
log.debug("Class not found: " + className);
}
return null;
}
}
/**
* Get a method by name and parameter types. If the method is not found then return null.
*
* @param claz