Server Connection and Web Application Initialization

Before the Web application can call any SIMULIA Execution Engine APIs, it must initialize the API environment and establish the connection to the SIMULIA Execution Engine. The one-time process is required before any SIMULIA Execution Engine APIs are called. Once the connection is established, it is held for the lifetime of the JVM and is used for all subsequent SIMULIA Execution Engine API calls. As a result, it is not possible to have a Web application that connects to more than one SIMULIA Execution Engine system. Even two Web applications running in the same server JVM instance cannot connect to different SIMULIA Execution Engines.

To ensure that the connection to the SIMULIA Execution Engine is established before any API calls are made, you can have the Web application implement a ServletContextListener class with the following ServletContextListener method:

public void contextInitialized(ServletContextEvent arg0)

This method is called by the servlet container before any servlet or filter in the Web application is initialized. Next, perform one-time initialization.

This class must be named in the web.xml file in a <listener-class> tag. For example:

<listener>
<listener-class>com.mycompany.MyServletContextHandler</listener
-class>
</listener>

The initialization method must do the following:

  1. Set the SIMULIA Execution Engine API environment with the location of the SIMULIA Execution Engine installation directory.

  2. Determine the connection information for the SIMULIA Execution Engine (e.g., identify the SIMULIA Execution Engine name, port number, etc.).

  3. Call the appropriate SIMULIA Execution Engine APIs to initialize the server connection.

How step 2 and step 3 above are done depends on how the Web application is to be configured. The Web application developer must decide how the Web administrator will specify the names of local directories and the name and port number of the SIMULIA Execution Engine. In some cases it may be acceptable to hard-code these values (e.g., in a services deployment for a particular customer). The developer may choose to obtain them from JVM system properties (specified by the Web server administrator), or the developer may retrieve them from the web.xml deployment file. It is up to the Web application developer to determine how and when the application will be configured.

The SysFiper class contains all the methods necessary to initialize the APIs. The local directory names are supplied on the SysFiper.setFiperEnv() method, and the server connection is initialized on the SysFiper.initConnection() method. There are three ways in which the Web application can supply server connection information by calling different forms of the initConnection() method on SysFiper:

  • public static void initConnection(ConnectionProfile). A com.engineous.sdk.server.ConnectionProfile object instance can be supplied. A ConnectionProfile can be constructed from a CPR file name or from a java.util.Properties object containing the same keys and values that would be found in a CPR file.

  • public static void initConnection(Properties). A java.util.Properties object can be supplied. The Properties object must contain the same property names and values as would be found in a connection profile (CPR) file. This form is a shortcut that creates a ConnectionProfile from the given Properties and calls the initConnection(ConnectionProfile) method.

  • public static void initConnection(File). The fully qualified name of a connection profile (.CPR) file can be supplied. Connection information will be read from the file. The CPR file can be created with the EDITCPR command. This form is a shortcut that creates a ConnectionProfile and calls the initConnection(ConnectionProfile) method.

The following is a sample ServletContextListener.contextInitialized() method for Websphere based SIMULIA Execution Engine. It obtains the Isight installation directory name from the Web deployment descriptor (web.xml file). The name of the SIMULIA Execution Engine server is hard-coded in this case:

public void contextInitialized(ServletContextEvent arg0) {

   //Perform one-time application initialization.

   try {
      // Lookup the location of the Fiper installation
      // in the environment settings, as defined      
      // in the web.xml deployment descriptor.
      Context env = (Context)new InitialContext().lookup ("java:comp/env");
      String esiHome = (String)env.lookup("esihome");

      // Init the Fiper system environment, disallow native code in server
      SysFiper.setFiperEnv(esiHome, null, false);

      // Setup connection properties for Fiper server
      Properties connProps = new Properties ();
      connProps.put ("server", "stingray"); // IP DNS name of Fiper server
      connProps.put ("port", "2809"); // IIOP port (bootstrap address)
      connProps.put ("Sys_ServerSupportDir", "websphere"); //Server dir

      // Init the Fiper ACS connection
      SysFiper.initConnection (connProps);
   }
   catch (Throwable t) {
      // The ServletContextListener interface defines no THROWS clause on
      // this method, so we can only throw runtime exceptions. Most web
      // containers will report this in their log or console file.
      throw new RuntimeException ("Failed to initialize Fiper API.", t);
   }
}

