001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ServiceUnit.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework;
030:
031: import java.util.Iterator;
032: import com.sun.jbi.ServiceUnitState;
033:
034: /**
035: * This class holds information about Service Unit deployed to a Binding
036: * Component (BC) or Service Engine (SE). This information is used by the JBI
037: * framework to manage Service Unit life cycles. The information is persisted
038: * by the Component Registry across restarts of the JBI framework.
039: *
040: * @author Sun Microsystems, Inc.
041: */
042: public class ServiceUnit implements com.sun.jbi.ServiceUnitInfo {
043: /**
044: * The unique name for this Service Unit.
045: */
046: private String mName;
047:
048: /**
049: * The full directory path to the deployment information for this Service
050: * Unit.
051: */
052: private String mFilePath;
053:
054: /**
055: * The unique name of the containing Service Assembly.
056: */
057: private String mSaName;
058:
059: /**
060: * The current state of this Service Unit. Valid states are:
061: * <UL>
062: * <LI><CODE>SHUTDOWN</CODE> - not initialized; initialization must be
063: * done before starting</LI>
064: * <LI><CODE>STOPPED</CODE> - initialized, but not yet started</LI>
065: * <LI><CODE>STARTED</CODE> - available to accept requests</LI>
066: * </UL>
067: * State changes allowed are:
068: * <UL>
069: * <LI><CODE>SHUTDOWN</CODE> --> <CODE>STOPPED</CODE></LI>
070: * <LI><CODE>STOPPED</CODE> --> <CODE>STARTED</CODE></LI>
071: * <LI><CODE>STARTED</CODE> --> <CODE>STOPPED</CODE></LI>
072: * <LI><CODE>STOPPED</CODE> --> <CODE>SHUTDOWN</CODE></LI>
073: * </UL>
074: */
075: private ServiceUnitState mState;
076:
077: /**
078: * The desired state of this Service Unit. This is persisted across restarts
079: * of the JBI framework. This state is set whenever a state change is
080: * requested, and is used to determine whether the last known state at
081: * shutdown is actually the desired state.
082: */
083: private ServiceUnitState mDesiredState;
084:
085: /**
086: * The StringTranslator to be used for constructing messages.
087: */
088: private transient StringTranslator mTranslator;
089:
090: /**
091: * The target component, i.e. the component to which this Service Unit is
092: * deployed.
093: */
094: private String mTargetComponent;
095:
096: /**
097: * Create a new ServiceUnit instance.
098: * @param saName is the unique name for the Service Assembly that contains
099: * this Service Unit.
100: * @param name is the unique name for this Service Unit.
101: * @param filePath is the full path to the file defining this Service Unit.
102: */
103: public ServiceUnit(String saName, String name, String filePath) {
104: mTranslator = (StringTranslator) EnvironmentContext
105: .getInstance().getStringTranslatorFor(this );
106: mSaName = saName;
107: mName = name;
108: mFilePath = filePath;
109: mState = ServiceUnitState.SHUTDOWN;
110: mDesiredState = ServiceUnitState.SHUTDOWN;
111: }
112:
113: /**
114: * Create a new ServiceUnit instance.
115: * @param saName is the unique name for the Service Assembly that contains
116: * this Service Unit.
117: * @param name is the unique name for this Service Unit.
118: * @param filePath is the full path to the file defining this Service Unit.
119: * @param targetComp is the name of the component to which this Service Unit
120: * is deployed.
121: */
122: public ServiceUnit(String saName, String name, String filePath,
123: String targetComp) {
124: mTranslator = (StringTranslator) EnvironmentContext
125: .getInstance().getStringTranslatorFor(this );
126: mSaName = saName;
127: mName = name;
128: mFilePath = filePath;
129: mState = ServiceUnitState.SHUTDOWN;
130: mDesiredState = ServiceUnitState.SHUTDOWN;
131: mTargetComponent = targetComp;
132: }
133:
134: /**
135: * Create a new ServiceUnit instance from a ServiceUnitInfo instance.
136: * @param suInfo is the instance to use to create the new instance.
137: */
138: public ServiceUnit(com.sun.jbi.ServiceUnitInfo suInfo) {
139: mTranslator = (StringTranslator) EnvironmentContext
140: .getInstance().getStringTranslatorFor(this );
141: mSaName = suInfo.getServiceAssemblyName();
142: mName = suInfo.getName();
143: mFilePath = suInfo.getFilePath();
144: mState = ServiceUnitState.SHUTDOWN;
145: mDesiredState = suInfo.getState();
146: mTargetComponent = suInfo.getTargetComponent();
147: }
148:
149: /**
150: * Compare another Object with this one for equality. Note that the
151: * state fields are not checked, only the fields that are not mutable.
152: * @param object The object to be compared with this one.
153: * @return True if the object is equal to this Component, false
154: * if they are not equal.
155: */
156: public boolean equals(Object object) {
157: if (null == object) {
158: return false;
159: }
160: if (!(object instanceof ServiceUnit)) {
161: return false;
162: }
163: ServiceUnit su = (ServiceUnit) object;
164: if (!mSaName.equals(su.getServiceAssemblyName())) {
165: return false;
166: }
167: if (!mName.equals(su.getName())) {
168: return false;
169: }
170: if (!mFilePath.equals(su.getFilePath())) {
171: return false;
172: }
173: return true;
174: }
175:
176: /**
177: * Get the desired state for this Service Unit.
178: * @return the desired state.
179: */
180: public ServiceUnitState getDesiredState() {
181: return mDesiredState;
182: }
183:
184: /**
185: * Get the file path for this Service Unit.
186: * @return the file path.
187: */
188: public String getFilePath() {
189: return mFilePath;
190: }
191:
192: /**
193: * Get the unique name for this Service Unit.
194: * @return the Service Unit name.
195: */
196: public String getName() {
197: return mName;
198: }
199:
200: /**
201: * Get the unique name for the Service Assembly that contains this Service
202: * Unit.
203: * @return the Service Assembly name.
204: */
205: public String getServiceAssemblyName() {
206: return mSaName;
207: }
208:
209: /**
210: * Get the state of this Service Unit.
211: * @return the state, either SHUTDOWN, STARTED, or STOPPED.
212: */
213: public ServiceUnitState getState() {
214: return mState;
215: }
216:
217: /**
218: * Get the state of this Service Unit as a string value.
219: * @return the state as a string, either "shutdown", "started", or "stopped".
220: */
221: public String getStateAsString() {
222: return mState.toString();
223: }
224:
225: /**
226: * Get a Service Unit state as a localized string value.
227: * @param state a state, either SHUTDOWN, STARTED, or STOPPED.
228: * @return the state as a string, either "Shutdown", "Started", or "Stopped"
229: * in the current locale.
230: */
231: public String getStateAsString(ServiceUnitState state) {
232: return state.toString();
233: }
234:
235: /**
236: * Get the target component for this Service Unit. This is the name of the
237: * component to which the Service Unit is deployed.
238: *
239: * @return the target component name.
240: */
241: public String getTargetComponent() {
242: return mTargetComponent;
243: }
244:
245: /**
246: * Get the hash code for this Service Unit instance.
247: * @return the hash code.
248: */
249: public int hashCode() {
250: int hashCode = 0;
251:
252: hashCode += mName.hashCode();
253: hashCode += mSaName.hashCode();
254: hashCode += mFilePath.hashCode();
255:
256: return hashCode;
257: }
258:
259: /**
260: * Check to see if this Service Unit is shut down.
261: * @return true if the Service Unit is shut down, false if not.
262: */
263: public boolean isShutdown() {
264: return (ServiceUnitState.SHUTDOWN == mState);
265: }
266:
267: /**
268: * Check to see if this Service Unit is started.
269: * @return true if the Service Unit is started, false if not.
270: */
271: public boolean isStarted() {
272: return (ServiceUnitState.STARTED == mState);
273: }
274:
275: /**
276: * Check to see if this Service Unit is stopped.
277: * @return true if the Service Unit is stopped, false if not.
278: */
279: public boolean isStopped() {
280: return (ServiceUnitState.STOPPED == mState);
281: }
282:
283: /**
284: * Set the desired Service Unit state.
285: * @param state the state to be set.
286: * Valid states are SHUTDOWN, STOPPED, and STARTED.
287: * @throws java.lang.IllegalArgumentException if the state is not valid.
288: */
289: void setDesiredState(ServiceUnitState state) {
290: if (ServiceUnitState.SHUTDOWN != state
291: && ServiceUnitState.STOPPED != state
292: && ServiceUnitState.STARTED != state) {
293: throw new java.lang.IllegalArgumentException(mTranslator
294: .getString(LocalStringKeys.INVALID_ARGUMENT,
295: "state", state));
296: }
297: mDesiredState = state;
298: }
299:
300: /**
301: * Set the Service Unit state. This method is used ONLY by startup and unit
302: * test code. It does no validation of the state.
303: * @param state the state to be set.
304: * Valid states are SHUTDOWN, STOPPED, and STARTED.
305: */
306: void setState(ServiceUnitState state) {
307: mState = state;
308: }
309:
310: /**
311: * Set the Service Unit state to shutdown.
312: */
313: public void setShutdown() {
314: // If the Service Unit is started it cannot be set to shutdown, only
315: // to stopped.
316:
317: if (isStarted()) {
318: throw new java.lang.IllegalStateException(
319: mTranslator
320: .getString(
321: LocalStringKeys.SU_INVALID_STATE_CHANGE,
322: mTranslator
323: .getString(LocalStringKeys.SU_STATE_STARTED),
324: mTranslator
325: .getString(LocalStringKeys.SU_STATE_SHUTDOWN)));
326: }
327: mState = ServiceUnitState.SHUTDOWN;
328: }
329:
330: /**
331: * Set the Service Unit state to started.
332: */
333: public void setStarted() {
334: if (isShutdown()) {
335: throw new java.lang.IllegalStateException(
336: mTranslator
337: .getString(
338: LocalStringKeys.SU_INVALID_STATE_CHANGE,
339: mTranslator
340: .getString(LocalStringKeys.SU_STATE_SHUTDOWN),
341: mTranslator
342: .getString(LocalStringKeys.SU_STATE_STARTED)));
343: }
344: mState = ServiceUnitState.STARTED;
345: }
346:
347: /**
348: * Set the Service Unit state to stopped.
349: */
350: public void setStopped() {
351: mState = ServiceUnitState.STOPPED;
352: }
353:
354: /**
355: * Set the target component for this Service Unit. This is the name of the
356: * component to which the Service Unit is deployed.
357: *
358: * @param componentName the target component name.
359: */
360: public void setTargetComponent(String componentName) {
361: mTargetComponent = componentName;
362: }
363:
364: /**
365: * Convert to a String.
366: * @return the String representation of this ServiceUnit.
367: */
368: public String toString() {
369: String sep = new String(",\n");
370: StringBuffer b = new StringBuffer();
371: b.append("Name = " + mName);
372: b.append(sep);
373: b.append("FilePath = " + mFilePath);
374: b.append(sep);
375: b.append("State = " + mState.toString());
376: b.append(sep);
377: b.append("Desired state = " + mDesiredState.toString());
378: b.append(sep);
379: b.append("Service Assembly name = " + mSaName);
380: b.append("\n");
381:
382: return b.toString();
383: }
384: }
|