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

org.apache.camel.guice.util.CloseableScope Maven / Gradle / Ivy

There is a newer version: 2.25.4
Show newest version
/**
 * 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.camel.guice.util;

import java.lang.annotation.Annotation;
import java.util.Map;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Scope;

import org.apache.camel.guice.inject.Injectors;
import org.apache.camel.guice.support.CloseFailedException;
import org.apache.camel.guice.support.HasScopeAnnotation;
import org.apache.camel.guice.support.internal.CloseErrorsImpl;


/**
 * Represents a scope which caches objects around until the scope is closed.
 * 
 * The scope can be closed as many times as required - there is no need to
 * recreate the scope instance each time a scope goes out of scope.
 * 
 * @version
 */
public class CloseableScope implements Scope, HasScopeAnnotation {

    private Class scopeAnnotation;
    private final Map, Object> map = Maps.newHashMap();

    @Inject
    private Injector injector;

    public CloseableScope(Class scopeAnnotation) {
        this.scopeAnnotation = scopeAnnotation;
    }

    @SuppressWarnings("unchecked")
    public  Provider scope(final Key key, final Provider creator) {
        return new CachingProvider() {
            public T get() {
                Object o;
                synchronized (map) {
                    o = map.get(key);
                    if (o == null) {
                        o = creator.get();
                        map.put(key, o);
                    }
                }
                return (T) o;
            }

            public T getCachedValue() {
                synchronized (map) {
                    return (T) map.get(key);
                }
            }
        };
    }

    /**
     * Closes all of the objects within this scope using the given injector and
     * scope annotation and clears the scope
     */
    public void close() throws CloseFailedException {
        close(injector);
    }

    /**
     * Closes all of the objects within the given injector of the specified
     * scope and clears the scope
     */
    public void close(Injector injector) throws CloseFailedException {
        Preconditions.checkNotNull(injector, "injector");
        CloseErrorsImpl errors = new CloseErrorsImpl(this);
        Injectors.close(injector, scopeAnnotation, errors);

        synchronized (map) {
            map.clear();
        }
        errors.throwIfNecessary();
    }

    public Class getScopeAnnotation() {
        return scopeAnnotation;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy