001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow;
020:
021: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023: import org.apache.beehive.netui.util.Bundle;
024: import org.apache.beehive.netui.pageflow.internal.InternalUtils;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletRequest;
028: import javax.servlet.ServletResponse;
029: import java.io.IOException;
030: import java.io.PrintWriter;
031:
032: /**
033: * Base class for exceptions related to Page Flow managed objects.
034: *
035: * @see PageFlowManagedObject
036: */
037: public abstract class PageFlowManagedObjectException extends
038: RuntimeException {
039: private PageFlowManagedObject _managedObject;
040: private String _messageKeyPrefix;
041:
042: protected PageFlowManagedObjectException(
043: PageFlowManagedObject object) {
044: super ();
045: init(object);
046: }
047:
048: protected PageFlowManagedObjectException(
049: PageFlowManagedObject object, Throwable rootCause) {
050: super (rootCause);
051: init(object);
052: }
053:
054: protected void init(PageFlowManagedObject object) {
055: _managedObject = object;
056:
057: String className = getClass().getName();
058: int lastDot = className.lastIndexOf('.');
059: assert lastDot != -1;
060: _messageKeyPrefix = "PageFlow_"
061: + className.substring(lastDot + 1);
062: }
063:
064: /**
065: * Get the related PageFlowManagedObject.
066: *
067: * @return the {@link PageFlowManagedObject} associated with this exception.
068: */
069: public PageFlowManagedObject getManagedObject() {
070: return _managedObject;
071: }
072:
073: /**
074: * Set the related PageFlowManagedObject.
075: *
076: * @param managedObject the {@link PageFlowManagedObject} associated with this exception.
077: */
078: protected void setManagedObject(PageFlowManagedObject managedObject) {
079: _managedObject = managedObject;
080: }
081:
082: /**
083: * Handle the error by writing a message to the response.
084: *
085: * @param request the current HttpServletRequest
086: * @param response the current HttpServletResponse
087: */
088: void sendError(ServletRequest request, ServletResponse response,
089: ServletContext servletContext, int productionTimeErrorCode)
090: throws IOException {
091: InternalUtils.sendDevTimeError(_messageKeyPrefix,
092: getMessageArgs(), null, productionTimeErrorCode,
093: request, response, servletContext);
094: }
095:
096: /**
097: * Handle the error by writing a message to the response.
098: *
099: * @param request the current HttpServletRequest
100: * @param response the current HttpServletResponse
101: */
102: public void sendError(ServletRequest request,
103: ServletResponse response) throws IOException {
104: InternalUtils.sendError(_messageKeyPrefix, getMessageArgs(),
105: request, response, null, InternalUtils
106: .avoidDirectResponseOutput(request));
107: }
108:
109: /**
110: * Print a formatted message.
111: *
112: * @param writer a writer to which to print the formatted message.
113: */
114: public void printError(PrintWriter writer) {
115: writer.println(Bundle.getString(_messageKeyPrefix + "_Page",
116: getMessageArgs()));
117: }
118:
119: public String getLocalizedMessage() {
120: return Bundle.getString(_messageKeyPrefix + "_Message",
121: getMessageArgs());
122: }
123:
124: public String getMessage() {
125: InternalStringBuilder buf = new InternalStringBuilder();
126: String[] parts = getMessageParts();
127: Object[] args = getMessageArgs();
128:
129: assert parts.length > args.length : parts.length + ", "
130: + args.length;
131:
132: for (int i = 0; i < parts.length; ++i) {
133: buf.append(parts[i]);
134:
135: if (i < args.length) {
136: buf.append(args[i]);
137: }
138: }
139:
140: return buf.toString();
141: }
142:
143: protected abstract Object[] getMessageArgs();
144:
145: protected abstract String[] getMessageParts();
146: }
|