Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
removing NativeExpection and adding others
[simgrid.git] / src / java / simgrid / msg / ApplicationHandler.java
index e5fd30d..ef245d7 100644 (file)
 /*
- * simgrid.msg.ApplicationHandler.java 1.00 07/05/01
+ * These are the upcalls used by the FleXML parser for application files
  *
- * Copyright 2006,2007 Martin Quinson, Malek Cherier           
+ * Copyright 2006,2007,2010 The SimGrid team.           
  * All right reserved. 
  *
  * This program is free software; you can redistribute 
  * it and/or modify it under the terms of the license 
- *(GNU LGPL) which comes with this package. 
+ * (GNU LGPL) which comes with this package.
  */
 
 package simgrid.msg;
 
-import org.xml.sax.*;
-import org.xml.sax.helpers.*;
-import java.lang.reflect.*;
+import java.util.Vector;
+import java.util.Hashtable;
 
-/**
- * The handler used to parse the deployment file which contains 
- * the description of the application (simulation).
- *
- * @author  Abdelmalek Cherier
- * @author  Martin Quinson
- * @version 1.00, 07/05/01
- * @see Host
- * @see Process
- * @see Simulation
- * @since SimGrid 3.3
- * @since JDK1.5011 
- */
-public final class ApplicationHandler extends DefaultHandler
-{
-       /* the current process. */
-    public simgrid.msg.Process process;
-    
-    public ApplicationHandler() {
-        super();
-    }
-    
-    public void startDocument() {} // NOTHING TODO
-    public void endDocument() {}   // NOTHING TODO
-    
-    public void characters(char[] chars, int beg, int lgr) {}   // NOTHING TODO
-    
-   /**
-    * element handlers
-    */
-    public void startElement(String nameSpace, String localName,String qName,Attributes attr)  {
-        if(localName.equals("process"))
-            onStartProcess(attr);   
-       else if(localName.equals("argument"))
-            onArgument(attr);
-    }
-    public void endElement(String nameSpace, String localName,String qName)  {
-        if(localName.equals("process"))
-            onEndProcess();   
-    }
-    
-    /**
-     * process element handler.
-     */
-    public void onStartProcess(Attributes attr) {
-        String hostName = attr.getValue(0);
-        String className = attr.getValue(1);
-               
-        try {
-               
-           Class cls = Class.forName(className);
-               
-           process = (simgrid.msg.Process)cls.newInstance();
-           process.name = className;
-           process.id = simgrid.msg.Process.nextProcessId++;
-           
-           Host host = Host.getByName(hostName);
-               
-           Msg.processCreate(process,host);
-               
-       } catch(Exception e) {
-           e.printStackTrace();
-        } 
-    }
-    
-    public void onEndProcess() {} // NOTHING TODO   
-    
-    /**
-     * function arguments handler.
-     */
-    public void onArgument(Attributes attr) {
-       //Msg.info("Add "+attr.getValue(0)+" as argument to "+process.msgName());
-        process.addArg(attr.getValue(0));      
-    }
-    
-   /**
-    * end of element handler.
-    */
-    
+public final class ApplicationHandler {
+
+
+               /**
+                * The vector which contains the arguments of the main function 
+                * of the process object.
+                */
+               public  static Vector<String> args;
+
+               public  static Hashtable<String,String> properties;
+
+               /**
+                * The name of the host of the process.
+                */
+               private  static String hostName;
+
+               /**
+                * The function of the process.
+                */
+               private  static String function;
+               
+               /**
+                * This method is called by the start element handler.
+                * It sets the host and the function of the process to create,
+                * and clear the vector containing the arguments of the 
+                * previouse process function if needed.
+                *
+                * @host                                The host of the process to create.
+                * @function                    The function of the process to create.
+                *
+                */
+               public  static void setProcessIdentity(String hostName_, String function_) {
+                       hostName = hostName_;    
+                       function = function_;
+
+                       if (!args.isEmpty())
+                               args.clear();
+
+                       if(!properties.isEmpty())
+                               properties.clear();
+               }
+               /**
+                * This method is called by the startElement() handler.
+                * It stores the argument of the function of the next
+                * process to create in the vector of arguments.
+                *
+                * @arg                                 The argument to add.
+                *
+                */ public  static void registerProcessArg(String arg) {
+                        args.add(arg);
+                }
+
+                public  static void setProperty(String id, String value)
+                {
+                        properties.put(id,value);      
+                }
+
+                public  static String getHostName()
+                {
+                        return hostName;
+                }
+
+                @SuppressWarnings("unchecked")
+                public  static void createProcess() {
+                        try {
+                                Class<simgrid.msg.Process> cls = (Class<Process>) Class.forName(function);
+
+                                simgrid.msg.Process process = cls.newInstance();
+                                process.name = function;
+                                process.id = simgrid.msg.Process.nextProcessId++;
+                                Host host = Host.getByName(hostName);
+
+                                MsgNative.processCreate(process, host);
+                                Vector<String> args_ = args;
+                                int size = args_.size();
+
+                                for (int index = 0; index < size; index++)
+                                        process.args.add(args_.get(index));
+
+                                process.properties = properties;
+                                properties = new Hashtable();
+
+                        } catch(HostNotFoundException e) {
+                                System.out.println(e.toString());
+                                e.printStackTrace();
+
+                        } catch(ClassNotFoundException e) {
+                                System.out.println(function +
+                                " 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)");
+                                e.printStackTrace();
+
+                        } catch(InstantiationException e) {
+                                System.out.println("Unable to create the process. I got an instantiation exception");
+                                e.printStackTrace();
+                        } catch(IllegalAccessException e) {
+                                System.out.println("Unable to create the process. I got an illegal access exception");
+                                e.printStackTrace();
+                        } 
+
+                }
+       
+
+       public  static void onStartDocument() {
+                       args = new Vector<String>();
+                       properties = new Hashtable<String,String>();
+                       hostName = null;
+                       function = null;
+       }
+
+       public  static void onBeginProcess(String hostName, String function) {
+               setProcessIdentity(hostName, function);
+               
+       }
+       public  static void onProperty(String id, String value) {
+               setProperty(id, value);
+       }
+
+       public  static void onProcessArg(String arg) {
+               registerProcessArg(arg);
+       }
+
+       public  static void onEndProcess() {
+               createProcess();
+       }        
+
+       public static void onEndDocument() {
+       }  
 }