KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > workflow > LayoutPlugin


1 package fr.improve.struts.taglib.layout.workflow;
2
3 import javax.servlet.ServletException JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.apache.struts.action.ActionMapping;
8 import org.apache.struts.action.ActionServlet;
9 import org.apache.struts.action.PlugIn;
10 import org.apache.struts.config.ActionConfig;
11 import org.apache.struts.config.ModuleConfig;
12
13 import fr.improve.struts.taglib.layout.skin.Skin;
14 import fr.improve.struts.taglib.layout.sort.SortAction;
15 import fr.improve.struts.taglib.layout.treeview.TreeviewAction;
16
17 /**
18  * Layout plugin for struts 1.1+.
19  *
20  * The plugin only allows to specify a different configuration file.
21  * People still using struts 1.0 can subclass the ActionServlet and
22  * call Skin.setResourcesName("the_skin_properties_file_name") in the init method.
23  *
24  * @author jnribette
25  */

26 public class LayoutPlugin implements PlugIn {
27     /**
28      * Log
29      */

30     private static final Log LOG = LogFactory.getLog(LayoutPlugin.class);
31     
32     /**
33      * Skin name.
34      * Default is "Struts-Layout".
35      */

36     private String JavaDoc skinResources = "Struts-Layout";
37     
38     /**
39      * Destroy the module.
40      */

41     public void destroy() {
42         // nothing to destroy
43
LOG.debug("Destroying Struts-Layout plugin");
44     }
45     
46     private static final String JavaDoc SORT_PATH = "/sort";
47     private static final String JavaDoc TREEVIEW_PATH = "/treeview";
48     
49     /**
50      * Initialize Struts-Layout :
51      * Reads in struts-config the name of the Skin name.
52      * Add the sort and treeview actions if there are not alreay here.
53      */

54     public void init(ActionServlet in_servlet, ModuleConfig in_config)
55         throws ServletException JavaDoc {
56         
57             // Set the skin properties file to use.
58
LOG.debug("Setting skin properties name to " + skinResources);
59             Skin.setResourcesName(skinResources);
60             
61             // Check for Struts-Layout actions.
62
boolean sortAction = false;
63             boolean treeAction = false;
64             boolean sortPath = false;
65             boolean treePath = false;
66             ActionConfig[] configs = in_config.findActionConfigs();
67             for (int i=0;i<configs.length;i++) {
68                 if (SortAction.class.getName().equals(configs[i].getType())) {
69                     sortAction = true;
70                     LOG.debug("Found Struts-Layout sort action mapped to " + configs[i].getPath());
71                 }
72                 if (SORT_PATH.equals(configs[i].getPath())) {
73                     sortPath = true;
74                 }
75                 if (TreeviewAction.class.getName().equals(configs[i].getType())) {
76                     treeAction = true;
77                     LOG.debug("Found Struts-Layout treeview action mapped to " + configs[i].getPath());
78                 }
79                 if (TREEVIEW_PATH.equals(configs[i].getPath())) {
80                     treePath = true;
81                 }
82             }
83             
84             // Add sort action.
85
if (!sortAction) {
86                 if (!sortPath) {
87                     ActionConfig config = new ActionMapping();
88                     config.setPath(SORT_PATH);
89                     config.setType(SortAction.class.getName());
90                     in_config.addActionConfig(config);
91                     LOG.debug("Mapping Struts-Layout sort action to /sort");
92                 } else {
93                     LOG.warn("Don't mapping Struts-Layout sort action : /sort is already used");
94                 }
95             }
96             
97             // Add tree action.
98
if (!treeAction) {
99                 if (!treePath) {
100                     ActionConfig config = new ActionMapping();
101                     config.setPath(TREEVIEW_PATH);
102                     config.setType(TreeviewAction.class.getName());
103                     in_config.addActionConfig(config);
104                     LOG.debug("Mapping Struts-Layout sort action to /treeview");
105                 } else {
106                     LOG.warn("Don't mapping Struts-Layout treeview action : /treeview is already used");
107                 }
108             }
109             
110             LOG.debug("Struts-Layout plugin initialized");
111     }
112         
113     /**
114      * Setter for initialization of the skin name.
115      * @param in_skinResources
116      */

117     public void setSkinResources(String JavaDoc in_skinResources) {
118         skinResources = in_skinResources;
119     }
120
121 }
122
Popular Tags