1 package org.jacorb.poa; 2 3 22 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 import org.omg.CORBA.INTERNAL ; 26 import org.omg.PortableServer.POAManagerPackage.AdapterInactive ; 27 import org.omg.PortableServer.POAManagerPackage.State ; 28 29 35 36 public class POAManager 37 extends org.omg.PortableServer._POAManagerLocalBase 38 { 39 public State state = State.HOLDING; 40 private org.jacorb.orb.ORB orb; 41 private Vector poas = new Vector (); 42 private POAManagerMonitor monitor; 43 protected boolean poaCreationFailed; 44 45 46 protected POAManager(org.jacorb.orb.ORB _orb) 47 { 48 orb = _orb; 49 monitor = new POAManagerMonitorLightImpl(); 50 try 51 { 52 monitor.configure(orb.getConfiguration()); 53 } 54 catch (org.apache.avalon.framework.configuration.ConfigurationException ce) 55 { 56 } 58 monitor.init(this); 59 monitor.openMonitor(); 60 monitor.printMessage("ready"); 61 } 62 63 64 public void activate() throws AdapterInactive 65 { 66 checkCreation (); 67 68 switch (state.value()) 69 { 70 case State._INACTIVE : 71 throw new AdapterInactive (); 72 case State._ACTIVE : 73 break; 74 default: 75 state = State.ACTIVE; 76 monitor.setToActive(); 77 78 final POA [] poaArray; 79 80 synchronized (this) 81 { 82 poaArray = new POA[poas.size()]; 83 poas.copyInto(poaArray); 84 } 85 Thread thread = new Thread () 87 { 88 public void run() 89 { 90 for (int i=0; i<poaArray.length; i++) 91 { 92 try { 93 poaArray[i].changeToActive(); 94 } catch (Throwable e) {} 95 } 96 } 97 }; 98 thread.start(); 99 } 100 } 101 102 103 public void deactivate 104 (boolean etherealize_objects, boolean wait_for_completion) 105 throws AdapterInactive 106 { 107 checkCreation (); 108 109 if (wait_for_completion && isInInvocationContext()) 110 { 111 throw new org.omg.CORBA.BAD_INV_ORDER (); 112 } 113 114 switch (state.value()) 115 { 116 case State._INACTIVE : 117 throw new AdapterInactive (); 118 default : 119 state = State.INACTIVE; 120 monitor.setToInactive(wait_for_completion, etherealize_objects); 121 122 final boolean etherealize = etherealize_objects; 123 final POA [] poaArray; 124 125 synchronized (this) 126 { 127 poaArray = new POA[poas.size()]; 128 poas.copyInto(poaArray); 129 } 130 Thread thread = new Thread () 132 { 133 public void run() 134 { 135 for (int i=poaArray.length-1; i>=0; i--) 136 { 137 try { 138 poaArray[i].changeToInactive(etherealize); 139 } catch (Throwable e) {} 140 } 141 } 142 }; 143 thread.start(); 144 if (wait_for_completion) 145 { 146 try { 147 thread.join(); 148 } catch (InterruptedException e) {} 149 } 150 } 151 } 152 153 154 public void discard_requests(boolean wait_for_completion) 155 throws AdapterInactive 156 { 157 checkCreation (); 158 159 if (wait_for_completion && isInInvocationContext()) 160 { 161 throw new org.omg.CORBA.BAD_INV_ORDER (); 162 } 163 164 switch (state.value()) 165 { 166 case State._INACTIVE : 167 throw new AdapterInactive (); 168 case State._DISCARDING : 169 break; 170 default : 171 state = State.DISCARDING; 172 monitor.setToDiscarding(wait_for_completion); 173 174 final POA [] poaArray; 175 176 synchronized (this) 177 { 178 poaArray = new POA[poas.size()]; 179 poas.copyInto(poaArray); 180 } 181 Thread thread = new Thread () 183 { 184 public void run() 185 { 186 for (int i=poaArray.length-1; i>=0; i--) 187 { 188 try { 189 poaArray[i].changeToDiscarding(); 190 } catch (Throwable e) {} 191 } 192 } 193 }; 194 thread.start(); 195 if (wait_for_completion) 196 { 197 try { 198 thread.join(); 199 } catch (InterruptedException e) {} 200 } 201 } 202 } 203 204 205 public State get_state() 206 { 207 return state; 208 } 209 210 211 protected synchronized POA getRegisteredPOA(String name) 212 { 213 POA result; 214 Enumeration en = poas.elements(); 215 while (en.hasMoreElements()) 216 { 217 result = (POA) en.nextElement(); 218 if (name.equals(result._getQualifiedName())) 219 { 220 return result; 221 } 222 } 223 throw new INTERNAL 224 ( 225 "POA not registered: " + 226 POAConstants.ROOT_POA_NAME+ 227 POAConstants.OBJECT_KEY_SEPARATOR+ 228 name 229 ); 230 } 231 232 233 public void hold_requests(boolean wait_for_completion) 234 throws AdapterInactive 235 { 236 checkCreation (); 237 238 if (wait_for_completion && isInInvocationContext()) 239 { 240 throw new org.omg.CORBA.BAD_INV_ORDER (); 241 } 242 switch (state.value()) 243 { 244 case State._INACTIVE : 245 throw new AdapterInactive (); 246 case State._HOLDING : 247 break; 248 default : 249 state = State.HOLDING; 250 monitor.setToHolding(wait_for_completion); 251 252 final POA [] poaArray; 253 254 synchronized (this) 255 { 256 poaArray = new POA[poas.size()]; 257 poas.copyInto(poaArray); 258 } 259 Thread thread = new Thread () 261 { 262 public void run() 263 { 264 for (int i=poaArray.length-1; i>=0; i--) 265 { 266 try { 267 poaArray[i].changeToHolding(); 268 } catch (Throwable e) {} 269 } 270 } 271 }; 272 thread.start(); 273 if (wait_for_completion) 274 { 275 try { 276 thread.join(); 277 } catch (InterruptedException e) {} 278 } 279 } 280 } 281 282 283 287 private boolean isInInvocationContext() 288 { 289 try { 290 if (orb.getPOACurrent().getORB() == orb) return true; 291 292 } catch (org.omg.PortableServer.CurrentPackage.NoContext e) {} 293 return false; 294 } 295 296 297 protected synchronized void registerPOA(POA poa) 298 { 299 if (!poas.contains(poa)) 300 { 301 poas.addElement(poa); 302 monitor.addPOA(poa._getQualifiedName()); 303 } 304 } 305 306 307 protected void setMonitor(POAManagerMonitor _monitor) 308 { 309 monitor = _monitor; 310 } 311 312 313 protected synchronized void unregisterPOA(POA poa) 314 { 315 poas.removeElement(poa); 316 monitor.removePOA(poa._getQualifiedName()); 317 } 318 319 320 private void checkCreation () 321 { 322 if (poaCreationFailed) 323 { 324 throw new org.omg.CORBA.INTERNAL ("POA Creation failed; unable to deactive"); 325 } 326 } 327 } 328 | Popular Tags |