1 package org.apache.maven.struts;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.File;
21 import java.io.InputStream;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.jar.JarEntry;
27
28 import org.apache.maven.j2ee.WarFile;
29
30 import org.dom4j.Document;
31 import org.dom4j.DocumentException;
32 import org.dom4j.Element;
33 import org.dom4j.io.SAXReader;
34
35 /**
36 * Encapsulates a Struts 1.0 War File. Holds functionality to access Struts
37 * specific resources and data in the war.
38 * @author dion
39 * @version $Id: Struts10WarFile.java 170200 2005-05-15 06:24:19Z brett $
40 */
41 public class Struts10WarFile extends WarFile
42 {
43 /** Default location of Struts configuration file in the war */
44 public static final String DEFAULT_CONFIG = "WEB-INF/struts-config.xml";
45 /** property for the location of the Struts configuration file in the war */
46 private String config = Struts10WarFile.DEFAULT_CONFIG;
47 /** Default name of the action servlet in web.xml */
48 public static final String DEFAULT_ACTIONSERVLET_NAME = "action";
49 /** name of the action servlet in web.xml */
50 private String actionServletName =
51 Struts10WarFile.DEFAULT_ACTIONSERVLET_NAME;
52
53 /**
54 * Creates a new instance of Struts10WarFile
55 * @param name the {@link File file} name of a war file containing a Struts
56 * web application
57 * @throws IOException when an I/O error occurs
58 */
59 public Struts10WarFile(String name) throws IOException
60 {
61 super(name);
62 }
63
64 /**
65 * Creates a new instance of Struts10WarFile
66 * @param name the {@link File file} name of a war file containing a Struts
67 * web application
68 * @param verify whether or not to verify the war file if it is signed
69 * @throws IOException when an I/O error occurs */
70 public Struts10WarFile(String name, boolean verify) throws IOException
71 {
72 super(name, verify);
73 }
74
75 /**
76 * Creates a new instance of Struts10WarFile
77 * @param warFile a J2EE .war {@link File file} containing a Struts web
78 * application
79 * @throws IOException when an I/O error occurs
80 */
81 public Struts10WarFile(File warFile) throws IOException
82 {
83 super(warFile);
84 }
85
86 /**
87 * Creates a new instance of Struts10WarFile
88 * @param warFile a J2EE .war {@link File file} containing a Struts web
89 * application
90 * @param verify whether or not to verify the war file if it is signed
91 * @throws IOException when an I/O error occurs
92 */
93 public Struts10WarFile(File warFile, boolean verify) throws IOException
94 {
95 super(warFile, verify);
96 }
97
98 /**
99 * Creates a new instance of Struts10WarFile
100 * @param warFile a J2EE .war {@link File file} containing a Struts web
101 * application
102 * @param verify whether or not to verify the war file if it is signed
103 * @param mode the mode in which the file is to be opened
104 * @throws IOException when an I/O error occurs
105 */
106 public Struts10WarFile(File warFile, boolean verify, int mode)
107 throws IOException
108 {
109 super(warFile, verify, mode);
110 }
111
112 /** Provide the location of the Struts configuration file in the war.
113 * @return Value of property config.
114 */
115 public String getConfig()
116 {
117 return config;
118 }
119
120 /** Set the location of the Struts configuration file in the war.
121 * @param config New value of property config.
122 */
123 public void setConfig(String config)
124 {
125 this.config = config;
126 }
127
128 /**
129 * Retrieves the Struts configuration (as specified by the config property)
130 * entry if it exists.
131 * @return a {@link JarEntry} for Struts config
132 */
133 public JarEntry getStrutsConfigEntry()
134 {
135 return getJarEntry(getConfig());
136 }
137
138 /** Get the Struts configuration back as a dom4j Document, for easier
139 * processing
140 * @return a {@link Document} representing the web.xml
141 * @throws IOException if there are any issues reading the web.xml
142 * or producing the xml document
143 */
144 private Document getStrutsConfig() throws IOException
145 {
146 if (getStrutsConfigEntry() == null)
147 {
148 throw new IOException("Attempted to get non-existent config");
149 }
150 try
151 {
152 SAXReader xmlReader = new SAXReader(false);
153 xmlReader.setEntityResolver(new StrutsEntityResolver());
154 InputStream configStream = getInputStream(getStrutsConfigEntry());
155 Document configXml = xmlReader.read(configStream);
156 return configXml;
157 }
158 catch (DocumentException de)
159 {
160 de.printStackTrace();
161 throw new IOException(de.getMessage());
162 }
163 }
164
165 /** retrieves the form beans defined in the struts configuration file
166 * @return a collection of {@link FormBean form beans}
167 * @throws IOException when there are problems reading from the war
168 */
169 public List getFormBeans() throws IOException
170 {
171 List formBeans = new ArrayList();
172 Document config = getStrutsConfig();
173 List formBeanNodes = config.selectNodes(
174 "/struts-config/form-beans/form-bean");
175 Element formBeanNode = null;
176 FormBean formBean = null;
177 for (Iterator nodes = formBeanNodes.iterator(); nodes.hasNext();)
178 {
179 formBeanNode = (Element) nodes.next();
180 formBean = new FormBean();
181 formBean.setClassName(formBeanNode.attributeValue("className"));
182 formBean.setName(formBeanNode.attributeValue("name"));
183 formBean.setType(formBeanNode.attributeValue("type"));
184 formBeans.add(formBean);
185 }
186
187 return formBeans;
188 }
189
190 /** retrieve the actions defined in the struts configuration file
191 * @return a {@link List] of {@link Action actions}
192 * @throws IOException when there are problems reading from the war
193 */
194 public List getActions() throws IOException
195 {
196 List actions = new ArrayList();
197 Document config = getStrutsConfig();
198 List actionNodes = config.selectNodes(
199 "/struts-config/action-mappings/action");
200 Element actionNode = null;
201 Action action = null;
202 for (Iterator nodes = actionNodes.iterator(); nodes.hasNext();)
203 {
204 actionNode = (Element) nodes.next();
205 action = new Action();
206 action.setClassName(actionNode.attributeValue("className"));
207 action.setName(actionNode.attributeValue("name"));
208 action.setPath(actionNode.attributeValue("path"));
209 action.setScope(actionNode.attributeValue("scope"));
210 action.setType(actionNode.attributeValue("type"));
211 action.setUnknown(actionNode.attributeValue("unknown"));
212 action.setValidate(actionNode.attributeValue("validate"));
213 actions.add(action);
214 }
215 return actions;
216 }
217
218 /** retrieves the type attribute of the <form-beans> element if
219 * it exists, or null otherwise
220 * @return the form-bean type attribute
221 * @throws IOException when there are problems reading from the war
222 */
223 public String getFormBeansType() throws IOException
224 {
225 String type = null;
226 Element formBeans = (Element) getStrutsConfig().selectSingleNode(
227 "/struts-config/form-beans");
228 if (formBeans != null)
229 {
230 type = formBeans.attributeValue("type");
231 }
232 return type;
233 }
234
235 /** retrieves the type attribute of the <global-forwards> element if
236 * it exists, or null otherwise
237 * @return the global-forwards type attribute
238 * @throws IOException when there are problems reading from the war
239 */
240 public String getGlobalForwardsType() throws IOException
241 {
242 String type = null;
243 Element forwards = (Element) getStrutsConfig().selectSingleNode(
244 "/struts-config/global-forwards");
245 if (forwards != null)
246 {
247 type = forwards.attributeValue("type");
248 }
249 return type;
250 }
251
252 /** retrieve the global forwards defined in the struts configuration
253 * @return a {@link List} of {@link Forward forwards}
254 * @throws IOException when there are problems reading from the war
255 */
256 public List getForwards() throws IOException
257 {
258 List forwards = new ArrayList();
259 Document config = getStrutsConfig();
260 List nodes = config.selectNodes(
261 "/struts-config/global-forwards/forward");
262 Element element = null;
263 Forward forward = null;
264 for (int index = 0; index < nodes.size(); index++)
265 {
266 element = (Element) nodes.get(index);
267 forward = new Forward();
268 forward.setName(element.attributeValue("name"));
269 forward.setPath(element.attributeValue("path"));
270 if (element.attribute("className") != null)
271 {
272 forward.setClassName(element.attributeValue("className"));
273 }
274 if (element.attribute("redirect") != null)
275 {
276 forward.setRedirect(element.attributeValue("redirect"));
277 }
278 forwards.add(forward);
279 }
280
281 return forwards;
282 }
283
284 /** retrieve the url pattern for the action servlet, or null if not found
285 * @return the <url-pattern> provided for the servlet named by
286 * {@link #getActionServletName}
287 * @throws IOException when there are problems reading from the war
288 */
289 public String getActionServletPattern() throws IOException
290 {
291 return (String) getServletMappings().get(getActionServletName());
292 }
293
294 /** Getter for property actionServletName.
295 * @return Value of property actionServletName.
296 */
297 public String getActionServletName()
298 {
299 return actionServletName;
300 }
301
302 /** Setter for property actionServletName.
303 * @param actionServletName New value of property actionServletName.
304 */
305 public void setActionServletName(String actionServletName)
306 {
307 if (actionServletName == null)
308 {
309 throw new NullPointerException("action servlet name can't be null");
310 }
311 this.actionServletName = actionServletName;
312 }
313
314 }