KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > view > DescriptorViewHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.guiframework.view;
25
26 import com.iplanet.jato.CompleteRequestException;
27 import com.iplanet.jato.RequestContext;
28 import com.iplanet.jato.command.Command;
29 import com.iplanet.jato.command.CommandEvent;
30 import com.iplanet.jato.command.CommandException;
31 import com.iplanet.jato.command.CommandDescriptor;
32 import com.iplanet.jato.model.ModelControlException;
33 import com.iplanet.jato.model.ModelFieldBinding;
34 import com.iplanet.jato.model.ModelReference;
35 import com.iplanet.jato.view.BasicCommandField;
36 import com.iplanet.jato.view.CommandField;
37 import com.iplanet.jato.view.CommandFieldBase;
38 import com.iplanet.jato.view.CommandFieldDescriptor;
39 import com.iplanet.jato.view.View;
40 import com.iplanet.jato.view.ViewBean;
41 import com.iplanet.jato.view.ContainerView;
42 import com.iplanet.jato.view.ContainerViewBase;
43 import com.iplanet.jato.view.event.ChildContentDisplayEvent;
44 import com.iplanet.jato.view.event.ChildDisplayEvent;
45 import com.iplanet.jato.view.event.DisplayEvent;
46
47 import com.sun.web.ui.model.wizard.WizardEvent;
48
49 import com.sun.enterprise.tools.guiframework.event.descriptors.EventDescriptor;
50 import com.sun.enterprise.tools.guiframework.event.descriptors.HandlerDescriptor;
51 import com.sun.enterprise.tools.guiframework.event.descriptors.UseHandlerDescriptor;
52 import com.sun.enterprise.tools.guiframework.exception.ChildNotRegisteredException;
53 import com.sun.enterprise.tools.guiframework.exception.FrameworkError;
54 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
55 import com.sun.enterprise.tools.guiframework.view.descriptors.DisplayFieldDescriptor;
56 import com.sun.enterprise.tools.guiframework.view.descriptors.FakeContainerDescriptor;
57 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
58 import com.sun.enterprise.tools.guiframework.view.event.AfterCreateEvent;
59 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent;
60 import com.sun.enterprise.tools.guiframework.util.Util;
61 import com.sun.enterprise.tools.guiframework.util.LogUtil;
62
63 import java.io.File JavaDoc;
64 import java.lang.reflect.InvocationTargetException JavaDoc;
65 import java.lang.reflect.Method JavaDoc;
66 import java.util.EventObject JavaDoc;
67 import java.util.HashMap JavaDoc;
68 import java.util.Iterator JavaDoc;
69 import java.util.List JavaDoc;
70 import java.util.Map JavaDoc;
71 import java.util.Stack JavaDoc;
72
73 import javax.servlet.ServletContext JavaDoc;
74
75
76 /**
77  * This class contains static utility methods for DescriptorView's.
78  */

