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.ViewScope.BeanResetter;
024 import org.granite.client.tide.ViewScopeHolder;
025 import org.springframework.beans.factory.ObjectFactory;
026 import org.springframework.beans.factory.config.Scope;
027
028 /**
029 * @author William DRAI
030 */
031 public class ViewScope implements Scope {
032
033 // private static final Logger log = Logger.getLogger(ViewScope.class);
034
035 private org.granite.client.tide.ViewScope beanCache;
036
037 public ViewScope() {
038 }
039
040 private org.granite.client.tide.ViewScope getBeanCache() {
041 if (beanCache == null) {
042 beanCache = ViewScopeHolder.get();
043 if (beanCache == null)
044 throw new RuntimeException("View bean cache not set");
045 }
046 return beanCache;
047 }
048
049 @Override
050 public String getConversationId() {
051 return getBeanCache().getViewId();
052 }
053
054 @Override
055 public Object get(String name, ObjectFactory<?> objectFactory) {
056 Object instance = getBeanCache().get(name);
057 if (instance == null) {
058 instance = objectFactory.getObject();
059 getBeanCache().put(name, instance);
060 }
061 return instance;
062 }
063
064 @Override
065 public Object resolveContextualObject(String name) {
066 return getBeanCache().get(name);
067 }
068
069 @Override
070 public void registerDestructionCallback(String name, final Runnable callback) {
071 getBeanCache().addResetter(name, new BeanResetter() {
072 @Override
073 public void reset(Object instance) {
074 callback.run();
075 }
076 });
077 }
078
079 @Override
080 public Object remove(String name) {
081 return getBeanCache().remove(name);
082 }
083
084 }
085