001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core;
015:
016: import java.util.Enumeration;
017: import org.itsnat.core.ItsNatServletContext;
018: import org.itsnat.core.ItsNatSessionCallback;
019: import org.itsnat.impl.core.util.UniqueIdGenerator;
020: import java.lang.ref.WeakReference;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.WeakHashMap;
024: import javax.servlet.ServletContext;
025: import org.itsnat.core.ItsNat;
026: import org.itsnat.core.ItsNatVariableResolver;
027: import org.itsnat.impl.core.http.ItsNatImpl;
028:
029: /**
030: *
031: * @author jmarranz
032: */
033: public class ItsNatServletContextImpl extends ItsNatUserDataImpl
034: implements ItsNatServletContext {
035: protected ItsNatImpl itsNat;
036: protected ServletContext servletContext;
037: protected Map sessionsByObj = new WeakHashMap();
038: protected Map sessionsById = new WeakHashMap();
039: protected UniqueIdGenerator idGenerator = new UniqueIdGenerator();
040:
041: /** Creates a new instance of ItsNatServletContextImpl */
042: public ItsNatServletContextImpl(ServletContext servletContext,
043: ItsNatImpl itsNat) {
044: super (true);
045:
046: this .servletContext = servletContext;
047: this .itsNat = itsNat;
048: }
049:
050: public ItsNatImpl getItsNatImpl() {
051: return itsNat;
052: }
053:
054: public ItsNat getItsNat() {
055: return itsNat;
056: }
057:
058: public String generateUniqueId() {
059: return idGenerator.generateUniqueId();
060: }
061:
062: public ServletContext getServletContext() {
063: return servletContext;
064: }
065:
066: public synchronized void addItsNatSessionWeakList(
067: ItsNatSessionImpl session) {
068: Object originalSession = session.getOriginalSessionObject(); // HttpSession
069: sessionsByObj.put(originalSession, new WeakReference(session));
070: sessionsById.put(session.getId(), new WeakReference(session));
071: }
072:
073: public synchronized ItsNatSessionImpl findItsNatSessionById(
074: String id) {
075: WeakReference weakRef = (WeakReference) sessionsById.get(id);
076: if (weakRef == null)
077: return null; // No está registrada la sesión
078: return (ItsNatSessionImpl) weakRef.get(); // No puede ser nulo
079: }
080:
081: public synchronized void enumerateSessions(
082: ItsNatSessionCallback callback) {
083: if (sessionsByObj.isEmpty())
084: return;
085:
086: for (Iterator it = sessionsByObj.entrySet().iterator(); it
087: .hasNext();) {
088: Map.Entry entry = (Map.Entry) it.next();
089: WeakReference weakRef = (WeakReference) entry.getValue();
090: ItsNatSessionImpl session = (ItsNatSessionImpl) weakRef
091: .get();
092: if (session == null) // ha sido garbage collected
093: continue;
094: if (!callback.handleSession(session))
095: break;
096: }
097: }
098:
099: public Object getVariable(String varName) {
100: return getServletContext().getAttribute(varName); // Puede ser null
101: }
102:
103: public ItsNatVariableResolver createItsNatVariableResolver() {
104: return new ItsNatVariableResolverImpl(null, null, null, null,
105: this);
106: }
107: }
|