KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > descriptor > data > VirtualNodeImpl


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.descriptor.data;
32
33 import org.objectweb.proactive.ProActive;
34 import org.objectweb.proactive.core.ProActiveException;
35 import org.objectweb.proactive.core.event.RuntimeRegistrationEvent;
36 import org.objectweb.proactive.core.event.RuntimeRegistrationEventListener;
37 import org.objectweb.proactive.core.node.Node;
38 import org.objectweb.proactive.core.node.NodeException;
39 import org.objectweb.proactive.core.node.NodeFactory;
40 import org.objectweb.proactive.core.node.NodeImpl;
41 import org.objectweb.proactive.core.process.ExternalProcess;
42 import org.objectweb.proactive.core.process.ExternalProcessDecorator;
43 import org.objectweb.proactive.core.process.JVMProcess;
44 import org.objectweb.proactive.core.process.globus.GlobusProcess;
45 import org.objectweb.proactive.core.process.lsf.LSFBSubProcess;
46 import org.objectweb.proactive.core.process.prun.PrunSubProcess;
47 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
48 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
49 import org.objectweb.proactive.core.runtime.RuntimeFactory;
50 import org.objectweb.proactive.core.util.UrlBuilder;
51 import org.objectweb.proactive.ext.security.PolicyServer;
52
53 import java.io.Serializable JavaDoc;
54
55 import java.security.cert.X509Certificate JavaDoc;
56
57 import java.util.Hashtable JavaDoc;
58
59
60 /**
61  * A <code>VirtualNode</code> is a conceptual entity that represents one or several nodes. After activation
62  * a <code>VirtualNode</code> represents one or several nodes.
63  *
64  * @author ProActive Team
65  * @version 1.0, 2002/09/20
66  * @since ProActive 0.9.3
67  * @see ProActiveDescriptor
68  * @see VirtualMachine
69  */

