Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Further cleanups of the Java bindings:
[simgrid.git] / src / java / simgrid / msg / ApplicationHandler.java
1 /*
2  * simgrid.msg.ApplicationHandler.java  1.00 07/05/01
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
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 org.xml.sax.*;
16 import org.xml.sax.helpers.*;
17
18 /**
19  * The handler used to parse the deployment file which contains 
20  * the description of the application (simulation).
21  *
22  * @author  Abdelmalek Cherier
23  * @author  Martin Quinson
24  * @version 1.00, 07/05/01
25  * @see Host
26  * @see Process
27  * @see Simulation
28  * @since SimGrid 3.3
29  * @since JDK1.5011 
30  */
31 public final class ApplicationHandler extends DefaultHandler {
32
33   /* 
34    * This class is used to create the processes descibed in the deployment file.
35    */
36   class ProcessFactory {
37                 /**
38                  * The vector which contains the arguments of the main function 
39                  * of the process object.
40                  */
41     public Vector args;
42
43                 /**
44                  * The name of the host of the process.
45                  */
46     private String hostName;
47
48                 /**
49                  * The function of the process.
50                  */
51     private String function;
52
53                 /**
54                  * Default constructor.
55                  */
56     public ProcessFactory() {
57       this.args = new Vector();
58       this.hostName = null;
59       this.function = null;
60     }
61                 /**
62                  * This method is called by the start element handler.
63                  * It sets the host and the function of the process to create,
64                  * and clear the vector containing the arguments of the 
65                  * previouse process function if needed.
66                  *
67                  * @host                                The host of the process to create.
68                  * @function                    The function of the process to create.
69                  *
70                  */
71       public void setProcessIdentity(String hostName, String function) {
72       this.hostName = hostName;
73       this.function = function;
74
75       if (!args.isEmpty())
76         args.clear();
77     }
78                 /**
79                  * This method is called by the startElement() handler.
80                  * It stores the argument of the function of the next
81                  * process to create in the vector of arguments.
82                  *
83                  * @arg                                 The argument to add.
84                  *
85                  */ public void registerProcessArg(String arg) {
86       this.args.add(arg);
87     }
88
89     public void createProcess() {
90       try {
91
92         Class cls = Class.forName(this.function);
93
94         simgrid.msg.Process process = (simgrid.msg.Process) cls.newInstance();
95         process.name = process.getName();       //this.function;
96         process.id = simgrid.msg.Process.nextProcessId++;
97         Host host = Host.getByName(this.hostName);
98
99         MsgNative.processCreate(process, host);
100         Vector args = processFactory.args;
101         int size = args.size();
102
103         for (int index = 0; index < size; index++)
104           process.args.add(args.get(index));
105
106       } catch(JniException e) {
107         System.out.println(e.toString());
108         e.printStackTrace();
109
110       } catch(NativeException e) {
111         System.out.println(e.toString());
112         e.printStackTrace();
113
114       } catch(HostNotFoundException e) {
115         System.out.println(e.toString());
116         e.printStackTrace();
117
118       } catch(ClassNotFoundException e) {
119         System.out.println(this.function +
120                            " 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)");
121         e.printStackTrace();
122
123       } catch(InstantiationException e) {
124         System.out.println("instantiation exception");
125         e.printStackTrace();
126       } catch(IllegalAccessException e) {
127         System.out.println("illegal access exception");
128         e.printStackTrace();
129       } catch(IllegalArgumentException e) {
130         System.out.println("illegal argument exception");
131         e.printStackTrace();
132       }
133
134     }
135   }
136
137   /* 
138    * the ProcessFactory object used to create the processes.
139    */
140   private ProcessFactory processFactory;
141
142   public ApplicationHandler() {
143     super();
144   }
145     /**
146      * instanciates the process factory 
147      */
148   public void startDocument() {
149     this.processFactory = new ProcessFactory();
150   }
151
152   public void characters(char[]caracteres, int debut, int longueur) {
153   }                             // NOTHING TODO
154
155    /**
156     * element handlers
157     */
158   public void startElement(String nameSpace, String localName, String qName,
159                            Attributes attr) {
160     if (localName.equals("process"))
161       onProcessIdentity(attr);
162     else if (localName.equals("argument"))
163       onProcessArg(attr);
164   }
165
166      /**
167      * process attributs handler.
168      */
169   public void onProcessIdentity(Attributes attr) {
170     processFactory.setProcessIdentity(attr.getValue(0), attr.getValue(1));
171   }
172
173         /**
174      * process arguments handler.
175      */
176   public void onProcessArg(Attributes attr) {
177     processFactory.registerProcessArg(attr.getValue(0));
178   }
179
180     /**
181      * creates the process
182      */
183   public void endElement(String nameSpace, String localName, String qName) {
184     if (localName.equals("process")) {
185       processFactory.createProcess();
186     }
187   }
188
189   public void endDocument() {
190   }                             // NOTHING TODO
191 }