KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > Slide


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/Slide.java,v 1.11 2004/08/05 15:42:32 unico Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/08/05 15:42:32 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.common;
25
26 import java.io.FileInputStream JavaDoc;
27
28 import javax.management.AttributeChangeNotification JavaDoc;
29 import javax.management.MBeanRegistration JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.Notification JavaDoc;
32 import javax.management.NotificationBroadcasterSupport JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.xml.parsers.SAXParser JavaDoc;
35 import javax.xml.parsers.SAXParserFactory JavaDoc;
36
37 import org.apache.slide.authenticate.SecurityToken;
38 import org.apache.slide.util.conf.Configuration;
39 import org.apache.slide.util.conf.ConfigurationElement;
40 import org.apache.slide.util.conf.Populate;
41 import org.xml.sax.InputSource JavaDoc;
42
43 /**
44  * Implementation of the Slide JMX MBean.
45  *
46  * @version $Revision: 1.11 $
47  */

48 public final class Slide
49     extends NotificationBroadcasterSupport JavaDoc
50     implements SlideMBean, MBeanRegistration JavaDoc {
51     
52     
53     // ----------------------------------------------------- Instance Variables
54

55     
56     /**
57      * Status of the Slide domain.
58      */

59     private int state = STOPPED;
60     
61     
62     /**
63      * Config file name.
64      */

65     private String JavaDoc configFile = null;
66     
67     
68     /**
69      * Notification sequence number.
70      */

71     private long sequenceNumber = 0;
72     
73     
74     // ---------------------------------------------- MBeanRegistration Methods
75

76     
77     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
78         throws Exception JavaDoc {
79         return new ObjectName JavaDoc(OBJECT_NAME);
80     }
81     
82     
83     public void postRegister(Boolean JavaDoc registrationDone) {
84         if (!registrationDone.booleanValue())
85             destroy();
86     }
87     
88     
89     public void preDeregister()
90         throws Exception JavaDoc {
91     }
92     
93     
94     public void postDeregister() {
95         destroy();
96     }
97     
98     
99     // ----------------------------------------------------- SlideMBean Methods
100

101     
102     /**
103      * Retruns the Slide component name.
104      */

105     public String JavaDoc getName() {
106         return NAME;
107     }
108     
109     
110     /**
111      * Returns the state of the Slide domain.
112      */

113     public int getState() {
114         return state;
115     }
116     
117     
118     /**
119      * Returns a String representation of the domain's state.
120      */

121     public String JavaDoc getStateString() {
122         return states[state];
123     }
124     
125     
126     /**
127      * Auto initializes domain.
128      */

129     public void init()
130         throws Exception JavaDoc {
131         
132     }
133     
134     
135     /**
136      * Initializes domain, and specify a configuration file to use.
137      */

138     public void init(String JavaDoc configFile)
139         throws Exception JavaDoc {
140         
141         this.configFile = configFile;
142         
143     }
144     
145     
146     /**
147      * Start the domain.
148      */

149     public void start()
150         throws Exception JavaDoc {
151         
152         Notification JavaDoc notification = null;
153         
154         if (state != STOPPED)
155             return;
156         
157         state = STARTING;
158         
159         // Notifying the MBEan server that we're starting
160

161         notification = new AttributeChangeNotification JavaDoc
162             (this, sequenceNumber++, System.currentTimeMillis(),
163              "Starting " + NAME, "State", "java.lang.Integer",
164              new Integer JavaDoc(STOPPED), new Integer JavaDoc(STARTING));
165         sendNotification(notification);
166         
167         try {
168             
169             if (configFile == null) {
170                 
171                 Domain.selfInit();
172                 
173             } else {
174                 
175                 SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
176                 factory.setNamespaceAware(false);
177                 factory.setValidating(false);
178                 SAXParser JavaDoc parser = factory.newSAXParser();
179                 
180                 FileInputStream JavaDoc is = new FileInputStream JavaDoc(configFile);
181                 //init(reader);
182
Populate pop = new Populate();
183                 Configuration slideConfiguration =
184                     new ConfigurationElement(pop.load(new InputSource JavaDoc(is),
185                                                       parser.getXMLReader()));
186                 
187                 Domain.init(slideConfiguration);
188                 
189                 Domain.start();
190                 
191             }
192             
193         } catch (Throwable JavaDoc t) {
194             state = STOPPED;
195             notification = new AttributeChangeNotification JavaDoc
196                 (this, sequenceNumber++, System.currentTimeMillis(),
197                  "Stopped " + NAME, "State", "java.lang.Integer",
198                  new Integer JavaDoc(STARTING), new Integer JavaDoc(STOPPED));
199             sendNotification(notification);
200         }
201         
202         state = STARTED;
203         notification = new AttributeChangeNotification JavaDoc
204             (this, sequenceNumber++, System.currentTimeMillis(),
205              "Started " + NAME, "State", "java.lang.Integer",
206              new Integer JavaDoc(STARTING), new Integer JavaDoc(STARTED));
207         sendNotification(notification);
208         
209     }
210     
211     
212     /**
213      * Close all access tokens to the domain.
214      */

215     public void stop() {
216         
217         Notification JavaDoc notification = null;
218         
219         if (state != STARTED)
220             return;
221         
222         state = STOPPING;
223         
224         notification = new AttributeChangeNotification JavaDoc
225             (this, sequenceNumber++, System.currentTimeMillis(),
226              "Stopping " + NAME, "State", "java.lang.Integer",
227              new Integer JavaDoc(STARTED), new Integer JavaDoc(STOPPING));
228         sendNotification(notification);
229         
230         try {
231             
232             Domain.stop();
233             
234         } catch (Throwable JavaDoc t) {
235             
236             // FIXME
237
t.printStackTrace();
238             
239         }
240         
241         state = STOPPED;
242         
243         notification = new AttributeChangeNotification JavaDoc
244             (this, sequenceNumber++, System.currentTimeMillis(),
245              "Stopped " + NAME, "State", "java.lang.Integer",
246              new Integer JavaDoc(STOPPING), new Integer JavaDoc(STOPPED));
247         sendNotification(notification);
248         
249     }
250     
251     
252     /**
253      * Destroy domain.
254      */

255     public void destroy() {
256         
257         if (getState() != STOPPED)
258             stop();
259         
260     }
261     
262     
263     /**
264      * Access a Namespace.
265      *
266      * @param token Entity which wants access
267      * @param namespaceName Name of the namespace on which access is requested
268      * @return NamespaceAccessToken Access token to the namespace
269      */

270     public NamespaceAccessToken accessNamespace(SecurityToken token,
271                                                 String JavaDoc namespaceName) {
272         return Domain.accessNamespace(token, namespaceName);
273     }
274     
275     
276     /**
277      * Close a namespace.
278      *
279      * @param token Namespace access token
280      */

281     public void closeNamespace(NamespaceAccessToken token) {
282         Domain.closeNamespace(token);
283     }
284     
285     
286     /**
287      * Clsose a namespace.
288      *
289      * @param token Entity which wants to close the namespace
290      * @param namespaceName Name of the namespace
291      */

292     public void closeNamespace(SecurityToken token, String JavaDoc namespaceName) {
293         Domain.closeNamespace(token, namespaceName);
294     }
295     
296     
297     /**
298      * Access a Domain.
299      *
300      * @param token Service who wants access
301      * @return DomainAccessToken Access token to the domain
302      */

303     public DomainAccessToken accessDomain(SecurityToken token) {
304         return Domain.accessDomain(token);
305     }
306     
307     
308 }
309
Popular Tags