001: /*
002: * $Id: ComponentResourceRequestTarget.java,v 1.7 2006/02/12 20:25:40 eelco12
003: * Exp $ $Revision: 485029 $ $Date: 2006-12-09 19:52:47 +0100 (Sat, 09 Dec 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.request.target.resource;
019:
020: import javax.servlet.http.HttpServletResponse;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import wicket.Component;
026: import wicket.IRequestTarget;
027: import wicket.Page;
028: import wicket.RequestCycle;
029: import wicket.RequestListenerInterface;
030: import wicket.Response;
031: import wicket.WicketRuntimeException;
032: import wicket.protocol.http.WebResponse;
033:
034: /**
035: * An implemenation of IRequestTarget that is used for the IResourceListener
036: * event request.
037: *
038: * @author jcompagner
039: */
040: public final class ComponentResourceRequestTarget implements
041: IRequestTarget {
042: private static final Log log = LogFactory
043: .getLog(ComponentResourceRequestTarget.class);
044:
045: private final Page page;
046: private final Component component;
047: private final RequestListenerInterface listener;
048:
049: /**
050: * Construct.
051: *
052: * @param page
053: * @param component
054: * @param listener
055: */
056: public ComponentResourceRequestTarget(Page page,
057: Component component, RequestListenerInterface listener) {
058: this .page = page;
059: this .component = component;
060: this .listener = listener;
061: }
062:
063: /**
064: * @see wicket.IRequestTarget#respond(wicket.RequestCycle)
065: */
066: public void respond(RequestCycle requestCycle) {
067: page.beforeCallComponent(component, listener);
068:
069: try {
070: // Invoke the interface method on the component
071: listener.getMethod().invoke(component, new Object[] {});
072: } catch (Exception e) {
073: Response response = requestCycle.getResponse();
074: if (response instanceof WebResponse) {
075: ((WebResponse) response)
076: .getHttpServletResponse()
077: .setStatus(
078: HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
079: log.error(
080: "error handling resource request for component "
081: + component + ", on page " + page
082: + ", listener " + listener.getName()
083: + " - " + e.getMessage(), e);
084: return;
085: } else {
086: throw new WicketRuntimeException("method "
087: + listener.getName() + " of "
088: + listener.getMethod().getDeclaringClass()
089: + " targetted at component " + component
090: + " threw an exception", e);
091: }
092: } finally {
093: page.afterCallComponent(component, listener);
094: }
095: }
096:
097: /**
098: * @see wicket.IRequestTarget#detach(wicket.RequestCycle)
099: */
100: public void detach(RequestCycle requestCycle) {
101: page.internalDetach();
102: }
103:
104: /**
105: * @see wicket.IRequestTarget#getLock(RequestCycle)
106: */
107: public Object getLock(RequestCycle requestCycle) {
108: return requestCycle.getSession();
109: }
110:
111: /**
112: * @see java.lang.Object#equals(java.lang.Object)
113: */
114: public boolean equals(Object obj) {
115: if (obj instanceof ComponentResourceRequestTarget) {
116: ComponentResourceRequestTarget that = (ComponentResourceRequestTarget) obj;
117: return page.equals(that.page)
118: && component.equals(that.component);
119: }
120: return false;
121: }
122:
123: /**
124: * @see java.lang.Object#hashCode()
125: */
126: public int hashCode() {
127: int result = getClass().hashCode();
128: result += page.hashCode();
129: result += component.hashCode();
130: return 17 * result;
131: }
132:
133: /**
134: * @see java.lang.Object#toString()
135: */
136: public String toString() {
137: StringBuffer b = new StringBuffer(getClass().getName()).append(
138: "@").append(hashCode()).append(page).append("->")
139: .append(component.getId())
140: .append("->IResourceListener");
141: return b.toString();
142: }
143: }
|