001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.engine.state; 016 017 import java.util.HashMap; 018 import java.util.Iterator; 019 import java.util.Map; 020 021 import org.apache.hivemind.PoolManageable; 022 023 /** 024 * This implementation expects to be pooled. 025 * 026 * @author Howard M. Lewis Ship 027 * @since 4.0 028 */ 029 public class ApplicationStateManagerImpl implements ApplicationStateManager, PoolManageable 030 { 031 032 /** 033 * Keyed on application static object name, value is the current state object. 034 */ 035 036 private Map _stateObjects = new HashMap(); 037 038 private StateObjectManagerRegistry _registry; 039 040 public void activateService() 041 { 042 } 043 044 public void passivateService() 045 { 046 _stateObjects.clear(); 047 } 048 049 public boolean exists(String objectName) 050 { 051 return _stateObjects.containsKey(objectName) || _registry.get(objectName).exists(); 052 } 053 054 public Object get(String objectName) 055 { 056 Object result = _stateObjects.get(objectName); 057 058 if (result == null) 059 { 060 result = _registry.get(objectName).get(); 061 062 _stateObjects.put(objectName, result); 063 } 064 065 return result; 066 } 067 068 public void store(String objectName, Object stateObject) 069 { 070 _registry.get(objectName).store(stateObject); 071 072 _stateObjects.put(objectName, stateObject); 073 } 074 075 public void flush() 076 { 077 Iterator i = _stateObjects.entrySet().iterator(); 078 while (i.hasNext()) 079 { 080 Map.Entry e = (Map.Entry) i.next(); 081 082 String objectName = (String) e.getKey(); 083 Object stateObject = e.getValue(); 084 085 // Slight bending of law-of-demeter 086 087 _registry.get(objectName).store(stateObject); 088 } 089 } 090 091 public void setRegistry(StateObjectManagerRegistry registry) 092 { 093 _registry = registry; 094 } 095 }