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 org.granite.client.tide.ContextAware;
024 import org.granite.client.tide.EventBus;
025 import org.granite.client.tide.NameAware;
026 import org.granite.client.tide.Platform;
027 import org.granite.client.tide.PlatformConfigurable;
028 import org.granite.client.tide.data.Conflicts;
029 import org.granite.client.tide.data.DataConflictListener;
030 import org.granite.client.tide.data.EntityManager;
031 import org.granite.client.tide.data.EntityManager.UpdateKind;
032 import org.granite.client.tide.impl.SimpleContextManager;
033 import org.springframework.beans.BeansException;
034 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
035 import org.springframework.beans.factory.config.BeanPostProcessor;
036 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
037 import org.springframework.context.ApplicationContext;
038 import org.springframework.context.ApplicationContextAware;
039
040 /**
041 * @author William DRAI
042 */
043 public class SpringContextManager extends SimpleContextManager implements ApplicationContextAware, BeanPostProcessor, BeanFactoryPostProcessor {
044
045 private ApplicationContext applicationContext;
046
047 public SpringContextManager(Platform platform) {
048 super(platform, new SpringEventBus());
049 }
050
051 public SpringContextManager(Platform platform, EventBus eventBus) {
052 super(platform, eventBus);
053 }
054
055 @Override
056 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
057 this.applicationContext = applicationContext;
058 setInstanceStoreFactory(new SpringInstanceStoreFactory(applicationContext));
059 }
060
061 @Override
062 public Object postProcessBeforeInitialization(Object instance, String name) throws BeansException {
063 if (name != null && instance instanceof NameAware)
064 ((NameAware)instance).setName(name);
065 if (instance instanceof ContextAware)
066 ((ContextAware)instance).setContext(getContext(null));
067 if (instance.getClass().isAnnotationPresent(PlatformConfigurable.class))
068 platform.configure(instance);
069 return instance;
070 }
071
072 @Override
073 public Object postProcessAfterInitialization(Object instance, String name) throws BeansException {
074 return instance;
075 }
076
077 @Override
078 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
079 beanFactory.registerSingleton("context", getContext(null));
080 EntityManager entityManager = getContext(null).getEntityManager();
081 entityManager.addListener(new SpringDataConflictListener());
082 beanFactory.registerSingleton("entityManager", entityManager);
083 beanFactory.registerSingleton("dataManager", getContext(null).getDataManager());
084 beanFactory.registerScope("view", new ViewScope());
085 }
086
087
088 private final class SpringDataConflictListener implements DataConflictListener {
089 @Override
090 public void onConflict(EntityManager entityManager, Conflicts conflicts) {
091 TideApplicationEvent event = new TideApplicationEvent(getContext(null), UpdateKind.CONFLICT.eventName(), conflicts);
092 applicationContext.publishEvent(event);
093 }
094 }
095 }