001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.tide.spring;
022    
023    import java.lang.annotation.Annotation;
024    import java.lang.reflect.Array;
025    import java.util.Arrays;
026    import java.util.List;
027    import java.util.Map;
028    
029    import org.granite.client.tide.Context;
030    import org.granite.client.tide.InstanceStore;
031    import org.granite.client.tide.InstanceStoreFactory;
032    import org.springframework.context.ApplicationContext;
033    
034    /**
035     * @author William DRAI
036     */
037    public class SpringInstanceStoreFactory implements InstanceStoreFactory {
038            
039            private final ApplicationContext applicationContext;
040            
041            public SpringInstanceStoreFactory(ApplicationContext applicationContext) {
042                    this.applicationContext = applicationContext;
043            }
044    
045            @Override
046            public InstanceStore createStore(Context context) {
047                    return new SpringInstanceStore(context, applicationContext);
048            }
049    
050            
051            public static class SpringInstanceStore implements InstanceStore {
052                    
053                    @SuppressWarnings("unused")
054                    private final Context context;
055                    private final ApplicationContext applicationContext;
056                    
057                    public SpringInstanceStore(Context context, ApplicationContext applicationContext) {
058                            this.context = context;
059                            this.applicationContext = applicationContext;
060                    }
061                
062                    @SuppressWarnings("unchecked")
063                    @Override
064                    public <T> T getNoProxy(String name) {
065                            return (T)applicationContext.getBean(name);
066                    }
067    
068                    @Override
069                    public void set(String name, Object instance) {
070                            // Nothing, managed by Spring
071                    }
072    
073                    @Override
074                    public void set(Object instance) {
075                            // Nothing, managed by Spring
076                    }
077    
078                    @Override
079                    public void remove(String name) {
080                            // Nothing, managed by Spring
081                    }
082                    
083                    @Override
084                    public void clear() {
085                            // Nothing, managed by Spring
086                    }
087    
088                    @Override
089                    public List<String> allNames() {
090                            return Arrays.asList(applicationContext.getBeanDefinitionNames());
091                    }
092    
093                    @SuppressWarnings("unchecked")
094                    @Override
095                    public <T> T byName(String name, Context context) {
096                            return (T)applicationContext.getBean(name);
097                    }
098    
099                    @Override
100                    public <T> T byType(Class<T> type, Context context) {
101                            return applicationContext.getBean(type);
102                    }
103                    
104                    @SuppressWarnings("unchecked")
105                    @Override
106                    public <T> T[] allByType(Class<T> type, Context context, boolean create) {
107                            Map<String, T> instancesMap = applicationContext.getBeansOfType(type, true, create);
108                            T[] all = (T[])Array.newInstance(type, instancesMap.size());
109                            return instancesMap.values().toArray(all);
110                    }
111                    
112                    @Override
113                    public Map<String, Object> allByAnnotatedWith(Class<? extends Annotation> annotationClass, Context context) {
114                            return applicationContext.getBeansWithAnnotation(annotationClass);
115                    }
116            }
117    }