org.apache.tiles.request.servlet.ServletApplicationContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tiles-request-servlet Show documentation
Show all versions of tiles-request-servlet Show documentation
Tiles request implementation for Servlet technology.
/*
* $Id: ServletApplicationContext.java 1297705 2012-03-06 20:44:30Z nlebas $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.tiles.request.servlet;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.ApplicationResource;
import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
import org.apache.tiles.request.collection.ScopeMap;
import org.apache.tiles.request.locale.URLApplicationResource;
import org.apache.tiles.request.servlet.extractor.ApplicationScopeExtractor;
import org.apache.tiles.request.servlet.extractor.InitParameterExtractor;
/**
* Servlet-based implementation of the TilesApplicationContext interface.
*
* @version $Rev: 1297705 $ $Date: 2012-03-06 15:44:30 -0500 (Tue, 06 Mar 2012) $
*/
public class ServletApplicationContext implements ApplicationContext {
/**
* The servlet context to use.
*/
private ServletContext servletContext;
/**
* The lazily instantiated Map
of application scope
* attributes.
*/
private Map applicationScope = null;
/**
* The lazily instantiated Map
of context initialization
* parameters.
*/
private Map initParam = null;
/**
* Creates a new instance of ServletTilesApplicationContext.
*
* @param servletContext The servlet context to use.
*/
public ServletApplicationContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/** {@inheritDoc} */
public Object getContext() {
return servletContext;
}
/** {@inheritDoc} */
public Map getApplicationScope() {
if ((applicationScope == null) && (servletContext != null)) {
applicationScope = new ScopeMap(new ApplicationScopeExtractor(servletContext));
}
return (applicationScope);
}
/** {@inheritDoc} */
public Map getInitParams() {
if ((initParam == null) && (servletContext != null)) {
initParam = new ReadOnlyEnumerationMap(new InitParameterExtractor(servletContext));
}
return (initParam);
}
/** {@inheritDoc} */
public ApplicationResource getResource(String localePath) {
try {
URL url = servletContext.getResource(localePath);
if (url != null) {
return new URLApplicationResource(localePath, url);
} else {
return null;
}
} catch (MalformedURLException e) {
return null;
}
}
/** {@inheritDoc} */
public ApplicationResource getResource(ApplicationResource base, Locale locale) {
try {
URL url = servletContext.getResource(base.getLocalePath(locale));
if (url != null) {
return new URLApplicationResource(base.getPath(), locale, url);
} else {
return null;
}
} catch (MalformedURLException e) {
return null;
}
}
/** {@inheritDoc} */
public Collection getResources(String path) {
ArrayList resources = new ArrayList();
resources.add(getResource(path));
return resources;
}
}