DynamicExtension.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.util;
  16. import java.io.FileInputStream;
  17. import java.io.FileNotFoundException;
  18. import java.util.Properties;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import net.sf.l2j.Config;
  23. /**
  24. * extension loader for l2j
  25. * @author galun
  26. * @version $Id: DynamicExtension.java,v 1.3 2006/05/14 17:19:39 galun Exp $
  27. */
  28. public class DynamicExtension {
  29. private static Logger _log = Logger.getLogger(DynamicExtension.class.getCanonicalName());
  30. private JarClassLoader _classLoader;
  31. private Properties _prop;
  32. private ConcurrentHashMap<String, Object> _loadedExtensions;
  33. private static DynamicExtension _instance;
  34. private ConcurrentHashMap<String, ExtensionFunction> _getters;
  35. private ConcurrentHashMap<String, ExtensionFunction> _setters;
  36. /**
  37. * create an instance of DynamicExtension
  38. * this will be done by GameServer according to the altsettings.properties
  39. *
  40. */
  41. private DynamicExtension() {
  42. if (_instance == null)
  43. _instance = this;
  44. _getters = new ConcurrentHashMap<String, ExtensionFunction>();
  45. _setters = new ConcurrentHashMap<String, ExtensionFunction>();
  46. initExtensions();
  47. }
  48. /**
  49. * get the singleton of DynamicInstance
  50. * @return the singleton instance
  51. */
  52. public static DynamicExtension getInstance() {
  53. if (_instance == null)
  54. _instance = new DynamicExtension();
  55. return _instance;
  56. }
  57. /**
  58. * get an extension object by class name
  59. * @param className he class name as defined in the extension properties
  60. * @return the object or null if not found
  61. */
  62. public Object getExtension(String className) {
  63. return _loadedExtensions.get(className);
  64. }
  65. /**
  66. * initialize all configured extensions
  67. *
  68. */
  69. public String initExtensions() {
  70. _prop = new Properties();
  71. String res = "";
  72. _loadedExtensions = new ConcurrentHashMap<String, Object>();
  73. try {
  74. _prop.load(new FileInputStream(Config.EXTENSIONS_CONFIG_FILE));
  75. } catch (FileNotFoundException ex) {
  76. _log.info(ex.getMessage() + ": no extensions to load");
  77. } catch (Exception ex) {
  78. _log.log(Level.WARNING, "could not load properties", ex);
  79. }
  80. _classLoader = new JarClassLoader();
  81. for (Object o : _prop.keySet()) {
  82. String k = (String)o;
  83. if (k.endsWith("Class")) {
  84. res += initExtension(_prop.getProperty(k)) + "\n";
  85. }
  86. }
  87. return res;
  88. }
  89. /**
  90. * init a named extension
  91. * @param name the class name and optionally a jar file name delimited with a '@' if the jar file is not
  92. * in the class path
  93. */
  94. public String initExtension(String name) {
  95. String className = name;
  96. String[] p = name.split("@");
  97. String res = name + " loaded";
  98. if (p.length > 1) {
  99. _classLoader.addJarFile(p[1]);
  100. className = p[0];
  101. }
  102. if (_loadedExtensions.containsKey(className))
  103. return "already loaded";
  104. try {
  105. Class<?> extension = Class.forName(className, true, _classLoader);
  106. Object obj = extension.newInstance();
  107. extension.getMethod("init", new Class[0]).invoke(obj, new Object[0]);
  108. _log.info("Extension " + className + " loaded.");
  109. _loadedExtensions.put(className, obj);
  110. } catch (Exception ex) {
  111. _log.log(Level.WARNING, name, ex);
  112. res = ex.toString();
  113. }
  114. return res;
  115. }
  116. /**
  117. * create a new class loader which resets the cache (jar files and loaded classes)
  118. * on next class loading request it will read the jar again
  119. */
  120. protected void clearCache() {
  121. _classLoader = new JarClassLoader();
  122. }
  123. /**
  124. * call unloadExtension() for all known extensions
  125. *
  126. */
  127. public String unloadExtensions() {
  128. String res = "";
  129. for (String e : _loadedExtensions.keySet())
  130. res += unloadExtension(e) + "\n";
  131. return res;
  132. }
  133. /**
  134. * get all loaded extensions
  135. * @return a String array with the class names
  136. */
  137. public String[] getExtensions() {
  138. String[] l = new String[_loadedExtensions.size()];
  139. _loadedExtensions.keySet().toArray(l);
  140. return l;
  141. }
  142. /**
  143. * unload a named extension
  144. * @param name the class name and optionally a jar file name delimited with a '@'
  145. */
  146. public String unloadExtension(String name) {
  147. String className = name;
  148. String[] p = name.split("@");
  149. if (p.length > 1) {
  150. _classLoader.addJarFile(p[1]);
  151. className = p[0];
  152. }
  153. String res = className + " unloaded";
  154. try {
  155. Object obj = _loadedExtensions.get(className);
  156. Class<?> extension = obj.getClass();
  157. _loadedExtensions.remove(className);
  158. extension.getMethod("unload", new Class[0]).invoke(obj, new Object[0]);
  159. _log.info("Extension " + className + " unloaded.");
  160. } catch (Exception ex) {
  161. _log.log(Level.WARNING, "could not unload " + className, ex);
  162. res = ex.toString();
  163. }
  164. return res;
  165. }
  166. /**
  167. * unloads all extensions, resets the cache and initializes all configured extensions
  168. *
  169. */
  170. public void reload() {
  171. unloadExtensions();
  172. clearCache();
  173. initExtensions();
  174. }
  175. /**
  176. * unloads a named extension, resets the cache and initializes the extension
  177. * @param name the class name and optionally a jar file name delimited with a '@' if the jar file is not
  178. * in the class path
  179. */
  180. public void reload(String name) {
  181. unloadExtension(name);
  182. clearCache();
  183. initExtension(name);
  184. }
  185. /**
  186. * register a getter function given a (hopefully) unique name
  187. * @param name the name of the function
  188. * @param function the ExtensionFunction implementation
  189. */
  190. public void addGetter(String name, ExtensionFunction function) {
  191. _getters.put(name, function);
  192. }
  193. /**
  194. * deregister a getter function
  195. * @param name the name used for registering
  196. */
  197. public void removeGetter(String name) {
  198. _getters.remove(name);
  199. }
  200. /**
  201. * call a getter function registered with DynamicExtension
  202. * @param name the function name
  203. * @param arg a function argument
  204. * @return an object from the extension
  205. */
  206. public Object get(String name, String arg) {
  207. ExtensionFunction func = _getters.get(name);
  208. if (func != null)
  209. return func.get(arg);
  210. return "<none>";
  211. }
  212. /**
  213. * register a setter function given a (hopefully) unique name
  214. * @param name the name of the function
  215. * @param function the ExtensionFunction implementation
  216. */
  217. public void addSetter(String name, ExtensionFunction function) {
  218. _setters.put(name, function);
  219. }
  220. /**
  221. * deregister a setter function
  222. * @param name the name used for registering
  223. */
  224. public void removeSetter(String name) {
  225. _setters.remove(name);
  226. }
  227. /**
  228. * call a setter function registered with DynamicExtension
  229. * @param name the function name
  230. * @param arg a function argument
  231. * @param obj an object to set
  232. */
  233. public void set(String name, String arg, Object obj) {
  234. ExtensionFunction func = _setters.get(name);
  235. if (func != null)
  236. func.set(arg, obj);
  237. }
  238. public JarClassLoader getClassLoader()
  239. {
  240. return _classLoader;
  241. }
  242. }