KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > callback > ExternalCallback


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

15 package org.apache.tapestry.callback;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.util.Defense;
19 import org.apache.tapestry.IExternalPage;
20 import org.apache.tapestry.IRequestCycle;
21
22 /**
23  * A callback for returning to an {@link org.apache.tapestry.IExternalPage}.
24  * <p>
25  * Example usage of <tt>ExternalCallback</tt>:
26  * <p>
27  * The External page ensure a user is authenticated in the
28  * {@link org.apache.tapestry.IPage#validate(IRequestCycle)} method. If the user is not
29  * authenticated, they are redirected to the Login page, after setting a callback in the Login page.
30  * <p>
31  * The Login page <tt>formSubmit()</tt> {@link org.apache.tapestry.IActionListener} authenticates
32  * the user and then invokes {@link ICallback#performCallback(IRequestCycle)} to the External page.
33  *
34  * <pre>
35  *
36  *
37  *
38  *
39  *
40  *
41  *
42  *
43  *
44  *
45  *
46  * public class External extends BasePage implements IExternalPage {
47  *
48  * private Integer _itemId;
49  *
50  * public void validate(IRequestCycle cycle) throws RequestCycleException {
51  * Visit visit = (Visit) getVisit();
52  *
53  * if (!visit.isAuthenticated()) {
54  * Login login = (Login) cycle.getPage(&quot;Login&quot;);
55  *
56  * login.setCallback
57  * (new ExternalCallback(this, cycle.getServiceParameters()));
58  *
59  * throw new PageRedirectException(login);
60  * }
61  * }
62  *
63  * public void activateExternalPage(Object[] params, IRequestCycle cycle)
64  * throws RequestCycleException {
65  * _itemId = (Integer) params[0];
66  * }
67  * }
68  *
69  * public Login extends BasePage {
70  *
71  * private ICallback _callback;
72  *
73  * public void setCallback(ICallback _callback) {
74  * _callback = callback;
75  * }
76  *
77  * public void formSubmit(IRequestCycle cycle) {
78  * // Authentication code
79  * ..
80  *
81  * Visit visit = (Visit) getVisit();
82  *
83  * visit.setAuthenticated(true);
84  *
85  * if (_callback != null) {
86  * _callback.performCallback(cycle);
87  * }
88  * }
89  * }
90  *
91  *
92  *
93  *
94  *
95  *
96  *
97  *
98  *
99  *
100  *
101  * </pre>
102  *
103  * @see org.apache.tapestry.IExternalPage
104  * @see org.apache.tapestry.engine.ExternalService
105  * @author Malcolm Edgar
106  * @since 2.3
107  */

108
109 public class ExternalCallback implements ICallback
110 {
111     private static final long serialVersionUID = -6783421589702643930L;
112
113     private String JavaDoc _pageName;
114
115     private Object JavaDoc[] _parameters;
116
117     /**
118      * Creates a new ExternalCallback for the named <tt>IExternalPage</tt>. The parameters (which
119      * may be null) is retained, not copied.
120      */

121
122     public ExternalCallback(String JavaDoc pageName, Object JavaDoc[] parameters)
123     {
124         Defense.notNull(pageName, "pageName");
125
126         _pageName = pageName;
127         _parameters = parameters;
128     }
129
130     /**
131      * Creates a new ExternalCallback for the page. The parameters (which may be null) is retained,
132      * not copied.
133      */

134
135     public ExternalCallback(IExternalPage page, Object JavaDoc[] parameters)
136     {
137         Defense.notNull(page, "page");
138
139         _pageName = page.getPageName();
140         _parameters = parameters;
141     }
142
143     /**
144      * Invokes {@link IRequestCycle#setPage(String)} to select the previously identified
145      * <tt>IExternalPage</tt> as the response page and activates the page by invoking
146      * <tt>activateExternalPage()</tt> with the callback parameters and request cycle.
147      */

148
149     public void performCallback(IRequestCycle cycle)
150     {
151         Defense.notNull(cycle, "cycle");
152
153         try
154         {
155             IExternalPage page = (IExternalPage) cycle.getPage(_pageName);
156
157             cycle.activate(page);
158
159             page.activateExternalPage(_parameters, cycle);
160         }
161         catch (ClassCastException JavaDoc ex)
162         {
163             throw new ApplicationRuntimeException(CallbackMessages.pageNotExternal(_pageName), ex);
164         }
165     }
166
167     public String JavaDoc toString()
168     {
169         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("ExternalCallback[");
170
171         buffer.append(_pageName);
172
173         if (_parameters != null)
174         {
175             for (int i = 0; i < _parameters.length; i++)
176             {
177                 if (i == 0)
178                     buffer.append('/');
179                 else
180                     buffer.append(", ");
181
182                 buffer.append(_parameters[i]);
183             }
184         }
185
186         buffer.append(']');
187
188         return buffer.toString();
189     }
190 }
Popular Tags