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