KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > webapps_deployer > orion > Default_Web_Site_Xml


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// Default_Web_Site_Xml
15
//
16
// NK 02.02.2001
17
//
18
//
19

20 package org.jahia.services.webapps_deployer.orion;
21
22
23 import org.jahia.data.xml.JahiaXmlDocument;
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.utils.xml.XMLParser;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30 import java.util.Vector JavaDoc;
31
32
33 /**
34  * Holds Informations about the Orion default-web-site.xml file
35  * Need to modify this file to activate web apps
36  * <pre>
37  * <web-site host="[ALL]" port="8080" display-name="Default Orion WebSite">
38  * <!-- The default web-app for this site, bound to the root -->
39  * <default-web-app application="default" name="defaultWebApp" />
40  * <!-- Uncomment this to activate the news app -->
41  * <!-- <web-app application="news" name="news-web" root="/news" /> -->
42  * <web-app application="AbsenceRequest" name="war-ic" root="/AbsenceRequest" />
43  * <web-app application="Bookmark" name="Bookmark" root="/Bookmark" />
44  * <!-- Access Log, where requests are logged to -->
45  * <access-log path="../log/default-web-access.log" />
46  * </web-site>
47  * </pre>
48  *
49  * @author Khue ng
50  * @version 1.0
51  */

