KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > NamingService


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
18 package org.apache.naming;
19
20 import javax.naming.Context JavaDoc;
21 import javax.management.NotificationBroadcasterSupport JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.MBeanRegistration JavaDoc;
25 import javax.management.AttributeChangeNotification JavaDoc;
26 import javax.management.Notification JavaDoc;
27
28 /**
29  * Implementation of the NamingService JMX MBean.
30  *
31  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
32  * @version $Revision: 467222 $
33  */

34
35 public final class NamingService
36     extends NotificationBroadcasterSupport JavaDoc
37     implements NamingServiceMBean, MBeanRegistration JavaDoc {
38     
39     
40     // ----------------------------------------------------- Instance Variables
41

42     
43     /**
44      * Status of the Slide domain.
45      */

46     private int state = STOPPED;
47     
48     
49     /**
50      * Notification sequence number.
51      */

52     private long sequenceNumber = 0;
53     
54     
55     /**
56      * Old URL packages value.
57      */

58     private String JavaDoc oldUrlValue = "";
59     
60     
61     /**
62      * Old initial context value.
63      */

64     private String JavaDoc oldIcValue = "";
65     
66     
67     // ---------------------------------------------- MBeanRegistration Methods
68

69     
70     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
71         throws Exception JavaDoc {
72         return new ObjectName JavaDoc(OBJECT_NAME);
73     }
74     
75     
76     public void postRegister(Boolean JavaDoc registrationDone) {
77         if (!registrationDone.booleanValue())
78             destroy();
79     }
80     
81     
82     public void preDeregister()
83         throws Exception JavaDoc {
84     }
85     
86     
87     public void postDeregister() {
88         destroy();
89     }
90     
91     
92     // ----------------------------------------------------- SlideMBean Methods
93

94     
95     /**
96      * Retruns the Catalina component name.
97      */

98     public String JavaDoc getName() {
99         return NAME;
100     }
101     
102     
103     /**
104      * Returns the state.
105      */

106     public int getState() {
107         return state;
108     }
109     
110     
111     /**
112      * Returns a String representation of the state.
113      */

114     public String JavaDoc getStateString() {
115         return states[state];
116     }
117     
118     
119     /**
120      * Start the servlet container.
121      */

122     public void start()
123         throws Exception JavaDoc {
124         
125         Notification JavaDoc notification = null;
126         
127         if (state != STOPPED)
128             return;
129         
130         state = STARTING;
131         
132         // Notifying the MBEan server that we're starting
133

134         notification = new AttributeChangeNotification JavaDoc
135             (this, sequenceNumber++, System.currentTimeMillis(),
136              "Starting " + NAME, "State", "java.lang.Integer",
137              new Integer JavaDoc(STOPPED), new Integer JavaDoc(STARTING));
138         sendNotification(notification);
139         
140         try {
141             
142             String JavaDoc value = "org.apache.naming";
143             String JavaDoc oldValue = System.getProperty(Context.URL_PKG_PREFIXES);
144             if (oldValue != null) {
145                 oldUrlValue = oldValue;
146                 value = oldValue + ":" + value;
147             }
148             System.setProperty(Context.URL_PKG_PREFIXES, value);
149             
150             oldValue = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
151             if ((oldValue != null) && (oldValue.length() > 0)) {
152                 oldIcValue = oldValue;
153             } else {
154                 System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
155                                    Constants.Package
156                                    + ".java.javaURLContextFactory");
157             }
158             
159         } catch (Throwable JavaDoc t) {
160             state = STOPPED;
161             notification = new AttributeChangeNotification JavaDoc
162                 (this, sequenceNumber++, System.currentTimeMillis(),
163                  "Stopped " + NAME, "State", "java.lang.Integer",
164                  new Integer JavaDoc(STARTING), new Integer JavaDoc(STOPPED));
165             sendNotification(notification);
166         }
167         
168         state = STARTED;
169         notification = new AttributeChangeNotification JavaDoc
170             (this, sequenceNumber++, System.currentTimeMillis(),
171              "Started " + NAME, "State", "java.lang.Integer",
172              new Integer JavaDoc(STARTING), new Integer JavaDoc(STARTED));
173         sendNotification(notification);
174         
175     }
176     
177     
178     /**
179      * Stop the servlet container.
180      */

181     public void stop() {
182         
183         Notification JavaDoc notification = null;
184         
185         if (state != STARTED)
186             return;
187         
188         state = STOPPING;
189         
190         notification = new AttributeChangeNotification JavaDoc
191             (this, sequenceNumber++, System.currentTimeMillis(),
192              "Stopping " + NAME, "State", "java.lang.Integer",
193              new Integer JavaDoc(STARTED), new Integer JavaDoc(STOPPING));
194         sendNotification(notification);
195         
196         try {
197             
198             System.setProperty(Context.URL_PKG_PREFIXES, oldUrlValue);
199             System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldIcValue);
200             
201         } catch (Throwable JavaDoc t) {
202             
203             // FIXME
204
t.printStackTrace();
205             
206         }
207         
208         state = STOPPED;
209         
210         notification = new AttributeChangeNotification JavaDoc
211             (this, sequenceNumber++, System.currentTimeMillis(),
212              "Stopped " + NAME, "State", "java.lang.Integer",
213              new Integer JavaDoc(STOPPING), new Integer JavaDoc(STOPPED));
214         sendNotification(notification);
215         
216     }
217     
218     
219     /**
220      * Destroy servlet container (if any is running).
221      */

222     public void destroy() {
223         
224         if (getState() != STOPPED)
225             stop();
226         
227     }
228     
229     
230 }
231
Popular Tags