PackageState.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -----------------
  28. * PackageState.java
  29. * -----------------
  30. * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
  31. *
  32. * Original Author: Thomas Morgner;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: PackageState.java,v 1.4 2006/04/14 13:00:56 taqua Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 10-Jul-2003 : Initial version
  40. * 07-Jun-2004 : Added JCommon header (DG);
  41. *
  42. */
  43. package org.jfree.base.modules;
  44. import org.jfree.util.Log;
  45. /**
  46. * The package state class is used by the package manager to keep track of
  47. * the activation level of the installed or errornous packages.
  48. *
  49. * @author Thomas Morgner
  50. */
  51. public class PackageState
  52. {
  53. /** A constant defining that the package is new. */
  54. public static final int STATE_NEW = 0;
  55. /** A constant defining that the package has been loaded and configured. */
  56. public static final int STATE_CONFIGURED = 1;
  57. /** A constant defining that the package was initialized and is ready to use. */
  58. public static final int STATE_INITIALIZED = 2;
  59. /** A constant defining that the package produced an error and is not available. */
  60. public static final int STATE_ERROR = -2;
  61. /** The module class that contains the package information. */
  62. private final Module module;
  63. /** The state of the module. */
  64. private int state;
  65. /**
  66. * Creates a new package state for the given module. The module state will
  67. * be initialized to STATE_NEW.
  68. *
  69. * @param module the module.
  70. */
  71. public PackageState(final Module module)
  72. {
  73. this (module, STATE_NEW);
  74. }
  75. /**
  76. * Creates a new package state for the given module. The module state will
  77. * be initialized to the given initial state.
  78. *
  79. * @param module the module.
  80. * @param state the initial state
  81. */
  82. public PackageState(final Module module, final int state)
  83. {
  84. if (module == null)
  85. {
  86. throw new NullPointerException("Module must not be null.");
  87. }
  88. if (state != STATE_CONFIGURED && state != STATE_ERROR
  89. && state != STATE_INITIALIZED && state != STATE_NEW)
  90. {
  91. throw new IllegalArgumentException("State is not valid");
  92. }
  93. this.module = module;
  94. this.state = state;
  95. }
  96. /**
  97. * Configures the module and raises the state to STATE_CONFIGURED if the
  98. * module is not yet configured.
  99. *
  100. * @param subSystem the sub-system.
  101. *
  102. * @return true, if the module was configured, false otherwise.
  103. */
  104. public boolean configure(final SubSystem subSystem)
  105. {
  106. if (this.state == STATE_NEW)
  107. {
  108. try
  109. {
  110. this.module.configure(subSystem);
  111. this.state = STATE_CONFIGURED;
  112. return true;
  113. }
  114. catch (NoClassDefFoundError noClassDef)
  115. {
  116. Log.warn (new Log.SimpleMessage("Unable to load module classes for ",
  117. this.module.getName(), ":", noClassDef.getMessage()));
  118. this.state = STATE_ERROR;
  119. }
  120. catch (Exception e)
  121. {
  122. if (Log.isDebugEnabled())
  123. {
  124. // its still worth a warning, but now we are more verbose ...
  125. Log.warn("Unable to configure the module " + this.module.getName(), e);
  126. }
  127. else if (Log.isWarningEnabled())
  128. {
  129. Log.warn("Unable to configure the module " + this.module.getName());
  130. }
  131. this.state = STATE_ERROR;
  132. }
  133. }
  134. return false;
  135. }
  136. /**
  137. * Returns the module managed by this state implementation.
  138. *
  139. * @return the module.
  140. */
  141. public Module getModule()
  142. {
  143. return this.module;
  144. }
  145. /**
  146. * Returns the current state of the module. This method returns either
  147. * STATE_NEW, STATE_CONFIGURED, STATE_INITIALIZED or STATE_ERROR.
  148. *
  149. * @return the module state.
  150. */
  151. public int getState()
  152. {
  153. return this.state;
  154. }
  155. /**
  156. * Initializes the contained module and raises the set of the module to
  157. * STATE_INITIALIZED, if the module was not yet initialized. In case of an
  158. * error, the module state will be set to STATE_ERROR and the module will
  159. * not be available.
  160. *
  161. * @param subSystem the sub-system.
  162. *
  163. * @return true, if the module was successfully initialized, false otherwise.
  164. */
  165. public boolean initialize(final SubSystem subSystem)
  166. {
  167. if (this.state == STATE_CONFIGURED)
  168. {
  169. try
  170. {
  171. this.module.initialize(subSystem);
  172. this.state = STATE_INITIALIZED;
  173. return true;
  174. }
  175. catch (NoClassDefFoundError noClassDef)
  176. {
  177. Log.warn (new Log.SimpleMessage("Unable to load module classes for ",
  178. this.module.getName(), ":", noClassDef.getMessage()));
  179. this.state = STATE_ERROR;
  180. }
  181. catch (ModuleInitializeException me)
  182. {
  183. if (Log.isDebugEnabled())
  184. {
  185. // its still worth a warning, but now we are more verbose ...
  186. Log.warn("Unable to initialize the module " + this.module.getName(), me);
  187. }
  188. else if (Log.isWarningEnabled())
  189. {
  190. Log.warn("Unable to initialize the module " + this.module.getName());
  191. }
  192. this.state = STATE_ERROR;
  193. }
  194. catch (Exception e)
  195. {
  196. if (Log.isDebugEnabled())
  197. {
  198. // its still worth a warning, but now we are more verbose ...
  199. Log.warn("Unable to initialize the module " + this.module.getName(), e);
  200. }
  201. else if (Log.isWarningEnabled())
  202. {
  203. Log.warn("Unable to initialize the module " + this.module.getName());
  204. }
  205. this.state = STATE_ERROR;
  206. }
  207. }
  208. return false;
  209. }
  210. /**
  211. * Compares this object with the given other object for equality.
  212. * @see java.lang.Object#equals(java.lang.Object)
  213. *
  214. * @param o the other object to be compared
  215. * @return true, if the other object is also a PackageState containing
  216. * the same module, false otherwise.
  217. */
  218. public boolean equals(final Object o)
  219. {
  220. if (this == o)
  221. {
  222. return true;
  223. }
  224. if (!(o instanceof PackageState))
  225. {
  226. return false;
  227. }
  228. final PackageState packageState = (PackageState) o;
  229. if (!this.module.getModuleClass().equals(packageState.module.getModuleClass()))
  230. {
  231. return false;
  232. }
  233. return true;
  234. }
  235. /**
  236. * Computes a hashcode for this package state.
  237. * @see java.lang.Object#hashCode()
  238. *
  239. * @return the hashcode.
  240. */
  241. public int hashCode()
  242. {
  243. return this.module.hashCode();
  244. }
  245. }