79 public class DescriptorViewHelper {
80
81     /**
82      * This method exists to ease creating a new DescriptorContainerView
83      * implementation.
84      */

85     public static void registerViewDescriptorChildren(ViewDescriptor viewDesc, ContainerViewBase container) {
86     try {
87         viewDesc.registerChildren(container);
88     } catch (CompleteRequestException ex) {
89         // Let this exception be caught by the Servlet
90
throw ex;
91     } catch (FrameworkException ex) {
92         // Change to an error
93
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
94     } catch (Exception JavaDoc ex) {
95         throw new FrameworkError(ex, viewDesc, container);
96     }
97     }
98
99
100     /**
101      *
102      */

103     public static View addCommandDescriptor(ContainerView container, View child, ViewDescriptor desc) {
104     // Set the CommandDescriptor (so that the default:
105
// handle<command-name>Request() method is not looked for)
106
if (!(child instanceof CommandField)) {
107         return child;
108     }
109     if (desc == null) {
110         if (LogUtil.isLoggable(LogUtil.INFO)) {
111         LogUtil.log(LogUtil.INFO, "framework.addCommandDescriptor",
112             child.getParent().getName()+"."+child.getName());
113         }
114         return child;
115     }
116
117     // Find a "Command" container
118
while (container != null) {
119         if (container instanceof Command) {
120         break;
121         }
122         container = (ContainerView)container.getParent();
123     }
124     if (container == null) {
125         throw new FrameworkException(
126         "The CommandField's container must implement Command in " +
127         "order to handle the CommandField!", desc, child);
128     }
129
130     Map JavaDoc map = new HashMap JavaDoc(1);
131     map.put(COMMAND_FIELD_DESCRIPTOR, desc);
132     CommandDescriptor commandDesc = new CommandDescriptor((Command)container, desc.getName(), map);
133     if (child instanceof BasicCommandField) {
134         // This is the exected way to declare command fields
135
((BasicCommandField)child).setCommandDescriptor(commandDesc);
136     } else if (child instanceof CommandFieldBase) {
137         // This is the old JATO way...
138
if (LogUtil.isLoggable(LogUtil.FINER)) {
139         LogUtil.log(LogUtil.FINER, "framework.oldCommandField",
140             child.getName());
141         }
142         ((CommandFieldBase)child).setDescriptor(
143         new CommandFieldDescriptor(commandDesc));
144     } else {
145         throw new FrameworkException("Unable to add CommandDescriptor " +
146         "to CommandField because it is not a 'BasicCommandField'" +
147         " or a 'CommandFieldBase'!", desc, child);
148     }
149
150     return child;
151     }
152
153
154     /**
155      * This method creates a child for the given container/name/descriptor.
156      * It is implemented as a static method so DescriptorViewBeanBase and
157      * other DescriptorContainerView's can share the same code as it should
158      * be identical.
159      */

160     public static View createChild(DescriptorContainerView container, String JavaDoc name) {
161     // Logging at FINEST
162
if (LogUtil.isLoggable(LogUtil.FINEST)) {
163         LogUtil.log(LogUtil.FINEST, "trace.createChild",
164         container.getName()+"."+name);
165     }
166
167     // Get the RequestContext
168
RequestContext ctx = container.getRequestContext();
169
170     // Find the child ViewDescriptor
171
ViewDescriptor containerDesc = container.getViewDescriptor();
172     ViewDescriptor childDesc = containerDesc.getChildDescriptor(name);
173
174     // **************************************
175
// ********** LOCKHART HACK *************
176
// **************************************
177
// The following code is necessary because Lockhart creates "children"
178
// that use "peers". To make Property Sheets understandable in the
179
// ViewXML, the peers must appear to be children. This is also
180
// necessary to correctly associate the Property Sheet fields with the
181
// correct Model.
182
ViewDescriptor fakeContDesc = null;
183     if (childDesc == null) {
184         // There's no descriptor defined for this given child name.
185
// See if it a subChild of one of the descriptors, e.g. CCActionTable or CCPropoertySheet.
186
// FIXME: BreadCrumb dynamically creates children based on "link*" where * is a number
187
List JavaDoc childDescriptors =
188         container.getViewDescriptor().getChildDescriptors();
189         Iterator JavaDoc it = childDescriptors.iterator();
190         while (it.hasNext()) {
191         fakeContDesc = (ViewDescriptor)it.next();
192         if (fakeContDesc instanceof FakeContainerDescriptor) {
193             childDesc = fakeContDesc.getChildDescriptor(name);
194             if (childDesc != null) {
195             break;
196             }
197         }
198         }
199     }
200     // **************************************
201
// ******** END LOCKHART HACK ***********
202
// **************************************
203

204     // If the Descriptor is still null, we have a problem.
205
if (childDesc == null) {
206         throw new ChildNotRegisteredException("Child '"+name+"' does not" +
207         " have a registered descriptor in '"+container.getName()+"'!",
208         container.getViewDescriptor(), container);
209     }
210
211     // Set the correct container
212
if (fakeContDesc != null) {
213         // Changing the container to the "FakeContainer" allows
214
// child.getParent() to get the "fake" container
215
View tmp = container.getChild(fakeContDesc.getName());
216 // FIXME: NOTE: WE have to do the following b/c Lockhart has "FakeContainers"
217
// FIXME: which don't implement ContainerView. Specifically, CCBreadCrumb has
218
// FIXME: children/peers of type HREF, but it does NOT implement containerView
219
if (tmp instanceof ContainerView) {
220         // Even fake containers can now create their own children, make
221
// them do it so we don't end up doing it 2x (once during
222
// registration, once during the JSP)
223
return ((ContainerView)tmp).getChild(name);
224         }
225     }
226
227     // Make sure "useContainer" is the parent of child ('.'s in the name may
228
// have changed this relationship). NOTE: Even in the unlikely case that
229
// a FakeContainer is asked for "a.b.c", this code should still work:
230
ContainerView useContainer = container;
231     ViewDescriptor tmpDesc = childDesc.getParent();
232     Stack JavaDoc stack = new Stack JavaDoc();
233     while (tmpDesc != containerDesc) {
234         stack.push(tmpDesc.getName());
235         tmpDesc = tmpDesc.getParent();
236     }
237     View tmpView = null;
238     if (!stack.isEmpty()) {
239         while (!stack.isEmpty()) {
240         if (useContainer instanceof DescriptorContainerView) {
241             // container = nearest DescriptorContainerView, maintain this
242
container = (DescriptorContainerView)useContainer;
243         }
244         tmpView = useContainer.getChild((String JavaDoc)stack.pop());
245         if (tmpView instanceof ContainerView) {
246             // This should always be the case, but in LH's world...
247
useContainer = (ContainerView)tmpView;
248         }
249         }
250             
251         // The following should usually be the case because the child was
252
// most likely already created and we should not create it again.
253
// name should have atleast 1 '.' in it at this point, it is more
254
// than one level deep
255
if (useContainer != container)
256                 return useContainer.getChild(childDesc.getName());
257     }
258
259     // Fire our "beforeCreate" event. This provides a spot for
260
// initializing stuff before we create something. This, of course, is
261
// different than beforeDisplay which happens after the child is
262
// created. This is useful for doing Model initialization-type
263
// operations.
264
DescriptorViewHelper.beforeCreate(ctx,
265         ((useContainer instanceof DescriptorContainerView) ?
266         (DescriptorContainerView)useContainer : container), childDesc);
267
268     // Create Child Using the Descriptor
269
//System.out.println("Creating: '"+useContainer.getName()+"."+name+"'");
270
View child = childDesc.getInstance(ctx, useContainer, name);
271 //System.out.println("Created: '"+useContainer.getName()+"."+name+"'");
272
if (child == null) {
273         throw new ChildNullException(name, childDesc, useContainer);
274     }
275
276     // If this is a CommandField, add a CommandDescriptor to process click
277
addCommandDescriptor(useContainer, child, childDesc);
278
279     // Fire our "afterCreate" event. This provides a spot for
280
// initializing stuff after we create something. This is useful for
281
// doing anything you would like to do once for this View per request
282
// and need a reference to the View. NOTE: The view will be passed in
283
// however, this View still has not be registered, therefor calling
284
// parent.getChild(view-name) will cause the View to be re-created,
285
// which will call this method again, and so on endlessly.
286
DescriptorViewHelper.afterCreate(ctx, child, childDesc);
287
288     return child;
289     }
290
291
292     /**
293      * This method is a helper method to handle a "Command". The Command
294      * implementation may delegate to this method, passing in a ViewBean and
295      * the View (event.getSource()) for the Command event.
296      *
297      * @param ctx The RequestContext.
298      * @param view The View that is the source of the Command (typically
299      * event.getSource())
300      * @param event The command event, contains information pertinent to
301      * to the invocation of this command
302      *
303      * @throws CommandException Thrown if an error occurs executing
304      * the command
305      */

306     public static void execute(RequestContext ctx, View view, CommandEvent event) throws CommandException {
307     // Get the DisplayFieldDescriptor
308
Map JavaDoc params = event.getParameters();
309     DisplayFieldDescriptor desc = (DisplayFieldDescriptor)params.get(
310         DescriptorViewHelper.COMMAND_FIELD_DESCRIPTOR);
311     try {
312         // Dispatch the event
313
DescriptorViewHelper.dispatchEvent(ctx, view, desc,
314         desc.getEventDescriptor(EventDescriptor.TYPES.COMMAND),
315         (EventObject JavaDoc)event);
316
317         // Forward to the next page (if specified)
318
Object JavaDoc nextPage = desc.getParameter(NEXT_PAGE);
319         ViewBean viewbean = null;
320         if (nextPage != null) {
321         viewbean =
322             ctx.getViewBeanManager().getViewBean(nextPage.toString());
323         } else {
324             // Reload the same page.
325
viewbean = Util.getParentViewBean(view);
326         }
327
328         // Make sure we got what we want
329
if (viewbean == null) {
330         throw new FrameworkException(
331             "Unable to display next page. '"+NEXT_PAGE+
332             "' not found!", desc, view);
333         }
334
335         // Forward to the specified NEXT_PAGE (only if it forwardTo hasn't
336
// been called yet)
337
if (ctx.getRequestPhase() == ctx.SUBMIT_PHASE) {
338         viewbean.forwardTo(ctx);
339         }
340
341         // Fire After Request Event
342
DescriptorViewHelper.dispatchEvent(ctx, view, desc,
343         desc.getEventDescriptor(EventDescriptor.TYPES.AFTER_REQUEST),
344         (EventObject JavaDoc) event);
345     } catch (CompleteRequestException ex) {
346         // Let this exception be caught by the Servlet
347
throw ex;
348     } catch (FrameworkException ex) {
349         // Change to an error
350
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
351     } catch (Exception JavaDoc ex) {
352         throw new FrameworkError(ex, desc, (View)event.getSource());
353     }
354     }
355
356
357     /**
358      * This is a helper method for firing the beforeCreate event.
359      *
360      * @param ctx The RequestContext
361      * @param view The parent view (if null, childDesc will be
362      * event source)
363      * @param childDesc The descriptor of the View to be created
364      */

365     public static void beforeCreate(RequestContext ctx, DescriptorContainerView view, ViewDescriptor childDesc) {
366     try {
367         DescriptorViewHelper.dispatchEvent(
368         ctx, view, childDesc,
369         childDesc.getEventDescriptor(
370             EventDescriptor.TYPES.BEFORE_CREATE),
371         new BeforeCreateEvent(view, childDesc));
372     } catch (CompleteRequestException ex) {
373         // Let this exception be caught by the Servlet
374
throw ex;
375     } catch (FrameworkException ex) {
376         // Change to an error
377
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
378     } catch (Exception JavaDoc ex) {
379         throw new FrameworkError(ex, childDesc, view);
380     }
381     }
382
383
384     /**
385      * This is a helper method for firing the afterCreate event.
386      *
387      * @param ctx The RequestContext
388      * @param view The View just created
389      * @param childDesc The descriptor of the View just created
390      */

391     public static void afterCreate(RequestContext ctx, View view, ViewDescriptor childDesc) {
392     try {
393         DescriptorViewHelper.dispatchEvent(
394         ctx, view, childDesc,
395         childDesc.getEventDescriptor(
396             EventDescriptor.TYPES.AFTER_CREATE),
397         new AfterCreateEvent(view, childDesc));
398     } catch (CompleteRequestException ex) {
399         // Let this exception be caught by the Servlet
400
throw ex;
401     } catch (FrameworkException ex) {
402         // Change to an error
403
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
404     } catch (Exception JavaDoc ex) {
405         throw new FrameworkError(ex, childDesc, view);
406     }
407     }
408
409
410     //////////////////////////////////////////////////////////////////////
411
// Event Methods //
412
//////////////////////////////////////////////////////////////////////
413

414     /**
415      * Helper method to be used by all DescriptorContainerView
416      * implementations.
417      */

418     public static void beginDisplay(DescriptorContainerView view, DisplayEvent event) throws ModelControlException {
419     if ((view.getParent() != null) &&
420         (view.getParent() instanceof DescriptorContainerView)) {
421         // Skip this one b/c container views invoke beginDisplay on their
422
// own + parent invokes beginChildDisplay. Don't do handlers 2x
423
//
424
// NOTE: we DO NOT skip Views in which their parent is NOT a
425
// DescriptorContainerView... since thier container is NOT a
426
// DescriptorContainerView, the container would NOT have performed
427
// the declared beginDisplay handlers.
428
return;
429     }
430     try {
431         // Dispatch Begin Display Event Handlers (and mappings)
432
ViewDescriptor vd = view.getViewDescriptor();
433         DescriptorViewHelper.dispatchEvent(
434         view.getRequestContext(),
435         view,
436         vd,
437         vd.getEventDescriptor(EventDescriptor.TYPES.BEGIN_DISPLAY),
438         (EventObject JavaDoc)event);
439     } catch (CompleteRequestException ex) {
440         // Let this exception be caught by the Servlet
441
throw ex;
442     } catch (FrameworkException ex) {
443         // Change to an error
444
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
445     } catch (Exception JavaDoc ex) {
446         throw new FrameworkError(ex, view.getViewDescriptor(), view);
447     }
448     }
449
450
451     /**
452      *
453      */

454     public static boolean beginChildDisplay(DescriptorContainerView view, ChildDisplayEvent event) throws ModelControlException {
455     Object JavaDoc result = null;
456
457     String JavaDoc childName = event.getChildName();
458
459     // Get the child descriptor that defines the EventHandlers
460
ViewDescriptor childDesc =
461         view.getViewDescriptor().getChildDescriptor(childName);
462     if (childDesc != null) {
463         // NOTE: tags like CCHtmlHeaderTag do not have corresponding View's
464
// NOTE: For these, we will just use the container
465
View child = null;
466         try {
467         child = (childName == null) ? (view) : (view.getChild(childName));
468         } catch (Exception JavaDoc ex) {
469         }
470
471         // If we couldn't obtain a child, use container
472
if (child == null) {
473         child = view;
474         }
475
476         try {
477         result = DescriptorViewHelper.dispatchEvent(
478             view.getRequestContext(),
479             child,
480             childDesc,
481             childDesc.getEventDescriptor(
482                 EventDescriptor.TYPES.BEGIN_DISPLAY),
483             (EventObject JavaDoc)event);
484         } catch (CompleteRequestException ex) {
485         // Let this exception be caught by the Servlet
486
throw ex;
487         } catch (FrameworkException ex) {
488         // Change to an error
489
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
490         } catch (Exception JavaDoc ex) {
491         throw new FrameworkError(ex, childDesc, child);
492         }
493     }
494
495     boolean retVal = true;
496     if (result instanceof Boolean JavaDoc) {
497         retVal = ((Boolean JavaDoc)result).booleanValue();
498     }
499     return retVal;
500     }
501
502
503     /**
504      *
505      */

506     public static String JavaDoc endChildDisplay(DescriptorContainerView view, ChildContentDisplayEvent event) throws ModelControlException {
507     Object JavaDoc result = null;
508
509     // Get the child descriptor that defines the EventHandlers
510
String JavaDoc childName = event.getChildName();
511
512     ViewDescriptor childDesc =
513         view.getViewDescriptor().getChildDescriptor(childName);
514     if (childDesc != null) {
515         // NOTE: tags like CCHtmlHeaderTag do not have corresponding View's
516
// NOTE: For these, we will just use the container
517
View child = null;
518         try {
519         child = (childName == null) ? (view) : (view.getChild(childName));
520         } catch (Exception JavaDoc ex) {
521         }
522
523         // If we couldn't obtain a child, use container
524
if (child == null) {
525         child = view;
526         }
527
528         try {
529         result = DescriptorViewHelper.dispatchEvent(
530             view.getRequestContext(),
531             child,
532             childDesc,
533             childDesc.getEventDescriptor(
534                 EventDescriptor.TYPES.END_DISPLAY),
535             (EventObject JavaDoc)event);
536         } catch (CompleteRequestException ex) {
537         // Let this exception be caught by the Servlet
538
throw ex;
539         } catch (FrameworkException ex) {
540         // Change to an error
541
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
542         } catch (Exception JavaDoc ex) {
543         throw new FrameworkError(ex, childDesc, child);
544         }
545     }
546     return (result == null) ? event.getContent() : result.toString();
547     }
548
549
550     /**
551      *
552      */

553     public static void endDisplay(DescriptorContainerView view, DisplayEvent event) {
554     if ((view.getParent() != null) &&
555         (view.getParent() instanceof DescriptorContainerView)) {
556         // Skip this one b/c container views invoke EndDisplay on their
557
// own + parent invokes endChildDisplay. Don't do handlers 2x
558
return;
559     }
560     try {
561         ViewDescriptor vd = view.getViewDescriptor();
562         DescriptorViewHelper.dispatchEvent(
563         view.getRequestContext(),
564         view,
565         vd,
566         vd.getEventDescriptor(EventDescriptor.TYPES.END_DISPLAY),
567         (EventObject JavaDoc)event);
568     } catch (CompleteRequestException ex) {
569         // Let this exception be caught by the Servlet
570
throw ex;
571     } catch (FrameworkException ex) {
572         // Change to an error
573
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
574     } catch (Exception JavaDoc ex) {
575         throw new FrameworkError(ex, view.getViewDescriptor(), view);
576     }
577     }
578
579
580     /**
581      *
582      */

583     public static boolean endWizard(ViewDescriptor viewDesc, WizardEvent event) throws ModelControlException {
584     Object JavaDoc result = null;
585         try {
586         // Dispatch Begin Display Event Handlers (and mappings)
587
result = DescriptorViewHelper.dispatchEvent(
588         event.getRequestContext(),
589         null,
590         viewDesc,
591         viewDesc.getEventDescriptor(EventDescriptor.TYPES.END_WIZARD),
592         new EventObject JavaDoc(event));
593     } catch (CompleteRequestException ex) {
594         // Let this exception be caught by the Servlet
595
throw ex;
596     } catch (FrameworkException ex) {
597         // Change to an error
598
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
599     } catch (Exception JavaDoc ex) {
600         throw new FrameworkError(ex, viewDesc, null);
601     }
602
603     boolean retVal = true;
604     if (result instanceof Boolean JavaDoc) {
605         retVal = ((Boolean JavaDoc)result).booleanValue();
606     }
607         return retVal;
608     }
609
610
611     /**
612      *
613      */

614     public static boolean nextWizardStep(DescriptorContainerView view, WizardEvent event) throws ModelControlException {
615     Object JavaDoc result = null;
616     try {
617         ViewDescriptor vd = view.getViewDescriptor();
618         result = DescriptorViewHelper.dispatchEvent(
619         view.getRequestContext(),
620         view,
621         vd,
622         vd.getEventDescriptor(EventDescriptor.TYPES.NEXT_WIZARD_STEP),
623         new EventObject JavaDoc(event));
624     } catch (CompleteRequestException ex) {
625         // Let this exception be caught by the Servlet
626
throw ex;
627     } catch (FrameworkException ex) {
628         // Change to an error
629
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
630     } catch (Exception JavaDoc ex) {
631         throw new FrameworkError(ex, view.getViewDescriptor(), view);
632     }
633     boolean retVal = true;
634     if (result instanceof Boolean JavaDoc) {
635         retVal = ((Boolean JavaDoc)result).booleanValue();
636     }
637         return retVal;
638     }
639
640
641     /**
642      *
643      */

644     public static boolean prevWizardStep(DescriptorContainerView view, WizardEvent event) throws ModelControlException {
645     Object JavaDoc result = null;
646     try {
647         ViewDescriptor vd = view.getViewDescriptor();
648         result = DescriptorViewHelper.dispatchEvent(
649         view.getRequestContext(),
650         view,
651         vd,
652         vd.getEventDescriptor(EventDescriptor.TYPES.PREV_WIZARD_STEP),
653         new EventObject JavaDoc(event));
654     } catch (CompleteRequestException ex) {
655         // Let this exception be caught by the Servlet
656
throw ex;
657     } catch (FrameworkException ex) {
658         // Change to an error
659
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
660     } catch (Exception JavaDoc ex) {
661         throw new FrameworkError(ex, view.getViewDescriptor(), view);
662     }
663     boolean retVal = true;
664     if (result instanceof Boolean JavaDoc) {
665         retVal = ((Boolean JavaDoc)result).booleanValue();
666     }
667         return retVal;
668     }
669
670
671     /**
672      *
673      */

674     public static boolean goToWizardStep(DescriptorContainerView view, WizardEvent event) throws ModelControlException {
675     Object JavaDoc result = null;
676     try {
677         ViewDescriptor vd = view.getViewDescriptor();
678         result = DescriptorViewHelper.dispatchEvent(
679         view.getRequestContext(),
680         view,
681         vd,
682         vd.getEventDescriptor(EventDescriptor.TYPES.GOTO_WIZARD_STEP),
683         new EventObject JavaDoc(event));
684     } catch (CompleteRequestException ex) {
685         // Let this exception be caught by the Servlet
686
throw ex;
687     } catch (FrameworkException ex) {
688         // Change to an error
689
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
690     } catch (Exception JavaDoc ex) {
691         throw new FrameworkError(ex, view.getViewDescriptor(), view);
692     }
693     boolean retVal = true;
694     if (result instanceof Boolean JavaDoc) {
695         retVal = ((Boolean JavaDoc)result).booleanValue();
696     }
697         return retVal;
698     }
699
700
701     /**
702      *
703      */

704     public static boolean finishWizardStep(DescriptorContainerView view, WizardEvent event) throws ModelControlException {
705     Object JavaDoc result = null;
706     try {
707         ViewDescriptor vd = view.getViewDescriptor();
708         result = DescriptorViewHelper.dispatchEvent(
709         view.getRequestContext(),
710         view,
711         vd,
712         vd.getEventDescriptor(EventDescriptor.TYPES.FINISH_WIZARD_STEP),
713         new EventObject JavaDoc(event));
714     } catch (CompleteRequestException ex) {
715         // Let this exception be caught by the Servlet
716
throw ex;
717     } catch (Exception JavaDoc ex) {
718         throw new FrameworkException(ex, view.getViewDescriptor(), view);
719     }
720     boolean retVal = true;
721     if (result instanceof Boolean JavaDoc) {
722         retVal = ((Boolean JavaDoc)result).booleanValue();
723     }
724         return retVal;
725     }
726
727
728     /**
729      *
730      */

731     public static boolean cancelWizardStep(DescriptorContainerView view, WizardEvent event) throws ModelControlException {
732     Object JavaDoc result = null;
733     try {
734         ViewDescriptor vd = view.getViewDescriptor();
735         result = DescriptorViewHelper.dispatchEvent(
736         view.getRequestContext(),
737         view,
738         vd,
739         vd.getEventDescriptor(EventDescriptor.TYPES.CANCEL_WIZARD_STEP),
740         new EventObject JavaDoc(event));
741     } catch (CompleteRequestException ex) {
742         // Let this exception be caught by the Servlet
743
throw ex;
744     } catch (FrameworkException ex) {
745         // Change to an error
746
throw new FrameworkError(ex, ex.getResponsibleViewDescriptor(), ex.getResponsibleView());
747     } catch (Exception JavaDoc ex) {
748         throw new FrameworkError(ex, view.getViewDescriptor(), view);
749     }
750     boolean retVal = true;
751     if (result instanceof Boolean JavaDoc) {
752         retVal = ((Boolean JavaDoc)result).booleanValue();
753     }
754         return retVal;
755     }
756
757
758     /**
759      * This method performs mappings. Besides the RquestContext, a List of
760      * MappingDescriptor objects is required. This method will iterate over
761      * the MappingDescriptor objects. For each MappingDescriptor, a source
762      * object will be obtained and set on the target as described by the
763      * MappingDescriptor.
764      *
765      * @param reqCtx The RequestContext
766      * @param mappings The Mappings to perform
767      * /
768     protected static void doMappings(RequestContext reqCtx, List mappings) {
769     if ((mappings == null) || (mappings.size() < 1)) {
770         return;
771     }
772     MappingDescriptor mapping = null;
773     Object source = null;
774
775     Iterator iter = mappings.iterator();
776     while (iter.hasNext()) {
777         // Iterate through each mapping and push the source object
778         // to the target
779         mapping = (MappingDescriptor)iter.next();
780         if (mapping.hasPermission(reqCtx)) {
781         try {
782             source = mapping.getSource().getMappedSource(
783             reqCtx, mapping, mapping.getSourceParameters());
784             mapping.getTarget().setMappedTarget(
785             reqCtx, mapping, mapping.getTargetParameters(), source);
786         } catch (Exception ex) {
787             if (mapping == null) {
788             throw new FrameworkException("(null) MappingDescriptor object included in list of mappings!", ex);
789             }
790             Object src = mapping.getSource();
791             if (src == null) {
792             src = "(null)";
793             } else {
794             src = src.getClass().getName();
795             }
796             Object target = mapping.getTarget();
797             if (target == null) {
798             target = "(null)";
799             } else {
800             target = target.getClass().getName();
801             }
802             throw new FrameworkException("Mapping from '"+src+"' to '"+target+"' failed!", ex);
803         }
804         }
805     }
806     }
807     */

808
809
810     /**
811      * This method handles the dispatching of Events. The given
812      * EventDescriptor will contain zero or more event handlers. Each of
813      * these event handlers are invoked sequentially. If 1 or more of these
814      * event handlers provide a return value, the last non-null return value
815      * will be returned from this method. Before and after each event handler
816      * is invoked, before and after mappings are performed respectively.
817      *
818      * @param reqCtx The RequestContext
819      *
820      * @param view The View to pass to the handler
821      *
822      * @param eventDesc The EventDescriptor which describes the event
823      * and how to handle it
824      *
825      * @param event The event itself (generated interally by JATO)
826      *
827      * @return Some events have return values. If one of the handlers returns
828      * a value, then that value will be returned. If more than 1
829      * handler returns a value, the last non-null value will be
830      * returned.
831      */

832     public static Object JavaDoc dispatchEvent(RequestContext reqCtx, View view, ViewDescriptor vd, EventDescriptor eventDesc, EventObject JavaDoc event) {
833     Object JavaDoc result = null;
834
835     // Make sure we have something to do
836
if (eventDesc == null) {
837         return result;
838     }
839     try {
840         // Invoke all the handlers
841
result = invokeHandlers(reqCtx, view, vd, event, eventDesc.getEventHandlers());
842     } catch (Exception JavaDoc ex) {
843         if (ex instanceof java.lang.reflect.InvocationTargetException JavaDoc) {
844         // Since reflection is used to invoke the handler, our exception will be
845
// wrapped... check for CompleteRequestException, if found throw it to
846
// be caught by the Servlet
847
Throwable JavaDoc root = ex.getCause();
848         if (root instanceof CompleteRequestException) {
849             throw (CompleteRequestException) root;
850         }
851         }
852         throw new FrameworkException(
853         ex.getClass().getName() + " while attempting to " +
854         "process a '" + eventDesc.getType() + "' event for '" +
855         eventDesc.getParent().getName() + "'.",
856         ex, eventDesc.getParent(), view);
857     }
858
859     // Return the result (if any)
860
return result;
861     }
862
863
864     /**
865      *
866      */

867     private static Object JavaDoc invokeHandlers(RequestContext ctx, View view, ViewDescriptor vd, EventObject JavaDoc event, List JavaDoc useHandlerDescs) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
868     Object JavaDoc result = null;
869     Object JavaDoc retVal = null;
870
871     Iterator JavaDoc it = useHandlerDescs.iterator();
872     UseHandlerDescriptor handler = null;
873     while (it.hasNext()) {
874         handler = (UseHandlerDescriptor)it.next();
875         retVal = invokeHandler(ctx, handler, view, vd, event);
876         if (retVal != null) {
877         result = retVal;
878         }
879     }
880
881     return result;
882     }
883
884
885     /**
886      * This method creates a HandlerContext. If for any reason you want to
887      * customize how a handler is created, let me know I can add this feature.
888      */

