1 18 package org.apache.beehive.netui.pageflow; 19 20 import org.apache.beehive.netui.pageflow.internal.JavaControlUtils; 21 import org.apache.beehive.netui.util.logging.Logger; 22 23 import javax.servlet.http.HttpSessionBindingListener ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import javax.servlet.http.HttpSessionBindingEvent ; 28 import javax.servlet.ServletContext ; 29 import java.io.Serializable ; 30 import java.lang.reflect.Field ; 31 32 35 public abstract class PageFlowManagedObject 36 implements Serializable , HttpSessionBindingListener 37 { 38 private static final Logger _log = Logger.getInstance( PageFlowManagedObject.class ); 39 40 43 private transient ServletContext _servletContext; 44 45 46 49 public void reinitialize( HttpServletRequest request, HttpServletResponse response, ServletContext servletContext ) 50 { 51 _servletContext = servletContext; 52 } 53 54 57 public synchronized void create( HttpServletRequest request, HttpServletResponse response, ServletContext servletContext ) 58 throws Exception 59 { 60 reinitialize( request, response, servletContext ); 61 JavaControlUtils.initJavaControls( request, response, servletContext, this ); 62 onCreate(); 63 } 64 65 69 void destroy( HttpSession session ) 70 { 71 onDestroy( session ); 72 JavaControlUtils.uninitJavaControls( session.getServletContext(), this ); 73 } 74 75 79 protected void onCreate() 80 throws Exception 81 { 82 } 83 84 88 protected void onDestroy( HttpSession session ) 89 { 90 } 91 92 95 public void valueBound( HttpSessionBindingEvent event ) 96 { 97 } 98 99 103 public void valueUnbound( HttpSessionBindingEvent event ) 104 { 105 destroy( event.getSession() ); 106 } 107 108 111 protected abstract void removeFromSession( HttpServletRequest request ); 112 113 117 public abstract void persistInSession( HttpServletRequest request, HttpServletResponse response ); 118 119 127 public abstract void ensureFailover( HttpServletRequest request ); 128 129 132 protected ServletContext getServletContext() 133 { 134 return _servletContext; 135 } 136 137 140 public abstract String getDisplayName(); 141 142 146 protected boolean fieldIsUninitialized( Field field ) 147 { 148 try 149 { 150 return field != null && field.get( this ) == null; 151 } 152 catch ( IllegalAccessException e ) 153 { 154 _log.error( "Error initializing field " + field.getName() + " in " + getDisplayName(), e ); 155 return false; 156 } 157 } 158 159 162 protected void initializeField( Field field, Object instance ) 163 { 164 if ( instance != null ) 165 { 166 if ( _log.isTraceEnabled() ) 167 { 168 _log.trace( "Initializing field " + field.getName() + " in " + getDisplayName() + " with " + instance ); 169 } 170 171 try 172 { 173 field.set( this, instance ); 174 } 175 catch ( IllegalArgumentException e ) 176 { 177 _log.error( "Could not set field " + field.getName() + " on " + getDisplayName() + 178 "; instance is of type " + instance.getClass().getName() + ", field type is " 179 + field.getType().getName() ); 180 } 181 catch ( IllegalAccessException e ) 182 { 183 _log.error( "Error initializing field " + field.getName() + " in " + getDisplayName(), e ); 184 } 185 } 186 } 187 } 188 | Popular Tags |