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: * @(#)ComponentLifeCycle.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 com.sun.jbi.ComponentState;
032: import com.sun.jbi.management.registry.Updater;
033: import javax.management.ObjectName;
034:
035: /**
036: * This class implements the LifeCycleMBean for a Component (BC or SE). This
037: * MBean acts as an agent between the JMX management service and the Component
038: * Framework to allow the Component Framework to keep track of the state of the
039: * component. Some methods in this MBean delegate to methods in the Component
040: * Framework, which in turn interact directly with the component. Other methods
041: * in this MBean interact directly with the component.
042: *
043: * @author Sun Microsystems, Inc.
044: */
045: public class ComponentLifeCycle implements
046: com.sun.jbi.framework.ComponentLifeCycleMBean {
047: /**
048: * Component instance handle.
049: */
050: private Component mComponent;
051:
052: /**
053: * Component Framework handle.
054: */
055: private ComponentFramework mComponentFramework;
056:
057: /**
058: * Local handle to StringTranslator.
059: */
060: private StringTranslator mTranslator;
061:
062: /**
063: * Local handle to registry updater.
064: */
065: private Updater mUpdater;
066:
067: /**
068: * Constructor.
069: * @param component is the Component instance for this component.
070: * @param componentFramework is the ComponentFramework service handle.
071: */
072: ComponentLifeCycle(Component component,
073: ComponentFramework componentFramework) {
074: mComponent = component;
075: mComponentFramework = componentFramework;
076: mTranslator = (StringTranslator) EnvironmentContext
077: .getInstance().getStringTranslatorFor(this );
078: }
079:
080: /**
081: * Get the current state of this component.
082: * @return The current state of the component as a string.
083: */
084: public String getCurrentState() {
085: String state;
086: if (mComponent.isStarted()) {
087: state = STARTED;
088: } else if (mComponent.isStopped()) {
089: state = STOPPED;
090: } else if (mComponent.isShutDown()) {
091: state = SHUTDOWN;
092: } else {
093: state = UNKNOWN;
094: }
095: return state;
096: }
097:
098: /**
099: * Get the JMX ObjectName for the DeployerMBean for this component. If
100: * there is none, return null.
101: * @throws javax.jbi.JBIException if there is a failure getting component
102: * information from the Component Registry.
103: * @return ObjectName the JMX object name of the DeployerMBean or null
104: * if there is no DeployerMBean.
105: */
106: public ObjectName getDeploymentMBeanName()
107: throws javax.jbi.JBIException {
108: ObjectName name = null;
109: name = mComponent.getDeployerMBeanName();
110: return name;
111: }
112:
113: /**
114: * Get the JMX ObjectName for any additional MBean for this component. If
115: * there is none, return null.
116: * @throws javax.jbi.JBIException if there is a failure getting component
117: * information from the Component Registry.
118: * @return ObjectName the JMX object name of the additional MBean or null
119: * if there is no additional MBean.
120: */
121: public ObjectName getExtensionMBeanName()
122: throws javax.jbi.JBIException {
123: ObjectName name = null;
124: javax.jbi.component.ComponentLifeCycle instance = mComponent
125: .getLifeCycleInstance(false);
126: if (instance != null) {
127: name = instance.getExtensionMBeanName();
128: }
129: return name;
130: }
131:
132: /**
133: * Start command. This starts up the component, calling its init() method
134: * if necessary.
135: * @throws javax.jbi.JBIException if the component fails to start or if
136: * another life cycle operation is already in progress.
137: */
138: public void start() throws javax.jbi.JBIException {
139: mComponent.setBusy();
140: mComponent.setDesiredState(ComponentState.STARTED);
141: persistState();
142: try {
143: mComponentFramework.startComponent(mComponent, true);
144: } finally {
145: mComponent.clearBusy();
146: }
147: return;
148: }
149:
150: /**
151: * Stop command. This stops the component but does not completely shut
152: * it down. It can be restarted without going through init() again.
153: * @throws javax.jbi.JBIException if the component fails to stop or if
154: * another life cycle operation is already in progress.
155: */
156: public void stop() throws javax.jbi.JBIException {
157: mComponent.setBusy();
158: mComponent.setDesiredState(ComponentState.STOPPED);
159: persistState();
160: try {
161: mComponentFramework.stopComponent(mComponent);
162: } finally {
163: mComponent.clearBusy();
164: }
165: return;
166: }
167:
168: /**
169: * Shutdown command. This completely shuts down the component and unloads
170: * its classes from the JBI Framework. The next start command will
171: * require init() to be called again.
172: * @throws javax.jbi.JBIException if the component fails to shut down or if
173: * another life cycle operation is already in progress.
174: */
175: public void shutDown() throws javax.jbi.JBIException {
176: shutDown(false);
177: }
178:
179: /**
180: * Shutdown command. This completely shuts down the component and unloads
181: * its classes from the JBI Framework. The next start command will require
182: * init() to be called again. If the force flag is set to true, the
183: * shutdown succeeds regardless of the success or failure of the component's
184: * shutDown() method.
185: * @throws javax.jbi.JBIException if any failure occurs or if another life
186: * cycle operation is already in progress.
187: */
188: public void shutDown(boolean force) throws javax.jbi.JBIException {
189: if (force) {
190: mComponent.setBusyForce();
191: } else {
192: mComponent.setBusy();
193: }
194: mComponent.setDesiredState(ComponentState.SHUTDOWN);
195: persistState();
196: try {
197: mComponentFramework.shutdownComponent(mComponent, force);
198: } finally {
199: mComponent.clearBusy();
200: }
201: return;
202: }
203:
204: /**
205: * Persist a change in the desired state of a component.
206: */
207: private void persistState() {
208: try {
209: if (null == mUpdater) {
210: mUpdater = ((com.sun.jbi.management.registry.Registry) EnvironmentContext
211: .getInstance().getRegistry()).getUpdater();
212: }
213: mUpdater.setComponentState(mComponent.getDesiredState(),
214: mComponent.getName());
215: } catch (com.sun.jbi.management.registry.RegistryException rEx) {
216: EnvironmentContext.getInstance().getLogger().log(
217: java.util.logging.Level.WARNING, rEx.getMessage(),
218: rEx);
219: }
220: }
221: }
|