KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > base > modules > PackageState


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
44 package org.jfree.base.modules;
45
46 import org.jfree.util.Log;
47
48 /**
49  * The package state class is used by the package manager to keep track of
50  * the activation level of the installed or errornous packages.
51  *
52  * @author Thomas Morgner
53  */

54 public class PackageState
55 {
56   /** A constant defining that the package is new. */
57   public static final int STATE_NEW = 0;
58   
59   /** A constant defining that the package has been loaded and configured. */
60   public static final int STATE_CONFIGURED = 1;
61   
62   /** A constant defining that the package was initialized and is ready to use. */
63   public static final int STATE_INITIALIZED = 2;
64   
65   /** A constant defining that the package produced an error and is not available. */
66   public static final int STATE_ERROR = -2;
67
68   /** The module class that contains the package information. */
69   private final Module module;
70   /** The state of the module. */
71   private int state;
72
73   /**
74    * Creates a new package state for the given module. The module state will
75    * be initialized to STATE_NEW.
76    *
77    * @param module the module.
78    */

79   public PackageState(final Module module)
80   {
81     this (module, STATE_NEW);
82   }
83
84   /**
85    * Creates a new package state for the given module. The module state will
86    * be initialized to the given initial state.
87    *
88    * @param module the module.
89    * @param state the initial state
90    */

91   public PackageState(final Module module, final int state)
92   {
93     if (module == null)
94     {
95       throw new NullPointerException JavaDoc("Module must not be null.");
96     }
97     if (state != STATE_CONFIGURED && state != STATE_ERROR
98             && state != STATE_INITIALIZED && state != STATE_NEW)
99     {
100       throw new IllegalArgumentException JavaDoc("State is not valid");
101     }
102     this.module = module;
103     this.state = state;
104   }
105
106   /**
107    * Configures the module and raises the state to STATE_CONFIGURED if the
108    * module is not yet configured.
109    *
110    * @param subSystem the sub-system.
111    *
112    * @return true, if the module was configured, false otherwise.
113    */

114   public boolean configure(final SubSystem subSystem)
115   {
116     if (this.state == STATE_NEW)
117     {
118       try
119       {
120         this.module.configure(subSystem);
121         this.state = STATE_CONFIGURED;
122         return true;
123       }
124       catch (NoClassDefFoundError JavaDoc noClassDef)
125       {
126         Log.warn (new Log.SimpleMessage("Unable to load module classes for ",
127                 this.module.getName(), ":", noClassDef.getMessage()));
128         this.state = STATE_ERROR;
129       }
130       catch (Exception JavaDoc e)
131       {
132         if (Log.isDebugEnabled())
133         {
134           // its still worth a warning, but now we are more verbose ...
135
Log.warn("Unable to configure the module " + this.module.getName(), e);
136         }
137         else if (Log.isWarningEnabled())
138         {
139           Log.warn("Unable to configure the module " + this.module.getName());
140         }
141         this.state = STATE_ERROR;
142       }
143     }
144     return false;
145   }
146
147   /**
148    * Returns the module managed by this state implementation.
149    *
150    * @return the module.
151    */

152   public Module getModule()
153   {
154     return this.module;
155   }
156
157   /**
158    * Returns the current state of the module. This method returns either
159    * STATE_NEW, STATE_CONFIGURED, STATE_INITIALIZED or STATE_ERROR.
160    *
161    * @return the module state.
162    */

163   public int getState()
164   {
165     return this.state;
166   }
167
168   /**
169    * Initializes the contained module and raises the set of the module to
170    * STATE_INITIALIZED, if the module was not yet initialized. In case of an
171    * error, the module state will be set to STATE_ERROR and the module will
172    * not be available.
173    *
174    * @param subSystem the sub-system.
175    *
176    * @return true, if the module was successfully initialized, false otherwise.
177    */

178   public boolean initialize(final SubSystem subSystem)
179   {
180     if (this.state == STATE_CONFIGURED)
181     {
182       try
183       {
184           this.module.initialize(subSystem);
185           this.state = STATE_INITIALIZED;
186           return true;
187       }
188       catch (NoClassDefFoundError JavaDoc noClassDef)
189       {
190         Log.warn (new Log.SimpleMessage("Unable to load module classes for ",
191                 this.module.getName(), ":", noClassDef.getMessage()));
192         this.state = STATE_ERROR;
193       }
194       catch (ModuleInitializeException me)
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(), me);
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       catch (Exception JavaDoc e)
208       {
209         if (Log.isDebugEnabled())
210         {
211           // its still worth a warning, but now we are more verbose ...
212
Log.warn("Unable to initialize the module " + this.module.getName(), e);
213         }
214         else if (Log.isWarningEnabled())
215         {
216           Log.warn("Unable to initialize the module " + this.module.getName());
217         }
218         this.state = STATE_ERROR;
219       }
220     }
221     return false;
222   }
223
224   /**
225    * Compares this object with the given other object for equality.
226    * @see java.lang.Object#equals(java.lang.Object)
227    *
228    * @param o the other object to be compared
229    * @return true, if the other object is also a PackageState containing
230    * the same module, false otherwise.
231    */

232   public boolean equals(final Object JavaDoc o)
233   {
234     if (this == o)
235     {
236       return true;
237     }
238     if (!(o instanceof PackageState))
239     {
240       return false;
241     }
242
243     final PackageState packageState = (PackageState) o;
244
245     if (!this.module.getModuleClass().equals(packageState.module.getModuleClass()))
246     {
247       return false;
248     }
249
250     return true;
251   }
252
253   /**
254    * Computes a hashcode for this package state.
255    * @see java.lang.Object#hashCode()
256    *
257    * @return the hashcode.
258    */

259   public int hashCode()
260   {
261     return this.module.hashCode();
262   }
263 }
264
Popular Tags