2 * These are the upcalls used by the FleXML parser for application files
4 * Copyright 2006,2007,2010,2011 The SimGrid team.
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.
12 package org.simgrid.msg;
13 import java.util.Hashtable;
14 import java.util.Vector;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
18 public final class ApplicationHandler {
22 * The vector which contains the arguments of the main function
23 * of the process object.
25 public static Vector<String> args;
30 public static Hashtable<String,String> properties;
33 * The name of the host of the process.
35 private static String hostName;
38 * The function of the process.
40 private static String function;
42 * Start time of the process
44 private static double startTime;
46 * Kill time of the process
48 private static double killTime;
50 * This method is called by the start element handler.
51 * It sets the host and the function of the process to create,
52 * and clear the vector containing the arguments of the
53 * previouse process function if needed.
57 * @host The host of the process to create.
58 * @function The function of the process to create.
61 public static void setProcessIdentity(String hostName, String function, String startTime, String killTime) {
62 ApplicationHandler.hostName = hostName;
63 ApplicationHandler.function = function;
64 ApplicationHandler.startTime = Double.valueOf(startTime);
65 ApplicationHandler.killTime = Double.valueOf(killTime);
70 if(!properties.isEmpty())
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.
79 * @arg The argument to add.
81 */ public static void registerProcessArg(String arg) {
90 public static void setProperty(String id, String value)
92 properties.put(id,value);
99 public static String getHostName()
105 * Method called to create the process
107 @SuppressWarnings("unchecked")
108 public static void createProcess() {
110 Class<Process> cls = (Class<Process>) Class.forName(function);
111 Constructor<Process> constructor = cls.getConstructor(new Class [] {Host.class, java.lang.String.class, java.lang.String[].class, double.class, double.class});
112 String[] args_ = args.toArray(new String[args.size()]);
113 Process process = constructor.newInstance(Host.getByName(hostName), function, args_, startTime, killTime);
116 catch (NoSuchMethodException e) {
117 throw new RuntimeException("Can't find the correct constructor for the class " + function + ". \n" +
118 "Is there a constructor with the signature: \"Host host, String name, String[]args, double startTime, double killTime\" in the class ?");
120 catch (InvocationTargetException e) {
123 catch(HostNotFoundException e) {
124 System.out.println(e.toString());
127 } catch(ClassNotFoundException e) {
128 System.out.println(function +
129 " 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)");
132 } catch(InstantiationException e) {
133 System.out.println("Unable to create the process. I got an instantiation exception");
135 } catch(IllegalAccessException e) {
136 System.out.println("Unable to create the process. I got an illegal access exception");
146 public static void onStartDocument() {
147 args = new Vector<String>();
148 properties = new Hashtable<String,String>();
160 public static void onBeginProcess(String hostName, String function, String startTime, String killTime) {
161 setProcessIdentity(hostName, function, startTime, killTime);
169 public static void onProperty(String id, String value) {
170 setProperty(id, value);
177 public static void onProcessArg(String arg) {
178 registerProcessArg(arg);
184 public static void onEndProcess() {
191 public static void onEndDocument() {