1 13 package com.tonbeller.jpivot.tags; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 18 import javax.servlet.http.HttpSession ; 19 import javax.servlet.http.HttpSessionBindingEvent ; 20 import javax.servlet.http.HttpSessionBindingListener ; 21 22 import org.apache.log4j.Logger; 23 24 import com.tonbeller.jpivot.core.ModelChangeEvent; 25 import com.tonbeller.jpivot.core.ModelChangeListener; 26 import com.tonbeller.jpivot.olap.model.OlapException; 27 import com.tonbeller.jpivot.olap.model.OlapModel; 28 import com.tonbeller.jpivot.olap.model.OlapModelDecorator; 29 import com.tonbeller.jpivot.olap.model.impl.Empty; 30 import com.tonbeller.jpivot.tags.StateManager.State; 31 import com.tonbeller.tbutils.testenv.Environment; 32 33 42 43 public class OlapModelProxy extends OlapModelDecorator implements HttpSessionBindingListener { 44 ArrayList listeners = new ArrayList (); 45 public static final String DEFAULT_NAME = "default"; 46 47 private static final Logger logger = Logger.getLogger(OlapModelProxy.class); 48 49 private ModelChangeListener modelChangeListener = new ModelChangeListener() { 50 public void modelChanged(ModelChangeEvent e) { 51 fireModelChanged(); 52 } 53 54 public void structureChanged(ModelChangeEvent e) { 55 fireStructureChanged(); 56 } 57 }; 58 59 class MyState implements State { 60 String name; 61 OlapModel model; 62 63 MyState(OlapModel model) { 64 this.name = DEFAULT_NAME; 65 this.model = model; 66 } 67 68 MyState(String name, OlapModel model) { 69 this.name = name; 70 this.model = model; 71 } 72 73 public void initialize() throws Exception { 74 try { 75 if (logger.isInfoEnabled()) 76 logger.info("initializing: " + model); 77 model.initialize(); 78 } catch (OlapException e) { 79 logger.error(null, e); 80 throw e; 81 } 82 } 83 84 public void destroy() throws Exception { 85 if (logger.isInfoEnabled()) 86 logger.info("destroying: " + model); 87 model.destroy(); 88 } 89 90 public void show() throws OlapException { 91 if (logger.isInfoEnabled()) 92 logger.info("activating: " + model); 93 model.addModelChangeListener(modelChangeListener); 94 setDelegate(model); 95 fireStructureChanged(); 96 } 97 98 public void hide() throws OlapException { 99 if (logger.isInfoEnabled()) 100 logger.info("deactivating: " + model); 101 model.removeModelChangeListener(modelChangeListener); 102 setDelegate(Empty.EMPTY_MODEL); 103 fireStructureChanged(); 104 } 105 106 public String getName() { 107 return name; 108 } 109 110 public void setName(String name) { 111 this.name = name; 112 } 113 } 114 115 StateManager stateManager; 116 117 private OlapModelProxy(boolean stackMode) { 118 super(Empty.EMPTY_MODEL); 119 if (stackMode) 120 stateManager = new StackStateManager(); 121 else 122 stateManager = new PageStateManager(); 123 if (Environment.isTest()) 124 stateManager.setLogger(new TestStateLogger()); 125 } 126 127 public static OlapModelProxy instance(String id, HttpSession session) { 128 return instance(id, session, true); 129 } 130 131 145 public static OlapModelProxy instance(String id, HttpSession session, boolean stackMode) { 146 OlapModelProxy omp = (OlapModelProxy) session.getAttribute(id); 147 if (omp == null) { 148 omp = new OlapModelProxy(stackMode); 149 session.setAttribute(id, omp); 150 } 151 return omp; 152 } 153 154 158 public void initializeAndShow(OlapModel model) throws Exception { 159 State s = new MyState(model); 160 stateManager.initializeAndShow(s); 161 } 162 163 String nonEmptyQueryName(String queryName) { 164 if (queryName == null || queryName.length() == 0) 165 return DEFAULT_NAME; 166 return queryName; 167 } 168 169 176 public void initializeAndShow(String queryName, OlapModel model) throws Exception { 177 queryName = nonEmptyQueryName(queryName); 178 State s = new MyState(queryName, model); 179 stateManager.initializeAndShow(s); 180 } 181 182 186 public void showByName(String queryName) throws Exception { 187 queryName = nonEmptyQueryName(queryName); 188 stateManager.showByName(queryName); 189 } 190 191 public void destroyAll() throws Exception { 192 stateManager.destroyAll(); 193 } 194 195 public void destroyQuery(String queryName) throws Exception { 196 queryName = nonEmptyQueryName(queryName); 197 stateManager.destroyByName(queryName); 198 } 199 200 private void fireModelChanged() { 201 ModelChangeEvent e = new ModelChangeEvent(this); 202 for (Iterator iter = listeners.iterator(); iter.hasNext();) { 203 ModelChangeListener l = (ModelChangeListener) iter.next(); 204 l.modelChanged(e); 205 } 206 } 207 208 private void fireStructureChanged() { 209 ModelChangeEvent e = new ModelChangeEvent(this); 210 for (Iterator iter = listeners.iterator(); iter.hasNext();) { 211 ModelChangeListener l = (ModelChangeListener) iter.next(); 212 l.structureChanged(e); 213 } 214 } 215 216 public void addModelChangeListener(ModelChangeListener l) { 217 listeners.add(l); 218 } 219 220 public void removeModelChangeListener(ModelChangeListener l) { 221 listeners.remove(l); 222 } 223 224 public void valueBound(HttpSessionBindingEvent ev) { 225 } 226 227 public void valueUnbound(HttpSessionBindingEvent ev) { 228 logger.info("session timeout"); 229 try { 230 stateManager.destroyAll(); 231 } catch (Exception e) { 232 logger.error(null, e); 233 } 234 } 235 236 240 public void destroy() { 241 throw new RuntimeException ("must not be called"); 242 } 243 244 248 public void initialize() { 249 throw new RuntimeException ("must not be called"); 250 } 251 252 255 public StateManager getStateManager() { 256 return stateManager; 257 } 258 } 259 | Popular Tags |