The following is a sample ServletContextListener.contextInitialized() method for TomEE based SIMULIA Execution Engine. It obtains the Isight installation directory name from the Web deployment descriptor (web.xml file). The name of the SIMULIA Execution Engine server is hard-coded in this case:

public void contextInitialized(ServletContextEvent arg0) {

  //Perform one-time application initialization.

   try {
      // Lookup the location of the Fiper installation
      // in the environment settings, as defined      
      // in the web.xml deployment descriptor.
      Context env = (Context)new InitialContext().lookup ("java:comp/env");
      String installPath = (String)env.lookup("esihome");
      String configPath = installPath + File.separatorChar + ".." + File.separatorChar + "config";
      // Init the Fiper system environment, disallow native code in server
      SysFiper.setFiperEnv(installPath, configPath, null, false);
      String cprFile = (String)env.lookup("cprfile");
      // Init the Fiper ACS connection
      SysFiper.initConnection (new File(cprFile));
   }
   catch (Throwable t) {
      // The ServletContextListener interface defines no THROWS clause on
      // this method, so we can only throw runtime exceptions. Most web
      // containers will report this in their log or console file.
      throw new RuntimeException ("Failed to initialize Fiper API.", t);
   }
}

The installation directory name in this example is read from the servlet context. The web.xml deployment descriptor must have the “esihome” environment setting specified. For example, in the Eclipse development environment the settings are specified on the Variables tab of the Web Deployment editor.

The environment variables are entries in the web.xml file:

<env-entry>
   <env-entry-name>esihome</env-entry-name>
   <env-entry-type>java.lang.String</env-entry-type>
   <env-entry-value>C:\SIMULIA\Isight\2022</env-entry-value>
</env-entry>

The SIMULIA Execution Engine connection properties are placed directly into a Properties object and passed to the SysFiper.initConnection() method. The property names are exactly the same as those that would appear in a CPR file. In this example, the server name and port number are hard-coded.

Once this initialization has been completed, the servlet can make any SIMULIA Execution Engine API calls. For example, the following servlet will display the ID of the user and the list of jobs the user has run.

// Import Java SDK classes
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

// Import J2EE classes
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// Import Fiper classes
import com.engineous.sdk.pse.JobInfoValue;
import com.engineous.sdk.pse.SysPSE;

/**
* Sample Fiper servlet that returns a web page containing the
* current user’s ID and list of Fiper jobs.
*/

public class MyFiperServlet
   extends javax.servlet.http.HttpServlet {

   /**
   * Defer to POST method
   */
   protected void doGet (HttpServletRequest request, HttpServletResponse 
   response) throws ServletException, IOException {
      doPost (request, response);
   }
   /**
   * Generate HTML response to the browser.
   */
   protected void doPost (HttpServletRequest request, 
   HttpServletResponse
   response throws ServletException, IOException {
      
      // Prepare to write HTML response
      response.setContentType ("text/html");
      PrintWriter out = response.getWriter();

      // HTML header tags
      out.println ("<html><head><title>Sample Fiper Web Application</title>
      </head>");
      out.println ("<body>");
      try {
         // get user ID from request object  
         String userID = request.getParameter("username");
		//or get userID using getUserPrincipal() method of HttpServletRequest 
		//String userID = request. getUserPrincipal().getName();
         out.println ("FIPER user = " + userID);

         // Call Fiper APIs to get list of jobs for this user
         Iterator jobList = 
         SysPSE.getPSE().getJobList(userID).iterator();
         while (jobList .hasNext () ) {
            JobInfoValue jobInfo = (JobInfoValue)jobList.next ();
            out.println ("<br>Job "+jobInfo.getID()) ;
         }
      }
      catch (Throwable t) {
         out.println ("<br>Error: "+t.toString () + "</b>");
      }
      // Finish the HTML
      out.println ("</body></html>");
      out.close ();
   }
}