KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > servlets > CommandBasedMVCServletHandler


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.servlets;
14
15 import info.magnolia.cms.util.AlertUtil;
16 import info.magnolia.commands.CommandsManager;
17 import info.magnolia.context.Context;
18 import info.magnolia.context.MgnlContext;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.apache.commons.chain.Command;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 /**
29  * This implementation tries first to get a command form it's command catalogue. If none is found it will call the
30  * execute method of the default MVCServletHandlerImpl, which tries to execute through reflection a related method.
31  * @author Philipp Bracher
32  * @version $Revision: 6341 $ ($Author: philipp $)
33  */

34 public abstract class CommandBasedMVCServletHandler extends MVCServletHandlerImpl {
35
36     /**
37      * @param name
38      * @param request
39      * @param response
40      */

41     protected CommandBasedMVCServletHandler(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
42         super(name, request, response);
43         this.setCatalogueName(name);
44     }
45
46     /**
47      * Try to get the command from this catalogue
48      */

49     private String JavaDoc catalogueName;
50
51     /**
52      * The logger use in this class
53      */

54     private static Logger log = LoggerFactory.getLogger(CommandBasedMVCServletHandler.class);
55
56     /**
57      * Try to get the command from the catalogue
58      */

59     public String JavaDoc execute(String JavaDoc commandName) {
60         // get command from command map in JCR repository
61
Command command = findCommand(commandName);
62         if (command == null) { // not found, do in the old ways
63
if (log.isDebugEnabled()) {
64                 log.debug("can not find command named " + commandName + " in tree command map");
65             }
66             return super.execute(commandName);
67         }
68
69         if (log.isDebugEnabled()) {
70             log.debug("found command for " + commandName + ": " + command);
71         }
72
73         // now prepare the context
74
Context ctx = getCommandContext(commandName);
75
76         // execute the command
77
try {
78             command.execute(ctx);
79         }
80         catch (Exception JavaDoc e) {
81             log.error("can't execute command", e);
82             AlertUtil.setException(e);
83         }
84         return getViewNameAfterExecution(commandName, ctx);
85     }
86
87     /**
88      * Default implemenation returns the commandName itself
89      * @param commandName
90      * @param ctx
91      * @return the view name returned by this execution
92      */

93     protected String JavaDoc getViewNameAfterExecution(String JavaDoc commandName, Context ctx) {
94         return commandName;
95     }
96
97     /**
98      * Used to get the command object
99      * @param commandName
100      * @return the callable command object
101      */

102     protected Command findCommand(String JavaDoc commandName) {
103         return CommandsManager.getInstance().getCommand(this.getCatalogueName(), commandName);
104     }
105
106     /**
107      * The default implementation returns the current context
108      * @param commandName the name of the command to be called
109      * @return the context to pass to the command
110      */

111     protected Context getCommandContext(String JavaDoc commandName) {
112         return MgnlContext.getInstance();
113     }
114
115     /**
116      * @return Returns the catalogueName.
117      */

118     public String JavaDoc getCatalogueName() {
119         return this.catalogueName;
120     }
121
122     /**
123      * @param catalogueName The catalogueName to set.
124      */

125     public void setCatalogueName(String JavaDoc catalogueName) {
126         this.catalogueName = catalogueName;
127     }
128 }
129
Popular Tags