File: PackageState.java

package info (click to toggle)
libbase 1.1.6-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 944 kB
  • sloc: java: 8,709; xml: 1,522; makefile: 18
file content (270 lines) | stat: -rw-r--r-- 7,320 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2007 - 2009 Pentaho Corporation and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.base.boot;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The package state class is used by the package manager to keep track of
 * the activation level of the installed or errornous packages.
 * <p/>
 * This is an internal class used by the PackageManager.
 *
 * @author Thomas Morgner
 */
public class PackageState
{
  /** A logger. */
  private static final Log LOGGER = LogFactory.getLog(PackageState.class);

  /**
   * A constant defining that the package is new.
   */
  public static final int STATE_NEW = 0;

  /**
   * A constant defining that the package has been loaded and configured.
   */
  public static final int STATE_CONFIGURED = 1;

  /**
   * A constant defining that the package was initialized and is ready to use.
   */
  public static final int STATE_INITIALIZED = 2;

  /**
   * A constant defining that the package produced an error and is not available.
   */
  public static final int STATE_ERROR = -2;

  /**
   * The module class that contains the package information.
   */
  private final Module module;
  /**
   * The state of the module.
   */
  private int state;

  /**
   * Creates a new package state for the given module. The module state will
   * be initialized to STATE_NEW.
   *
   * @param module the module.
   */
  public PackageState(final Module module)
  {
    this(module, STATE_NEW);
  }

  /**
   * Creates a new package state for the given module. The module state will
   * be initialized to the given initial state.
   *
   * @param module the module.
   * @param state  the initial state
   */
  public PackageState(final Module module, final int state)
  {
    if (module == null)
    {
      throw new NullPointerException("Module must not be null.");
    }
    if (state != STATE_CONFIGURED && state != STATE_ERROR
        && state != STATE_INITIALIZED && state != STATE_NEW)
    {
      throw new IllegalArgumentException("State is not valid");
    }
    this.module = module;
    this.state = state;
  }

  /**
   * Configures the module and raises the state to STATE_CONFIGURED if the
   * module is not yet configured.
   *
   * @param subSystem the sub-system.
   * @return true, if the module was configured, false otherwise.
   */
  public boolean configure(final SubSystem subSystem)
  {
    if (subSystem == null)
    {
      throw new NullPointerException();
    }

    if (this.state == STATE_NEW)
    {
      try
      {
        this.module.configure(subSystem);
        this.state = STATE_CONFIGURED;
        return true;
      }
      catch (NoClassDefFoundError noClassDef)
      {
        LOGGER.warn("Unable to load module classes for " +
            this.module.getName() + ':' + noClassDef.getMessage());
        this.state = STATE_ERROR;
      }
      catch (Exception e)
      {
        if (LOGGER.isDebugEnabled())
        {
          // its still worth a warning, but now we are more verbose ...
          LOGGER.warn("Unable to configure the module " + this.module.getName(), e);
        }
        else if (LOGGER.isWarnEnabled())
        {
          LOGGER.warn("Unable to configure the module " + this.module.getName());
        }
        this.state = STATE_ERROR;
      }
    }
    return false;
  }

  /**
   * Returns the module managed by this state implementation.
   *
   * @return the module.
   */
  public Module getModule()
  {
    return this.module;
  }

  /**
   * Returns the current state of the module. This method returns either
   * STATE_NEW, STATE_CONFIGURED, STATE_INITIALIZED or STATE_ERROR.
   *
   * @return the module state.
   */
  public int getState()
  {
    return this.state;
  }

  /**
   * Initializes the contained module and raises the set of the module to
   * STATE_INITIALIZED, if the module was not yet initialized. In case of an
   * error, the module state will be set to STATE_ERROR and the module will
   * not be available.
   *
   * @param subSystem the sub-system.
   * @return true, if the module was successfully initialized, false otherwise.
   */
  public boolean initialize(final SubSystem subSystem)
  {
    if (subSystem == null)
    {
      throw new NullPointerException();
    }

    if (this.state == STATE_CONFIGURED)
    {
      try
      {
        this.module.initialize(subSystem);
        this.state = STATE_INITIALIZED;
        return true;
      }
      catch (NoClassDefFoundError noClassDef)
      {
        LOGGER.warn("Unable to load module classes for " + this.module.getName() + ':' + noClassDef.getMessage());
        this.state = STATE_ERROR;
      }
      catch (ModuleInitializeException me)
      {
        if (LOGGER.isDebugEnabled())
        {
          // its still worth a warning, but now we are more verbose ...
          LOGGER.warn("Unable to initialize the module " + this.module.getName(), me);
        }
        else if (LOGGER.isWarnEnabled())
        {
          LOGGER.warn("Unable to initialize the module " + this.module.getName());
        }
        this.state = STATE_ERROR;
      }
      catch (Exception e)
      {
        if (LOGGER.isDebugEnabled())
        {
          // its still worth a warning, but now we are more verbose ...
          LOGGER.warn("Unable to initialize the module " + this.module.getName(), e);
        }
        else if (LOGGER.isWarnEnabled())
        {
          LOGGER.warn("Unable to initialize the module " + this.module.getName());
        }
        this.state = STATE_ERROR;
      }
    }
    return false;
  }

  /**
   * Compares this object with the given other object for equality.
   *
   * @param o the other object to be compared
   * @return true, if the other object is also a PackageState containing
   *         the same module, false otherwise.
   * @see Object#equals(Object)
   */
  public boolean equals(final Object o)
  {
    if (this == o)
    {
      return true;
    }
    if (!(o instanceof PackageState))
    {
      return false;
    }

    final PackageState packageState = (PackageState) o;

    if (!this.module.getModuleClass().equals(packageState.module.getModuleClass()))
    {
      return false;
    }

    return true;
  }

  /**
   * Computes a hashcode for this package state.
   *
   * @return the hashcode.
   * @see Object#hashCode()
   */
  public int hashCode()
  {
    return this.module.hashCode();
  }

  /**
   * Marks this package state as invalid.
   */
  public void markError()
  {
    this.state = STATE_ERROR;
  }
}