org.thymeleaf.context.WebServletContextVariablesMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf Show documentation
Show all versions of thymeleaf Show documentation
XML/XHTML/HTML5 template engine for Java
/*
* =============================================================================
*
* Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
*
* 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.thymeleaf.context;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import org.thymeleaf.util.Validate;
/**
*
* Specialization of {@link VariablesMap} that forwards read and write
* calls to the attributes of a ServletContext.
*
*
* @author Daniel Fernández
*
* @since 2.0.9
*
*/
class WebServletContextVariablesMap extends VariablesMap {
private static final long serialVersionUID = 6338751359164152136L;
private final ServletContext servletContext;
WebServletContextVariablesMap(final ServletContext servletContext) {
super(1, 1.0f);
Validate.notNull(servletContext, "Servlet context cannot be null");
this.servletContext = servletContext;
}
@Override
@SuppressWarnings("unchecked")
public int size() {
int size = 0;
final Enumeration attributeNames = this.servletContext.getAttributeNames();
while (attributeNames.hasMoreElements()) {
attributeNames.nextElement();
size++;
}
return size;
}
@Override
public boolean isEmpty() {
return !this.servletContext.getAttributeNames().hasMoreElements();
}
@Override
public Object get(final Object key) {
return this.servletContext.getAttribute((String)key);
}
@Override
@SuppressWarnings("unchecked")
public boolean containsKey(final Object key) {
final Enumeration attributeNames = this.servletContext.getAttributeNames();
while (attributeNames.hasMoreElements()) {
final String attributeName = attributeNames.nextElement();
if (key == null) {
if (attributeName == null) {
return true;
}
} else {
if (key.equals(attributeName)) {
return true;
}
}
}
return false;
}
@Override
public Object put(final String key, final Object value) {
this.servletContext.setAttribute(key, value);
return value;
}
@Override
public void putAll(final Map m) {
for (final Map.Entry mEntry : m.entrySet()) {
this.servletContext.setAttribute(mEntry.getKey(), mEntry.getValue());
}
}
@Override
public Object remove(final Object key) {
final Object value = this.servletContext.getAttribute((String)key);
this.servletContext.removeAttribute((String)key);
return value;
}
@Override
@SuppressWarnings("unchecked")
public void clear() {
final List attributeNamesList = new ArrayList(5);
final Enumeration attributeNames = this.servletContext.getAttributeNames();
// We first iterate, then remove, in order to protect the Enumeration.
while (attributeNames.hasMoreElements()) {
attributeNamesList.add(attributeNames.nextElement());
}
for (final String attributeName : attributeNamesList) {
this.servletContext.removeAttribute(attributeName);
}
}
@Override
@SuppressWarnings("unchecked")
public boolean containsValue(final Object value) {
final Enumeration attributeNames = this.servletContext.getAttributeNames();
while (attributeNames.hasMoreElements()) {
final String attributeName = attributeNames.nextElement();
final Object attributeValue = this.servletContext.getAttribute(attributeName);
if (value == null) {
if (attributeValue == null) {
return true;
}
} else {
if (value.equals(attributeValue)) {
return true;
}
}
}
return false;
}
@Override
public WebServletContextVariablesMap clone() {
return (WebServletContextVariablesMap) super.clone();
}
@Override
@SuppressWarnings("unchecked")
public Set keySet() {
final Set keySet = new LinkedHashSet(5);
final Enumeration attributeNames = this.servletContext.getAttributeNames();
while (attributeNames.hasMoreElements()) {
keySet.add(attributeNames.nextElement());
}
return keySet;
}
@Override
@SuppressWarnings("unchecked")
public Collection