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;
43 * This method is called by the start element handler.
44 * It sets the host and the function of the process to create,
45 * and clear the vector containing the arguments of the
46 * previouse process function if needed.
50 * @host The host of the process to create.
51 * @function The function of the process to create.
54 public static void setProcessIdentity(String hostName_, String function_) {
61 if(!properties.isEmpty())
65 * This method is called by the startElement() handler.
66 * It stores the argument of the function of the next
67 * process to create in the vector of arguments.
70 * @arg The argument to add.
72 */ public static void registerProcessArg(String arg) {
81 public static void setProperty(String id, String value)
83 properties.put(id,value);
90 public static String getHostName()
96 * Method called to create the process
98 @SuppressWarnings("unchecked")
99 public static void createProcess() {
101 Class<Process> cls = (Class<Process>) Class.forName(function);
102 Constructor<Process> constructor = cls.getConstructor(new Class [] {Host.class, java.lang.String.class, java.lang.String[].class});
103 String[] args_ = args.toArray(new String[args.size()]);
104 Process process = constructor.newInstance(Host.getByName(hostName), function, args_);
107 catch (NoSuchMethodException e) {
108 throw new RuntimeException("Can't find the correct constructor for the class " + function + ". \n" +
109 "Is there a constructor with the signature: \"Host host, String name, String[]args\" in the class ?");
111 catch (InvocationTargetException e) {
114 catch(HostNotFoundException e) {
115 System.out.println(e.toString());
118 } catch(ClassNotFoundException e) {
119 System.out.println(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)");
123 } catch(InstantiationException e) {
124 System.out.println("Unable to create the process. I got an instantiation exception");
126 } catch(IllegalAccessException e) {
127 System.out.println("Unable to create the process. I got an illegal access exception");
137 public static void onStartDocument() {
138 args = new Vector<String>();
139 properties = new Hashtable<String,String>();
149 public static void onBeginProcess(String hostName, String function) {
150 setProcessIdentity(hostName, function);
158 public static void onProperty(String id, String value) {
159 setProperty(id, value);
166 public static void onProcessArg(String arg) {
167 registerProcessArg(arg);
173 public static void onEndProcess() {
180 public static void onEndDocument() {