📄 applicationscope.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.spring.factory;import java.util.Enumeration;import javax.portlet.PortletContext;import javax.servlet.ServletContext;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.ObjectFactory;import org.springframework.beans.factory.config.Scope;import org.springframework.util.Assert;/** * Alternative singleton Scope implementation. This Scope relies on a * ServletContext and a PortletContext repectively and therefore will work with * Servlet and Portlet environments. * * @author Andreas Kuhrwahl * */public final class ApplicationScope implements Scope { /** * Base class for the internal used scope implementation. Valid * implementations relies on a ServletContext or a PortletContext * respectively. * * @author Andreas Kuhrwahl * */ private abstract class AbstractApplicationScope implements Scope { /** * {@inheritDoc} */ public String getConversationId() { return null; } /** * {@inheritDoc} */ public void registerDestructionCallback(final String name, final Runnable callback) { logger .warn("Registering destruction callback is not supported by this scope: use destruction facilities of the used BeanFactory"); } /** * Clears the scope. */ public abstract void clear(); } /** * Alternative singleton Scope implementation. This Scope relies on a * PortletContext repectively. * * @author Andreas Kuhrwahl * */ private class PortletApplicationScope extends AbstractApplicationScope { /** * {@inheritDoc} */ public Object get(final String name, final ObjectFactory objectFactory) { Object scopedObject = getPortletContext().getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); getPortletContext().setAttribute(name, scopedObject); } return scopedObject; } /** * {@inheritDoc} */ public Object remove(final String name) { Object scopedObject = getPortletContext().getAttribute(name); getPortletContext().removeAttribute(name); return scopedObject; } /** * {@inheritDoc} */ public void clear() { for (Enumeration e = getPortletContext().getAttributeNames(); e .hasMoreElements();) { String attrName = (String) e.nextElement(); getPortletContext().removeAttribute(attrName); } } /** * @return The PortletContext */ private PortletContext getPortletContext() { return (PortletContext) context; } } /** * Alternative singleton Scope implementation. This Scope relies on a * ServletContext repectively. * * @author Andreas Kuhrwahl * */ private class ServletApplicationScope extends AbstractApplicationScope { /** * {@inheritDoc} */ public Object get(final String name, final ObjectFactory objectFactory) { Object scopedObject = getServletContext().getAttribute(name); if (scopedObject == null) { scopedObject = objectFactory.getObject(); getServletContext().setAttribute(name, scopedObject); } return scopedObject; } /** * {@inheritDoc} */ public Object remove(final String name) { Object scopedObject = getServletContext().getAttribute(name); getServletContext().removeAttribute(name); return scopedObject; } /** * {@inheritDoc} */ public void clear() { for (Enumeration e = getServletContext().getAttributeNames(); e .hasMoreElements();) { String attrName = (String) e.nextElement(); getServletContext().removeAttribute(attrName); } } /** * @return The ServletContext */ private ServletContext getServletContext() { return (ServletContext) context; } } /** For logging. */ protected Log logger = LogFactory.getLog(this.getClass()); /** The ServletContext or the PortletContext respectively. */ private final Object context; /** The internal used scope. */ private final AbstractApplicationScope internalScope; /** * Creates a scope relying on a ServletContext or a PortletContext * respectively (depends on the type of <code>context</code>). * * @param context * The ServletContext or the PortletContext respectively */ public ApplicationScope(final Object context) { Assert.notNull(context); this.context = context; if (context instanceof ServletContext) { this.internalScope = new ServletApplicationScope(); } else if (context instanceof PortletContext) { this.internalScope = new PortletApplicationScope(); } else { throw new IllegalArgumentException("Context '" + context + "' must be of type ServletContext or PortletContext"); } } /** * {@inheritDoc} */ public Object get(final String name, final ObjectFactory objectFactory) { return this.internalScope.get(name, objectFactory); } /** * {@inheritDoc} */ public Object remove(final String name) { return this.internalScope.remove(name); } /** * {@inheritDoc} */ public void registerDestructionCallback(final String name, final Runnable callback) { this.internalScope.registerDestructionCallback(name, callback); } /** * {@inheritDoc} */ public String getConversationId() { return this.internalScope.getConversationId(); } /** * Clears the scope. */ public void clear() { this.internalScope.clear(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -