KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > actions > AbstractPropertiesAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.properties.actions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.commons.beanutils.BeanUtils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.struts.Globals;
36 import org.apache.struts.action.ActionForm;
37 import org.apache.struts.action.ActionForward;
38 import org.apache.struts.action.ActionMapping;
39 import org.apache.struts.action.ActionMessage;
40 import org.apache.struts.action.ActionMessages;
41
42 import com.sslexplorer.boot.AbstractPropertyKey;
43 import com.sslexplorer.boot.PropertyClass;
44 import com.sslexplorer.boot.PropertyDefinition;
45 import com.sslexplorer.boot.PropertyDefinitionCategory;
46 import com.sslexplorer.boot.PropertyList;
47 import com.sslexplorer.boot.Util;
48 import com.sslexplorer.core.CoreUtil;
49 import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
50 import com.sslexplorer.properties.Property;
51 import com.sslexplorer.properties.PropertyItem;
52 import com.sslexplorer.properties.forms.AbstractPropertiesForm;
53 import com.sslexplorer.properties.forms.PropertiesForm;
54 import com.sslexplorer.security.LogonControllerFactory;
55 import com.sslexplorer.security.SessionInfo;
56 import com.sslexplorer.security.User;
57
58 public abstract class AbstractPropertiesAction extends AuthenticatedDispatchAction {
59     static Log log = LogFactory.getLog(AbstractPropertiesAction.class);
60
61     public AbstractPropertiesAction() {
62         super();
63     }
64
65     public ActionForward reset(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
66                     throws Exception JavaDoc {
67         AbstractPropertiesForm f = (AbstractPropertiesForm) form;
68         f.clearValues();
69         User user = isSetupMode() ? null : LogonControllerFactory.getInstance().getUser((HttpServletRequest JavaDoc) request);
70         return rebuildItems(mapping, f.getParentCategory(), f, request, user);
71     }
72
73     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
74                                      HttpServletResponse JavaDoc response) throws Exception JavaDoc {
75         // Initialise form
76
AbstractPropertiesForm pf = (AbstractPropertiesForm) form;
77         pf.clearValues();
78         pf.setUpdateAction(mapping.getPath() + ".do");
79         pf.setInput(mapping.getInput());
80
81         // Now try the struts supplied action mapping parameter
82
if (mapping.getParameter() != null && !mapping.getParameter().equals("")) {
83             PropertyList pl = new PropertyList(mapping.getParameter());
84             Properties JavaDoc pr = pl.getAsNameValuePairs();
85             BeanUtils.populate(pf, pr);
86         }
87
88         if ("changeSelectedCategory".equalsIgnoreCase(pf.getActionTarget())) {
89             pf.setSelectedCategory(pf.getNewSelectedCategory());
90         }
91
92         // Build and display
93
return rebuildItems(mapping, pf.getParentCategory(), pf, request, getSessionInfo(request).getUser());
94     }
95
96     public ActionForward changeSelectedCategory(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
97                                                 HttpServletResponse JavaDoc response) throws Exception JavaDoc {
98         if (log.isDebugEnabled())
99             log.debug("Storing properties");
100         AbstractPropertiesForm f = (AbstractPropertiesForm) form;
101         f.storeItems();
102         User user = isSetupMode() ? null : LogonControllerFactory.getInstance().getUser((HttpServletRequest JavaDoc) request);
103         f.setSelectedCategory(f.getNewSelectedCategory());
104         f.setNewSelectedCategory(-1);
105         return rebuildItems(mapping, f.getParentCategory(), f, request, user);
106     }
107
108     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
109                     throws Exception JavaDoc {
110         AbstractPropertiesForm pf = (AbstractPropertiesForm) form;
111         User user = isSetupMode() ? null : LogonControllerFactory.getInstance().getUser((HttpServletRequest JavaDoc) request);
112         pf.setSelectedCategory(-1);
113         pf.clearValues();
114         int newCategory = pf.popCategory();
115         pf.setParentCategory(newCategory);
116         ActionForward fwd = rebuildItems(mapping, newCategory, pf, request, user);
117         ActionForward cancel = mapping.findForward("cancel");
118         return cancel != null ? cancel : fwd;
119     }
120
121     public ActionForward displayCategory(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
122                                          HttpServletResponse JavaDoc response) throws Exception JavaDoc {
123         AbstractPropertiesForm pf = (AbstractPropertiesForm) form;
124         User user = isSetupMode() ? null : LogonControllerFactory.getInstance().getUser((HttpServletRequest JavaDoc) request);
125         pf.setSelectedCategory(-1);
126         pf.pushCategory(pf.getParentCategory());
127         pf.setParentCategory(pf.getNewSelectedCategory());
128         pf.setNewSelectedCategory(-1);
129         return rebuildItems(mapping, pf.getParentCategory(), pf, request, user);
130     }
131
132     public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
133                     throws Exception JavaDoc {
134         SessionInfo sessionInfo = getSessionInfo(request);
135         boolean restartRequired = false;
136         
137         // Temporarily store the properties
138
if (log.isDebugEnabled())
139             log.debug("Commiting properties");
140         AbstractPropertiesForm f = (AbstractPropertiesForm) form;
141         f.storeItems();
142
143         // Check for save abilitiy
144
if (!((PropertiesForm) form).getEnabled()) {
145             throw new Exception JavaDoc("Disabled.");
146         }
147
148         // Set the properties
149
for (Iterator JavaDoc i = f.storedItems(); i.hasNext();) {
150             PropertyItem item = (PropertyItem) i.next();
151             PropertyDefinition def = item.getDefinition();
152             if(def.isRestartRequired()) {
153                 restartRequired = true;
154             }
155             String JavaDoc val = null;
156             val = String.valueOf(item.getPropertyValue());
157             if (log.isDebugEnabled())
158                 log.debug("Setting '" + def.getName() + "' to '" + val + "'");
159             Property.setProperty(createKey(item.getDefinition(), f, sessionInfo), val, sessionInfo);
160         }
161         CoreUtil.resetMainNavigation(request.getSession());
162
163         // Messages
164
ActionMessages messages = new ActionMessages();
165         messages.add(Globals.MESSAGES_KEY, new ActionMessage("message.propertiesSaved"));
166         saveMessages(request, messages);
167
168         // Clean up and forward
169
f.clearValues();
170         ActionForward fwd;
171         if (f.getForwardTo() != null && !f.getForwardTo().equals("")) {
172             fwd = new ActionForward(f.getForwardTo(), f.isRedirect());
173         } else {
174             fwd = cancel(mapping, form, request, response);
175         }
176         if(restartRequired) {
177             String JavaDoc orig = fwd.getPath();
178             fwd = mapping.findForward("restartRequired");
179             fwd = CoreUtil.addParameterToForward(fwd, "no", orig);
180         }
181         return fwd;
182     }
183
184     protected String JavaDoc getMethodName(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
185                                    HttpServletResponse JavaDoc response, String JavaDoc parameter) throws Exception JavaDoc {
186         return request.getParameter("actionTarget");
187     }
188
189     protected ActionForward rebuildItems(ActionMapping mapping, int parentCategory, AbstractPropertiesForm pf, HttpServletRequest JavaDoc request, User user)
190                     throws Exception JavaDoc {
191         SessionInfo sessionInfo = getSessionInfo(request);
192         List JavaDoc<PropertyDefinitionCategory> categoryDefinitions = new ArrayList JavaDoc<PropertyDefinitionCategory>();
193         List JavaDoc<PropertyDefinitionCategory> subCategories = new ArrayList JavaDoc<PropertyDefinitionCategory>();
194         Collection JavaDoc<PropertyDefinitionCategory> sourceCategories = null;
195         List JavaDoc<PropertyItem> propertyItems = new ArrayList JavaDoc<PropertyItem>();
196         for (PropertyClass propertyClass : pf.getPropertyClasses()) {
197
198             /*
199              * If no parent category is supplied, then assume all categories in
200              * the class, otherwise get all the child categories of the supplied
201              * one
202              */

203             if (parentCategory == 0) {
204                 sourceCategories = propertyClass.getCategories();
205             } else {
206                 PropertyDefinitionCategory category = propertyClass.getPropertyDefinitionCategory(parentCategory);
207                 if (category != null) {
208                     sourceCategories = category.getCategories();
209                 }
210                 else {
211                     sourceCategories = null;
212                 }
213             }
214
215             if (sourceCategories != null) {
216                 for (PropertyDefinitionCategory def : sourceCategories) {
217                     if(def.isEnabled()) {
218                         if (def.size() > 0) {
219                             if(!subCategories.contains(def)) {
220                                 // Only add the subcategory if it has at least one enabled subcategory within it
221
for(PropertyDefinitionCategory subcat : def.getCategories()) {
222                                     if(subcat.isEnabled()) {
223                                         subCategories.add(def);
224                                         break;
225                                     }
226                                 }
227                             }
228                         } else {
229                             if(!categoryDefinitions.contains(def)) {
230                                 categoryDefinitions.add(def);
231                                 if (pf.getSelectedCategory() == -1) {
232                                     pf.setSelectedCategory(def.getId());
233                                 }
234                             }
235                         }
236                     }
237                 }
238             }
239
240             for (PropertyDefinition def : propertyClass.getDefinitions()) {
241                 if (!def.isHidden() && def.getCategory() == pf.getSelectedCategory()) {
242                     propertyItems.add(pf.retrieveItem(def.getName(), new PropertyItem(this, request, def, Property
243                                     .getProperty(createKey(def, pf, sessionInfo)))));
244                 }
245             }
246         }
247         pf.setParentCategory(parentCategory);
248         pf.setSubCategories(subCategories);
249         pf.setCategoryDefinitions(categoryDefinitions);
250         Collections.sort(propertyItems);
251         pf.setPropertyItems(propertyItems.toArray(new PropertyItem[propertyItems.size()]));
252
253         if(propertyItems.size() != 0 || subCategories.size() != 0) {
254             return mapping.findForward("display");
255         }
256         else {
257             log.warn("No categories or definitions to display. May be the result of a session timeout.");
258             return mapping.findForward("home");
259         }
260     }
261
262     public abstract AbstractPropertyKey createKey(PropertyDefinition definition, AbstractPropertiesForm form, SessionInfo sessionInfo);
263 }
Popular Tags