001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------
028: * PackageState.java
029: * -----------------
030: * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: PackageState.java,v 1.4 2006/04/14 13:00:56 taqua Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jul-2003 : Initial version
040: * 07-Jun-2004 : Added JCommon header (DG);
041: *
042: */
043:
044: package org.jfree.base.modules;
045:
046: import org.jfree.util.Log;
047:
048: /**
049: * The package state class is used by the package manager to keep track of
050: * the activation level of the installed or errornous packages.
051: *
052: * @author Thomas Morgner
053: */
054: public class PackageState {
055: /** A constant defining that the package is new. */
056: public static final int STATE_NEW = 0;
057:
058: /** A constant defining that the package has been loaded and configured. */
059: public static final int STATE_CONFIGURED = 1;
060:
061: /** A constant defining that the package was initialized and is ready to use. */
062: public static final int STATE_INITIALIZED = 2;
063:
064: /** A constant defining that the package produced an error and is not available. */
065: public static final int STATE_ERROR = -2;
066:
067: /** The module class that contains the package information. */
068: private final Module module;
069: /** The state of the module. */
070: private int state;
071:
072: /**
073: * Creates a new package state for the given module. The module state will
074: * be initialized to STATE_NEW.
075: *
076: * @param module the module.
077: */
078: public PackageState(final Module module) {
079: this (module, STATE_NEW);
080: }
081:
082: /**
083: * Creates a new package state for the given module. The module state will
084: * be initialized to the given initial state.
085: *
086: * @param module the module.
087: * @param state the initial state
088: */
089: public PackageState(final Module module, final int state) {
090: if (module == null) {
091: throw new NullPointerException("Module must not be null.");
092: }
093: if (state != STATE_CONFIGURED && state != STATE_ERROR
094: && state != STATE_INITIALIZED && state != STATE_NEW) {
095: throw new IllegalArgumentException("State is not valid");
096: }
097: this .module = module;
098: this .state = state;
099: }
100:
101: /**
102: * Configures the module and raises the state to STATE_CONFIGURED if the
103: * module is not yet configured.
104: *
105: * @param subSystem the sub-system.
106: *
107: * @return true, if the module was configured, false otherwise.
108: */
109: public boolean configure(final SubSystem subSystem) {
110: if (this .state == STATE_NEW) {
111: try {
112: this .module.configure(subSystem);
113: this .state = STATE_CONFIGURED;
114: return true;
115: } catch (NoClassDefFoundError noClassDef) {
116: Log.warn(new Log.SimpleMessage(
117: "Unable to load module classes for ",
118: this .module.getName(), ":", noClassDef
119: .getMessage()));
120: this .state = STATE_ERROR;
121: } catch (Exception e) {
122: if (Log.isDebugEnabled()) {
123: // its still worth a warning, but now we are more verbose ...
124: Log.warn("Unable to configure the module "
125: + this .module.getName(), e);
126: } else if (Log.isWarningEnabled()) {
127: Log.warn("Unable to configure the module "
128: + this .module.getName());
129: }
130: this .state = STATE_ERROR;
131: }
132: }
133: return false;
134: }
135:
136: /**
137: * Returns the module managed by this state implementation.
138: *
139: * @return the module.
140: */
141: public Module getModule() {
142: return this .module;
143: }
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: return this .state;
153: }
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: if (this .state == STATE_CONFIGURED) {
167: try {
168: this .module.initialize(subSystem);
169: this .state = STATE_INITIALIZED;
170: return true;
171: } catch (NoClassDefFoundError noClassDef) {
172: Log.warn(new Log.SimpleMessage(
173: "Unable to load module classes for ",
174: this .module.getName(), ":", noClassDef
175: .getMessage()));
176: this .state = STATE_ERROR;
177: } catch (ModuleInitializeException me) {
178: if (Log.isDebugEnabled()) {
179: // its still worth a warning, but now we are more verbose ...
180: Log.warn("Unable to initialize the module "
181: + this .module.getName(), me);
182: } else if (Log.isWarningEnabled()) {
183: Log.warn("Unable to initialize the module "
184: + this .module.getName());
185: }
186: this .state = STATE_ERROR;
187: } catch (Exception e) {
188: if (Log.isDebugEnabled()) {
189: // its still worth a warning, but now we are more verbose ...
190: Log.warn("Unable to initialize the module "
191: + this .module.getName(), e);
192: } else if (Log.isWarningEnabled()) {
193: Log.warn("Unable to initialize the module "
194: + this .module.getName());
195: }
196: this .state = STATE_ERROR;
197: }
198: }
199: return false;
200: }
201:
202: /**
203: * Compares this object with the given other object for equality.
204: * @see java.lang.Object#equals(java.lang.Object)
205: *
206: * @param o the other object to be compared
207: * @return true, if the other object is also a PackageState containing
208: * the same module, false otherwise.
209: */
210: public boolean equals(final Object o) {
211: if (this == o) {
212: return true;
213: }
214: if (!(o instanceof PackageState)) {
215: return false;
216: }
217:
218: final PackageState packageState = (PackageState) o;
219:
220: if (!this .module.getModuleClass().equals(
221: packageState.module.getModuleClass())) {
222: return false;
223: }
224:
225: return true;
226: }
227:
228: /**
229: * Computes a hashcode for this package state.
230: * @see java.lang.Object#hashCode()
231: *
232: * @return the hashcode.
233: */
234: public int hashCode() {
235: return this.module.hashCode();
236: }
237: }
|