InstanceTracker.java
001 /*
002  * Copyright 2008-2017 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 griffon.util.GriffonClassUtils.hasMethodAnnotatedwith;
032 import static griffon.util.GriffonClassUtils.invokeAnnotatedMethod;
033 import static java.util.Collections.synchronizedMap;
034 import static java.util.Objects.requireNonNull;
035 
036 /**
037  @author Andres Almiray
038  @since 2.6.0
039  */
040 class InstanceTracker {
041     private static final String ERROR_INSTANCE_NULL = "Argument 'instance' must not be null";
042     private static final String ERROR_BINDING_NULL = "Argument 'binding' must not be null";
043 
044     private final Map<Object, Binding<?>> instanceToKeyMap = synchronizedMap(new LinkedHashMap<Object, Binding<?>>());
045 
046     private com.google.inject.Injector injector;
047 
048     public void setInjector(@Nonnull Injector injector) {
049         this.injector = injector;
050     }
051 
052     @Nonnull
053     public Injector getInjector() {
054         return injector;
055     }
056 
057     @Nonnull
058     public <T> T track(@Nonnull Binding<?> binding, @Nonnull final T instance) {
059         requireNonNull(binding, ERROR_BINDING_NULL);
060         requireNonNull(instance, ERROR_INSTANCE_NULL);
061 
062         if (hasMethodAnnotatedwith(instance, PreDestroy.class)) {
063             if (isSingleton(binding)) {
064                 instanceToKeyMap.put(instance, binding);
065             else {
066                 try {
067                     InjectionUnitOfWork.track(instance);
068                 catch (IllegalStateException ise) {
069                     instanceToKeyMap.put(instance, binding);
070                 }
071             }
072         }
073         return instance;
074     }
075 
076     public <T> void release(@Nonnull T instance) {
077         requireNonNull(instance, ERROR_INSTANCE_NULL);
078 
079         invokeAnnotatedMethod(instance, PreDestroy.class);
080 
081         Binding<?> binding = instanceToKeyMap.get(instance);
082         if (binding != null) {
083             instanceToKeyMap.remove(instance);
084         }
085     }
086 
087     public void releaseAll() {
088         List<Object> instances = new ArrayList<>();
089 
090         instances.addAll(instanceToKeyMap.keySet());
091         instanceToKeyMap.clear();
092 
093         Collections.reverse(instances);
094 
095         for (Object instance : instances) {
096             invokeAnnotatedMethod(instance, PreDestroy.class);
097         }
098 
099         instances.clear();
100     }
101 }