1 17 18 package org.apache.naming; 19 20 import javax.naming.Context ; 21 import javax.management.NotificationBroadcasterSupport ; 22 import javax.management.ObjectName ; 23 import javax.management.MBeanServer ; 24 import javax.management.MBeanRegistration ; 25 import javax.management.AttributeChangeNotification ; 26 import javax.management.Notification ; 27 28 34 35 public final class NamingService 36 extends NotificationBroadcasterSupport 37 implements NamingServiceMBean, MBeanRegistration { 38 39 40 42 43 46 private int state = STOPPED; 47 48 49 52 private long sequenceNumber = 0; 53 54 55 58 private String oldUrlValue = ""; 59 60 61 64 private String oldIcValue = ""; 65 66 67 69 70 public ObjectName preRegister(MBeanServer server, ObjectName name) 71 throws Exception { 72 return new ObjectName (OBJECT_NAME); 73 } 74 75 76 public void postRegister(Boolean registrationDone) { 77 if (!registrationDone.booleanValue()) 78 destroy(); 79 } 80 81 82 public void preDeregister() 83 throws Exception { 84 } 85 86 87 public void postDeregister() { 88 destroy(); 89 } 90 91 92 94 95 98 public String getName() { 99 return NAME; 100 } 101 102 103 106 public int getState() { 107 return state; 108 } 109 110 111 114 public String getStateString() { 115 return states[state]; 116 } 117 118 119 122 public void start() 123 throws Exception { 124 125 Notification notification = null; 126 127 if (state != STOPPED) 128 return; 129 130 state = STARTING; 131 132 134 notification = new AttributeChangeNotification 135 (this, sequenceNumber++, System.currentTimeMillis(), 136 "Starting " + NAME, "State", "java.lang.Integer", 137 new Integer (STOPPED), new Integer (STARTING)); 138 sendNotification(notification); 139 140 try { 141 142 String value = "org.apache.naming"; 143 String 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 t) { 160 state = STOPPED; 161 notification = new AttributeChangeNotification 162 (this, sequenceNumber++, System.currentTimeMillis(), 163 "Stopped " + NAME, "State", "java.lang.Integer", 164 new Integer (STARTING), new Integer (STOPPED)); 165 sendNotification(notification); 166 } 167 168 state = STARTED; 169 notification = new AttributeChangeNotification 170 (this, sequenceNumber++, System.currentTimeMillis(), 171 "Started " + NAME, "State", "java.lang.Integer", 172 new Integer (STARTING), new Integer (STARTED)); 173 sendNotification(notification); 174 175 } 176 177 178 181 public void stop() { 182 183 Notification notification = null; 184 185 if (state != STARTED) 186 return; 187 188 state = STOPPING; 189 190 notification = new AttributeChangeNotification 191 (this, sequenceNumber++, System.currentTimeMillis(), 192 "Stopping " + NAME, "State", "java.lang.Integer", 193 new Integer (STARTED), new Integer (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 t) { 202 203 t.printStackTrace(); 205 206 } 207 208 state = STOPPED; 209 210 notification = new AttributeChangeNotification 211 (this, sequenceNumber++, System.currentTimeMillis(), 212 "Stopped " + NAME, "State", "java.lang.Integer", 213 new Integer (STOPPING), new Integer (STOPPED)); 214 sendNotification(notification); 215 216 } 217 218 219 222 public void destroy() { 223 224 if (getState() != STOPPED) 225 stop(); 226 227 } 228 229 230 } 231 | Popular Tags |