001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.jbi.management;
018
019 import java.beans.PropertyChangeEvent;
020 import java.beans.PropertyChangeListener;
021 import javax.jbi.JBIException;
022 import javax.jbi.management.LifeCycleMBean;
023 import javax.management.JMException;
024 import javax.management.MBeanAttributeInfo;
025 import javax.management.MBeanOperationInfo;
026
027 /**
028 * A BasicLifeCycle implementation
029 *
030 * @version $Revision: 564607 $
031 */
032 public abstract class BaseLifeCycle implements LifeCycleMBean, MBeanInfoProvider {
033
034 public static final String INITIALIZED = "Initialized";
035
036 protected String currentState = LifeCycleMBean.UNKNOWN;
037
038 protected PropertyChangeListener listener;
039
040 /**
041 * Get the name of the item
042 *
043 * @return the name
044 */
045 public String getName() {
046 String name = getClass().getName();
047 int index = name.lastIndexOf(".");
048 if (index >= 0 && (index + 1) < name.length()) {
049 name = name.substring(index + 1);
050 }
051 return name;
052 }
053
054 /**
055 * Get the type of the item
056 *
057 * @return the type
058 */
059 public String getType() {
060 String name = getClass().getName();
061 int index = name.lastIndexOf(".");
062 if (index >= 0 && (index + 1) < name.length()) {
063 name = name.substring(index + 1);
064 }
065 return name;
066 }
067
068 public String getSubType() {
069 return null;
070 }
071
072 /**
073 * set state to initialized
074 *
075 * @throws JBIException
076 *
077 */
078 protected void init() throws JBIException {
079 setCurrentState(INITIALIZED);
080 }
081
082 /**
083 * Start the item.
084 *
085 * @exception javax.jbi.JBIException
086 * if the item fails to start.
087 */
088 public void start() throws javax.jbi.JBIException {
089 setCurrentState(LifeCycleMBean.STARTED);
090 }
091
092 /**
093 * Stop the item. This suspends current messaging activities.
094 *
095 * @exception javax.jbi.JBIException
096 * if the item fails to stop.
097 */
098 public void stop() throws javax.jbi.JBIException {
099 setCurrentState(LifeCycleMBean.STOPPED);
100 }
101
102 /**
103 * Shut down the item. The releases resources, preparatory to
104 * uninstallation.
105 *
106 * @exception javax.jbi.JBIException
107 * if the item fails to shut down.
108 */
109 public void shutDown() throws javax.jbi.JBIException {
110 setCurrentState(LifeCycleMBean.SHUTDOWN);
111 }
112
113 /**
114 * Get the current state of this managed compononent.
115 *
116 * @return the current state of this managed component (must be one of the
117 * string constants defined by this interface)
118 * @org.apache.xbean.Property hidden="true"
119 */
120 public String getCurrentState() {
121 return currentState;
122 }
123
124 /**
125 * Set the current state
126 *
127 * @param newValue
128 */
129 protected void setCurrentState(String newValue) {
130 String oldValue = currentState;
131 this.currentState = newValue;
132 firePropertyChanged("currentState", oldValue, newValue);
133 }
134
135 /**
136 * @return true if the object is in the started state
137 */
138 public boolean isStarted() {
139 return currentState != null && currentState.equals(LifeCycleMBean.STARTED);
140 }
141
142 /**
143 * @return true if the object is stopped
144 */
145 public boolean isStopped() {
146 return currentState != null && currentState.equals(LifeCycleMBean.STOPPED);
147 }
148
149 /**
150 * @return true if the object is shutDown
151 */
152 public boolean isShutDown() {
153 return currentState != null && currentState.equals(LifeCycleMBean.SHUTDOWN);
154 }
155
156 /**
157 * @return true if the object is shutDown
158 */
159 public boolean isInitialized() {
160 return currentState != null && currentState.equals(INITIALIZED);
161 }
162
163 /**
164 * @return true if the object is shutDown
165 */
166 public boolean isUnknown() {
167 return currentState == null || currentState.equals(LifeCycleMBean.UNKNOWN);
168 }
169
170 /**
171 * Get an array of MBeanAttributeInfo
172 *
173 * @return array of AttributeInfos
174 * @throws JMException
175 */
176 public MBeanAttributeInfo[] getAttributeInfos() throws JMException {
177 AttributeInfoHelper helper = new AttributeInfoHelper();
178 helper.addAttribute(getObjectToManage(), "currentState", "Current State of Managed Item");
179 helper.addAttribute(getObjectToManage(), "name", "name of the Item");
180 helper.addAttribute(getObjectToManage(), "description", "description of the Item");
181 return helper.getAttributeInfos();
182 }
183
184 /**
185 * Get an array of MBeanOperationInfo
186 *
187 * @return array of OperationInfos
188 * @throws JMException
189 */
190 public MBeanOperationInfo[] getOperationInfos() throws JMException {
191 OperationInfoHelper helper = new OperationInfoHelper();
192 helper.addOperation(getObjectToManage(), "start", "start the item");
193 helper.addOperation(getObjectToManage(), "stop", "stop the item");
194 helper.addOperation(getObjectToManage(), "shutDown", "shutdown the item");
195 return helper.getOperationInfos();
196 }
197
198 /**
199 * Get the Object to Manage
200 *
201 * @return the Object to Manage
202 */
203 public Object getObjectToManage() {
204 return this;
205 }
206
207 /**
208 * Register for propertyChange events
209 *
210 * @param l
211 * @org.apache.xbean.Property hidden="true"
212 */
213 public void setPropertyChangeListener(PropertyChangeListener l) {
214 this.listener = l;
215 }
216
217 protected void firePropertyChanged(String name, Object oldValue, Object newValue) {
218 PropertyChangeListener l = listener;
219 if (l != null) {
220 PropertyChangeEvent event = new PropertyChangeEvent(this, name, oldValue, newValue);
221 l.propertyChange(event);
222 }
223 }
224 }