KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > flow > CommandBase


1 /*
2  * $Id: CommandBase.java,v 1.11 2004/06/07 20:38:42 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/flow/CommandBase.java,v $
4  */

5
6 package org.infohazard.maverick.flow;
7
8 import java.io.IOException JavaDoc;
9 import javax.servlet.*;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 /**
15  * <p>
16  * Base class for implementing a Maverick {@link Command}.
17  * Implementors must define {@link #getView}.
18  * </p>
19  * <p>
20  * CommandBase obtains a {@link View} name by calling its Controller's
21  * {@link Controller#go go} method, and then invokes the View's
22  * {@link View#go go} method.
23  * <p>
24  * <p>
25  * The {@link ModelLifetime} interface is honored.
26  * After invoking View.go, if the model object is an instance of
27  * ModelLifetime, its {@link ModelLifetime#discard discard} method is called.
28  *
29  */

30 abstract class CommandBase implements Command
31 {
32     /**
33      * <p>
34      * CommandBase logger.
35      * </p>
36      */

37     private static Log log = LogFactory.getLog(CommandBase.class);
38
39     /**
40      * <p>
41      * Reference to our {@link Controller}.
42      * </p>
43      */

44     protected Controller controller;
45
46     /**
47      * <p>
48      * Set reference to out {@link Controller}.
49      * </p>
50      */

51     public CommandBase(Controller ctl)
52     {
53         this.controller = ctl;
54     }
55
56     /**
57      *
58      */

59     public void go(MaverickContext mctx) throws IOException JavaDoc, ServletException
60     {
61         Object JavaDoc model = null;
62         
63         try
64         {
65             // There must be a controller class to distinguish between views
66
String JavaDoc viewName = this.controller.go(mctx);
67             
68             // Hold on to the model now because chained commands might replace it
69
model = mctx.getModel();
70     
71             if (log.isDebugEnabled())
72                 log.debug("Switching to view: " + viewName);
73     
74             View target = this.getView(viewName);
75             if (null == target)
76                 throw new ServletException("Controller specified view \"" + viewName
77                                             + "\", but no view with that name is defined.");
78             
79             target.go(mctx);
80         }
81         finally
82         {
83             // Allow the model to manage resources
84
// instanceof returns false if object is null
85
if (model instanceof ModelLifetime)
86                 ((ModelLifetime)model).discard();
87         }
88     }
89
90     /**
91      */

92     protected abstract View getView(String JavaDoc name);
93 }
Popular Tags