Logo AND Algorithmique Numérique Distribuée

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