Logo AND Algorithmique Numérique Distribuée

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