52 public class Default_Web_Site_Xml extends JahiaXmlDocument {
53
54     /** The list of <web-app..></web-app> elements * */
55     private Vector JavaDoc m_WebApps = new Vector JavaDoc ();
56     /** The list of web-app Nodes * */
57     private Vector JavaDoc m_WebAppNodes = new Vector JavaDoc ();
58
59     private static org.apache.log4j.Logger logger =
60             org.apache.log4j.Logger.getLogger (Default_Web_Site_Xml.class);
61
62     /**
63      * Constructor
64      *
65      * @param (String) path, the full path to the server.xml file
66      */

67     public Default_Web_Site_Xml (String JavaDoc docPath) throws JahiaException {
68         super (docPath);
69         extractDocumentData ();
70
71     }
72
73
74     /**
75      * Constructor using a gived parser
76      *
77      * @param (String) path, the full path to a xml file
78      * @param (Parser) parser, the parser to use
79      */

80     public Default_Web_Site_Xml (String JavaDoc docPath, org.xml.sax.helpers.ParserAdapter JavaDoc parser)
81             throws JahiaException {
82         super (docPath, parser);
83     }
84
85
86     /**
87      * Extracts data from the web.xml file.
88      */

89     public void extractDocumentData () throws JahiaException {
90
91         logger.info (" started ");
92
93         if (m_XMLDocument == null) {
94
95             throw new JahiaException ("Default_Web_Site_Xml",
96                     "Parsed default-web-site.xml document is null",
97                     JahiaException.ERROR_SEVERITY,
98                     JahiaException.SERVICE_ERROR);
99         }
100
101
102         if (!m_XMLDocument.hasChildNodes ()) {
103
104             throw new JahiaException ("Default_Web_Site_Xml",
105                     "Main document node has no children",
106                     JahiaException.ERROR_SEVERITY,
107                     JahiaException.SERVICE_ERROR);
108         }
109
110
111         // get web-site node
112
Element JavaDoc docElNode = (Element JavaDoc) m_XMLDocument.getDocumentElement ();
113
114         if (!docElNode.getNodeName ().equalsIgnoreCase ("web-site")) {
115
116             throw new JahiaException ("Invalid XML format",
117                     "web-site tag is not present as starting tag in file",
118                     JahiaException.ERROR_SEVERITY,
119                     JahiaException.SERVICE_ERROR);
120         }
121
122         logger.debug ("default-web-site.xml file has web-site element");
123
124         // get web app elements
125
Vector JavaDoc appNodes = XMLParser.getChildNodes (docElNode, "web-app");
126         Node JavaDoc nodeItem = null;
127         Web_App_Element appEl = null;
128         NamedNodeMap JavaDoc attribs = null;
129
130         String JavaDoc application;
131         String JavaDoc root;
132         String JavaDoc name;
133         String JavaDoc loadOnStartup;
134         String JavaDoc maxInactivityTime;
135         String JavaDoc shared;
136
137         int size = appNodes.size ();
138
139         for (int i = 0; i < size; i++) {
140
141             nodeItem = (Node JavaDoc) appNodes.get (i);
142
143             logger.debug ("web app element " + nodeItem.getNodeName ());
144
145             application = XMLParser.getAttributeValue (nodeItem, "application");
146             loadOnStartup = XMLParser.getAttributeValue (nodeItem, "load-on-startup");
147             maxInactivityTime = XMLParser.getAttributeValue (nodeItem, "max-inactivity-time");
148             name = XMLParser.getAttributeValue (nodeItem, "name");
149             root = XMLParser.getAttributeValue (nodeItem, "root");
150             shared = XMLParser.getAttributeValue (nodeItem, "shared");
151
152             if (application != null && root != null && name != null) {
153
154                 appEl = new Web_App_Element (
155                         application,
156                         root,
157                         name,
158                         loadOnStartup,
159                         shared,
160                         maxInactivityTime
161                 );
162
163                 m_WebApps.add (appEl);
164                 m_WebAppNodes.add (nodeItem);
165
166                 logger.debug (" Web App application :" + appEl.getApplication ());
167                 logger.debug (" root :" + appEl.getRoot ());
168                 logger.debug (" name :" + appEl.getName ());
169                 logger.debug (" loadOnStratup :" + appEl.getLoadOnStartup ());
170                 logger.debug (" shared :" + appEl.getShared ());
171                 logger.debug (" maxInactivityTime :" + appEl.getMaxInactivityTime ());
172             }
173
174         }
175
176         logger.debug (" extraction done");
177     }
178
179
180     /**
181      * Return the list of Web App Elements
182      *
183      * @return (Vector) list of Web App Elements
184      */

185     public Vector JavaDoc getWebApps () {
186
187         return m_WebApps;
188
189     }
190
191
192     /**
193      * Append a new <web-app..> Element in the xml file
194      * Add it in the Web App list
195      *
196      * @param (Web_App_Element) appEl
197      */

198     public boolean addWebApp (Web_App_Element appEl) {
199
200
201         if (!isDeclared (appEl.getApplication (), appEl.getName ())) {
202
203
204             Element JavaDoc docElNode = (Element JavaDoc) m_XMLDocument.getDocumentElement ();
205
206             Element JavaDoc newNode = (Element JavaDoc) m_XMLDocument.createElement ("web-app");
207
208             XMLParser.setAttribute (newNode, "application", appEl.getApplication ());
209             XMLParser.setAttribute (newNode, "root", appEl.getRoot ());
210             XMLParser.setAttribute (newNode, "name", appEl.getName ());
211             XMLParser.setAttribute (newNode, "loadOnStartup", appEl.getLoadOnStartup ());
212             XMLParser.setAttribute (newNode, "shared", appEl.getShared ());
213             XMLParser.setAttribute (newNode, "maxInactivityTime",
214                     appEl.getMaxInactivityTime ());
215
216             if (m_WebAppNodes.size () > 0) {
217                 Node JavaDoc lastAppNode = (Node JavaDoc) m_WebAppNodes.get ((m_WebAppNodes.size () - 1));
218                 docElNode.insertBefore (newNode, lastAppNode);
219             } else {
220                 docElNode.appendChild (newNode);
221             }
222             m_WebApps.add (appEl);
223             m_WebAppNodes.add (newNode);
224             return true;
225         }
226         return false;
227
228     }
229
230
231     /**
232      * Check if a web app is already declared
233      *
234      * @param (String) appName, application name
235      * @param (String) webAppName, web app name
236      */

237     public boolean isDeclared (String JavaDoc appName, String JavaDoc webAppName) {
238
239         Web_App_Element webApp = null;
240         int size = m_WebApps.size ();
241         for (int i = 0; i < size; i++) {
242             webApp = (Web_App_Element) m_WebApps.get (i);
243             if (webApp.getApplication ().equals (appName) && webApp.getName ().equals (
244                     webAppName)) {
245                 return true;
246             }
247         }
248
249         return false;
250     }
251
252
253 } // end Default_Web_Site_Xml
254
Popular Tags