InstanceTracker.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.codehaus.griffon.runtime.injection;
019 
020 import com.google.inject.Binding;
021 import com.google.inject.Injector;
022 import org.codehaus.griffon.runtime.core.injection.InjectionUnitOfWork;
023 
024 import javax.annotation.Nonnull;
025 import javax.annotation.PreDestroy;
026 import java.util.ArrayList;
027 import java.util.Collections;
028 import java.util.LinkedHashMap;
029 import java.util.List;
030 import java.util.Map;
031 
032 import static com.google.inject.Scopes.isSingleton;
033 import static griffon.util.GriffonClassUtils.hasMethodAnnotatedwith;
034 import static griffon.util.GriffonClassUtils.invokeAnnotatedMethod;
035 import static java.util.Collections.synchronizedMap;
036 import static java.util.Objects.requireNonNull;
037 
038 /**
039  @author Andres Almiray
040  @since 2.6.0
041  */
042 class InstanceTracker {
043     private static final String ERROR_INSTANCE_NULL = "Argument 'instance' must not be null";
044     private static final String ERROR_BINDING_NULL = "Argument 'binding' must not be null";
045 
046     private final Map<Object, Binding<?>> instanceToKeyMap = synchronizedMap(new LinkedHashMap<Object, Binding<?>>());
047 
048     private com.google.inject.Injector injector;
049 
050     public void setInjector(@Nonnull Injector injector) {
051         this.injector = injector;
052     }
053 
054     @Nonnull
055     public Injector getInjector() {
056         return injector;
057     }
058 
059     @Nonnull
060     public <T> T track(@Nonnull Binding<?> binding, @Nonnull final T instance) {
061         requireNonNull(binding, ERROR_BINDING_NULL);
062         requireNonNull(instance, ERROR_INSTANCE_NULL);
063 
064         if (hasMethodAnnotatedwith(instance, PreDestroy.class)) {
065             if (isSingleton(binding)) {
066                 instanceToKeyMap.put(instance, binding);
067             else {
068                 try {
069                     InjectionUnitOfWork.track(instance);
070                 catch (IllegalStateException ise) {
071                     instanceToKeyMap.put(instance, binding);
072                 }
073             }
074         }
075         return instance;
076     }
077 
078     public <T> void release(@Nonnull T instance) {
079         requireNonNull(instance, ERROR_INSTANCE_NULL);
080 
081         invokeAnnotatedMethod(instance, PreDestroy.class);
082 
083         Binding<?> binding = instanceToKeyMap.get(instance);
084         if (binding != null) {
085             instanceToKeyMap.remove(instance);
086         }
087     }
088 
089     public void releaseAll() {
090         List<Object> instances = new ArrayList<>();
091 
092         instances.addAll(instanceToKeyMap.keySet());
093         instanceToKeyMap.clear();
094 
095         Collections.reverse(instances);
096 
097         for (Object instance : instances) {
098             invokeAnnotatedMethod(instance, PreDestroy.class);
099         }
100 
101         instances.clear();
102     }
103 }