889     protected static HandlerContext createHandlerContext(UseHandlerDescriptor useHandler, HandlerDescriptor handler, View view, ViewDescriptor vd, EventObject JavaDoc event) {
890     return new HandlerContextImpl(useHandler, handler, view, vd, event);
891     }
892
893
894     /**
895      *
896      */

897     protected static Object JavaDoc invokeHandler(RequestContext ctx, UseHandlerDescriptor handler,
898             View view, ViewDescriptor vd, EventObject JavaDoc event)
899         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
900             
901     HandlerDescriptor handlerDef = handler.getHandlerDescriptor();
902     Method JavaDoc method = handlerDef.getHandlerMethod();
903                 
904     Object JavaDoc retVal = null;
905     Object JavaDoc result = null;
906
907     // Logging at FINER for tracing information
908
if (LogUtil.isLoggable(LogUtil.FINER)) {
909         LogUtil.log(LogUtil.FINER, "trace.invoke", handlerDef.getName());
910     }
911
912     // Check permissions
913
if (handler.hasPermission(vd) && handlerDef.hasPermission(vd)) {
914         // First execute all child handlers
915
result = invokeHandlers(
916         ctx, view, vd, event, handlerDef.getChildHandlerDescriptors());
917
918         // Create the HandlerContext object
919
HandlerContext handlerCtx = createHandlerContext(handler, handlerDef, view, vd, event);
920
921         // Only attempt to do this if there is a handler method, there
922
// might only be child handlers
923
if (method != null) {
924 // FIXME: We might want to consider using static methods, or providing an
925
// FIXME: interface for getting an instance of these classes... not sure if we
926
// FIXME: want to share instances (singleton) or not. For now I'll just create
927
// FIXME: a new instance each time.
928

929         // Get the class that contains the method
930
Object JavaDoc instance = method.getDeclaringClass().newInstance();
931
932         // Logging at FINER for tracing information
933
if (LogUtil.isLoggable(LogUtil.FINER)) {
934             LogUtil.log(LogUtil.FINER, "trace.invoke",
935             instance.getClass().getName()+"."+method.getName());
936         }
937
938         retVal = method.invoke(instance, new Object JavaDoc[] {ctx, handlerCtx});
939         if (retVal != null) {
940             result = retVal;
941         }
942         }
943     } else if (LogUtil.isLoggable(LogUtil.FINER)) {
944         // Tracing at FINER
945
String JavaDoc ifName = (vd.getParent() == null) ?
946         ("") : (vd.getParent().getName()+".");
947         ifName += vd.getName()+"."+handlerDef.getName();
948         LogUtil.log(LogUtil.FINER, "trace.ifFailed", ifName);
949     }
950
951     // Return the result (null if no result)
952
return result;
953     }
954
955
956     /**
957      *
958      */

