KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > PageFlowManagedObjectException


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30
31
32 /**
33  * Base class for exceptions related to Page Flow managed objects.
34  *
35  * @see PageFlowManagedObject
36  */

37 public abstract class PageFlowManagedObjectException
38         extends RuntimeException JavaDoc
39 {
40     private PageFlowManagedObject _managedObject;
41     private String JavaDoc _messageKeyPrefix;
42     
43     
44     protected PageFlowManagedObjectException( PageFlowManagedObject object )
45     {
46         super();
47         init( object );
48     }
49     
50     protected PageFlowManagedObjectException( PageFlowManagedObject object, Throwable JavaDoc rootCause )
51     {
52         super( rootCause );
53         init( object );
54     }
55     
56     protected void init( PageFlowManagedObject object )
57     {
58         _managedObject = object;
59         
60         String JavaDoc className = getClass().getName();
61         int lastDot = className.lastIndexOf( '.' );
62         assert lastDot != -1;
63         _messageKeyPrefix = "PageFlow_" + className.substring( lastDot + 1 );
64     }
65     
66     /**
67      * Get the related PageFlowManagedObject.
68      *
69      * @return the {@link PageFlowManagedObject} associated with this exception.
70      */

71     public PageFlowManagedObject getManagedObject()
72     {
73         return _managedObject;
74     }
75
76     /**
77      * Set the related PageFlowManagedObject.
78      *
79      * @param managedObject the {@link PageFlowManagedObject} associated with this exception.
80      */

81     protected void setManagedObject( PageFlowManagedObject managedObject )
82     {
83         _managedObject = managedObject;
84     }
85
86     /**
87      * Handle the error by writing a message to the response.
88      *
89      * @param request the current HttpServletRequest
90      * @param response the current HttpServletResponse
91      */

92     void sendError( ServletRequest JavaDoc request, ServletResponse JavaDoc response, ServletContext JavaDoc servletContext,
93                     int productionTimeErrorCode )
94         throws IOException JavaDoc
95     {
96         InternalUtils.sendDevTimeError( _messageKeyPrefix, getMessageArgs(), null, productionTimeErrorCode,
97                                         request, response, servletContext );
98     }
99     
100     /**
101      * Handle the error by writing a message to the response.
102      *
103      * @param request the current HttpServletRequest
104      * @param response the current HttpServletResponse
105      */

106     public void sendError( ServletRequest JavaDoc request, ServletResponse JavaDoc response )
107         throws IOException JavaDoc
108     {
109         InternalUtils.sendError( _messageKeyPrefix, getMessageArgs(), request, response, null,
110                                  InternalUtils.avoidDirectResponseOutput( request ) );
111     }
112     
113     /**
114      * Print a formatted message.
115      *
116      * @param writer a writer to which to print the formatted message.
117      */

118     public void printError( PrintWriter JavaDoc writer )
119     {
120         writer.println( Bundle.getString( _messageKeyPrefix + "_Page", getMessageArgs() ) );
121     }
122
123     public String JavaDoc getLocalizedMessage()
124     {
125         return Bundle.getString( _messageKeyPrefix + "_Message", getMessageArgs() );
126     }
127     
128     public String JavaDoc getMessage()
129     {
130         InternalStringBuilder buf = new InternalStringBuilder();
131         String JavaDoc[] parts = getMessageParts();
132         Object JavaDoc[] 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 JavaDoc[] getMessageArgs();
150     
151     protected abstract String JavaDoc[] getMessageParts();
152 }
153
Popular Tags