PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/components/DefaultContextManager.java

https://github.com/apache/cocoon
Java | 352 lines | 230 code | 37 blank | 85 comment | 38 complexity | 48c155cbee29c78f87b735744a8084c0 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.cocoon.webapps.session.components;
  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import javax.servlet.http.HttpSession;
  22. import org.apache.avalon.framework.activity.Disposable;
  23. import org.apache.avalon.framework.context.Context;
  24. import org.apache.avalon.framework.context.ContextException;
  25. import org.apache.avalon.framework.context.Contextualizable;
  26. import org.apache.avalon.framework.service.ServiceException;
  27. import org.apache.avalon.framework.service.ServiceManager;
  28. import org.apache.avalon.framework.service.ServiceSelector;
  29. import org.apache.avalon.framework.service.Serviceable;
  30. import org.apache.avalon.framework.thread.ThreadSafe;
  31. import org.apache.excalibur.source.SourceResolver;
  32. import org.apache.excalibur.xml.xpath.XPathProcessor;
  33. import org.apache.cocoon.ProcessingException;
  34. import org.apache.cocoon.components.ContextHelper;
  35. import org.apache.cocoon.environment.Request;
  36. import org.apache.cocoon.util.AbstractLogEnabled;
  37. import org.apache.cocoon.util.Deprecation;
  38. import org.apache.cocoon.webapps.session.ContextManager;
  39. import org.apache.cocoon.webapps.session.context.SessionContext;
  40. import org.apache.cocoon.webapps.session.context.SessionContextProvider;
  41. import org.apache.cocoon.webapps.session.context.SimpleSessionContext;
  42. import org.xml.sax.SAXException;
  43. /**
  44. * Context manager
  45. *
  46. * @deprecated This block is deprecated and will be removed in future versions.
  47. * @version $Id$
  48. */
  49. public final class DefaultContextManager extends AbstractLogEnabled
  50. implements Serviceable, ContextManager, ThreadSafe, Contextualizable, Disposable {
  51. /** The <code>ServiceManager</code> */
  52. private ServiceManager manager;
  53. /** The context */
  54. private Context context;
  55. /** selector for context provider */
  56. private ServiceSelector contextSelector;
  57. /** The xpath processor */
  58. private XPathProcessor xpathProcessor;
  59. /** The source resolver */
  60. private SourceResolver resolver;
  61. /* The list of reserved contexts */
  62. static private final String[] reservedContextNames = {"session",
  63. "context"};
  64. /**
  65. * Avalon Serviceable Interface
  66. */
  67. public void service(ServiceManager manager)
  68. throws ServiceException {
  69. this.manager = manager;
  70. this.contextSelector = (ServiceSelector)this.manager.lookup(SessionContextProvider.ROLE+"Selector");
  71. this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
  72. this.resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE);
  73. Deprecation.logger.warn("The session-fw block is deprecated. Please use the provided alternatives instead.");
  74. }
  75. /**
  76. * Get the session
  77. */
  78. private HttpSession getSession(boolean create) {
  79. final Request request = ContextHelper.getRequest( this.context );
  80. return request.getSession( create );
  81. }
  82. /**
  83. * Get the list of contexts
  84. */
  85. private Map getSessionContexts(HttpSession session) {
  86. Map contexts;
  87. contexts = (Map)session.getAttribute(SessionContext.class.getName());
  88. if (contexts == null) {
  89. contexts = new HashMap(5, 3);
  90. session.setAttribute(SessionContext.class.getName(), contexts);
  91. }
  92. return contexts;
  93. }
  94. /**
  95. * Checks if the context name is a reserved context.
  96. */
  97. private boolean isReservedContextName(String name) {
  98. // synchronized (not needed)
  99. int i, l;
  100. boolean found;
  101. found = false;
  102. i = 0;
  103. l = reservedContextNames.length;
  104. while (i < l && found == false) {
  105. found = reservedContextNames[i].equals(name);
  106. i++;
  107. }
  108. if (!found ) {
  109. found = false;
  110. SessionContextProvider provider = null;
  111. try {
  112. provider = (SessionContextProvider)this.contextSelector.select( name );
  113. found = true;
  114. } catch (ServiceException ignore) {
  115. } finally {
  116. this.contextSelector.release(provider);
  117. }
  118. }
  119. return found;
  120. }
  121. /**
  122. * Get a reserved context
  123. */
  124. private boolean existsReservedContext(String name)
  125. throws ProcessingException {
  126. // synchronized (not needed)
  127. boolean exists = false;
  128. SessionContextProvider provider = null;
  129. try {
  130. provider = (SessionContextProvider)this.contextSelector.select( name );
  131. exists = provider.existsSessionContext( name );
  132. } catch (ServiceException ignore) {
  133. } finally {
  134. this.contextSelector.release(provider);
  135. }
  136. return exists;
  137. }
  138. /**
  139. * Get a reserved context
  140. */
  141. private SessionContext getReservedContext(String name)
  142. throws ProcessingException {
  143. // synchronized
  144. SessionContext context = null;
  145. SessionContextProvider provider = null;
  146. try {
  147. provider = (SessionContextProvider)this.contextSelector.select( name );
  148. synchronized (provider) {
  149. context = provider.getSessionContext(name);
  150. }
  151. } catch (ServiceException ignore) {
  152. } finally {
  153. this.contextSelector.release(provider);
  154. }
  155. return context;
  156. }
  157. /**
  158. * Create a new public context in the session.
  159. * Create a new public session context for this user. If this context
  160. * already exists no new context is created and the old one will be used
  161. * instead.
  162. */
  163. public SessionContext createContext(String name, String loadURI, String saveURI)
  164. throws IOException, SAXException, ProcessingException {
  165. // synchronized
  166. if (this.getLogger().isDebugEnabled()) {
  167. this.getLogger().debug("BEGIN createContext name=" + name +
  168. "load=" + loadURI +
  169. "save=" + saveURI);
  170. }
  171. // test arguments
  172. if (name == null) {
  173. throw new ProcessingException("CreateContext: Name is required");
  174. }
  175. HttpSession session = this.getSession(true);
  176. if (session == null) {
  177. throw new ProcessingException("CreateContext: Session is required");
  178. }
  179. SessionContext context;
  180. synchronized(session) {
  181. // test for reserved context
  182. if (this.isReservedContextName(name)) {
  183. throw new ProcessingException("SessionContext with name " + name + " is reserved and cannot be created manually.");
  184. }
  185. if (this.existsContext(name)) {
  186. context = this.getContext(name);
  187. } else {
  188. Map contexts = this.getSessionContexts(session);
  189. context = new SimpleSessionContext(this.xpathProcessor, this.resolver);
  190. context.setup(name, loadURI, saveURI);
  191. contexts.put(name, context);
  192. }
  193. }
  194. if (this.getLogger().isDebugEnabled()) {
  195. this.getLogger().debug("END createContext context="+context);
  196. }
  197. return context;
  198. }
  199. /**
  200. * Delete a public context in the session.
  201. * If the context exists for this user, it and all of its information
  202. * is deleted.
  203. */
  204. public void deleteContext(String name)
  205. throws ProcessingException {
  206. // synchronized
  207. if (this.getLogger().isDebugEnabled() ) {
  208. this.getLogger().debug("BEGIN deleteContext name=" + name);
  209. }
  210. // test arguments
  211. if (name == null) {
  212. throw new ProcessingException("SessionManager.deleteContext: Name is required");
  213. }
  214. if (this.isReservedContextName(name)) {
  215. throw new ProcessingException("SessionContext with name " + name + " is reserved and cannot be deleted manually.");
  216. }
  217. HttpSession session = this.getSession(false);
  218. if (session == null) {
  219. throw new ProcessingException("SessionManager.deleteContext: Session is required");
  220. }
  221. synchronized(session) {
  222. final Map contexts = this.getSessionContexts(session);
  223. if (contexts.containsKey(name)) {
  224. contexts.remove(name);
  225. }
  226. }
  227. if (this.getLogger().isDebugEnabled() ) {
  228. this.getLogger().debug("END deleteContext");
  229. }
  230. }
  231. /**
  232. * Get a public context.
  233. * The session context with the given name is returned. If the context does
  234. * not exist <CODE>null</CODE> is returned.
  235. */
  236. public SessionContext getContext(String name)
  237. throws ProcessingException {
  238. // synchronized
  239. if (this.getLogger().isDebugEnabled() ) {
  240. this.getLogger().debug("BEGIN getContext name=" + name);
  241. }
  242. SessionContext context;
  243. if (this.isReservedContextName(name) ) {
  244. context = this.getReservedContext(name);
  245. } else {
  246. HttpSession session = this.getSession(false);
  247. if ( session != null) {
  248. synchronized (session) {
  249. final Map contexts = this.getSessionContexts( session );
  250. context = (SessionContext)contexts.get(name);
  251. }
  252. } else {
  253. context = null;
  254. }
  255. }
  256. if (this.getLogger().isDebugEnabled() ) {
  257. this.getLogger().debug("END getContext context=" + context);
  258. }
  259. return context;
  260. }
  261. /**
  262. * Check if a context exists
  263. */
  264. public boolean hasSessionContext()
  265. throws ProcessingException {
  266. HttpSession session = this.getSession(false);
  267. if (session == null) {
  268. throw new ProcessingException("SessionManager.hasSessionContext: Session is required.");
  269. }
  270. synchronized (session) {
  271. final Map contexts = this.getSessionContexts(session);
  272. return !(contexts.isEmpty());
  273. }
  274. }
  275. /**
  276. * Check if a public context exists.
  277. * If the session context with the given name exists, <CODE>true</CODE> is
  278. * returned.
  279. */
  280. public boolean existsContext(String name)
  281. throws ProcessingException {
  282. HttpSession session = this.getSession(false);
  283. if (session == null) {
  284. throw new ProcessingException("SessionManager.existsContext: Session is required.");
  285. }
  286. synchronized (session) {
  287. final Map contexts = this.getSessionContexts(session);
  288. boolean result = contexts.containsKey(name);
  289. if (!result && this.isReservedContextName(name) ) {
  290. result = this.existsReservedContext(name);
  291. }
  292. return result;
  293. }
  294. }
  295. /* (non-Javadoc)
  296. * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
  297. */
  298. public void contextualize(Context context) throws ContextException {
  299. this.context = context;
  300. }
  301. /* (non-Javadoc)
  302. * @see org.apache.avalon.framework.activity.Disposable#dispose()
  303. */
  304. public void dispose() {
  305. if ( this.manager != null) {
  306. this.manager.release( this.contextSelector );
  307. this.manager.release( this.xpathProcessor );
  308. this.manager.release( this.resolver );
  309. this.contextSelector = null;
  310. this.xpathProcessor = null;
  311. this.resolver = null;
  312. this.manager = null;
  313. }
  314. }
  315. }