959     protected static void verifyClassExists(ServletContext JavaDoc sc, String JavaDoc fileName) throws ClassNotFoundException JavaDoc {
960     String JavaDoc className = null;
961     if (fileName.endsWith(".jsp")) {
962         className = fileName.substring(0,fileName.lastIndexOf(".jsp")) + "_jsp";
963         className = className.replaceAll("/", "._");
964         className = "_jasper" + className;
965
966         try {
967         Class JavaDoc clazz = Class.forName(className);
968         return;
969         } catch (ClassNotFoundException JavaDoc ex) {
970         } catch (Exception JavaDoc ex) {
971         }
972     }
973     if (sc != null) {
974         // look for the file itself
975
String JavaDoc path = sc.getRealPath(fileName);
976         File JavaDoc f = new File JavaDoc(path);
977         if (f.exists() == false) {
978         if (LogUtil.isLoggable(LogUtil.FINE)) {
979             if (className != null) {
980             LogUtil.log(LogUtil.FINE, "framework.classNotFound",
981                 className);
982             } else {
983             LogUtil.log(LogUtil.FINE, "framework.fileNotFound",
984                 path);
985             }
986         }
987         throw new ClassNotFoundException JavaDoc((className == null) ? path : className);
988         }
989     } else {
990         throw new RuntimeException JavaDoc("ServletContext is null!");
991     }
992     }
993
994
995     /**
996      *
997      */

998     public static final String JavaDoc NEXT_PAGE = "nextPage";
999
1000
1001    /**
1002     *
1003     */

1004    public static final String JavaDoc COMMAND_FIELD_DESCRIPTOR = "CommandField";
1005}
1006
Popular Tags