70 public class VirtualNodeImpl extends RuntimeDeploymentProperties
71     implements VirtualNode, Serializable JavaDoc, RuntimeRegistrationEventListener {
72     //
73
// ----- PRIVATE MEMBERS -----------------------------------------------------------------------------------
74
//
75

76     /** Reference on the local runtime*/
77     protected transient ProActiveRuntimeImpl proActiveRuntimeImpl;
78
79     /** the name of this VirtualNode */
80     private String JavaDoc name;
81
82     /** the property of this virtualNode, property field can take five value: null,unique, unique_singleAO, multiple, multiple_cyclic */
83     private String JavaDoc property;
84
85     /** the list of virtual machines associated with this VirtualNode */
86     private java.util.ArrayList JavaDoc virtualMachines;
87
88     /** index of the last associated jvm used */
89     private int lastVirtualMachineIndex;
90
91     /** the list of nodes linked to this VirtualNode that have been created*/
92     private java.util.ArrayList JavaDoc createdNodes;
93
94     /** index of the last node used */
95     private int lastNodeIndex;
96
97     /** Number of Nodes mapped to this VirtualNode in the XML Descriptor */
98     private int nodeCount;
99
100     /** Number of Nodes mapped to this VitualNode in the XML Descriptor that are actually created */
101     private int nodeCountCreated;
102
103     /** true if the node has been created*/
104     private boolean nodeCreated = false;
105     private boolean isActivated = false;
106
107     /** the list of VitualNodes Id that this VirualNode is waiting for in order to create Nodes on a JVM
108      * already assigned in the XML descriptor */

109     private Hashtable JavaDoc awaitedVirtualNodes;
110     private String JavaDoc registrationProtocol;
111     private boolean registration = false;
112     private boolean waitForTimeout = false;
113     protected int MAX_RETRY = 70;
114     private Object JavaDoc uniqueActiveObject = null;
115     private X509Certificate JavaDoc creatorCertificate;
116     private PolicyServer policyServer;
117     private String JavaDoc policyServerFile;
118     private String JavaDoc jobID = ProActive.getJobId();
119
120     //
121
// ----- CONSTRUCTORS -----------------------------------------------------------------------------------
122
//
123

124     /**
125      * Contructs a new intance of VirtualNode
126      */

127     VirtualNodeImpl() {
128     }
129
130     /**
131      * Contructs a new intance of VirtualNode
132      */

133     VirtualNodeImpl(String JavaDoc name, X509Certificate JavaDoc creatorCertificate,
134         PolicyServer policyServer) {
135         this.name = name;
136         virtualMachines = new java.util.ArrayList JavaDoc(5);
137         createdNodes = new java.util.ArrayList JavaDoc();
138         awaitedVirtualNodes = new Hashtable JavaDoc();
139         proActiveRuntimeImpl = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime();
140         if (logger.isDebugEnabled()) {
141             logger.debug("vn " + this.name + " registered on " +
142                 proActiveRuntimeImpl.getVMInformation().getVMID().toString());
143         }
144         proActiveRuntimeImpl.addRuntimeRegistrationEventListener(this);
145         proActiveRuntimeImpl.registerLocalVirtualNode(this, this.name);
146         // SECURITY
147
this.creatorCertificate = creatorCertificate;
148         this.policyServer = policyServer;
149     }
150
151     //
152
// ----- PUBLIC METHODS -----------------------------------------------------------------------------------
153
//
154
public void setProperty(String JavaDoc value) {
155         this.property = value;
156     }
157
158     public String JavaDoc getProperty() {
159         return property;
160     }
161
162     public void setTimeout(String JavaDoc timeout, boolean waitForTimeout) {
163         MAX_RETRY = new Integer JavaDoc(timeout).intValue();
164         this.waitForTimeout = waitForTimeout;
165     }
166
167     public void setName(String JavaDoc s) {
168         this.name = s;
169     }
170
171     public String JavaDoc getName() {
172         return name;
173     }
174
175     public void addVirtualMachine(VirtualMachine virtualMachine) {
176         virtualMachines.add(virtualMachine);
177         if (!((virtualMachine.getCreatorId()).equals(this.name))) {
178             // add in the hashtable the vm's creator id, and the number of nodes that should be created
179
awaitedVirtualNodes.put(virtualMachine.getCreatorId(),
180                 virtualMachine);
181             //we need to do it here otherwise event could occurs, whereas vm 's creator id is not in the hash map
182
//just synchro pb, this workaround solves the pb
183
}
184         if (logger.isDebugEnabled()) {
185             logger.debug("mapped VirtualNode=" + name +
186                 " with VirtualMachine=" + virtualMachine.getName());
187         }
188     }
189
190     public VirtualMachine getVirtualMachine() {
191         if (virtualMachines.isEmpty()) {
192             return null;
193         }
194         VirtualMachine vm = (VirtualMachine) virtualMachines.get(lastVirtualMachineIndex);
195         return vm;
196     }
197
198     /**
199      * Activates all the Nodes mapped to this VirtualNode in the XML Descriptor
200      */

201     public void activate() {
202         if (!isActivated) {
203             for (int i = 0; i < virtualMachines.size(); i++) {
204                 VirtualMachine vm = getVirtualMachine();
205                 boolean vmAlreadyAssigned = !((vm.getCreatorId()).equals(this.name));
206                 ExternalProcess process = getProcess(vm, vmAlreadyAssigned);
207
208                 // Test if that is this virtual Node that originates the creation of the vm
209
// else the vm was already created by another virtualNode, in that case, nothing is
210
// done at this point, nodes creation will occur when the runtime associated with the jvm
211
// will register.
212
if (!vmAlreadyAssigned) {
213                     setParameters(process, vm);
214                     process.setSecurityFile(policyServerFile);
215                     // It is this virtual Node that originates the creation of the vm
216
try {
217                         proActiveRuntimeImpl.createVM(process);
218                     } catch (java.io.IOException JavaDoc e) {
219                         e.printStackTrace();
220                         logger.error("cannot activate virtualNode " +
221                             this.name + " with the process " +
222                             process.getCommand());
223                     }
224                 }
225
226                 // }else{
227
// // add in the hashtable the vm's creator id, and the number of nodes that should be created
228
// awaitedVirtualNodes.put(vm.getCreatorId(),vm.getNodeNumber());
229
// }
230
increaseIndex();
231             }
232             isActivated = true;
233             if (registration) {
234                 register();
235             }
236         } else {
237             logger.info("VirtualNode " + this.name + " already activated !!!");
238         }
239     }
240
241     /**
242      * Returns the number of Nodes mapped to this VirtualNode in the XML Descriptor
243      * @return int
244      */

245     public int getNodeCount() {
246         return nodeCount;
247     }
248
249     /**
250      * Returns the number of Nodes already created among the Nodes mapped to this VirtualNode in the XML Descriptor
251      * @return int
252      */

253     public int createdNodeCount() {
254         return nodeCountCreated;
255     }
256
257     /**
258      * Returns the first Node created among Nodes mapped to this VirtualNode in the XML Descriptor
259      * Another call to this method will return the following created node if any.
260      * @return Node
261      */

262     public Node getNode() throws NodeException {
263         //try first to get the Node from the createdNodes array to be continued
264
Node node;
265         waitForNodeCreation();
266         if (!createdNodes.isEmpty()) {
267             node = (Node) createdNodes.get(lastNodeIndex);
268             increaseNodeIndex();
269             return node;
270         } else {
271             throw new NodeException("Cannot get the node " + this.name);
272         }
273     }
274
275     public Node getNode(int index) throws NodeException {
276         Node node = (Node) createdNodes.get(index);
277         if (node == null) {
278             throw new NodeException(
279                 "Cannot return the first node, no nodes hava been created");
280         }
281         return node;
282     }
283
284     public String JavaDoc[] getNodesURL() throws NodeException {
285         String JavaDoc[] nodeNames;
286         try {
287             waitForAllNodesCreation();
288         } catch (NodeException e) {
289             logger.error(e.getMessage());
290         }
291         if (!createdNodes.isEmpty()) {
292             synchronized (createdNodes) {
293                 nodeNames = new String JavaDoc[createdNodes.size()];
294                 for (int i = 0; i < createdNodes.size(); i++) {
295                     nodeNames[i] = ((Node) createdNodes.get(i)).getNodeInformation()
296                                     .getURL();
297                 }
298             }
299         } else {
300             throw new NodeException(
301                 "Cannot return nodes, no nodes hava been created");
302         }
303         return nodeNames;
304     }
305
306     public Node[] getNodes() throws NodeException {
307         Node[] nodeTab;
308         try {
309             waitForAllNodesCreation();
310         } catch (NodeException e) {
311             logger.error(e.getMessage());
312         }
313         if (!createdNodes.isEmpty()) {
314             synchronized (createdNodes) {
315                 nodeTab = new Node[createdNodes.size()];
316                 for (int i = 0; i < createdNodes.size(); i++) {
317                     nodeTab[i] = ((Node) createdNodes.get(i));
318                 }
319             }
320         } else {
321             throw new NodeException(
322                 "Cannot return nodes, no nodes hava been created");
323         }
324         return nodeTab;
325     }
326
327     public Node getNode(String JavaDoc url) throws NodeException {
328         Node node = null;
329         try {
330             waitForAllNodesCreation();
331         } catch (NodeException e) {
332             logger.error(e.getMessage());
333         }
334         if (!createdNodes.isEmpty()) {
335             synchronized (createdNodes) {
336                 for (int i = 0; i < createdNodes.size(); i++) {
337                     if (((Node) createdNodes.get(i)).getNodeInformation()
338                              .getURL().equals(url)) {
339                         node = (Node) createdNodes.get(i);
340                         break;
341                     }
342                 }
343                 return node;
344             }
345         } else {
346             throw new NodeException(
347                 "Cannot return nodes, no nodes hava been created");
348         }
349     }
350
351     public void killAll(boolean softly) {
352         Node node;
353         ProActiveRuntime part = null;
354         if (isActivated) {
355             for (int i = 0; i < createdNodes.size(); i++) {
356                 node = (Node) createdNodes.get(i);
357                 part = node.getProActiveRuntime();
358
359                 //we have to be carefull. Indeed if the node is local, we do not
360
// want to kill the runtime, otherwise the application is over
361
// so if the node is local, we just unregister this node from any registry
362
if (!NodeFactory.isNodeLocal(node)) {
363                     try {
364                         part.killRT(softly);
365                     } catch (ProActiveException e1) {
366                         e1.printStackTrace();
367                     } catch (Exception JavaDoc e) {
368                         logger.info(" Virutal Machine " +
369                             part.getVMInformation().getVMID() + " on host " +
370                             part.getVMInformation().getInetAddress()
371                                 .getCanonicalHostName() + " terminated!!!");
372                     }
373                 } else {
374                     try {
375                         // the node is local, unregister it.
376
part.killNode(node.getNodeInformation().getURL());
377                     } catch (ProActiveException e) {
378                         e.printStackTrace();
379                     }
380                 }
381             }
382             isActivated = false;
383             try {
384                 //if registered in any regigistry, unregister everything
385
if (registration) {
386                     ProActive.unregisterVirtualNode(this);
387                 }
388                 //else unregister just in the local runtime
389
else {
390                     proActiveRuntimeImpl.unregisterVirtualNode(this.name);
391                 }
392             } catch (ProActiveException e) {
393                 e.printStackTrace();
394             }
395
396             // if not activated unregister it from the local runtime
397
} else {
398             proActiveRuntimeImpl.unregisterVirtualNode(this.name);
399         }
400     }
401
402     public void createNodeOnCurrentJvm(String JavaDoc protocol) {
403         try {
404             // this method should be called when in the xml document the tag currenJVM is encountered. It means that one node must be created
405
// on the jvm that originates the creation of this virtualNode(the current jvm) and mapped on this virtualNode
406
// we must increase the node count
407
String JavaDoc url;
408             increaseNodeCount(1);
409             String JavaDoc nodeName = this.name +
410                 Integer.toString(new java.util.Random JavaDoc(
411                         System.currentTimeMillis()).nextInt());
412
413             // get the Runtime for the given protocol
414
ProActiveRuntime defaultRuntime = RuntimeFactory.getProtocolSpecificRuntime(checkProtocol(
415                         protocol));
416
417             //create the node
418
url = defaultRuntime.createLocalNode(nodeName, false, policyServer,
419                     this.getName(), ProActive.getJobId());
420             //add this node to this virtualNode
421
performOperations(defaultRuntime, url, protocol);
422         } catch (Exception JavaDoc e) {
423             e.printStackTrace();
424         }
425     }
426
427     public Object JavaDoc getUniqueAO() throws ProActiveException {
428         if (!property.equals("unique_singleAO")) {
429             logger.warn(
430                 "!!!!!!!!!!WARNING. This VirtualNode is not defined with unique_single_AO property in the XML descriptor. Calling getUniqueAO() on this VirtualNode can lead to unexpected behaviour");
431         }
432
433         if (uniqueActiveObject == null) {
434             try {
435                 Node node = getNode();
436
437                 if (node.getActiveObjects().length > 1) {
438                     logger.warn(
439                         "!!!!!!!!!!WARNING. More than one active object is created on this VirtualNode.");
440                 }
441
442                 uniqueActiveObject = node.getActiveObjects()[0];
443             } catch (Exception JavaDoc e) {
444                 e.printStackTrace();
445             }
446         }
447         if (uniqueActiveObject == null) {
448             throw new ProActiveException(
449                 "No active object are registered on this VirtualNode");
450         }
451
452         return uniqueActiveObject;
453     }
454
455     public boolean isActivated() {
456         return isActivated;
457     }
458
459     //
460
//-------------------IMPLEMENTS Job-----------------------------------
461
//
462
public String JavaDoc getJobID() {
463         return this.jobID;
464     }
465
466     //
467
//-------------------IMPLEMENTS RuntimeRegistrationEventListener------------
468
//
469
public synchronized void runtimeRegistered(RuntimeRegistrationEvent event) {
470         String JavaDoc nodeName;
471         String JavaDoc[] nodeNames = null;
472         ProActiveRuntime proActiveRuntimeRegistered;
473         String JavaDoc nodeHost;
474         String JavaDoc protocol;
475         String JavaDoc url;
476         int port = 0;
477         VirtualMachine virtualMachine = null;
478
479         for (int i = 0; i < virtualMachines.size(); i++) {
480             if (((VirtualMachine) virtualMachines.get(i)).getName().equals(event.getVmName())) {
481                 virtualMachine = (VirtualMachine) virtualMachines.get(i);
482             }
483         }
484
485         //Check if it this virtualNode that originates the process
486
if ((event.getCreatorID().equals(this.name)) &&
487                 (virtualMachine != null)) {
488             if (logger.isDebugEnabled()) {
489                 logger.debug("runtime " + event.getCreatorID() +
490                     " registered on virtualnode " + this.name);
491             }
492             protocol = event.getProtocol();
493             //gets the registered runtime
494
proActiveRuntimeRegistered = proActiveRuntimeImpl.getProActiveRuntime(event.getRegisteredRuntimeName());
495
496             // get the host of nodes
497
nodeHost = proActiveRuntimeRegistered.getVMInformation()
498                                                  .getInetAddress()
499                                                  .getCanonicalHostName();
500
501             try {
502                 port = UrlBuilder.getPortFromUrl(proActiveRuntimeRegistered.getURL());
503             } catch (ProActiveException e) {
504                 logger.warn("port unknown: " + port);
505             }
506
507             try {
508                 //get the node on the registered runtime
509
// nodeNames = proActiveRuntimeRegistered.getLocalNodeNames();
510
int nodeNumber = (new Integer JavaDoc((String JavaDoc) virtualMachine.getNodeNumber())).intValue();
511                 for (int i = 1; i <= nodeNumber; i++) {
512                     nodeName = this.name +
513                         Integer.toString(new java.util.Random JavaDoc(
514                                 System.currentTimeMillis()).nextInt());
515                     url = buildURL(nodeHost, nodeName, protocol, port);
516                     // nodes are created from the registered runtime, since this virtualNode is
517
// waiting for runtime registration to perform co-allocation in the jvm.
518
proActiveRuntimeRegistered.createLocalNode(url, false,
519                         policyServer, this.getName(), this.jobID);
520                     performOperations(proActiveRuntimeRegistered, url, protocol);
521                 }
522             } catch (ProActiveException e) {
523                 e.printStackTrace();
524             }
525         }
526
527         //Check if the virtualNode that originates the process is among awaited VirtualNodes
528
if (awaitedVirtualNodes.containsKey(event.getCreatorID())) {
529             //gets the registered runtime
530
System.out.println("Virtual Node ready to create node");
531             proActiveRuntimeRegistered = proActiveRuntimeImpl.getProActiveRuntime(event.getRegisteredRuntimeName());
532             // get the host for the node to be created
533
nodeHost = proActiveRuntimeRegistered.getVMInformation()
534                                                  .getInetAddress()
535                                                  .getCanonicalHostName();
536             protocol = event.getProtocol();
537             try {
538                 port = UrlBuilder.getPortFromUrl(proActiveRuntimeRegistered.getURL());
539             } catch (ProActiveException e) {
540                 logger.warn("port unknown: " + port);
541             }
542
543             // it is the only way to get accurate value of nodeNumber
544
VirtualMachine vm = (VirtualMachine) awaitedVirtualNodes.get(event.getCreatorID());
545             int nodeNumber = (new Integer JavaDoc((String JavaDoc) vm.getNodeNumber())).intValue();
546             for (int i = 1; i <= nodeNumber; i++) {
547                 try {
548                     nodeName = this.name +
549                         Integer.toString(new java.util.Random JavaDoc(
550                                 System.currentTimeMillis()).nextInt());
551                     url = buildURL(nodeHost, nodeName, protocol, port);
552                     // nodes are created from the registered runtime, since this virtualNode is
553
// waiting for runtime registration to perform co-allocation in the jvm.
554
proActiveRuntimeRegistered.createLocalNode(url, false,
555                         policyServer, this.getName(), this.jobID);
556                     performOperations(proActiveRuntimeRegistered, url, protocol);
557                 } catch (ProActiveException e) {
558                     e.printStackTrace();
559                 }
560             }
561         }
562     }
563
564     /**
565      * @see org.objectweb.proactive.core.descriptor.data.VirtualNode#setRuntimeInformations(String,String)
566      * At the moment no property can be set at runtime on a VirtualNodeImpl.
567      */

568     public void setRuntimeInformations(String JavaDoc information, String JavaDoc value)
569         throws ProActiveException {
570         try {
571             checkProperty(information);
572         } catch (ProActiveException e) {
573             throw new ProActiveException("No property can be set at runtime on this VirtualNode",
574                 e);
575         }
576     }
577
578     public void setRegistrationProtocol(String JavaDoc protocol) {
579         setRegistrationValue(true);
580         this.registrationProtocol = protocol;
581     }
582
583     public String JavaDoc getRegistrationProtocol() {
584         return this.registrationProtocol;
585     }
586
587     //
588
//-------------------PRIVATE METHODS--------------------------------------
589
//
590

591     /**
592      * Waits until at least one Node mapped to this VirtualNode in the XML Descriptor is created
593      */

594     private void waitForNodeCreation() throws NodeException {
595         int count = 0;
596         while (!nodeCreated) {
597             if (count < MAX_RETRY) {
598                 count++;
599                 try {
600                     Thread.sleep(1000);
601                 } catch (InterruptedException JavaDoc e2) {
602                     e2.printStackTrace();
603                 }
604             } else {
605                 throw new NodeException(
606                     "After many retries, not even one node can be found");
607             }
608         }
609         return;
610     }
611
612     /**
613      * Waits until all Nodes mapped to this VirtualNode in the XML Descriptor are created
614      */

615     private void waitForAllNodesCreation() throws NodeException {
616         int count = 0;
617
618         if (waitForTimeout) {
619             while (count < MAX_RETRY) {
620                 count++;
621                 try {
622                     Thread.sleep(1000);
623                 } catch (InterruptedException JavaDoc e2) {
624                     e2.printStackTrace();
625                 }
626             }
627         } else {
628             while (nodeCountCreated != nodeCount) {
629                 if (count < MAX_RETRY) {
630                     count++;
631                     try {
632                         Thread.sleep(1000);
633                     } catch (InterruptedException JavaDoc e2) {
634                         e2.printStackTrace();
635                     }
636                 } else {
637                     throw new NodeException("After many retries, only " +
638                         nodeCountCreated + " nodes are created on " +
639                         nodeCount + " expected");
640                 }
641             }
642         }
643         return;
644     }
645
646     /**
647      * Returns the process mapped to the given virtual machine mapped to this virtual node
648      * @param VirtualMachine
649      * @return ExternalProcess
650      */

651     private ExternalProcess getProcess(VirtualMachine vm,
652         boolean vmAlreadyAssigned) {
653         ExternalProcess copyProcess;
654
655         //VirtualMachine vm = getVirtualMachine();
656
ExternalProcess process = vm.getProcess();
657
658         // we need to do a deep copy of the process otherwise,
659
//modifications will be applied on one object that might
660
// be referenced by other virtualNodes .i.e check started
661
if (!vmAlreadyAssigned) {
662             copyProcess = makeDeepCopy(process);
663             vm.setProcess(copyProcess);
664             return copyProcess;
665         } else {
666             //increment the node count by nodeNumber
667
increaseNodeCount(new Integer JavaDoc(vm.getNodeNumber()).intValue());
668             return process;
669         }
670     }
671
672     /**
673      * Sets parameters to the JVMProcess linked to the ExternalProcess
674      * @param process
675      */

676     private void setParameters(ExternalProcess process, VirtualMachine vm) {
677         ExternalProcess processImpl = process;
678         ExternalProcessDecorator processImplDecorator;
679         JVMProcess jvmProcess;
680         LSFBSubProcess bsub = null;
681         PrunSubProcess prun = null;
682         GlobusProcess globus = null;
683         String JavaDoc protocolId = "";
684         int nodeNumber = new Integer JavaDoc(vm.getNodeNumber()).intValue();
685         if (logger.isDebugEnabled()) {
686             logger.debug("nodeNumber " + nodeNumber);
687         }
688
689         while (ExternalProcessDecorator.class.isInstance(processImpl)) {
690             String JavaDoc processClassname = processImpl.getClass().getName();
691             protocolId = protocolId +
692                 findProtocolId(processClassname).toLowerCase();
693             if (processImpl instanceof LSFBSubProcess) {
694                 //if the process is bsub we have to increase the node count by the number of processors
695
bsub = (LSFBSubProcess) processImpl;
696                 increaseNodeCount((new Integer JavaDoc(bsub.getProcessorNumber()).intValue()) * nodeNumber);
697             }
698             if (processImpl instanceof PrunSubProcess) {
699                 //if the process is prun we have to increase the node count by the number of processors
700
prun = (PrunSubProcess) processImpl;
701                 if (logger.isDebugEnabled()) {
702                     logger.debug("VirtualNodeImpl getHostsNumber() " +
703                         prun.getHostsNumber());
704                     logger.debug("VirtualNodeImpl getnodeNumber() " +
705                         prun.getProcessorPerNodeNumber());
706                     logger.debug("VM " + vm);
707                 }
708
709                 increaseNodeCount((new Integer JavaDoc(prun.getHostsNumber()).intValue()) * nodeNumber);
710             }
711             if (processImpl instanceof GlobusProcess) {
712                 //if the process is globus we have to increase the node count by the number of processors
713
globus = (GlobusProcess) processImpl;
714                 increaseNodeCount((new Integer JavaDoc(globus.getCount()).intValue()) * nodeNumber);
715             }
716
717             processImplDecorator = (ExternalProcessDecorator) processImpl;
718             processImpl = processImplDecorator.getTargetProcess();
719             if (logger.isDebugEnabled()) {
720                 logger.debug("processImplDecorator " +
721                     processImplDecorator.getClass().getName());
722             }
723         }
724         protocolId = protocolId + "jvm";
725         //When the virtualNode will be activated, it has to launch the process
726
//with such parameter.See StartRuntime
727
jvmProcess = (JVMProcess) processImpl;
728         //if the target class is StartRuntime, then give parameters otherwise keep parameters
729
if (jvmProcess.getClassname().equals("org.objectweb.proactive.core.runtime.StartRuntime")) {
730             //we increment the index of nodecount
731
if ((bsub == null) && (prun == null) && (globus == null)) {
732                 //if bsub and prun and globus are null we can increase the nodeCount
733
increaseNodeCount(nodeNumber);
734             }
735
736             //if(!vmAlreadyAssigned){
737
String JavaDoc vnName = this.name;
738
739             String JavaDoc localruntimeURL = null;
740             try {
741                 localruntimeURL = RuntimeFactory.getDefaultRuntime().getURL();
742             } catch (ProActiveException e) {
743                 e.printStackTrace();
744             }
745
746             if (logger.isDebugEnabled()) {
747                 logger.debug(localruntimeURL);
748             }
749             jvmProcess.setJvmOptions("-Dproactive.jobid=" + this.jobID);
750             jvmProcess.setParameters(vnName + " " + localruntimeURL + " " +
751                 nodeNumber + " " + protocolId + " " + vm.getName());
752         }
753     }
754
755     /**
756      * @param processClassname
757      * @return
758      */

759     private String JavaDoc findProtocolId(String JavaDoc processClassname) {
760         int index = processClassname.lastIndexOf(".") + 1;
761         int lastIndex = processClassname.lastIndexOf("Process");
762         return processClassname.substring(index, lastIndex) + "-";
763     }
764
765     /**
766      * Returns a deepcopy of the process
767      * @param process the process to copy
768      * @return ExternalProcess, the copy version of the process
769      */

770     private ExternalProcess makeDeepCopy(ExternalProcess process) {
771         //deepCopyTag = true;
772
ExternalProcess result = null;
773         try {
774             java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
775             java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc(baos);
776             oos.writeObject(process);
777             oos.flush();
778             oos.close();
779             java.io.ByteArrayInputStream JavaDoc bais = new java.io.ByteArrayInputStream JavaDoc(baos.toByteArray());
780             java.io.ObjectInputStream JavaDoc ois = new java.io.ObjectInputStream JavaDoc(bais);
781             result = (ExternalProcess) ois.readObject();
782             ois.close();
783         } catch (Exception JavaDoc e) {
784             e.printStackTrace();
785         }
786
787         //deepCopyTag = false;
788
return result;
789     }
790
791     private String JavaDoc buildURL(String JavaDoc host, String JavaDoc name, String JavaDoc protocol, int port) {
792         if (port != 0) {
793             return UrlBuilder.buildUrl(host, name, protocol, port);
794         } else {
795             return UrlBuilder.buildUrl(host, name, protocol);
796         }
797     }
798
799     private void increaseIndex() {
800         if (virtualMachines.size() > 1) {
801             lastVirtualMachineIndex = (lastVirtualMachineIndex + 1) % virtualMachines.size();
802         }
803     }
804
805     private void increaseNodeCount(int n) {
806         nodeCount = nodeCount + n;
807         if (logger.isDebugEnabled()) {
808             logger.debug("NodeCount: " + nodeCount);
809         }
810     }
811
812     private void increaseNodeIndex() {
813         if (createdNodes.size() > 1) {
814             lastNodeIndex = (lastNodeIndex + 1) % createdNodes.size();
815         }
816     }
817
818     private String JavaDoc checkProtocol(String JavaDoc protocol) {
819         if (protocol.indexOf(":") == -1) {
820             return protocol.concat(":");
821         }
822         return protocol;
823     }
824
825     private void performOperations(ProActiveRuntime part, String JavaDoc url,
826         String JavaDoc protocol) {
827         createdNodes.add(new NodeImpl(part, url, checkProtocol(protocol),
828                 this.jobID));
829         logger.info("**** Mapping VirtualNode " + this.name + " with Node: " +
830             url + " done");
831         nodeCreated = true;
832         nodeCountCreated++;
833     }
834
835     private void register() {
836         try {
837             waitForAllNodesCreation();
838             // ProActiveRuntime part = RuntimeFactory.getProtocolSpecificRuntime(registrationProtocol);
839
// part.registerVirtualnode(this.name,false);
840
ProActive.registerVirtualNode(this, registrationProtocol, false);
841         } catch (NodeException e) {
842             logger.error(e.getMessage());
843         } catch (ProActiveException e) {
844             e.printStackTrace();
845         }
846     }
847
848     private void setRegistrationValue(boolean value) {
849         this.registration = value;
850     }
851
852     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
853         throws java.io.IOException JavaDoc {
854         try {
855             waitForAllNodesCreation();
856         } catch (NodeException e) {
857             e.printStackTrace();
858         }
859
860         out.defaultWriteObject();
861     }
862
863     private void readObject(java.io.ObjectInputStream JavaDoc in)
864         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
865         in.defaultReadObject();
866     }
867
868     // SECURITY
869

870     /* (non-Javadoc)
871      * @see org.objectweb.proactive.core.descriptor.data.VirtualNode#getCreatorCertificate()
872      */

873     public X509Certificate JavaDoc getCreatorCertificate() {
874         return creatorCertificate;
875     }
876
877     /* (non-Javadoc)
878      * @see org.objectweb.proactive.core.descriptor.data.VirtualNode#getPolicyServer()
879      */

880     public PolicyServer getPolicyServer() {
881         return policyServer;
882     }
883
884     /**
885      * @param server
886      */

887     public void setPolicyServer(PolicyServer server) {
888         // logger.debug("Setting PolicyServer " + server + " to VN " +name);
889
policyServer = server;
890     }
891
892     /* (non-Javadoc)
893      * @see org.objectweb.proactive.core.descriptor.data.VirtualNode#setPolicyFile(java.lang.String)
894      */

895     public void setPolicyFile(String JavaDoc file) {
896         policyServerFile = file;
897     }
898 }
899
Popular Tags