01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.transport.http.server;
21:
22: import org.apache.axiom.om.util.UUIDGenerator;
23: import org.apache.axis2.context.ServiceContext;
24: import org.apache.axis2.context.ServiceGroupContext;
25: import org.apache.axis2.context.SessionContext;
26: import org.apache.axis2.engine.DependencyManager;
27:
28: import java.util.HashMap;
29: import java.util.Iterator;
30: import java.util.Map;
31:
32: public class SessionManager {
33:
34: private final Map sessionmap;
35:
36: public SessionManager() {
37: super ();
38: this .sessionmap = new HashMap();
39: }
40:
41: public synchronized SessionContext getSessionContext(
42: String sessionKey) {
43: SessionContext sessionContext = null;
44: if (sessionKey != null && sessionKey.length() != 0) {
45: sessionContext = (SessionContext) this .sessionmap
46: .get(sessionKey);
47: }
48: if (sessionContext == null) {
49: sessionKey = UUIDGenerator.getUUID();
50: sessionContext = new SessionContext(null);
51: sessionContext.setCookieID(sessionKey);
52: this .sessionmap.put(sessionKey, sessionContext);
53: }
54: sessionContext.touch();
55: cleanupServiceGroupContexts();
56: return sessionContext;
57: }
58:
59: private void cleanupServiceGroupContexts() {
60: long currentTime = System.currentTimeMillis();
61: for (Iterator it = this .sessionmap.keySet().iterator(); it
62: .hasNext();) {
63: String cookieID = (String) it.next();
64: SessionContext sessionContext = (SessionContext) this .sessionmap
65: .get(cookieID);
66: if ((currentTime - sessionContext.getLastTouchedTime()) > sessionContext.sessionContextTimeoutInterval) {
67: it.remove();
68: Iterator serviceGroupContext = sessionContext
69: .getServiceGroupContext();
70: if (serviceGroupContext != null) {
71: while (serviceGroupContext.hasNext()) {
72: ServiceGroupContext groupContext = (ServiceGroupContext) serviceGroupContext
73: .next();
74: cleanupServiceContexts(groupContext);
75: }
76: }
77: }
78: }
79: }
80:
81: private void cleanupServiceContexts(
82: final ServiceGroupContext serviceGroupContext) {
83: for (Iterator it = serviceGroupContext.getServiceContexts(); it
84: .hasNext();) {
85: ServiceContext serviceContext = (ServiceContext) it.next();
86: DependencyManager.destroyServiceObject(serviceContext);
87: }
88: }
89:
90: }
|