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: * @(#)ComponentOperation.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: /**
032: * This class extends the Operation abstract class, and is designed to run on a
033: * separate thread. The main purpose of this class is to provide a way to run
034: * operations on multiple components (BCs and SEs) in parallel, and optionally
035: * wait for all of them to complete.
036: *
037: * To use this class, create an instance for each BC or SE, providing the
038: * appropriate Component instances and and operation types to the constructor.
039: * Then, create a thread for each instance and call its start() method.
040: *
041: * If the parent thread needs to wait for all of the operations to complete,
042: * a single OperationCounter instance must be created and provided to the
043: * constructor when creating the instances of this class. Then after all of the
044: * threads have been started, the parent thread calls wait() on the instance
045: * of OperationCounter. Optionally, the wait() call can have a timeout parameter
046: * to limit the amount of time the parent thread will wait for completion of
047: * all of the threads.
048: *
049: * In the Component Framework, this class is used in conjunction with the
050: * OperationCounter class to multi-thread startup and shutdown of BCs and SEs
051: * during AppServer startup and shutdown. This design prevents an ill-behaved
052: * BC or SE from hanging the AppServer startup or shutdown should the BC or
053: * SE hang.
054: *
055: * @author Sun Microsystems, Inc.
056: */
057: class ComponentOperation extends Operation {
058: /**
059: * The Component to be operated upon.
060: */
061: private Component mComponent;
062:
063: /**
064: * Flag indicating whether the admin service needs notification of
065: * operation completion.
066: */
067: private boolean mNotify;
068:
069: /**
070: * The type of operation to be performed.
071: */
072: private int mOperation;
073:
074: /**
075: * The JBI environment context.
076: */
077: private EnvironmentContext mEnv;
078:
079: /**
080: * The message string translator.
081: */
082: private StringTranslator mTranslator;
083:
084: /**
085: * Value for mOperation for an initialize operation.
086: */
087: public static final int INITIALIZE = 1;
088:
089: /**
090: * Value for mOperation for a startup operation.
091: */
092: public static final int STARTUP = 2;
093:
094: /**
095: * Value for mOperation for a shutdown operation.
096: */
097: public static final int SHUTDOWN = 3;
098:
099: /**
100: * Constructor.
101: *
102: * @param component the Component instance that represents the BC or SE
103: * to be operated upon.
104: * @param operation the type of operation to be performed, either
105: * INITIALIZE, STARTUP, or SHUTDOWN.
106: */
107: ComponentOperation(Component component, int operation) {
108: super (null, null);
109: init(component, operation);
110: }
111:
112: /**
113: * Constructor.
114: *
115: * @param counter the OperationCounter instance associated with this
116: * operation.
117: * @param component the Component instance that represents the BC or SE
118: * to be operated upon.
119: * @param operation the type of operation to be performed, either
120: * INITIALIZE, STARTUP, or SHUTDOWN.
121: */
122: ComponentOperation(OperationCounter counter, Component component,
123: int operation) {
124: super (counter, null);
125: init(component, operation);
126: }
127:
128: /**
129: * Constructor.
130: *
131: * @param counter the OperationCounter instance associated with this
132: * operation.
133: * @param component the Component instance that represents the BC or SE
134: * to be operated upon.
135: * @param operation the type of operation to be performed, either
136: * INITIALIZE, STARTUP, or SHUTDOWN.
137: * @param notify set to True if the framework should notify the admin
138: * service when the operation is complete.
139: */
140: ComponentOperation(OperationCounter counter, Component component,
141: int operation, boolean notify) {
142: super (counter, null);
143: init(component, operation);
144: mNotify = notify;
145: }
146:
147: /**
148: * Common constructor initialization code.
149: *
150: * @param component the Component instance that represents the BC or SE
151: * to be operated upon.
152: * @param operation the type of operation to be performed, either
153: * INITIALIZE, STARTUP, or SHUTDOWN.
154: */
155: void init(Component component, int operation) {
156: mEnv = EnvironmentContext.getInstance();
157: mTranslator = (StringTranslator) mEnv
158: .getStringTranslatorFor(this );
159:
160: if (null == component) {
161: throw new IllegalArgumentException(mTranslator.getString(
162: LocalStringKeys.NULL_ARGUMENT, "component"));
163: }
164: mComponent = component;
165:
166: if (INITIALIZE == operation || STARTUP == operation
167: || SHUTDOWN == operation) {
168: mOperation = operation;
169: } else {
170: throw new IllegalArgumentException(mTranslator.getString(
171: LocalStringKeys.INVALID_ARGUMENT, "operation",
172: new Integer(operation)));
173: }
174: }
175:
176: /**
177: * Return the Component instance for which this ComponentOperation was
178: * created.
179: *
180: * @return the instance of Component for this operation.
181: */
182: Component getComponent() {
183: return mComponent;
184: }
185:
186: /**
187: * Return the operation that this instance represents.
188: *
189: * @return the integer value of the operation code.
190: */
191: int getOperation() {
192: return mOperation;
193: }
194:
195: /**
196: * Overridden method to call the ComponentFramework to initialize, startup
197: * or shut down the BC or SE.
198: *
199: * @param argumentList the arguments for the operation. For this class this
200: * is always null.
201: * @return the returned string from operations that return a string or null
202: * for operations that have no return value.
203: * @throws Throwable if any error occurs.
204: */
205: Object process(Object argumentList[]) throws Throwable {
206: ComponentFramework cf = mEnv.getComponentFramework();
207: try {
208: if (mComponent.isBinding() || mComponent.isEngine()) {
209: if (INITIALIZE == mOperation) {
210: cf.initializeComponent(mComponent);
211: } else if (STARTUP == mOperation) {
212: cf.startComponent(mComponent);
213: } else if (SHUTDOWN == mOperation) {
214: cf.shutdownComponent(mComponent);
215: }
216: } else {
217: cf
218: .getLogger()
219: .warning(
220: mTranslator
221: .getString(
222: LocalStringKeys.CF_COMP_UNRECOGNIZED_TYPE,
223: mComponent.getName(),
224: mComponent
225: .getComponentType()
226: .toString()));
227: }
228: } catch (javax.jbi.JBIException jbiEx) {
229: // All exceptions caught here already have a formatted
230: // message that requires no further formatting.
231: cf.getLogger().warning(jbiEx.getMessage());
232: }
233:
234: // If requested, notify the admin service of completion.
235:
236: if (mNotify) {
237: mEnv.getManagementService().updateComponentState(
238: mComponent.getName());
239: }
240: return null;
241: }
242:
243: }
|