1 18 package org.apache.beehive.netui.pageflow; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.util.Bundle; 23 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 24 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 31 32 37 public abstract class PageFlowManagedObjectException 38 extends RuntimeException 39 { 40 private PageFlowManagedObject _managedObject; 41 private String _messageKeyPrefix; 42 43 44 protected PageFlowManagedObjectException( PageFlowManagedObject object ) 45 { 46 super(); 47 init( object ); 48 } 49 50 protected PageFlowManagedObjectException( PageFlowManagedObject object, Throwable rootCause ) 51 { 52 super( rootCause ); 53 init( object ); 54 } 55 56 protected void init( PageFlowManagedObject object ) 57 { 58 _managedObject = object; 59 60 String className = getClass().getName(); 61 int lastDot = className.lastIndexOf( '.' ); 62 assert lastDot != -1; 63 _messageKeyPrefix = "PageFlow_" + className.substring( lastDot + 1 ); 64 } 65 66 71 public PageFlowManagedObject getManagedObject() 72 { 73 return _managedObject; 74 } 75 76 81 protected void setManagedObject( PageFlowManagedObject managedObject ) 82 { 83 _managedObject = managedObject; 84 } 85 86 92 void sendError( ServletRequest request, ServletResponse response, ServletContext servletContext, 93 int productionTimeErrorCode ) 94 throws IOException 95 { 96 InternalUtils.sendDevTimeError( _messageKeyPrefix, getMessageArgs(), null, productionTimeErrorCode, 97 request, response, servletContext ); 98 } 99 100 106 public void sendError( ServletRequest request, ServletResponse response ) 107 throws IOException 108 { 109 InternalUtils.sendError( _messageKeyPrefix, getMessageArgs(), request, response, null, 110 InternalUtils.avoidDirectResponseOutput( request ) ); 111 } 112 113 118 public void printError( PrintWriter writer ) 119 { 120 writer.println( Bundle.getString( _messageKeyPrefix + "_Page", getMessageArgs() ) ); 121 } 122 123 public String getLocalizedMessage() 124 { 125 return Bundle.getString( _messageKeyPrefix + "_Message", getMessageArgs() ); 126 } 127 128 public String getMessage() 129 { 130 InternalStringBuilder buf = new InternalStringBuilder(); 131 String [] parts = getMessageParts(); 132 Object [] args = getMessageArgs(); 133 134 assert parts.length > args.length : parts.length + ", " + args.length; 135 136 for ( int i = 0; i < parts.length; ++i ) 137 { 138 buf.append( parts[i] ); 139 140 if ( i < args.length ) 141 { 142 buf.append( args[i] ); 143 } 144 } 145 146 return buf.toString(); 147 } 148 149 protected abstract Object [] getMessageArgs(); 150 151 protected abstract String [] getMessageParts(); 152 } 153 | Popular Tags |