Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9a5f58a7680c7ca6c9a3a0b4d9460433c50ef0bb
[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                          } 
106                          catch (NoSuchMethodException e) {
107                                  throw new RuntimeException("Can't find the correct constructor for the class " + function + ". \n" +
108                                  "Is there a constructor with the signature: \"Host host, String name, String[]args\" in the class ?");
109                          }
110                          catch (InvocationTargetException e) {
111                                  e.printStackTrace();
112                          }
113                          catch(HostNotFoundException e) {
114                                  System.out.println(e.toString());
115                                  e.printStackTrace();
116
117                          } catch(ClassNotFoundException e) {
118                                  System.out.println(function +
119                                  " 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)");
120                                  e.printStackTrace();
121
122                          } catch(InstantiationException e) {
123                                  System.out.println("Unable to create the process. I got an instantiation exception");
124                                  e.printStackTrace();
125                          } catch(IllegalAccessException e) {
126                                  System.out.println("Unable to create the process. I got an illegal access exception");
127                                  e.printStackTrace();
128                          } 
129
130                  }
131         
132
133          /**
134           *
135           */
136          public  static void onStartDocument() {
137                         args = new Vector<String>();
138                         properties = new Hashtable<String,String>();
139                         hostName = null;
140                         function = null;
141         }
142
143          /**
144           *
145           * @param hostName
146           * @param function
147           */
148          public  static void onBeginProcess(String hostName, String function) {
149                 setProcessIdentity(hostName, function);
150                 
151         }
152     /**
153      *
154      * @param id
155      * @param value
156      */
157     public  static void onProperty(String id, String value) {
158                 setProperty(id, value);
159         }
160
161     /**
162      *
163      * @param arg
164      */
165     public  static void onProcessArg(String arg) {
166                 registerProcessArg(arg);
167         }
168
169     /**
170      *
171      */
172     public  static void onEndProcess() {
173                 createProcess();
174         }        
175
176     /**
177      *
178      */
179     public static void onEndDocument() {
180         }  
181 }