KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > thread > ComponentLifeCycleThread


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id$
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.petals.jbi.component.thread;
26
27 import javax.jbi.JBIException;
28 import javax.jbi.component.Component;
29 import javax.jbi.component.ComponentContext;
30 import javax.management.ObjectName JavaDoc;
31
32 /**
33  * The component life cycle thread. Each component life cycle have its own
34  * thread to proces operation that requires own ClassLoader.
35  *
36  * <b>NOTE : This must implements the javax.jbi.component.ComponentLifeCycle
37  * interface but due to the use of Thread and the start/stop methods it is
38  * impossible to do this. LET'S SEE IF FUTURE JAVA VERSION ALLOWS TO DO THINGS
39  * LIKE THAT</b>
40  *
41  * @author Christophe Hamerling - EBM WebSourcing
42  *
43  */

44 public class ComponentLifeCycleThread extends AbstractThread {
45
46     /**
47      * The component
48      */

49     private Component component;
50
51     /**
52      * The component context
53      */

54     private ComponentContext context;
55
56     /**
57      *
58      */

59     private ObjectName JavaDoc objectName;
60
61     /*
62      * STATICS : The actions to process
63      */

64     public static final int INIT = 0;
65
66     public static final int START = 1;
67
68     public static final int STOP = 2;
69
70     public static final int SHUTDOWN = 3;
71
72     public static final int MBEANNAME = 4;
73
74     /**
75      * Creates a new instance of ComponentLifeCycle
76      *
77      * @param component
78      * @param context
79      */

80     public ComponentLifeCycleThread(Component component) {
81         super();
82         this.component = component;
83         this.setName(component.toString() + "-life Cycle Thread");
84     }
85
86     /**
87      * Excute the task.
88      *
89      * @return -1 if the current thread needs to be shutdowned
90      */

91     protected int doTask(int action) {
92         int result = 0;
93         try {
94             switch (action) {
95                 case INIT:
96                     getComponent().getLifeCycle().init(context);
97                     break;
98
99                 case START:
100                     getComponent().getLifeCycle().start();
101                     break;
102
103                 case STOP:
104                     getComponent().getLifeCycle().stop();
105                     break;
106
107                 case SHUTDOWN:
108                     getComponent().getLifeCycle().shutDown();
109                     break;
110
111                 case MBEANNAME:
112                     objectName = getComponent().getLifeCycle()
113                         .getExtensionMBeanName();
114                     break;
115
116                 case SHUTDOWNTHREAD:
117                     result = -1;
118                     break;
119
120                 default:
121                     // NOTHING
122
break;
123             }
124
125         } catch (JBIException jbie) {
126             jbiException = jbie;
127         }
128
129         return result;
130     }
131
132     /**
133      * @return the component
134      */

135     public Component getComponent() {
136         return component;
137     }
138
139     /**
140      * Start life cycle step
141      *
142      */

143     public void doStart() throws JBIException {
144         execute(START);
145
146         JBIException jbie = getJbiException();
147         if (jbie != null) {
148             throw jbie;
149         }
150     }
151
152     /**
153      * Stop life cycle step
154      *
155      */

156     public void doStop() throws JBIException {
157         execute(STOP);
158
159         JBIException jbie = getJbiException();
160         if (jbie != null) {
161             throw jbie;
162         }
163     }
164
165     /**
166      * Shutdown life cycle step
167      *
168      */

169     public void doShutdown() throws JBIException {
170         execute(SHUTDOWN);
171
172         JBIException jbie = getJbiException();
173         if (jbie != null) {
174             throw jbie;
175         }
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see javax.jbi.component.ComponentLifeCycle#init(javax.jbi.component.ComponentContext)
182      */

183     public void doInit(ComponentContext context) throws JBIException {
184         this.context = context;
185         execute(INIT);
186
187         JBIException jbie = getJbiException();
188         if (jbie != null) {
189             throw jbie;
190         }
191     }
192
193     /*
194      * (non-Javadoc)
195      *
196      * @see javax.jbi.component.ComponentLifeCycle#getExtensionMBeanName()
197      */

198     public ObjectName JavaDoc getExtensionMBeanName() {
199         execute(MBEANNAME);
200         return objectName;
201     }
202 }
Popular Tags