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: * @(#)ServiceUnitOperation.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 javax.jbi.component.ServiceUnitManager;
032:
033: /**
034: * This class extends the Operation abstract class, and is designed to run on a
035: * separate thread. The main purpose of this class is to provide a way to run
036: * operations on Service Units with a timeout to prevent a hang if the call to
037: * the component's ServiceUnitManager fails to return
038: *
039: * To use this class, create an instance for the Service Unit, providing the
040: * appropriate Service Unit Manager, operation type, and operation arguments to
041: * the constructor. Then create a thread for the instance and call its start()
042: * method.
043: *
044: * If the parent thread needs to wait for the operation to complete, it can call
045: * wait() on the instance of this class. Optionally, the wait() call can have a
046: * a timeout parameter to limit the amount of time to wait for completion of the
047: * operation.
048: *
049: * @author Sun Microsystems, Inc.
050: */
051: class ServiceUnitOperation extends Operation {
052: /**
053: * The JBI environment context.
054: */
055: private EnvironmentContext mEnv;
056:
057: /**
058: * The type of operation to be performed.
059: */
060: private int mOperation;
061:
062: /**
063: * The name of the component.
064: */
065: private String mComponentName;
066:
067: /**
068: * The ServiceUnitManager for the component.
069: */
070: private ServiceUnitManager mSuManager;
071:
072: /**
073: * The message string translator.
074: */
075: private StringTranslator mTranslator;
076:
077: /**
078: * Value for mOperation for a deploy operation.
079: */
080: public static final int DEPLOY = 1;
081:
082: /**
083: * Value for mOperation for an init operation.
084: */
085: public static final int INIT = 2;
086:
087: /**
088: * Value for mOperation for a start operation.
089: */
090: public static final int START = 3;
091:
092: /**
093: * Value for mOperation for a stop operation.
094: */
095: public static final int STOP = 4;
096:
097: /**
098: * Value for mOperation for a shutdown operation.
099: */
100: public static final int SHUTDOWN = 5;
101:
102: /**
103: * Value for mOperation for an undeploy operation.
104: */
105: public static final int UNDEPLOY = 6;
106:
107: /**
108: * Constructor.
109: *
110: * @param counter the OperationCounter for synchronization of the operation.
111: * @param compName the name of the component.
112: * @param suManager the ServiceUnitManager instance for the component.
113: * @param operation the type of operation to be performed, either DEPLOY,
114: * INIT, START, STOP, SHUTDOWN, or UNDEPLOY.
115: * @param args array of arguments for the operation.
116: */
117: ServiceUnitOperation(OperationCounter counter, String compName,
118: ServiceUnitManager suManager, int operation, Object args[]) {
119: super (counter, args);
120:
121: mComponentName = compName;
122: mSuManager = suManager;
123: mEnv = EnvironmentContext.getInstance();
124: mTranslator = (StringTranslator) mEnv
125: .getStringTranslatorFor(this );
126:
127: if (null == suManager) {
128: throw new IllegalArgumentException(mTranslator.getString(
129: LocalStringKeys.NULL_ARGUMENT, "suManager"));
130: }
131:
132: if (DEPLOY == operation || INIT == operation
133: || START == operation || STOP == operation
134: || SHUTDOWN == operation || UNDEPLOY == operation) {
135: mOperation = operation;
136: } else {
137: throw new IllegalArgumentException(mTranslator.getString(
138: LocalStringKeys.INVALID_ARGUMENT, "operation",
139: new Integer(operation)));
140: }
141: }
142:
143: /**
144: * Call the ServiceUnitManager to perform the operation on the Service Unit.
145: *
146: * @param argumentList - the arguments for the operation provided to the
147: * constructor.
148: * @return the returned string from operations that return a string or null
149: * for operations that have no return value.
150: * @throws Throwable if any error occurs.
151: */
152: Object process(Object argumentList[]) throws Throwable {
153: String returnValue = null;
154:
155: ClassLoader cl = Thread.currentThread().getContextClassLoader();
156: try {
157: Thread.currentThread().setContextClassLoader(
158: ClassLoaderFactory.getInstance()
159: .getComponentClassLoader(mComponentName));
160: switch (mOperation) {
161: case DEPLOY:
162: returnValue = mSuManager.deploy(
163: (String) argumentList[0],
164: (String) argumentList[1]);
165: break;
166: case INIT:
167: mSuManager.init((String) argumentList[0],
168: (String) argumentList[1]);
169: break;
170: case START:
171: mSuManager.start((String) argumentList[0]);
172: break;
173: case STOP:
174: mSuManager.stop((String) argumentList[0]);
175: break;
176: case SHUTDOWN:
177: mSuManager.shutDown((String) argumentList[0]);
178: break;
179: case UNDEPLOY:
180: returnValue = mSuManager.undeploy(
181: (String) argumentList[0],
182: (String) argumentList[1]);
183: break;
184: default:
185: break;
186: }
187: } finally {
188: Thread.currentThread().setContextClassLoader(cl);
189: }
190:
191: return (Object) returnValue;
192: }
193:
194: /**
195: * Reset the instance with a new operation. Values must be either DEPLOY,
196: * INIT, START, STOP, SHUTDOWN, or UNDEPLOY. This method reinitializes the
197: * instance to reuse for another operation.
198: *
199: * @param operation the type of operation to be performed, either DEPLOY,
200: * INIT, START, STOP, SHUTDOWN, or UNDEPLOY.
201: */
202: void resetOperation(int operation) {
203: if (DEPLOY == operation || INIT == operation
204: || START == operation || STOP == operation
205: || SHUTDOWN == operation || UNDEPLOY == operation) {
206: mOperation = operation;
207: super .reset();
208: } else {
209: throw new IllegalArgumentException(mTranslator.getString(
210: LocalStringKeys.INVALID_ARGUMENT, "operation",
211: new Integer(operation)));
212: }
213: }
214:
215: }
|