Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b485d773130ca31aa9a4d950f48d187f7cfa6de9
[simgrid.git] / org / simgrid / msg / ApplicationHandler.java
1 /*
2  * These are the upcalls used by the FleXML parser for application files
3  *
4  * Copyright 2006,2007,2010,2011 The SimGrid team.           
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 org.simgrid.msg;
13 import java.util.Hashtable;
14 import java.util.Vector;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17
18 public final class ApplicationHandler {
19
20
21                 /**
22                  * The vector which contains the arguments of the main function 
23                  * of the process object.
24                  */
25                 public  static Vector<String> args;
26
27         /**
28          *
29          */
30         public  static Hashtable<String,String> properties;
31
32                 /**
33                  * The name of the host of the process.
34                  */
35                 private  static String hostName;
36
37                 /**
38                  * The function of the process.
39                  */
40                 private  static String function;
41                 /**
42                  * Start time of the process
43                  */
44                 private static double startTime;
45                 /**
46                  * Kill time of the process
47                  */
48                 private static double killTime;
49                 /**
50                  * This method is called by the start element handler.
51                  * It sets the host and the function of the process to create,
52                  * and clear the vector containing the arguments of the 
53                  * previouse process function if needed.
54                  *
55          * @param hostName_
56          * @param function_
57          * @host                                The host of the process to create.
58                  * @function                    The function of the process to create.
59                  *
60                  */
61                 public  static void setProcessIdentity(String hostName, String function, String startTime, String killTime) {
62                         ApplicationHandler.hostName = hostName;    
63                         ApplicationHandler.function = function;
64                         ApplicationHandler.startTime = Double.valueOf(startTime);
65                         ApplicationHandler.killTime = Double.valueOf(killTime);
66
67                         if (!args.isEmpty())
68                                 args.clear();
69
70                         if(!properties.isEmpty())
71                                 properties.clear();
72                 }
73                 /**
74                  * This method is called by the startElement() handler.
75                  * It stores the argument of the function of the next
76                  * process to create in the vector of arguments.
77                  *
78          * @param arg
79          * @arg                                 The argument to add.
80                  *
81                  */ public  static void registerProcessArg(String arg) {
82                          args.add(arg);
83                  }
84
85          /**
86           *
87           * @param id
88           * @param value
89           */
90          public  static void setProperty(String id, String value)
91                  {
92                          properties.put(id,value);      
93                  }
94
95          /**
96           *
97           * @return
98           */
99          public  static String getHostName()
100                  {
101                          return hostName;
102                  }
103
104          /**
105           * Method called to create the process
106           */
107          @SuppressWarnings("unchecked")
108                  public  static void createProcess() {
109                          try {
110                                  Class<Process> cls = (Class<Process>) Class.forName(function);
111                                  Constructor<Process> constructor = cls.getConstructor(new Class [] {Host.class, java.lang.String.class, java.lang.String[].class, double.class, double.class});
112                                  String[] args_ = args.toArray(new String[args.size()]);
113                                  Process process = constructor.newInstance(Host.getByName(hostName), function, args_, startTime, killTime);
114                                  process.start();
115                          } 
116                          catch (NoSuchMethodException e) {
117                                  throw new RuntimeException("Can't find the correct constructor for the class " + function + ". \n" +
118                                  "Is there a constructor with the signature: \"Host host, String name, String[]args, double startTime, double killTime\" in the class ?");
119                          }
120                          catch (InvocationTargetException e) {
121                                  e.printStackTrace();
122                          }
123                          catch(HostNotFoundException e) {
124                                  System.out.println(e.toString());
125                                  e.printStackTrace();
126
127                          } catch(ClassNotFoundException e) {
128                                  System.out.println(function +
129                                  " 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)");
130                                  e.printStackTrace();
131
132                          } catch(InstantiationException e) {
133                                  System.out.println("Unable to create the process. I got an instantiation exception");
134                                  e.printStackTrace();
135                          } catch(IllegalAccessException e) {
136                                  System.out.println("Unable to create the process. I got an illegal access exception");
137                                  e.printStackTrace();
138                          } 
139
140                  }
141         
142
143          /**
144           *
145           */
146          public  static void onStartDocument() {
147                         args = new Vector<String>();
148                         properties = new Hashtable<String,String>();
149                         hostName = null;
150                         function = null;
151                         startTime = 0;
152                         killTime = -1;
153         }
154
155          /**
156           *
157           * @param hostName
158           * @param function
159           */
160          public  static void onBeginProcess(String hostName, String function, String startTime, String killTime) {
161                 setProcessIdentity(hostName, function, startTime, killTime);
162                 
163         }
164     /**
165      *
166      * @param id
167      * @param value
168      */
169     public  static void onProperty(String id, String value) {
170         setProperty(id, value);
171         }
172
173     /**
174      *
175      * @param arg
176      */
177     public  static void onProcessArg(String arg) {
178         registerProcessArg(arg);
179         }
180
181     /**
182      *
183      */
184     public  static void onEndProcess() {
185                 createProcess();
186         }        
187
188     /**
189      *
190      */
191     public static void onEndDocument() {
192         }  
193 }