KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > AbstractStore


1 // ========================================================================
2
// $Id: AbstractStore.java,v 1.5 2004/06/22 16:23:44 jules_gosnell Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.j2ee.session;
17
18 //----------------------------------------
19

20 import java.util.Timer JavaDoc;
21 import java.util.TimerTask JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.jfox.ioc.logger.Logger;
26
27 //----------------------------------------
28

29 public abstract class
30   AbstractStore
31   implements Store
32 {
33   protected final static Logger _log=Logger.getLogger(AbstractStore.class);
34
35   // distributed scavenging
36

37   /**
38    * The period between scavenges
39    */

40   protected int _scavengerPeriod=60*30; // 1/2 an hour
41
public int getScavengerPeriod() {return _scavengerPeriod;}
42   public void setScavengerPeriod(int secs) {_scavengerPeriod=secs;}
43   /**
44    * The extra time we wait before tidying up a CMPState to ensure
45    * that if it loaded locally it will be scavenged locally first...
46    */

47   protected int _scavengerExtraTime=60*30; // 1/2 an hour
48
public int getScavengerExtraTime() {return _scavengerExtraTime;}
49   public void setScavengerExtraTime(int secs) {_scavengerExtraTime=secs;}
50   /**
51    * A maxInactiveInterval of -1 means never scavenge. The DB would
52    * fill up very quickly - so we can override -1 with a real value
53    * here.
54    */

55   protected int _actualMaxInactiveInterval=60*60*24*28; // 28 days
56
public int getActualMaxInactiveInterval() {return _actualMaxInactiveInterval;}
57   public void setActualMaxInactiveInterval(int secs) {_actualMaxInactiveInterval=secs;}
58
59   public Object JavaDoc
60     clone()
61     {
62       try
63       {
64     AbstractStore as=(AbstractStore)(getClass().newInstance());
65     as.setScavengerPeriod(_scavengerPeriod);
66     as.setScavengerExtraTime(_scavengerExtraTime);
67     as.setActualMaxInactiveInterval(_actualMaxInactiveInterval);
68     return as;
69       }
70       catch (Exception JavaDoc e)
71       {
72     _log.warn("could not clone Store", e);
73     return null;
74       }
75     }
76
77   protected Manager _manager;
78   public Manager getManager(){return _manager;}
79   public void setManager(Manager manager){_manager=manager;}
80
81
82   protected Timer JavaDoc _scavenger;
83
84   // Store LifeCycle
85
public void
86     start()
87     throws Exception JavaDoc
88     {
89       _log.trace("starting...");
90       boolean isDaemon=true;
91       _scavenger=new Timer JavaDoc(isDaemon);
92       long delay=_scavengerPeriod+Math.round(Math.random()*_scavengerPeriod);
93       if (_log.isDebugEnabled()) _log.debug("starting distributed scavenger thread...(period: "+delay+" secs)");
94       _scavenger.scheduleAtFixedRate(new Scavenger(), delay*1000, _scavengerPeriod*1000);
95       _log.debug("...distributed scavenger thread started");
96       _log.trace("...started");
97     }
98
99   public void
100     stop()
101     {
102       _log.trace("stopping...");
103       _log.debug("stopping distributed scavenger thread...");
104       _scavenger.cancel();
105       _scavenger=null;
106       _log.debug("...distributed scavenger thread stopped");
107
108       try
109       {
110     scavenge();
111       }
112       catch (Exception JavaDoc e)
113       {
114     _log.warn("error scavenging distributed sessions", e);
115       }
116
117       _log.trace("...stopped");
118     }
119
120   public void
121     destroy()
122     {
123       _log.trace("destroying...");
124       _log.trace("...destroyed");
125     }
126
127   class Scavenger
128     extends TimerTask JavaDoc
129   {
130     public void
131       run()
132     {
133       try
134       {
135     scavenge();
136       }
137       catch (Throwable JavaDoc e)
138       {
139     _log.warn("could not scavenge distributed sessions", e);
140       }
141     }
142   }
143
144   // ID allocation
145

146   public String JavaDoc
147     allocateId(HttpServletRequest JavaDoc request)
148     {
149       return getManager().getIdGenerator().nextId(request);
150     }
151
152   public void
153     deallocateId(String JavaDoc id)
154     {
155       // these ids are disposable
156
}
157
158   // still abstract...
159

160   // // Store LifeCycle
161
// void destroy(); // corresponds to ctor
162
//
163
// boolean isDistributed();
164
//
165
// // State LifeCycle
166
// State newState(String id, int maxInactiveInterval) throws Exception;
167
// State loadState(String id) throws Exception;
168
// void storeState(State state) throws Exception;
169
// void removeState(State state) throws Exception;
170
//
171
// // Store misc
172
// void scavenge() throws Exception;
173
// void passivateSession(StateAdaptor sa);
174
}
175
Popular Tags