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