All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.semanticcms.core.servlet.SynchronizedCache 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.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;

/**
 * A page cache that is thread safe using synchronization on the default HashMaps.
 */
class SynchronizedCache extends MapCache {

	SynchronizedCache() {
		super(
			new HashMap(),
			VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS ? new HashMap>() : null,
			VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS ? new HashMap>() : null,
			new HashMap()
		);
	}

	@Override
	synchronized Page get(CaptureKey key) {
		return super.get(key);
	}

	@Override
	synchronized void put(CaptureKey key, Page page) throws ServletException {
		super.put(key, page);
	}

	@Override
	protected void verifyAdded(Page page) throws ServletException {
		assert Thread.holdsLock(this);
		super.verifyAdded(page);
	}

	@Override
	public  Map newMap() {
		return Collections.synchronizedMap(new HashMap());
	}

	@Override
	public  Map newMap(int size) {
		return Collections.synchronizedMap(new HashMap(size *4/3+1));
	}

	@Override
	public void setAttribute(String key, Object value) {
		synchronized(attributes) {
			super.setAttribute(key, value);
		}
	}

	@Override
	public Object getAttribute(String key) {
		synchronized(attributes) {
			return super.getAttribute(key);
		}
	}

	@Override
	public  V getAttribute(String key, Class clazz, Callable callable) throws E {
		synchronized(attributes) {
			return super.getAttribute(key, clazz, callable);
		}
	}

	@Override
	public void removeAttribute(String key) {
		synchronized(attributes) {
			super.removeAttribute(key);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy