| 1 | |
package org.jtheque.core.managers.module.loaders; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.jtheque.core.managers.Managers; |
| 20 | |
import org.jtheque.core.managers.log.ILoggingManager; |
| 21 | |
import org.jtheque.utils.io.FileUtils; |
| 22 | |
|
| 23 | |
import java.io.File; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.util.jar.JarFile; |
| 26 | |
import java.util.jar.Manifest; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public final class SystemModuleLoader implements IModuleLoader { |
| 34 | 0 | private static final IModuleLoader INSTANCE = new SystemModuleLoader(); |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
private SystemModuleLoader() { |
| 40 | 0 | super(); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public static IModuleLoader get() { |
| 49 | 0 | return INSTANCE; |
| 50 | |
} |
| 51 | |
|
| 52 | |
@Override |
| 53 | |
public Object installModule(File file) { |
| 54 | 0 | if (isValidModuleFile(file)) { |
| 55 | 0 | File modulesFolder = Managers.getCore().getFolders().getModulesFolder(); |
| 56 | |
|
| 57 | 0 | boolean inDirectory = FileUtils.putFileInDirectoryIfNot(file, modulesFolder); |
| 58 | |
|
| 59 | 0 | if (inDirectory) { |
| 60 | 0 | return loadModule(file); |
| 61 | |
} |
| 62 | |
} |
| 63 | |
|
| 64 | 0 | return null; |
| 65 | |
} |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private boolean isValidModuleFile(File file) { |
| 74 | 0 | boolean valid = true; |
| 75 | |
|
| 76 | 0 | if (file.isFile()) { |
| 77 | 0 | JarFile jarFile = null; |
| 78 | |
try { |
| 79 | 0 | jarFile = new JarFile(file); |
| 80 | |
|
| 81 | 0 | Manifest manifest = jarFile.getManifest(); |
| 82 | |
|
| 83 | 0 | if (manifest == null) { |
| 84 | 0 | valid = false; |
| 85 | |
} else { |
| 86 | 0 | String moduleClassName = manifest.getMainAttributes().getValue("ModuleContainer-Class"); |
| 87 | |
|
| 88 | 0 | if (moduleClassName == null) { |
| 89 | 0 | valid = false; |
| 90 | |
} |
| 91 | |
} |
| 92 | 0 | } catch (IOException e) { |
| 93 | 0 | valid = false; |
| 94 | |
} finally { |
| 95 | 0 | if (jarFile != null) { |
| 96 | |
try { |
| 97 | 0 | jarFile.close(); |
| 98 | 0 | } catch (IOException e) { |
| 99 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
| 100 | 0 | } |
| 101 | |
} |
| 102 | |
} |
| 103 | 0 | } else { |
| 104 | 0 | valid = false; |
| 105 | |
} |
| 106 | |
|
| 107 | 0 | return valid; |
| 108 | |
} |
| 109 | |
|
| 110 | |
@Override |
| 111 | |
public Object loadModule(File file) { |
| 112 | 0 | Object module = null; |
| 113 | |
|
| 114 | 0 | JarFile jarFile = null; |
| 115 | |
try { |
| 116 | 0 | jarFile = new JarFile(file); |
| 117 | |
|
| 118 | 0 | Manifest manifest = jarFile.getManifest(); |
| 119 | |
|
| 120 | 0 | if (manifest != null) { |
| 121 | 0 | String moduleClassName = manifest.getMainAttributes().getValue("Module-Context"); |
| 122 | |
|
| 123 | 0 | if (moduleClassName != null) { |
| 124 | 0 | ModuleLoader.addResource(file.toURI().toURL()); |
| 125 | |
|
| 126 | 0 | module = loadModuleClass(moduleClassName); |
| 127 | |
} |
| 128 | |
} |
| 129 | 0 | } catch (IOException e) { |
| 130 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
| 131 | |
} finally { |
| 132 | 0 | if (jarFile != null) { |
| 133 | |
try { |
| 134 | 0 | jarFile.close(); |
| 135 | 0 | } catch (IOException e) { |
| 136 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
| 137 | 0 | } |
| 138 | |
} |
| 139 | |
} |
| 140 | |
|
| 141 | 0 | return module; |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
private Object loadModuleClass(String moduleClassName) { |
| 151 | 0 | Object module = null; |
| 152 | |
|
| 153 | |
try { |
| 154 | 0 | Class<?> moduleClass = Class.forName(moduleClassName, true, ClassLoader.getSystemClassLoader()); |
| 155 | |
|
| 156 | 0 | module = moduleClass.newInstance(); |
| 157 | 0 | } catch (ClassNotFoundException e) { |
| 158 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
| 159 | 0 | } catch (InstantiationException e) { |
| 160 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
| 161 | 0 | } catch (IllegalAccessException e) { |
| 162 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
| 163 | 0 | } |
| 164 | |
|
| 165 | 0 | return module; |
| 166 | |
} |
| 167 | |
} |