|
Glassfish example source code file (ApplicationContextFacade.java)
The Glassfish ApplicationContextFacade.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.catalina.core;
import org.apache.catalina.Globals;
import org.apache.catalina.security.SecurityUtil;
import javax.servlet.*;
import javax.servlet.descriptor.JspConfigDescriptor;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Facade object which masks the internal <code>ApplicationContext
* object from the web application.
*
* @author Remy Maucherat
* @author Jean-Francois Arcand
* @version $Revision: 1.7.6.1 $ $Date: 2008/04/17 18:37:06 $
*/
public final class ApplicationContextFacade
implements ServletContext {
// ---------------------------------------------------------- Attributes
/**
* Cache Class object used for reflection.
*/
private static HashMap classCache = new HashMap();
static {
Class[] clazz = new Class[]{String.class};
classCache.put("getContext", clazz);
classCache.put("getMimeType", clazz);
classCache.put("getResourcePaths", clazz);
classCache.put("getResource", clazz);
classCache.put("getResourceAsStream", clazz);
classCache.put("getRequestDispatcher", clazz);
classCache.put("getNamedDispatcher", clazz);
classCache.put("getServlet", clazz);
classCache.put("getInitParameter", clazz);
classCache.put("setAttribute", new Class[]{String.class, Object.class});
classCache.put("removeAttribute", clazz);
classCache.put("getRealPath", clazz);
classCache.put("getAttribute", clazz);
classCache.put("log", clazz);
}
/**
* Cache method object.
*/
private HashMap objectCache;
private static Logger sysLog = Logger.getLogger(
ApplicationContextFacade.class.getName());
// ----------------------------------------------------------- Constructors
/**
* Construct a new instance of this class, associated with the specified
* Context instance.
*
* @param context The associated Context instance
*/
public ApplicationContextFacade(ApplicationContext context) {
super();
this.context = context;
objectCache = new HashMap();
}
// ----------------------------------------------------- Instance Variables
/**
* Wrapped application context.
*/
private ApplicationContext context = null;
// ------------------------------------------------- ServletContext Methods
public String getContextPath() {
return context.getContextPath();
}
public ServletContext getContext(String uripath) {
ServletContext theContext = null;
if (SecurityUtil.isPackageProtectionEnabled()) {
theContext = (ServletContext)
doPrivileged("getContext", new Object[]{uripath});
} else {
theContext = context.getContext(uripath);
}
if ((theContext != null) &&
(theContext instanceof ApplicationContext)){
theContext = ((ApplicationContext)theContext).getFacade();
}
return (theContext);
}
public ApplicationContext getApplicationContext() {
return context;
}
public int getMajorVersion() {
return context.getMajorVersion();
}
public int getMinorVersion() {
return context.getMinorVersion();
}
/**
* Gets the major version of the Servlet specification that the
* application represented by this ServletContext is based on.
*/
public int getEffectiveMajorVersion() {
return context.getEffectiveMajorVersion();
}
/**
* Gets the minor version of the Servlet specification that the
* application represented by this ServletContext is based on.
*/
public int getEffectiveMinorVersion() {
return context.getEffectiveMinorVersion();
}
public String getMimeType(String file) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (String)doPrivileged("getMimeType", new Object[]{file});
} else {
return context.getMimeType(file);
}
}
public Set<String> getResourcePaths(String path) {
if (SecurityUtil.isPackageProtectionEnabled()){
return (Set<String>)doPrivileged("getResourcePaths",
new Object[]{path});
} else {
return context.getResourcePaths(path);
}
}
public URL getResource(String path)
throws MalformedURLException {
if (Globals.IS_SECURITY_ENABLED) {
try {
return (URL) invokeMethod(context, "getResource",
new Object[]{path});
} catch(Throwable t) {
if (t instanceof MalformedURLException){
throw (MalformedURLException)t;
}
return null;
}
} else {
return context.getResource(path);
}
}
public InputStream getResourceAsStream(String path) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (InputStream) doPrivileged("getResourceAsStream",
new Object[]{path});
} else {
return context.getResourceAsStream(path);
}
}
public RequestDispatcher getRequestDispatcher(final String path) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (RequestDispatcher) doPrivileged("getRequestDispatcher",
new Object[]{path});
} else {
return context.getRequestDispatcher(path);
}
}
public RequestDispatcher getNamedDispatcher(String name) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (RequestDispatcher) doPrivileged("getNamedDispatcher",
new Object[]{name});
} else {
return context.getNamedDispatcher(name);
}
}
public Servlet getServlet(String name)
throws ServletException {
if (SecurityUtil.isPackageProtectionEnabled()) {
try {
return (Servlet) invokeMethod(context, "getServlet",
new Object[]{name});
} catch (Throwable t) {
if (t instanceof ServletException) {
throw (ServletException) t;
}
return null;
}
} else {
return context.getServlet(name);
}
}
public Enumeration<Servlet> getServlets() {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (Enumeration<Servlet>) doPrivileged("getServlets", null);
} else {
return context.getServlets();
}
}
public Enumeration<String> getServletNames() {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (Enumeration<String>) doPrivileged("getServletNames", null);
} else {
return context.getServletNames();
}
}
public void log(String msg) {
if (SecurityUtil.isPackageProtectionEnabled()) {
doPrivileged("log", new Object[]{msg} );
} else {
context.log(msg);
}
}
public void log(Exception exception, String msg) {
if (SecurityUtil.isPackageProtectionEnabled()) {
doPrivileged("log", new Class[]{Exception.class, String.class},
new Object[]{exception,msg});
} else {
context.log(exception, msg);
}
}
public void log(String message, Throwable throwable) {
if (SecurityUtil.isPackageProtectionEnabled()) {
doPrivileged("log", new Class[]{String.class, Throwable.class},
new Object[]{message, throwable});
} else {
context.log(message, throwable);
}
}
public String getRealPath(String path) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (String) doPrivileged("getRealPath", new Object[]{path});
} else {
return context.getRealPath(path);
}
}
public String getServerInfo() {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (String) doPrivileged("getServerInfo", null);
} else {
return context.getServerInfo();
}
}
public String getInitParameter(String name) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (String) doPrivileged("getInitParameter",
new Object[]{name});
} else {
return context.getInitParameter(name);
}
}
public Enumeration<String> getInitParameterNames() {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (Enumeration<String>) doPrivileged(
"getInitParameterNames", null);
} else {
return context.getInitParameterNames();
}
}
/**
* @return true if the context initialization parameter with the given
* name and value was set successfully on this ServletContext, and false
* if it was not set because this ServletContext already contains a
* context initialization parameter with a matching name
*/
public boolean setInitParameter(String name, String value) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return ((Boolean) doPrivileged(
"setInitParameter", new Object[]{name, value})).booleanValue();
} else {
return context.setInitParameter(name, value);
}
}
public Object getAttribute(String name) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return doPrivileged("getAttribute", new Object[]{name});
} else {
return context.getAttribute(name);
}
}
public Enumeration<String> getAttributeNames() {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (Enumeration<String>) doPrivileged(
"getAttributeNames", null);
} else {
return context.getAttributeNames();
}
}
public void setAttribute(String name, Object object) {
if (SecurityUtil.isPackageProtectionEnabled()) {
doPrivileged("setAttribute", new Object[]{name,object});
} else {
context.setAttribute(name, object);
}
}
public void removeAttribute(String name) {
if (SecurityUtil.isPackageProtectionEnabled()) {
doPrivileged("removeAttribute", new Object[]{name});
} else {
context.removeAttribute(name);
}
}
public String getServletContextName() {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (String) doPrivileged("getServletContextName", null);
} else {
return context.getServletContextName();
}
}
/*
* Adds the servlet with the given name and class name to this
* servlet context.
*/
public ServletRegistration.Dynamic addServlet(
String servletName, String className) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (ServletRegistration.Dynamic) doPrivileged(
"addServlet", new Object[] {servletName, className});
} else {
return context.addServlet(servletName, className);
}
}
/*
* Registers the given servlet instance with this ServletContext
* under the given <tt>servletName.
*/
public ServletRegistration.Dynamic addServlet(
String servletName, Servlet servlet) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (ServletRegistration.Dynamic) doPrivileged(
"addServlet", new Object[] {servletName, servlet});
} else {
return context.addServlet(servletName, servlet);
}
}
/*
* Adds the servlet with the given name and class type to this
* servlet context.
*/
public ServletRegistration.Dynamic addServlet(String servletName,
Class <? extends Servlet> servletClass) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (ServletRegistration.Dynamic) doPrivileged(
"addServlet", new Object[] {servletName, servletClass});
} else {
return context.addServlet(servletName, servletClass);
}
}
/**
* Instantiates the given Servlet class and performs any required
* resource injection into the new Servlet instance before returning
* it.
*/
public <T extends Servlet> T createServlet(Class
Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish ApplicationContextFacade.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.