⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persistenthttpservlet.java

📁 JAVA网络编程技术内幕一书的源代码
💻 JAVA
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public abstract class PersistentHttpServlet extends HttpServlet {
  protected PersistentHttpServlet () {
  }

  public void init (ServletConfig config) throws ServletException {
    init (config, -1);
  }

  protected Serializable state;
  protected Thread saver;
  protected int savePeriod;
  
  protected void init (ServletConfig config, int savePeriod) throws ServletException {
    super.init (config);
    try {
      if (getStatePath () == null)
        throw new FileNotFoundException ();
      ObjectInputStream objectIn = new ObjectInputStream (
        new FileInputStream (getStatePath ()));
      state = (Serializable) objectIn.readObject ();
      objectIn.close ();
      log ("state loaded");
    } catch (FileNotFoundException ex) {
      state = createState ();
      log ("state created");
    } catch (IOException ex) {
      throw new UnavailableException (this, ex.toString ());
    } catch (ClassNotFoundException ex) {
      throw new UnavailableException (this, ex.toString ());
    }
    if ((savePeriod > 0) && (getStatePath () != null)) {
      this.savePeriod = savePeriod;
      saver = new Thread () {
        public void run () {
          autosave ();
        }
      };
      saver.start ();
    }
  }

  protected String getStatePath () {
    return getInitParameter ("persistentStatePath");
  }

  protected abstract Serializable createState ();

  protected int stateCount;
  
  void autosave () {
    Thread myself = Thread.currentThread ();
    try {
      int savedState = stateCount;
      while (saver == myself) {
        Thread.sleep (savePeriod);
        int newState = stateCount;
        if (savedState != newState) {
          ObjectOutputStream objectOut = new ObjectOutputStream (
            new FileOutputStream (getStatePath ()));
          synchronized (state) {
            objectOut.writeObject (state);
          }
          objectOut.close ();
          log ("state saved");
          savedState = newState;
        }
      }
    } catch (InterruptedException ignored) {
    } catch (IOException ex) {
      String name = getClass ().getName ();
      getServletContext ().log (ex, name + ": error saving state");
    }
  }

  protected void stateChanged () {
    ++ stateCount;
  }

  public void destroy () {
    Thread saver = this.saver;
    this.saver = null;
    if (saver != null) {
      saver.interrupt ();
      try {
        saver.join ();
      } catch (InterruptedException ignored) {
      }
    }
    if (getStatePath () != null) {
      try {
        ObjectOutputStream objectOut = new ObjectOutputStream (
          new FileOutputStream (getStatePath ()));
        objectOut.writeObject (state);
        objectOut.close ();
        log ("state saved");
      } catch (IOException ex) {
        log (ex.toString ());
      }
    }
    super.destroy ();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -