Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fd59889e31c870ca06d4bedd8b1faecaee3723a7
[simgrid.git] / src / java / simgrid / msg / ApplicationHandler.java
1 /*
2  * simgrid.msg.ApplicationHandler.java  1.00 07/05/01
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  */
11
12 package simgrid.msg;
13
14 import java.util.Vector;
15 import java.util.Hashtable;
16 import org.xml.sax.*;
17 import org.xml.sax.helpers.*;
18
19 /**
20  * The handler used to parse the deployment file which contains 
21  * the description of the application (simulation).
22  *
23  * @author  Abdelmalek Cherier
24  * @author  Martin Quinson
25  * @version 1.00, 07/05/01
26  * @see Host
27  * @see Process
28  * @see Simulation
29  * @since SimGrid 3.3
30  * @since JDK1.5011 
31  */
32 public final class ApplicationHandler extends DefaultHandler {
33
34   /* 
35    * This class is used to create the processes descibed in the deployment file.
36    */
37   class ProcessFactory {
38                 /**
39                  * The vector which contains the arguments of the main function 
40                  * of the process object.
41                  */
42     public Vector args;
43     
44     public Hashtable properties;
45
46                 /**
47                  * The name of the host of the process.
48                  */
49     private String hostName;
50
51                 /**
52                  * The function of the process.
53                  */
54     private String function;
55     
56
57                 /**
58                  * Default constructor.
59                  */
60     public ProcessFactory() {
61       this.args = new Vector();
62       this.properties = new Hashtable();
63       this.hostName = null;
64       this.function = null;
65     }
66                 /**
67                  * This method is called by the start element handler.
68                  * It sets the host and the function of the process to create,
69                  * and clear the vector containing the arguments of the 
70                  * previouse process function if needed.
71                  *
72                  * @host                                The host of the process to create.
73                  * @function                    The function of the process to create.
74                  *
75                  */
76       public void setProcessIdentity(String hostName, String function) {
77       this.hostName = hostName;
78       this.function = function;
79
80       if (!this.args.isEmpty())
81         this.args.clear();
82         
83       if(!this.properties.isEmpty())
84         this.properties.clear();
85     }
86                 /**
87                  * This method is called by the startElement() handler.
88                  * It stores the argument of the function of the next
89                  * process to create in the vector of arguments.
90                  *
91                  * @arg                                 The argument to add.
92                  *
93                  */ public void registerProcessArg(String arg) {
94       this.args.add(arg);
95     }
96     
97     public void setProperty(String id, String value)
98     {
99         this.properties.put(id,value);  
100     }
101     
102     public String getHostName()
103     {
104         return this.hostName;
105     }
106
107     public void createProcess() {
108       try {
109
110         Class cls = Class.forName(this.function);
111
112         simgrid.msg.Process process = (simgrid.msg.Process) cls.newInstance();
113         process.name = /*process.getName();*/       this.function;
114         process.id = simgrid.msg.Process.nextProcessId++;
115         Host host = Host.getByName(this.hostName);
116
117         MsgNative.processCreate(process, host);
118         Vector args = processFactory.args;
119         int size = args.size();
120
121         for (int index = 0; index < size; index++)
122           process.args.add(args.get(index));
123           
124         process.properties = this.properties;
125         this.properties = new Hashtable();
126
127       } catch(JniException e) {
128         System.out.println(e.toString());
129         e.printStackTrace();
130
131       } catch(NativeException e) {
132         System.out.println(e.toString());
133         e.printStackTrace();
134
135       } catch(HostNotFoundException e) {
136         System.out.println(e.toString());
137         e.printStackTrace();
138
139       } catch(ClassNotFoundException e) {
140         System.out.println(this.function +
141                            " class not found\n The attribut function of the element process  of your deployment file\n must correspond to the name of a Msg Proces class)");
142         e.printStackTrace();
143
144       } catch(InstantiationException e) {
145         System.out.println("instantiation exception");
146         e.printStackTrace();
147       } catch(IllegalAccessException e) {
148         System.out.println("illegal access exception");
149         e.printStackTrace();
150       } catch(IllegalArgumentException e) {
151         System.out.println("illegal argument exception");
152         e.printStackTrace();
153       }
154
155     }
156   }
157
158   /* 
159    * the ProcessFactory object used to create the processes.
160    */
161   private ProcessFactory processFactory;
162
163   public ApplicationHandler() {
164     super();
165   }
166     /**
167      * instanciates the process factory 
168      */
169   public void startDocument() {
170     this.processFactory = new ProcessFactory();
171   }
172
173   public void characters(char[]caracteres, int debut, int longueur) {
174   }                             // NOTHING TODO
175
176    /**
177     * element handlers
178     */
179   public void startElement(String nameSpace, String localName, String qName,
180                            Attributes attr) {
181     if (localName.equals("process"))
182       onProcessIdentity(attr);
183     else if(localName.equals("prop"))
184       onProperty(attr);
185     else if (localName.equals("argument"))
186       onProcessArg(attr);
187   }
188
189      /**
190      * process attributs handler.
191      */
192   public void onProcessIdentity(Attributes attr) {
193     processFactory.setProcessIdentity(attr.getValue(0), attr.getValue(1));
194   }
195   
196   public void onProperty(Attributes attr) {
197     processFactory.setProperty(attr.getValue(0), attr.getValue(1));
198   }
199
200         /**
201      * process arguments handler.
202      */
203   public void onProcessArg(Attributes attr) {
204     processFactory.registerProcessArg(attr.getValue(0));
205   }
206
207     /**
208      * creates the process
209      */
210   public void endElement(String nameSpace, String localName, String qName) {
211     if (localName.equals("process")) {
212       processFactory.createProcess();
213     }
214   }
215
216   public void endDocument() {
217   }                             // NOTHING TODO
218 }