/* * @(#)AutoRun.java (Metawerx customer example classes) * * Copyright (c) 1998-2005 Metawerx. All Rights Reserved. * * This software is the confidential and proprietary information of Metawerx * ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the * license agreement you entered into with Metawerx. * * METAWERX MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE OR THE SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METAWERX SHALL NOT BE LIABLE * FOR ANY DAMAGES SUFFERED BY ANY PARTY AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE AND DOCUMENTATION OR ITS DERIVATIVES * * * Synopsis * ===================================================================================== * This code demonstrates an AutoRun servlet using the <load-on-startup> tag * * Initialisation code is placed into the runInitialisation() routine. * * The following code is placed into web.xml to run this servlet when the webapp * is started. <servlet> <servlet-name>AutoRun</servlet-name> <servlet-class>AutoRun</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AutoRun</servlet-name> <url-pattern>/autorunme</url-pattern> </servlet-mapping> * If this is the ROOT web-app... * the servlet can also be started by going to URL http://site/autorunme * * If this is not the ROOT web-app... * the servlet can also be started by going to URL http://site/appName/autorunme * * * Complete file structure for Tomcat for this sample * ===================================================================================== JAKARTA-WEBAPPS JAKARTA-WEBAPPS/AutoSample JAKARTA-WEBAPPS/AutoSample/WEB-INF JAKARTA-WEBAPPS/AutoSample/WEB-INF/web.xml JAKARTA-WEBAPPS/AutoSample/WEB-INF/classes/AutoRun.class * * Complete, minimal web.xml file for this sample * ===================================================================================== <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Metawerx Samples</display-name> <description>Metawerx Samples</description> <servlet> <servlet-name>AutoRun</servlet-name> <servlet-class>AutoRun</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AutoRun</servlet-name> <url-pattern>/autorunme</url-pattern> </servlet-mapping> </web-app> * History * ===================================================================================== * 9/17/2005 17:13 Neale Rudd * Added more documentation * 7/22/2005 08:42 Neale Rudd * Added web.xml example and file tree layout * 7/17/2005 22:23 Neale Rudd * Created * */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AutoRun extends HttpServlet { // Flag to indicate whether we initialised or not protected static boolean initialisedOk = false; // Dummy object just used for synchronization in the example, // to ensure only one thread executes the initialization. private static Object thisClassSync = new Object(); // This function is run when the servlet is initialised // Therefore, it will get run by the servlet-container when the application starts // because of the load-on-startup tag in web.xml public void init(ServletConfig config) { // Run initialiser, get response as string String message = runInitialisation(); // Output to System.out for debugging System.out.println("[AutoRun]: "+message); } // This function is only run when accessed by the browser (user-agent) public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // Only run this once at a time synchronized (thisClassSync) { if(!initialisedOk) { // Run initialiser, get response as string String message = runInitialisation(); // Output response to browser PrintWriter out = new PrintWriter(response.getOutputStream()); out.println(message); out.close(); } else { // Output to System.out for debugging // Note that System.out.println() in a synchronised block will cancel the synchronisation // because of operating system I/O, therefore only uncomment this line during debugging. System.out.println("[AutoRun]: Already initialised"); // Already initialised PrintWriter out = new PrintWriter(response.getOutputStream()); out.println("Already initialised"); out.close(); } } } // Perform work in here private String runInitialisation() { // ... // Do something here on startup, such as initialising data structures, or obtaining // data from a remote server. // ... // Set flag to tell if we are initialised on subsequent calls initialisedOk = true; // Return something which can be used either by init() or by doGet() return("Ok - initialised"); } }