~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/servlet/ServletUtil.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.servlet;
3
 
 
4
 
import javax.servlet.http.*;
5
 
 
6
 
/**
7
 
* Some generic functions to help Servlet writing.
8
 
*
9
 
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
10
 
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
11
 
* Distribution Licence:<br />
12
 
* Project B is free software; you can redistribute it
13
 
* and/or modify it under the terms of the GNU General Public License,
14
 
* version 2 as published by the Free Software Foundation.<br />
15
 
* This program is distributed in the hope that it will be useful,
16
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
 
* General Public License for more details.<br />
19
 
* The License is available on the internet
20
 
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
21
 
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22
 
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
23
 
* The copyright to this program is held by it's authors.
24
 
* </font></td></tr></table>
25
 
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
26
 
* @see <{docs.Licence}>
27
 
* @author Joe Walker
28
 
*/
29
 
public class ServletUtil
30
 
{
31
 
    /**
32
 
    * You can create an instance of this class to do reference counting
33
 
    */
34
 
    public ServletUtil()
35
 
    {
36
 
    }
37
 
 
38
 
    /**
39
 
    * Called at the start of any service call. The average service method
40
 
    * (or a <code>doGet()</code> or <code>doPost()</code>) method should
41
 
    * do something like this:<pre>
42
 
    *   public void service()
43
 
    *   {
44
 
    *       sutil.startService();
45
 
    *       // stuff
46
 
    *       sutil.endService();
47
 
    *   }
48
 
    * </pre>
49
 
    *
50
 
    * If there is any lengthy processing to be done in this method then
51
 
    * something like this is a good idea:<pre>
52
 
    *       while (inProgress() && sutil.isAlive())
53
 
    *       {
54
 
    *           // lengthy stuff
55
 
    *       }
56
 
    * </pre>
57
 
    *
58
 
    * In the destroy method you should do stuff like this:<pre>
59
 
    *   public void destroy()
60
 
    *   {
61
 
    *       try
62
 
    *       {
63
 
    *           die();
64
 
    *           // shutdown;
65
 
    *       }
66
 
    *       catch (InterruptedException ex)
67
 
    *       {
68
 
    *           log(ex);
69
 
    *       }
70
 
    *   }
71
 
    * </pre>
72
 
    */
73
 
    public void startService()
74
 
    {
75
 
        counter++;
76
 
    }
77
 
 
78
 
    /**
79
 
    * Called at the end of any service call
80
 
    */
81
 
    public void endService()
82
 
    {
83
 
        counter--;
84
 
 
85
 
        synchronized (this)
86
 
        {
87
 
            notify();
88
 
        }
89
 
    }
90
 
 
91
 
    /**
92
 
    * Check to see if destroy has been called. Service methods should
93
 
    * call this before any lengthy operations, and back down if there
94
 
    * is a problem.
95
 
    */
96
 
    public boolean isAlive()
97
 
    {
98
 
        return !dying;
99
 
    }
100
 
 
101
 
    /**
102
 
    * This method first sets a flag to inform any service methods that
103
 
    * ask, that we are dying, and then waits for them all to finish
104
 
    * before returning.
105
 
    */
106
 
    public void die() throws InterruptedException
107
 
    {
108
 
        dying = true;
109
 
 
110
 
        while (counter > 0)
111
 
        {
112
 
            synchronized (this)
113
 
            {
114
 
                wait();
115
 
            }
116
 
        }
117
 
    }
118
 
 
119
 
    /** The number of instances noted */
120
 
    private int counter = 0;
121
 
 
122
 
    /** The number of instances noted */
123
 
    private boolean dying = false;
124
 
}