1 31 package org.objectweb.proactive.core.descriptor.data; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.proactive.core.ProActiveException; 36 import org.objectweb.proactive.core.process.ExternalProcess; 37 import org.objectweb.proactive.core.process.ExternalProcessDecorator; 38 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl; 39 import org.objectweb.proactive.ext.security.PolicyServer; 40 import org.objectweb.proactive.ext.security.ProActiveSecurityDescriptorHandler; 41 42 import org.xml.sax.SAXException ; 43 44 import java.io.IOException ; 45 46 import java.security.cert.X509Certificate ; 47 48 import java.util.Collection ; 49 import java.util.Iterator ; 50 51 52 65 public class ProActiveDescriptorImpl implements ProActiveDescriptor { 66 protected static Logger logger = Logger.getLogger(ProActiveDescriptorImpl.class.getName()); 70 71 72 private java.util.HashMap virtualNodeMapping; 73 74 75 private java.util.HashMap virtualMachineMapping; 76 77 78 private java.util.HashMap processMapping; 79 80 81 private java.util.HashMap pendingProcessMapping; 82 83 84 private String url; 85 86 87 public PolicyServer policyServer; 88 public X509Certificate creatorCertificate; 89 public String securityFile; 90 91 95 98 public ProActiveDescriptorImpl(String url) { 99 virtualNodeMapping = new java.util.HashMap (); 100 virtualMachineMapping = new java.util.HashMap (); 101 processMapping = new java.util.HashMap (); 102 pendingProcessMapping = new java.util.HashMap (); 103 this.url = url; 104 } 105 106 public VirtualNode[] getVirtualNodes() { 110 int i = 0; 111 VirtualNode[] virtualNodeArray = new VirtualNode[virtualNodeMapping.size()]; 112 Collection collection = virtualNodeMapping.values(); 113 for (Iterator iter = collection.iterator(); iter.hasNext();) { 114 virtualNodeArray[i] = (VirtualNode) iter.next(); 115 i++; 116 } 117 return virtualNodeArray; 118 } 119 120 public VirtualNode getVirtualNode(String name) { 121 return (VirtualNode) virtualNodeMapping.get(name); 122 } 123 124 public VirtualMachine getVirtualMachine(String name) { 125 return (VirtualMachine) virtualMachineMapping.get(name); 126 } 127 128 public ExternalProcess getProcess(String name) { 129 return (ExternalProcess) processMapping.get(name); 130 } 131 132 public VirtualNode createVirtualNode(String vnName, boolean lookup) { 133 VirtualNode vn = getVirtualNode(vnName); 134 if (vn == null) { 135 if (lookup) { 136 vn = new VirtualNodeLookup(vnName); 137 } else { 138 vn = new VirtualNodeImpl(vnName, creatorCertificate, 139 policyServer); 140 } 141 142 virtualNodeMapping.put(vnName, vn); 143 if (logger.isInfoEnabled()) { 144 logger.info("created VirtualNode name=" + vnName); 145 } 146 } 147 return vn; 148 } 149 150 public VirtualMachine createVirtualMachine(String vmName) { 151 VirtualMachine vm = getVirtualMachine(vmName); 152 if (vm == null) { 153 vm = new VirtualMachineImpl(); 154 vm.setName(vmName); 155 virtualMachineMapping.put(vmName, vm); 156 } 157 if (logger.isDebugEnabled()) { 158 logger.debug("created virtualMachine name=" + vmName); 159 } 160 return vm; 161 } 162 163 public ExternalProcess createProcess(String processID, 164 String processClassName) throws ProActiveException { 165 ExternalProcess process = getProcess(processID); 166 if (process == null) { 167 process = createProcess(processClassName); 168 addExternalProcess(processID, process); 169 } 170 return process; 171 } 172 173 public ExternalProcess createProcess(String processClassName) 174 throws ProActiveException { 175 try { 176 Class processClass = Class.forName(processClassName); 177 return (ExternalProcess) processClass.newInstance(); 178 } catch (ClassNotFoundException e) { 179 throw new ProActiveException(e); 180 } catch (InstantiationException e) { 181 throw new ProActiveException(e); 182 } catch (IllegalAccessException e) { 183 throw new ProActiveException(e); 184 } 185 } 186 187 public void registerProcess(VirtualMachine virtualMachine, String processID) { 188 ExternalProcess process = getProcess(processID); 189 if (process == null) { 190 addPendingProcess(processID, virtualMachine); 191 } else { 192 virtualMachine.setProcess(process); 193 } 194 } 195 196 public void registerProcess(ExternalProcessDecorator compositeProcess, 197 String processID) { 198 ExternalProcess process = getProcess(processID); 199 if (process == null) { 200 addPendingProcess(processID, compositeProcess); 201 } else { 202 compositeProcess.setTargetProcess(process); 203 } 204 } 205 206 public void activateMappings() { 207 VirtualNode[] virtualNodeArray = getVirtualNodes(); 208 for (int i = 0; i < virtualNodeArray.length; i++) { 209 virtualNodeArray[i].activate(); 210 } 211 } 212 213 public void activateMapping(String virtualNodeName) { 214 VirtualNode virtualNode = getVirtualNode(virtualNodeName); 215 virtualNode.activate(); 216 } 217 218 public void killall(boolean softly) throws ProActiveException { 219 ProActiveRuntimeImpl part = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime(); 220 part.removeDescriptor(this.url); 221 VirtualNode[] vnArray = getVirtualNodes(); 222 for (int i = 0; i < vnArray.length; i++) { 223 vnArray[i].killAll(softly); 224 virtualNodeMapping.remove(vnArray[i].getName()); 225 } 227 } 228 229 233 public int getVirtualNodeMappingSize() { 234 return virtualNodeMapping.size(); 235 } 236 237 239 243 public void createPolicyServer(String file) { 244 securityFile = file; 245 try { 246 policyServer = ProActiveSecurityDescriptorHandler.createPolicyServer(file); 247 } catch (IOException e) { 254 e.printStackTrace(); 256 } catch (SAXException e) { 257 e.printStackTrace(); 259 } 260 } 261 262 public PolicyServer getPolicyServer() { 263 return policyServer; 264 } 265 266 269 public String getSecurityFilePath() { 270 return securityFile; 271 } 272 273 private void addExternalProcess(String processID, ExternalProcess process) { 280 ProcessUpdater processUpdater = (ProcessUpdater) pendingProcessMapping.remove(processID); 281 if (processUpdater != null) { 282 if (logger.isDebugEnabled()) { 283 logger.debug("Updating Process name=" + processID); 284 } 285 processUpdater.updateProcess(process); 286 } 287 processMapping.put(processID, process); 288 } 289 290 private void addPendingProcess(String processID, 291 VirtualMachine virtualMachine) { 292 ProcessUpdater updater = new VirtualMachineProcessUpdater(virtualMachine); 293 294 addUpdater(processID, updater); 296 } 297 298 private void addPendingProcess(String processID, 299 ExternalProcessDecorator compositeProcess) { 300 ProcessUpdater updater = new CompositeExternalProcessUpdater(compositeProcess); 301 302 addUpdater(processID, updater); 304 } 305 306 private void addUpdater(String processID, ProcessUpdater processUpdater) { 307 CompositeProcessUpdater compositeProcessUpdater = (CompositeProcessUpdater) pendingProcessMapping.get(processID); 308 if (compositeProcessUpdater == null) { 309 compositeProcessUpdater = new CompositeProcessUpdater(); 310 pendingProcessMapping.put(processID, compositeProcessUpdater); 312 } 313 compositeProcessUpdater.addProcessUpdater(processUpdater); 314 } 315 316 private interface ProcessUpdater { 320 public void updateProcess(ExternalProcess p); 321 } 322 323 private class CompositeProcessUpdater implements ProcessUpdater { 324 private java.util.ArrayList updaterList; 325 326 public CompositeProcessUpdater() { 327 updaterList = new java.util.ArrayList (); 328 } 329 330 public void addProcessUpdater(ProcessUpdater p) { 331 updaterList.add(p); 332 } 333 334 public void updateProcess(ExternalProcess p) { 335 java.util.Iterator it = updaterList.iterator(); 336 while (it.hasNext()) { 337 ProcessUpdater processUpdater = (ProcessUpdater) it.next(); 338 processUpdater.updateProcess(p); 339 } 340 updaterList.clear(); 341 } 342 } 343 344 private class CompositeExternalProcessUpdater implements ProcessUpdater { 345 private ExternalProcessDecorator compositeExternalProcess; 346 347 public CompositeExternalProcessUpdater( 348 ExternalProcessDecorator compositeExternalProcess) { 349 this.compositeExternalProcess = compositeExternalProcess; 350 } 351 352 public void updateProcess(ExternalProcess p) { 353 if (logger.isDebugEnabled()) { 354 logger.debug("Updating CompositeExternal Process"); 355 } 356 compositeExternalProcess.setTargetProcess(p); 357 } 358 } 359 360 private class VirtualMachineProcessUpdater implements ProcessUpdater { 361 private VirtualMachine virtualMachine; 362 363 public VirtualMachineProcessUpdater(VirtualMachine virtualMachine) { 364 this.virtualMachine = virtualMachine; 365 } 366 367 public void updateProcess(ExternalProcess p) { 368 if (logger.isDebugEnabled()) { 369 logger.debug("Updating VirtualMachine Process"); 370 } 371 virtualMachine.setProcess(p); 372 } 373 } 374 } 375 | Popular Tags |