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_);
106 catch (NoSuchMethodException e) {
107 throw new RuntimeException("Can't find the correct constructor for the class " + function + ". \n" +
108 "Is there a constructor with the signature: \"Host host, String name, String[]args\" in the class ?");
110 catch (InvocationTargetException e) {
113 catch(HostNotFoundException e) {
114 System.out.println(e.toString());
117 } catch(ClassNotFoundException e) {
118 System.out.println(function +
119 " 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)");
122 } catch(InstantiationException e) {
123 System.out.println("Unable to create the process. I got an instantiation exception");
125 } catch(IllegalAccessException e) {
126 System.out.println("Unable to create the process. I got an illegal access exception");
136 public static void onStartDocument() {
137 args = new Vector<String>();
138 properties = new Hashtable<String,String>();
148 public static void onBeginProcess(String hostName, String function) {
149 setProcessIdentity(hostName, function);
157 public static void onProperty(String id, String value) {
158 setProperty(id, value);
165 public static void onProcessArg(String arg) {
166 registerProcessArg(arg);
172 public static void onEndProcess() {
179 public static void onEndDocument() {