All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.directwebremoting.guice.DwrScopes Maven / Gradle / Ivy

Go to download

DWR is easy Ajax for Java. It makes it simple to call Java code directly from Javascript. It gets rid of almost all the boiler plate code between the web browser and your Java code.

The newest version!
/*
 * Copyright 2007 Tim Peierls
 *
 * 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 org.directwebremoting.guice;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.guice.util.AbstractSimpleContextScope;
import org.directwebremoting.guice.util.ContextScope;

import static org.directwebremoting.guice.DwrGuiceUtil.*;

/**
 * Scopes available to DWR applications.
 * @author Tim Peierls [tim at peierls dot net]
 */
public class DwrScopes
{
    /**
     * HTTP request scope.
     */
    public static final ContextScope REQUEST =
        new AbstractSimpleContextScope(
            HttpServletRequest.class, "DwrScopes.REQUEST")
        {
            @Override
            public HttpServletRequest get()
            {
                return WebContextFactory.get().getHttpServletRequest();
            }

            @Override
            public Object get(HttpServletRequest request, String name)
            {
                return request.getAttribute(name);
            }

            @Override
            public void put(HttpServletRequest request, String name, Object value)
            {
                request.setAttribute(name, value);
            }
        };

    /**
     * DWR script session scope.
     */
    public static final ContextScope SCRIPT =
        new AbstractSimpleContextScope(ScriptSession.class, "DwrScopes.SCRIPT")
        {
            @Override
            public ScriptSession get()
            {
                return WebContextFactory.get().getScriptSession();
            }

            @Override
            public Object get(ScriptSession scriptSession, String name)
            {
                return scriptSession.getAttribute(name);
            }

            @Override
            public void put(ScriptSession scriptSession, String name, Object value)
            {
                scriptSession.setAttribute(name, value);
            }
        };

    /**
     * HTTP session scope. The implementation uses session identity to
     * to track which sessions are open. Since the servlet spec doesn't
     * guarantee identity of sessions between requests, don't rely on
     * {@code getOpenContexts()} or {@code close(session, handlers)} to
     * work correctly for this scope.
     */
    public static final ContextScope SESSION =
        new AbstractSimpleContextScope(HttpSession.class, "DwrScopes.SESSION")
        {
            @Override
            public HttpSession get()
            {
                return WebContextFactory.get().getSession();
            }

            @Override
            public Object get(HttpSession session, String name)
            {
                return session.getAttribute(name);
            }

            @Override
            public void put(HttpSession session, String name, Object value)
            {
                session.setAttribute(name, value);
            }
        };

    /**
     * Application scope: objects in this scope are eagerly initialized
     * during DWR servlet initialization, and Closeable objects in this scope are
     * closed during DWR servlet destruction.
     */
    public static final ContextScope APPLICATION =
        new ApplicationScope("DwrScopes.APPLICATION");

    /**
     * Global application scope: like {@link #APPLICATION}, but objects in
     * this scope are not eagerly initialized and Closeable objects
     * in this scope are closed during servlet context destruction (not
     * during DWR servlet destruction).
     */
    public static final ContextScope GLOBAL =
        new ApplicationScope("DwrScopes.GLOBAL");


    static class ApplicationScope extends AbstractSimpleContextScope
    {
        ApplicationScope(String scopeName)
        {
            super(ServletContext.class, scopeName);
        }

        @Override
        public ServletContext get()
        {
            return getServletContext();
        }

        @Override
        public Object get(ServletContext servletContext, String name)
        {
            /*
            if (log.isDebugEnabled())
            {
                log.debug(String.format("servletContext.getAttribute(%s)", name));
            }
            //*/
            return servletContext.getAttribute(name);
        }

        @Override
        public void put(ServletContext servletContext, String name, Object value)
        {
            /*
            if (log.isDebugEnabled())
            {
                log.debug(String.format("servletContext.setAttribute(%s, %s)", name, value));
            }
            //*/
            servletContext.setAttribute(name, value);
        }
    }

    private DwrScopes() { /* uninstantiable */ }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy