Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change "ProcessKilledException" to "ProcessKilledError" -> make it not catchable...
[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                 /**
43                  * This method is called by the start element handler.
44                  * It sets the host and the function of the process to create,
45                  * and clear the vector containing the arguments of the 
46                  * previouse process function if needed.
47                  *
48          * @param hostName_
49          * @param function_
50          * @host                                The host of the process to create.
51                  * @function                    The function of the process to create.
52                  *
53                  */
54                 public  static void setProcessIdentity(String hostName_, String function_) {
55                         hostName = hostName_;    
56                         function = function_;
57
58                         if (!args.isEmpty())
59                                 args.clear();
60
61                         if(!properties.isEmpty())
62                                 properties.clear();
63                 }
64                 /**
65                  * This method is called by the startElement() handler.
66                  * It stores the argument of the function of the next
67                  * process to create in the vector of arguments.
68                  *
69          * @param arg
70          * @arg                                 The argument to add.
71                  *
72                  */ public  static void registerProcessArg(String arg) {
73                          args.add(arg);
74                  }
75
76          /**
77           *
78           * @param id
79           * @param value
80           */
81          public  static void setProperty(String id, String value)
82                  {
83                          properties.put(id,value);      
84                  }
85
86          /**
87           *
88           * @return
89           */
90          public  static String getHostName()
91                  {
92                          return hostName;
93                  }
94
95          /**
96           * Method called to create the process
97           */
98          @SuppressWarnings("unchecked")
99                  public  static void createProcess() {
100                          try {
101                                  Class<Process> cls = (Class<Process>) Class.forName(function);
102                                  Constructor<Process> constructor = cls.getConstructor(new Class [] {Host.class, java.lang.String.class, java.lang.String[].class});
103                                  String[] args_ = args.toArray(new String[args.size()]);
104                                  Process process = constructor.newInstance(Host.getByName(hostName), function, args_);
105                                  process.start();
106                          } 
107                          catch (NoSuchMethodException e) {
108                                  throw new RuntimeException("Can't find the correct constructor for the class " + function + ". \n" +
109                                  "Is there a constructor with the signature: \"Host host, String name, String[]args\" in the class ?");
110                          }
111                          catch (InvocationTargetException e) {
112                                  e.printStackTrace();
113                          }
114                          catch(HostNotFoundException e) {
115                                  System.out.println(e.toString());
116                                  e.printStackTrace();
117
118                          } catch(ClassNotFoundException e) {
119                                  System.out.println(function +
120                                  " 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)");
121                                  e.printStackTrace();
122
123                          } catch(InstantiationException e) {
124                                  System.out.println("Unable to create the process. I got an instantiation exception");
125                                  e.printStackTrace();
126                          } catch(IllegalAccessException e) {
127                                  System.out.println("Unable to create the process. I got an illegal access exception");
128                                  e.printStackTrace();
129                          } 
130
131                  }
132         
133
134          /**
135           *
136           */
137          public  static void onStartDocument() {
138                         args = new Vector<String>();
139                         properties = new Hashtable<String,String>();
140                         hostName = null;
141                         function = null;
142         }
143
144          /**
145           *
146           * @param hostName
147           * @param function
148           */
149          public  static void onBeginProcess(String hostName, String function) {
150                 setProcessIdentity(hostName, function);
151                 
152         }
153     /**
154      *
155      * @param id
156      * @param value
157      */
158     public  static void onProperty(String id, String value) {
159                 setProperty(id, value);
160         }
161
162     /**
163      *
164      * @param arg
165      */
166     public  static void onProcessArg(String arg) {
167                 registerProcessArg(arg);
168         }
169
170     /**
171      *
172      */
173     public  static void onEndProcess() {
174                 createProcess();
175         }        
176
177     /**
178      *
179      */
180     public static void onEndDocument() {
181         }  
182 }