
com.semanticcms.core.servlet.SingleThreadCache Maven / Gradle / Ivy
/*
* semanticcms-core-servlet - Java API for modeling web page content and relationships in a Servlet environment.
* Copyright (C) 2016 AO Industries, Inc.
* [email protected]
* 7262 Bull Pen Cir
* Mobile, AL 36695
*
* This file is part of semanticcms-core-servlet.
*
* semanticcms-core-servlet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* semanticcms-core-servlet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with semanticcms-core-servlet. If not, see .
*/
package com.semanticcms.core.servlet;
import com.semanticcms.core.model.Page;
import com.semanticcms.core.model.PageRef;
import static com.semanticcms.core.servlet.Cache.VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
/**
* A page cache that is not thread safe and should only be used within the
* context of a single thread.
*/
class SingleThreadCache extends MapCache {
private final Thread assertingThread;
SingleThreadCache() {
super(
new HashMap(),
VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS ? new HashMap>() : null,
VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS ? new HashMap>() : null,
new HashMap()
);
Thread t = null;
// Intentional side-effect from assert
assert (t = Thread.currentThread()) != null;
assertingThread = t;
}
@Override
Page get(CaptureKey key) {
assert assertingThread == Thread.currentThread();
return super.get(key);
}
@Override
void put(CaptureKey key, Page page) throws ServletException {
assert assertingThread == Thread.currentThread();
super.put(key, page);
}
@Override
public Map newMap() {
assert assertingThread == Thread.currentThread();
return new HashMap();
}
@Override
public Map newMap(int size) {
assert assertingThread == Thread.currentThread();
return new HashMap(size *4/3+1);
}
@Override
public void setAttribute(String key, Object value) {
assert assertingThread == Thread.currentThread();
super.setAttribute(key, value);
}
@Override
public Object getAttribute(String key) {
assert assertingThread == Thread.currentThread();
return super.getAttribute(key);
}
@Override
public V getAttribute(String key, Class clazz, Callable extends V, E> callable) throws E {
assert assertingThread == Thread.currentThread();
return super.getAttribute(key, clazz, callable);
}
@Override
public void removeAttribute(String key) {
assert assertingThread == Thread.currentThread();
super.removeAttribute(key);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy