001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.webengine.jaxrs.session.impl; 020 021import java.util.concurrent.locks.ReentrantLock; 022 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpSession; 025import javax.servlet.http.HttpSessionBindingEvent; 026import javax.servlet.http.HttpSessionBindingListener; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.webengine.jaxrs.session.CoreSessionProvider; 032import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory; 033import org.nuxeo.ecm.webengine.jaxrs.session.SessionRef; 034import org.nuxeo.ecm.webengine.jaxrs.session.impl.PerSessionCoreProvider.Ref; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 039 */ 040public class PerSessionCoreProvider extends CoreSessionProvider<Ref> implements HttpSessionBindingListener { 041 042 private static final Log log = LogFactory.getLog(PerSessionCoreProvider.class); 043 044 public static class Ref implements SessionRef { 045 protected CoreSession session; 046 047 protected ReentrantLock lock; 048 049 public Ref(CoreSession session) { 050 this.session = session; 051 this.lock = new ReentrantLock(); 052 } 053 054 @Override 055 public CoreSession get() { 056 lock.lock(); 057 return session; 058 } 059 060 @Override 061 public void unget() { 062 // unlock only if the current thread holds the lock otherwise ignore. 063 try { 064 lock.unlock(); 065 } catch (IllegalMonitorStateException e) { 066 // do nothing 067 } 068 } 069 070 @Override 071 public void destroy() { 072 session = null; 073 lock = null; 074 } 075 076 } 077 078 public static synchronized void install(HttpServletRequest request) { 079 HttpSession s = request.getSession(true); 080 if (s.getAttribute(SessionFactory.SESSION_FACTORY_KEY) == null) { 081 s.setAttribute(SessionFactory.SESSION_FACTORY_KEY, new PerSessionCoreProvider()); 082 } 083 } 084 085 @Override 086 protected Ref createSessionRef(CoreSession session) { 087 return new Ref(session); 088 } 089 090 @Override 091 public void onRequestDone(HttpServletRequest request) { 092 // unlock sessions if any was locked 093 for (SessionRef ref : getSessions()) { 094 ref.unget(); 095 } 096 } 097 098 @Override 099 public void valueBound(HttpSessionBindingEvent event) { 100 // do nothing 101 } 102 103 @Override 104 public void valueUnbound(HttpSessionBindingEvent event) { 105 // destroy all sessions 106 if (!hasSessions()) { 107 destroy(); 108 return; 109 } 110 111 Framework.doPrivileged(this::destroy); 112 } 113 114 @Override 115 public synchronized SessionRef[] getSessions() { 116 return super.getSessions(); 117 } 118 119 @Override 120 public synchronized SessionRef getSessionRef(HttpServletRequest request, String repoName) { 121 return super.getSessionRef(request, repoName); 122 } 123 124 @Override 125 public synchronized boolean hasSessions() { 126 return super.hasSessions(); 127 } 128 129 @Override 130 protected synchronized void destroy() { 131 super.destroy(); 132 } 133 134}