001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.session;
019:
020: import java.beans.PropertyChangeListener;
021: import java.beans.PropertyChangeSupport;
022: import java.io.IOException;
023:
024: import org.apache.catalina.Lifecycle;
025: import org.apache.catalina.LifecycleException;
026: import org.apache.catalina.LifecycleListener;
027: import org.apache.catalina.Manager;
028: import org.apache.catalina.Store;
029: import org.apache.catalina.util.LifecycleSupport;
030: import org.apache.catalina.util.StringManager;
031:
032: /**
033: * Abstract implementation of the Store interface to
034: * support most of the functionality required by a Store.
035: *
036: * @author Bip Thelin
037: * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
038: */
039:
040: public abstract class StoreBase implements Lifecycle, Store {
041:
042: // ----------------------------------------------------- Instance Variables
043:
044: /**
045: * The descriptive information about this implementation.
046: */
047: protected static String info = "StoreBase/1.0";
048:
049: /**
050: * Name to register for this Store, used for logging.
051: */
052: protected static String storeName = "StoreBase";
053:
054: /**
055: * Has this component been started yet?
056: */
057: protected boolean started = false;
058:
059: /**
060: * The lifecycle event support for this component.
061: */
062: protected LifecycleSupport lifecycle = new LifecycleSupport(this );
063:
064: /**
065: * The property change support for this component.
066: */
067: protected PropertyChangeSupport support = new PropertyChangeSupport(
068: this );
069:
070: /**
071: * The string manager for this package.
072: */
073: protected StringManager sm = StringManager
074: .getManager(Constants.Package);
075:
076: /**
077: * The Manager with which this JDBCStore is associated.
078: */
079: protected Manager manager;
080:
081: // ------------------------------------------------------------- Properties
082:
083: /**
084: * Return the info for this Store.
085: */
086: public String getInfo() {
087: return (info);
088: }
089:
090: /**
091: * Return the name for this Store, used for logging.
092: */
093: public String getStoreName() {
094: return (storeName);
095: }
096:
097: /**
098: * Set the Manager with which this Store is associated.
099: *
100: * @param manager The newly associated Manager
101: */
102: public void setManager(Manager manager) {
103: Manager oldManager = this .manager;
104: this .manager = manager;
105: support.firePropertyChange("manager", oldManager, this .manager);
106: }
107:
108: /**
109: * Return the Manager with which the Store is associated.
110: */
111: public Manager getManager() {
112: return (this .manager);
113: }
114:
115: // --------------------------------------------------------- Public Methods
116:
117: /**
118: * Add a lifecycle event listener to this component.
119: *
120: * @param listener The listener to add
121: */
122: public void addLifecycleListener(LifecycleListener listener) {
123: lifecycle.addLifecycleListener(listener);
124: }
125:
126: /**
127: * Get the lifecycle listeners associated with this lifecycle. If this
128: * Lifecycle has no listeners registered, a zero-length array is returned.
129: */
130: public LifecycleListener[] findLifecycleListeners() {
131:
132: return lifecycle.findLifecycleListeners();
133:
134: }
135:
136: /**
137: * Remove a lifecycle event listener from this component.
138: *
139: * @param listener The listener to add
140: */
141: public void removeLifecycleListener(LifecycleListener listener) {
142: lifecycle.removeLifecycleListener(listener);
143: }
144:
145: /**
146: * Add a property change listener to this component.
147: *
148: * @param listener a value of type 'PropertyChangeListener'
149: */
150: public void addPropertyChangeListener(
151: PropertyChangeListener listener) {
152: support.addPropertyChangeListener(listener);
153: }
154:
155: /**
156: * Remove a property change listener from this component.
157: *
158: * @param listener The listener to remove
159: */
160: public void removePropertyChangeListener(
161: PropertyChangeListener listener) {
162: support.removePropertyChangeListener(listener);
163: }
164:
165: // --------------------------------------------------------- Protected Methods
166:
167: /**
168: * Called by our background reaper thread to check if Sessions
169: * saved in our store are subject of being expired. If so expire
170: * the Session and remove it from the Store.
171: *
172: */
173: public void processExpires() {
174: long timeNow = System.currentTimeMillis();
175: String[] keys = null;
176:
177: if (!started) {
178: return;
179: }
180:
181: try {
182: keys = keys();
183: } catch (IOException e) {
184: manager.getContainer().getLogger().error(
185: "Error getting keys", e);
186: return;
187: }
188: if (manager.getContainer().getLogger().isDebugEnabled()) {
189: manager.getContainer().getLogger().debug(
190: getStoreName()
191: + ": processExpires check number of "
192: + keys.length + " sessions");
193: }
194:
195: for (int i = 0; i < keys.length; i++) {
196: try {
197: StandardSession session = (StandardSession) load(keys[i]);
198: if (session == null) {
199: continue;
200: }
201: if (session.isValid()) {
202: continue;
203: }
204: if (manager.getContainer().getLogger().isDebugEnabled()) {
205: manager
206: .getContainer()
207: .getLogger()
208: .debug(
209: getStoreName()
210: + ": processExpires expire store session "
211: + keys[i]);
212: }
213: if (((PersistentManagerBase) manager).isLoaded(keys[i])) {
214: // recycle old backup session
215: session.recycle();
216: } else {
217: // expire swapped out session
218: session.expire();
219: }
220: remove(session.getIdInternal());
221: } catch (Exception e) {
222: manager.getContainer().getLogger().error(
223: "Session: " + keys[i] + "; ", e);
224: try {
225: remove(keys[i]);
226: } catch (IOException e2) {
227: manager.getContainer().getLogger().error(
228: "Error removing key", e2);
229: }
230: }
231: }
232: }
233:
234: // --------------------------------------------------------- Thread Methods
235:
236: /**
237: * Prepare for the beginning of active use of the public methods of this
238: * component. This method should be called after <code>configure()</code>,
239: * and before any of the public methods of the component are utilized.
240: *
241: * @exception LifecycleException if this component detects a fatal error
242: * that prevents this component from being used
243: */
244: public void start() throws LifecycleException {
245: // Validate and update our current component state
246: if (started)
247: throw new LifecycleException(sm.getString(getStoreName()
248: + ".alreadyStarted"));
249: lifecycle.fireLifecycleEvent(START_EVENT, null);
250: started = true;
251:
252: }
253:
254: /**
255: * Gracefully terminate the active use of the public methods of this
256: * component. This method should be the last one called on a given
257: * instance of this component.
258: *
259: * @exception LifecycleException if this component detects a fatal error
260: * that needs to be reported
261: */
262: public void stop() throws LifecycleException {
263: // Validate and update our current component state
264: if (!started)
265: throw new LifecycleException(sm.getString(getStoreName()
266: + ".notStarted"));
267: lifecycle.fireLifecycleEvent(STOP_EVENT, null);
268: started = false;
269:
270: }
271:
272: }
|