home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / icsclass.jar / lotus / domino / servlet / DominoServletInvoker.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-15  |  7.7 KB  |  290 lines

  1. package lotus.domino.servlet;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.OptionalDataException;
  9. import java.io.PrintStream;
  10. import java.io.StreamCorruptedException;
  11. import java.io.StringReader;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import javax.servlet.Servlet;
  15. import javax.servlet.ServletConfig;
  16. import javax.servlet.ServletException;
  17.  
  18. public class DominoServletInvoker {
  19.    public static final int UNINITIALIZED = 0;
  20.    public static final int READY = 1;
  21.    public static final int WAITING_TO_DIE = 2;
  22.    public static final int DEAD = 3;
  23.    public static final int ERROR = 4;
  24.    public static final String INTERNAL_ERROR_MESSAGE = "Internal Error";
  25.    protected String i_name;
  26.    private String i_classname;
  27.    private Servlet i_servlet;
  28.    private ServletConfig i_config;
  29.    private int i_state = 0;
  30.  
  31.    public DominoServletInvoker(String var1, ServletConfig var2, String var3) {
  32.       this.i_classname = var1;
  33.       this.i_config = var2;
  34.       this.i_name = var3;
  35.       if (this.i_config != null) {
  36.          String var4 = this.i_config.getInitParameter("GO_LOAD_AT_STARTUP");
  37.          if (var4 != null && var4.toUpperCase().equals("YES")) {
  38.             this.load();
  39.          }
  40.       }
  41.  
  42.    }
  43.  
  44.    public void load() {
  45.       try {
  46.          Object var1 = null;
  47.          int var2 = this.i_classname.lastIndexOf(".");
  48.          if (var2 != -1) {
  49.             String var3 = this.i_classname.substring(var2);
  50.             if (var3.toUpperCase().equals(".SER")) {
  51.                FileInputStream var4 = new FileInputStream(this.i_classname);
  52.                ObjectInputStream var5 = new ObjectInputStream(var4);
  53.                var1 = var5.readObject();
  54.             } else if (var3.toUpperCase().equals(".CLASS")) {
  55.                this.i_classname = this.i_classname.substring(0, var2);
  56.                Class var19 = Class.forName(this.i_classname);
  57.                var1 = var19.newInstance();
  58.             } else {
  59.                Class var20 = Class.forName(this.i_classname);
  60.                var1 = var20.newInstance();
  61.             }
  62.          } else {
  63.             Class var17 = Class.forName(this.i_classname);
  64.             var1 = var17.newInstance();
  65.          }
  66.  
  67.          if (var1 instanceof Servlet) {
  68.             this.i_servlet = (Servlet)var1;
  69.             Hashtable var18 = ServletManager.getServletTable();
  70.             var18.put(this.i_name, this.i_servlet);
  71.             this.i_servlet.init(this.i_config);
  72.             this.i_state = 1;
  73.          } else {
  74.             throw new ClassCastException("Cannot convert Class " + this.i_classname + " to java.lang.Servlet");
  75.          }
  76.       } catch (FileNotFoundException var6) {
  77.          this.i_state = 4;
  78.          System.err.println("Error occurred while loading Servlet (" + this.i_name + ")");
  79.          System.err.println("File " + this.i_classname + " was not found");
  80.          this.printInfo(this.toString(), System.err);
  81.          ((Throwable)var6).printStackTrace(System.err);
  82.       } catch (IllegalArgumentException var7) {
  83.          this.i_state = 4;
  84.          System.err.println("Error loading Servlet (" + this.i_name + "): " + this.i_classname + " is not a valid class name");
  85.          this.printInfo(this.toString(), System.err);
  86.          ((Throwable)var7).printStackTrace(System.err);
  87.       } catch (ClassNotFoundException var8) {
  88.          this.i_state = 4;
  89.          System.err.println("Error occurred while loading Servlet (" + this.i_name + ")");
  90.          System.err.println("Class " + this.i_classname + " was not found: check the CLASSPATH");
  91.          this.printInfo(this.toString(), System.err);
  92.          ((Throwable)var8).printStackTrace(System.err);
  93.       } catch (IllegalAccessException var9) {
  94.          this.i_state = 4;
  95.          System.err.println("Error occurred while loading Servlet (" + this.i_name + ")");
  96.          System.err.println("Could not create an instance of class: " + this.i_classname);
  97.          this.printInfo(this.toString(), System.err);
  98.          ((Throwable)var9).printStackTrace(System.err);
  99.       } catch (InstantiationException var10) {
  100.          this.i_state = 4;
  101.          System.err.println("Error occurred while loading Servlet (" + this.i_name + ")");
  102.          System.err.println("Could not create an instance of class: " + this.i_classname);
  103.          this.printInfo(this.toString(), System.err);
  104.          ((Throwable)var10).printStackTrace(System.err);
  105.       } catch (OptionalDataException var11) {
  106.          this.i_state = 4;
  107.          System.err.println("Error occurred while loading Servlet (" + this.i_name + ")");
  108.          System.err.println("Could not create an instance of class: " + this.i_classname);
  109.          this.printInfo(this.toString(), System.err);
  110.          ((Throwable)var11).printStackTrace(System.err);
  111.       } catch (StreamCorruptedException var12) {
  112.          this.i_state = 4;
  113.          System.err.println("Error occurred while loading Servlet (" + this.i_name + ")");
  114.          System.err.println("The serialization file is corrupt");
  115.          this.printInfo(this.toString(), System.err);
  116.          ((Throwable)var12).printStackTrace(System.err);
  117.       } catch (IOException var13) {
  118.          this.i_state = 4;
  119.          System.err.println("Error occurred while loading Servlet (" + this.i_name + ")");
  120.          System.err.println("I/O error while loading class: " + this.i_classname);
  121.          this.printInfo(this.toString(), System.err);
  122.          ((Throwable)var13).printStackTrace(System.err);
  123.       } catch (ServletException var14) {
  124.          this.i_state = 4;
  125.          System.err.println("Error occurred while initializing Servlet (" + this.i_name + ")");
  126.          this.printInfo(this.toString(), System.err);
  127.          ((Throwable)var14).printStackTrace(System.err);
  128.       } catch (Throwable var15) {
  129.          this.i_state = 4;
  130.          System.err.println("Error occurred while initializing Servlet (" + this.i_name + ")");
  131.          this.printInfo(this.toString(), System.err);
  132.          var15.printStackTrace(System.err);
  133.       }
  134.    }
  135.  
  136.    public void destroy() {
  137.       if (this.i_state == 1) {
  138.          this.i_state = 2;
  139.          this.i_servlet.destroy();
  140.       }
  141.  
  142.       this.i_state = 3;
  143.    }
  144.  
  145.    public int invokeSSI() throws ServletException, IOException {
  146.       try {
  147.          if (this.i_state == 4) {
  148.             NativeStub.set("HTTP_REASON", "Internal Error");
  149.             return 500;
  150.          } else {
  151.             return this.executeServlet(true);
  152.          }
  153.       } catch (NativeStubException var2) {
  154.          ((Throwable)var2).printStackTrace();
  155.          throw new ServletException("Set HTTP_REASON for SSI failed");
  156.       }
  157.    }
  158.  
  159.    public int service() throws ServletException, IOException {
  160.       return this.executeServlet(false);
  161.    }
  162.  
  163.    private int executeServlet(boolean var1) throws ServletException, IOException {
  164.       if (this.i_state == 4) {
  165.          try {
  166.             NativeStub.set("HTTP_REASON", "Internal Error");
  167.          } catch (NativeStubException var6) {
  168.             ((Throwable)var6).printStackTrace();
  169.          }
  170.  
  171.          return 500;
  172.       } else {
  173.          if (this.i_state == 0 && this.i_config != null) {
  174.             this.load();
  175.          }
  176.  
  177.          if (this.i_state == 1) {
  178.             NativeStubContext var2 = new NativeStubContext();
  179.             Object var3 = null;
  180.             Object var4 = null;
  181.             if (this.i_servlet instanceof Servlet) {
  182.                DominoHttpServletRequest var10 = new DominoHttpServletRequest(var2);
  183.                DominoHttpServletResponse var11 = new DominoHttpServletResponse(var2);
  184.                var10.setResponse(var11);
  185.                var11.setRequest(var10);
  186.                ((DominoServletRequest)var10).getInputStream();
  187.  
  188.                try {
  189.                   this.i_servlet.service(var10, var11);
  190.                } catch (Throwable var8) {
  191.                   Throwable var5 = var8;
  192.                   System.err.println("Exception detected while executing while executing Servlet (" + this.i_name + ")");
  193.                   var8.printStackTrace();
  194.  
  195.                   try {
  196.                      if (!var11.containsHeader("HTTP_REASON")) {
  197.                         var2.set("HTTP_REASON", "Internal Error");
  198.                      }
  199.  
  200.                      var5.printStackTrace();
  201.                   } catch (NativeStubException var7) {
  202.                      var8.printStackTrace();
  203.                   }
  204.                }
  205.  
  206.                ((DominoServletRequest)var10).close();
  207.                ((DominoServletResponse)var11).close();
  208.                return var2.getReturnCode();
  209.             } else {
  210.                System.err.println(this.i_name + " is not a valid servlet");
  211.                this.i_state = 4;
  212.                return 500;
  213.             }
  214.          } else {
  215.             try {
  216.                NativeStub.set("HTTP_REASON", "Internal Error");
  217.             } catch (NativeStubException var9) {
  218.                ((Throwable)var9).printStackTrace();
  219.             }
  220.  
  221.             return 500;
  222.          }
  223.       }
  224.    }
  225.  
  226.    public String getName() {
  227.       return this.i_name;
  228.    }
  229.  
  230.    public Servlet getServlet() {
  231.       return this.i_servlet;
  232.    }
  233.  
  234.    public String toString() {
  235.       String var1 = "-----Servlet Information-----\n";
  236.       var1 = var1 + "Name: " + this.i_name.toString() + "\n";
  237.       var1 = var1 + "Base Class: " + this.i_classname.toString() + "\n";
  238.       var1 = var1 + "State: ";
  239.       switch (this.i_state) {
  240.          case 0:
  241.             var1 = var1 + "UNITIALIZED";
  242.             break;
  243.          case 1:
  244.             var1 = var1 + "READY";
  245.             break;
  246.          case 2:
  247.             var1 = var1 + "WAITING_TO_DIE";
  248.             break;
  249.          case 3:
  250.             var1 = var1 + "DEAD";
  251.             break;
  252.          case 4:
  253.             var1 = var1 + "ERROR";
  254.       }
  255.  
  256.       var1 = var1 + "\n";
  257.       if (this.i_config != null) {
  258.          Enumeration var2 = this.i_config.getInitParameterNames();
  259.          String var3;
  260.          if (!var2.hasMoreElements()) {
  261.             var1 = var1 + "Configuration parameters: <none>\n";
  262.          } else {
  263.             for(var1 = var1 + "Configuration parameters:\n"; var2.hasMoreElements(); var1 = var1 + this.i_name + " parameter: " + var3 + " = " + this.i_config.getInitParameter(var3) + "\n") {
  264.                var3 = var2.nextElement().toString();
  265.             }
  266.          }
  267.       } else {
  268.          var1 = var1 + "Servlet Configuration Object is null\n";
  269.       }
  270.  
  271.       if (this.i_servlet != null) {
  272.          var1 = var1 + "Info for: " + this.i_servlet.toString() + "\n";
  273.       }
  274.  
  275.       return var1;
  276.    }
  277.  
  278.    public void printInfo(String var1, PrintStream var2) {
  279.       BufferedReader var3 = new BufferedReader(new StringReader(var1));
  280.  
  281.       try {
  282.          for(String var4 = var3.readLine(); var4 != null; var4 = var3.readLine()) {
  283.             var2.println(var4);
  284.          }
  285.  
  286.       } catch (IOException var5) {
  287.       }
  288.    }
  289. }
  290.