Logo AND Algorithmique Numérique Distribuée

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