Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
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.
/*
* Copyright 2005 Joe Walker
*
* 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.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.xml.parsers.ParserConfigurationException;
import org.directwebremoting.Container;
import org.directwebremoting.HubFactory;
import org.directwebremoting.ServerContext;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.WebContextFactory.WebContextBuilder;
import org.directwebremoting.annotations.AnnotationsConfigurator;
import org.directwebremoting.event.ScriptSessionListener;
import org.directwebremoting.extend.AccessControl;
import org.directwebremoting.extend.AjaxFilterManager;
import org.directwebremoting.extend.CallbackHelperFactory;
import org.directwebremoting.extend.Configurator;
import org.directwebremoting.extend.ContainerAbstraction;
import org.directwebremoting.extend.ContainerConfigurationException;
import org.directwebremoting.extend.ConverterManager;
import org.directwebremoting.extend.Creator;
import org.directwebremoting.extend.CreatorManager;
import org.directwebremoting.extend.DwrConstants;
import org.directwebremoting.extend.Handler;
import org.directwebremoting.extend.ModuleManager;
import org.directwebremoting.extend.ScriptSessionManager;
import org.directwebremoting.extend.ServerLoadMonitor;
import org.directwebremoting.extend.TaskDispatcherFactory;
import org.directwebremoting.json.parse.JsonParserFactory;
import org.directwebremoting.json.serialize.JsonSerializerFactory;
import org.directwebremoting.servlet.PathConstants;
import org.directwebremoting.servlet.UrlProcessor;
import org.directwebremoting.util.FakeServletConfig;
import org.directwebremoting.util.FakeServletContextFactory;
import org.directwebremoting.util.LocalUtil;
import org.directwebremoting.util.Loggers;
import org.directwebremoting.util.VersionUtil;
import org.xml.sax.SAXException;
/**
* Some utilities to help get DWR up and running
* @author Joe Walker [joe at getahead dot ltd dot uk]
*/
public class StartupUtil
{
/**
* Init parameter: Set a dwr.xml config file.
* This is only a prefix since we might have more than 1 config file.
*/
public static final String INIT_CONFIG = "config";
/**
* Init parameter: Skip reading the default config file if none are specified.
*/
public static final String INIT_SKIP_DEFAULT = "skipDefaultConfig";
/**
* Init parameter: If we are doing Servlet.log logging, to what level?
*/
public static final String INIT_LOGLEVEL = "logLevel";
/**
* Init parameter: If you wish to use a custom configurator, place its
* class name here
*/
public static final String INIT_CUSTOM_CONFIGURATOR = "customConfigurator";
/**
* The name under which we publish all {@link Container}s.
*/
public static final String ATTRIBUTE_CONTAINER_LIST = "org.directwebremoting.ContainerList";
/**
* A way to setup DWR outside of any Containers.
* This method can also serve as a template for in container code wanting
* to get DWR setup. Callers of this method should clean up after themselves
* by calling {@link #outOfContainerDestroy(Container)}
* @return A new initialized container.
* @throws ContainerConfigurationException If we can't use a bean
*/
public static Container outOfContainerInit() throws ContainerConfigurationException
{
try
{
ServletConfig servletConfig = new FakeServletConfig("test", FakeServletContextFactory.create());
logStartup("DWR:OutOfContainer", servletConfig);
Container container = createAndSetupDefaultContainer(servletConfig);
configureContainerFully(container, servletConfig);
return container;
}
catch (ContainerConfigurationException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new ContainerConfigurationException(ex);
}
}
/**
* Clean up the current thread when {@link #outOfContainerInit()} has been
* called.
* @param container The container created by {@link #outOfContainerInit()}.
*/
public static void outOfContainerDestroy(Container container)
{
WebContextBuilder webContextBuilder = container.getBean(WebContextBuilder.class);
if (webContextBuilder != null)
{
webContextBuilder.disengageThread();
}
}
/**
* Some logging so we have a good clue what we are working with.
* @param name The servlet name (so we can distinguish implementations)
* @param config The servlet config
*/
public static void logStartup(String name, ServletConfig config)
{
ServletContext servletContext = config.getServletContext();
// SERVLET24: Use getContextPath directly in 2.5
String contextPath = LocalUtil.getProperty(servletContext, "ContextPath", String.class);
contextPath = contextPath == null ? "" : " at " + contextPath;
Loggers.STARTUP.info("Starting: " + name + " v" + VersionUtil.getLabel() + " on " + servletContext.getServerInfo() + " / JDK " + System.getProperty("java.version") + " from " + System.getProperty("java.vendor") + contextPath);
}
/**
* A combination of {@link #createDefaultContainer(ServletConfig)} and
* {@link #setupDefaultContainer(DefaultContainer, ServletConfig)}.
* @param servletConfig The source of init parameters
* @return A setup implementation of DefaultContainer
* @throws ContainerConfigurationException If the specified class could not be found or instantiated
*/
public static Container createAndSetupDefaultContainer(ServletConfig servletConfig) throws ContainerConfigurationException
{
Container container;
try
{
String typeName = servletConfig.getInitParameter(LocalUtil.originalDwrClassName(Container.class.getName()));
if (typeName == null)
{
container = new DefaultContainer();
}
else
{
Loggers.STARTUP.debug("Using alternate Container implementation: " + typeName);
Class> type = LocalUtil.classForName(typeName);
container = (Container) type.newInstance();
}
if (container instanceof DefaultContainer)
{
DefaultContainer defaultContainer = (DefaultContainer) container;
setupDefaultContainer(defaultContainer, servletConfig);
}
}
catch (Exception ex)
{
throw new ContainerConfigurationException(ex);
}
return container;
}
/**
* Create a {@link DefaultContainer}, allowing users to upgrade to a child
* of DefaultContainer using an {@link ServletConfig} init parameter of
* org.directwebremoting.Container. Note that while the
* parameter name is the classname of {@link Container}, currently the only
* this can only be used to create children that inherit from
* {@link DefaultContainer}. This restriction may be relaxed in the future.
* Unlike {@link #setupDefaultContainer(DefaultContainer, ServletConfig)},
* this method does not call any setup methods.
* @param servletConfig The source of init parameters
* @return An un'setup' implementation of DefaultContainer
* @throws ContainerConfigurationException If the specified class could not be found
* @see ServletConfig#getInitParameter(String)
*/
public static DefaultContainer createDefaultContainer(ServletConfig servletConfig) throws ContainerConfigurationException
{
try
{
String typeName = servletConfig.getInitParameter(LocalUtil.originalDwrClassName(Container.class.getName()));
if (typeName == null)
{
return new DefaultContainer();
}
Loggers.STARTUP.debug("Using alternate Container implementation: " + typeName);
Class> type = LocalUtil.classForName(typeName);
return (DefaultContainer) type.newInstance();
}
catch (Exception ex)
{
throw new ContainerConfigurationException(ex);
}
}
/**
* Setup a {@link DefaultContainer}.
* Using {@link #createDefaultContainer(ServletConfig)} followed by
* {@link #setupFromServletConfig(DefaultContainer, ServletConfig)} before
* calling {@link DefaultContainer#setupFinished()}.
* @param container The container to configure
* @param servletConfig The source of init parameters
* @throws ContainerConfigurationException If we can't use a bean
*/
public static void setupDefaultContainer(DefaultContainer container, ServletConfig servletConfig) throws ContainerConfigurationException
{
Loggers.STARTUP.debug("Setup: Getting parameters from defaults.properties:");
setupDefaults(container);
// Add the ServletConfig and ServletContext to the container so they can
// be injected into contained beans
Loggers.STARTUP.debug("Setup: Getting parameters from environment:");
container.addBean(Container.class, container);
container.addBean(ServletConfig.class, servletConfig);
container.addBean(ServletContext.class, servletConfig.getServletContext());
Loggers.STARTUP.debug("Setup: Getting parameters from ServletConfig:");
setupFromServletConfig(container, servletConfig);
Loggers.STARTUP.debug("Setup: Applying long versions of shortcut parameters:");
applyParameterShortcuts(container);
Loggers.STARTUP.debug("Setup: Resolving multiple implementations:");
resolveMultipleImplementations(container, servletConfig);
Loggers.STARTUP.debug("Setup: Autowire beans");
container.setupFinished();
Loggers.STARTUP.debug("Setup: Resolving listener implementations:");
resolveListenerImplementations(container, servletConfig);
Loggers.STARTUP.debug("Setup: Initializing Factories:");
ServerContext serverContext = ServerContextFactory.attach(container);
WebContextFactory.attach(container);
HubFactory.attach(container);
JsonParserFactory.attach(container);
JsonSerializerFactory.attach(container);
CallbackHelperFactory.attach(container);
TaskDispatcherFactory.attach(container);
// Make some changes to the ServletContext so {@link DwrWebContextFilter}
// can find the Container etc.
WebContextBuilder webContextBuilder = container.getBean(WebContextBuilder.class);
ServletContext servletContext = servletConfig.getServletContext();
servletContext.setAttribute(Container.class.getName(), container);
servletContext.setAttribute(WebContextBuilder.class.getName(), webContextBuilder);
servletContext.setAttribute(ServletConfig.class.getName(), servletConfig);
publishContainer(container, serverContext, servletConfig);
}
/**
* Some parameters might be shortcuts for one or more other parameters.
* For example 'interactivity. This method resolves those shortcuts by
* adding new values into the container.
*/
private static void applyParameterShortcuts(DefaultContainer container)
{
Object bean = container.getBean("interactivity");
if (bean != null)
{
if (bean instanceof String)
{
String level = (String) bean;
if ("stateless".equals(level))
{
container.addImplementation(ScriptSessionManager.class, TransientScriptSessionManager.class);
}
else if ("passiveReverseAjax".equals(level))
{
// The default - do nothing
}
else if ("activeReverseAjax".equals(level))
{
container.addParameter("activeReverseAjaxEnabled", "true");
}
else
{
Loggers.STARTUP.error("Illegal value for 'interactivity' parameter of '" + level + "'. Valid values are [stateless|passiveReverseAjax|activeReverseAjax]. Ignoring.");
}
}
else
{
Loggers.STARTUP.error("Found non-string value for 'interactivity' parameter. Ignoring.");
}
}
String allowGetForSafariButMakeForgeryEasier = container.getParameter("allowGetForSafariButMakeForgeryEasier");
String allowGetButMakeForgeryEasier = container.getParameter("allowGetButMakeForgeryEasier");
if (allowGetForSafariButMakeForgeryEasier != null && allowGetButMakeForgeryEasier == null)
{
container.addParameter("allowGetButMakeForgeryEasier", allowGetForSafariButMakeForgeryEasier);
}
}
/**
* We need to decide which {@link ContainerAbstraction} should be the
* default for this {@link Container}, also we should prepare the default
* {@link ServerLoadMonitor}.
* @param container The container to configure
* @param servletConfig Information about the environment
* @throws ContainerConfigurationException If we can't use a bean
*/
@SuppressWarnings("unchecked")
public static void resolveMultipleImplementations(DefaultContainer container, ServletConfig servletConfig) throws ContainerConfigurationException
{
try
{
resolveMultipleImplementation(container, LocalUtil.originalDwrClassName("org.directwebremoting.dwrp.FileUpload"));
}
catch(Exception fue)
{
Loggers.STARTUP.debug("A FileUpload implementation is not available. Details: " + fue, fue);
}
try
{
resolveMultipleImplementation(container, LocalUtil.originalDwrClassName("org.directwebremoting.extend.Compressor"));
} catch(Exception ce)
{
Loggers.STARTUP.debug("A Compressor implemenation is not available. Details: " + ce, ce);
}
Object value = container.getBean(LocalUtil.originalDwrClassName(ContainerAbstraction.class.getName()));
List