alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (ServletCache.java)

This example Struts source code file (ServletCache.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

callable, concurrentmap, error, future, future, futuretask, illegalstateexception, interruptedexception, jsploader, jsploader, runtimeexception, runtimeexception, servlet, servlet, threading, threads

The Struts ServletCache.java source code

/*
 * $Id$
 *
 * 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.struts2;

import javax.servlet.Servlet;
import java.util.concurrent.*;

/**
 * Caches servlet instances by jsp location. If a requested jsp is not in the cache, "get"
 * will block and wait for the jsp to be loaded
 */
public class ServletCache {
    protected final ConcurrentMap<String, Future cache
            = new ConcurrentHashMap<String, Future();

    private final JSPLoader jspLoader = new JSPLoader();

    public void clear() {
        cache.clear();        
    }

    public Servlet get(final String location) throws InterruptedException {
        while (true) {
            Future<Servlet> future = cache.get(location);
            if (future == null) {
                Callable<Servlet> loadJSPCallable = new Callable() {
                    public Servlet call() throws Exception {
                        return jspLoader.load(location);
                    }
                };
                FutureTask<Servlet> futureTask = new FutureTask(loadJSPCallable);
                future = cache.putIfAbsent(location, futureTask);
                if (future == null) {
                    future = futureTask;
                    futureTask.run();
                }
            }
            try {
                return future.get();
            } catch (CancellationException e) {
                cache.remove(location, future);
            } catch (ExecutionException e) {
                throw launderThrowable(e.getCause());
            }
        }
    }

    public static RuntimeException launderThrowable(Throwable t) {
        if (t instanceof RuntimeException)
            return (RuntimeException) t;
        else if (t instanceof Error)
            throw (Error) t;
        else
            throw new IllegalStateException(t);
    }

}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts ServletCache.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.