Logo AND Algorithmique Numérique Distribuée

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