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