Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ef245d7c7cfa47774e1c9953e732719fc4c31d00
[simgrid.git] / src / java / 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 simgrid.msg;
13
14 import java.util.Vector;
15 import java.util.Hashtable;
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                 public  static Hashtable<String,String> properties;
27
28                 /**
29                  * The name of the host of the process.
30                  */
31                 private  static String hostName;
32
33                 /**
34                  * The function of the process.
35                  */
36                 private  static String function;
37                 
38                 /**
39                  * This method is called by the start element handler.
40                  * It sets the host and the function of the process to create,
41                  * and clear the vector containing the arguments of the 
42                  * previouse process function if needed.
43                  *
44                  * @host                                The host of the process to create.
45                  * @function                    The function of the process to create.
46                  *
47                  */
48                 public  static void setProcessIdentity(String hostName_, String function_) {
49                         hostName = hostName_;    
50                         function = function_;
51
52                         if (!args.isEmpty())
53                                 args.clear();
54
55                         if(!properties.isEmpty())
56                                 properties.clear();
57                 }
58                 /**
59                  * This method is called by the startElement() handler.
60                  * It stores the argument of the function of the next
61                  * process to create in the vector of arguments.
62                  *
63                  * @arg                                 The argument to add.
64                  *
65                  */ public  static void registerProcessArg(String arg) {
66                          args.add(arg);
67                  }
68
69                  public  static void setProperty(String id, String value)
70                  {
71                          properties.put(id,value);      
72                  }
73
74                  public  static String getHostName()
75                  {
76                          return hostName;
77                  }
78
79                  @SuppressWarnings("unchecked")
80                  public  static void createProcess() {
81                          try {
82                                  Class<simgrid.msg.Process> cls = (Class<Process>) Class.forName(function);
83
84                                  simgrid.msg.Process process = cls.newInstance();
85                                  process.name = function;
86                                  process.id = simgrid.msg.Process.nextProcessId++;
87                                  Host host = Host.getByName(hostName);
88
89                                  MsgNative.processCreate(process, host);
90                                  Vector<String> args_ = args;
91                                  int size = args_.size();
92
93                                  for (int index = 0; index < size; index++)
94                                          process.args.add(args_.get(index));
95
96                                  process.properties = properties;
97                                  properties = new Hashtable();
98
99                          } catch(HostNotFoundException e) {
100                                  System.out.println(e.toString());
101                                  e.printStackTrace();
102
103                          } catch(ClassNotFoundException e) {
104                                  System.out.println(function +
105                                  " 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)");
106                                  e.printStackTrace();
107
108                          } catch(InstantiationException e) {
109                                  System.out.println("Unable to create the process. I got an instantiation exception");
110                                  e.printStackTrace();
111                          } catch(IllegalAccessException e) {
112                                  System.out.println("Unable to create the process. I got an illegal access exception");
113                                  e.printStackTrace();
114                          } 
115
116                  }
117         
118
119         public  static void onStartDocument() {
120                         args = new Vector<String>();
121                         properties = new Hashtable<String,String>();
122                         hostName = null;
123                         function = null;
124         }
125
126         public  static void onBeginProcess(String hostName, String function) {
127                 setProcessIdentity(hostName, function);
128                 
129         }
130         public  static void onProperty(String id, String value) {
131                 setProperty(id, value);
132         }
133
134         public  static void onProcessArg(String arg) {
135                 registerProcessArg(arg);
136         }
137
138         public  static void onEndProcess() {
139                 createProcess();
140         }        
141
142         public static void onEndDocument() {
143         }  
144 }