KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > management > BaseLifeCycle


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.management;
18
19 import java.beans.PropertyChangeEvent JavaDoc;
20 import java.beans.PropertyChangeListener JavaDoc;
21 import javax.jbi.JBIException;
22 import javax.jbi.management.LifeCycleMBean;
23 import javax.management.JMException JavaDoc;
24 import javax.management.MBeanAttributeInfo JavaDoc;
25 import javax.management.MBeanOperationInfo JavaDoc;
26
27 /**
28  * A BasicLifeCycle implementation
29  *
30  * @version $Revision: 426415 $
31  */

32 public abstract class BaseLifeCycle implements LifeCycleMBean, MBeanInfoProvider {
33     
34     public static final String JavaDoc INITIALIZED = "Initialized";
35     
36     protected String JavaDoc currentState = LifeCycleMBean.UNKNOWN;
37     
38     protected PropertyChangeListener JavaDoc listener;
39     
40     
41     /**
42      * Get the name of the item
43      * @return the name
44      */

45     public String JavaDoc getName() {
46         String JavaDoc name = getClass().getName();
47         int index = name.lastIndexOf(".");
48         if (index >= 0 && (index+1) < name.length()) {
49             name = name.substring(index+1);
50         }
51         return name;
52     }
53     
54     /**
55      * Get the type of the item
56      * @return the type
57      */

58     public String JavaDoc getType() {
59         String JavaDoc name = getClass().getName();
60         int index = name.lastIndexOf(".");
61         if (index >= 0 && (index+1) < name.length()) {
62             name = name.substring(index+1);
63         }
64         return name;
65     }
66     
67     public String JavaDoc getSubType() {
68         return null;
69     }
70     
71     /**
72      * set state to initialized
73      * @throws JBIException
74      *
75      */

76     protected void init() throws JBIException{
77         setCurrentState(INITIALIZED);
78     }
79
80     /**
81      * Start the item.
82      *
83      * @exception javax.jbi.JBIException if the item fails to start.
84      */

85     public void start() throws javax.jbi.JBIException {
86         setCurrentState(LifeCycleMBean.STARTED);
87     }
88
89     /**
90      * Stop the item. This suspends current messaging activities.
91      *
92      * @exception javax.jbi.JBIException if the item fails to stop.
93      */

94     public void stop() throws javax.jbi.JBIException {
95         setCurrentState(LifeCycleMBean.STOPPED);
96     }
97
98     /**
99      * Shut down the item. The releases resources, preparatory to uninstallation.
100      *
101      * @exception javax.jbi.JBIException if the item fails to shut down.
102      */

103     public void shutDown() throws javax.jbi.JBIException {
104         setCurrentState(LifeCycleMBean.SHUTDOWN);
105     }
106
107     /**
108      * Get the current state of this managed compononent.
109      *
110      * @return the current state of this managed component (must be one of the string constants defined by this
111      * interface)
112      */

113     public String JavaDoc getCurrentState() {
114         return currentState;
115     }
116     
117     /**
118      * Set the current state
119      * @param newValue
120      */

121     protected void setCurrentState(String JavaDoc newValue){
122         String JavaDoc oldValue = currentState;
123         this.currentState = newValue;
124         firePropertyChanged("currentState",oldValue,newValue);
125     }
126     
127     /**
128      * @return true if the object is in the started state
129      */

130     public boolean isStarted(){
131         return currentState != null && currentState.equals(LifeCycleMBean.STARTED);
132     }
133     
134     /**
135     * @return true if the object is stopped
136     */

137    public boolean isStopped(){
138        return currentState != null && currentState.equals(LifeCycleMBean.STOPPED);
139    }
140    
141    /**
142     * @return true if the object is shutDown
143     */

144    public boolean isShutDown(){
145        return currentState != null && currentState.equals(LifeCycleMBean.SHUTDOWN);
146    }
147    
148    /**
149     * @return true if the object is shutDown
150     */

151    public boolean isInitialized(){
152        return currentState != null && currentState.equals(INITIALIZED);
153    }
154    
155    /**
156     * @return true if the object is shutDown
157     */

158    public boolean isUnknown(){
159        return currentState == null || currentState.equals(LifeCycleMBean.UNKNOWN);
160    }
161     
162     /**
163      * Get an array of MBeanAttributeInfo
164      *
165      * @return array of AttributeInfos
166      * @throws JMException
167      */

168     public MBeanAttributeInfo JavaDoc[] getAttributeInfos() throws JMException JavaDoc {
169         AttributeInfoHelper helper = new AttributeInfoHelper();
170         helper.addAttribute(getObjectToManage(), "currentState", "Current State of Managed Item");
171         helper.addAttribute(getObjectToManage(), "name", "name of the Item");
172         helper.addAttribute(getObjectToManage(), "description", "description of the Item");
173         return helper.getAttributeInfos();
174     }
175
176     /**
177      * Get an array of MBeanOperationInfo
178      *
179      * @return array of OperationInfos
180      * @throws JMException
181      */

182     public MBeanOperationInfo JavaDoc[] getOperationInfos() throws JMException JavaDoc {
183         OperationInfoHelper helper = new OperationInfoHelper();
184         helper.addOperation(getObjectToManage(), "start", "start the item");
185         helper.addOperation(getObjectToManage(), "stop", "stop the item");
186         helper.addOperation(getObjectToManage(), "shutDown", "shutdown the item");
187         return helper.getOperationInfos();
188     }
189
190     /**
191      * Get the Object to Manage
192      *
193      * @return the Object to Manage
194      */

195     public Object JavaDoc getObjectToManage() {
196         return this;
197     }
198     
199     /**
200      * Register for propertyChange events
201      * @param l
202      */

203     public void setPropertyChangeListener(PropertyChangeListener JavaDoc l){
204         this.listener = l;
205     }
206     
207     protected void firePropertyChanged(String JavaDoc name,Object JavaDoc oldValue, Object JavaDoc newValue){
208         PropertyChangeListener JavaDoc l = listener;
209         if (l != null){
210             PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc(this,name,oldValue,newValue);
211             l.propertyChange(event);
212         }
213     }
214 }
Popular Tags