KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > webapps_deployer > orion > Server_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
// Server_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 Server.xml file
35  * We are only interesting to add or remove <application../>
36  * element to deploy or undeploy web apps
37  * <application-server
38  * application-directory="../applications"
39  * deployment-directory="../application-deployments">
40  * <rmi-config path="./rmi.xml" />
41  * <!-- JMS-server config link, uncomment to activate the JMS service -->
42  * <!-- <jms-config path="./jms.xml" /> -->
43  * <principals path="./principals.xml" />
44  * <log>
45  * <file path="../log/server.log" />
46  * </log>
47  * <global-application name="default" path="application.xml" />
48  * <application name="Bookmark" path="../applications/Bookmark.ear" />
49  * <application name="Jahia" path="../applications/Jahia.ear" />
50  * <global-web-app-config path="global-web-application.xml" />
51  * <web-site path="./default-web-site.xml" />
52  * <!-- Compiler, activate this to specify an alternative compiler such
53  * as jikes for EJB/JSP compiling. -->
54  * <!-- <compiler executable="jikes" classpath="/myjdkdir/jre/lib/rt.jar" /> -->
55  * </application-server>
56  *
57  * @author Khue ng
58  * @version 1.0
59  */

60 public class Server_Xml extends JahiaXmlDocument {
61
62     /** The list of <application..></application> elements * */
63     private Vector JavaDoc m_Applications = new Vector JavaDoc ();
64     /** The list of Application Nodes * */
65     private Vector JavaDoc m_ApplicationNodes = new Vector JavaDoc ();
66
67     private static org.apache.log4j.Logger logger =
68             org.apache.log4j.Logger.getLogger (Server_Xml.class);
69
70     /**
71      * Constructor
72      *
73      * @param (String) path, the full path to the server.xml file
74      */

75     public Server_Xml (String JavaDoc docPath) throws JahiaException {
76         super (docPath);
77         extractDocumentData ();
78
79     }
80
81
82     /**
83      * Constructor using a gived parser
84      *
85      * @param (String) path, the full path to a xml file
86      * @param (Parser) parser, the parser to use
87      */

88     public Server_Xml (String JavaDoc docPath, org.xml.sax.helpers.ParserAdapter JavaDoc parser)
89             throws JahiaException {
90         super (docPath, parser);
91     }
92
93
94     /**
95      * Extracts data from the web.xml file. Build the JahiaWebAppsWarPackage object
96      * to store extracted data
97      */

98     public void extractDocumentData () throws JahiaException {
99
100         logger.debug (" Server_Xml::extractDocumentData started ");
101
102         if (m_XMLDocument == null) {
103
104             throw new JahiaException ("Server_Xml",
105                     "Parsed web.xml document is null",
106                     JahiaException.ERROR_SEVERITY,
107                     JahiaException.SERVICE_ERROR);
108         }
109
110
111         if (!m_XMLDocument.hasChildNodes ()) {
112
113             throw new JahiaException ("Server_Xml",
114                     "Main document node has no children",
115                     JahiaException.ERROR_SEVERITY,
116                     JahiaException.SERVICE_ERROR);
117         }
118
119
120         // get application-server node
121
Element JavaDoc appServNode;
122         appServNode = (Element JavaDoc) m_XMLDocument.getDocumentElement ();
123
124         if (!appServNode.getNodeName ().equalsIgnoreCase ("application-server")) {
125
126             throw new JahiaException ("Invalid XML format",
127                     "application-server tag is not present as starting tag in file",
128                     JahiaException.ERROR_SEVERITY,
129                     JahiaException.SERVICE_ERROR);
130         }
131
132         logger.debug ("server.xml file has application-server element");
133
134         // get application elements
135
Vector JavaDoc appNodes = XMLParser.getChildNodes (appServNode, "application");
136         Node JavaDoc nodeItem = null;
137         Application_Element appEl = null;
138         NamedNodeMap JavaDoc attribs = null;
139
140         String JavaDoc name;
141         String JavaDoc path;
142         String JavaDoc deployDir;
143         String JavaDoc parent;
144         String JavaDoc autoStart;
145
146         int size = appNodes.size ();
147
148         for (int i = 0; i < size; i++) {
149
150             nodeItem = (Node JavaDoc) appNodes.get (i);
151
152             logger.debug ("application element " + nodeItem.getNodeName ());
153
154             name = XMLParser.getAttributeValue (nodeItem, "name");
155             path = XMLParser.getAttributeValue (nodeItem, "path");
156             deployDir = XMLParser.getAttributeValue (nodeItem, "deployment-directory");
157             parent = XMLParser.getAttributeValue (nodeItem, "parent");
158             autoStart = XMLParser.getAttributeValue (nodeItem, "auto-start");
159
160             if (name != null && path != null) {
161
162                 appEl = new Application_Element (name,
163                         path,
164                         deployDir,
165                         parent,
166                         autoStart
167                 );
168
169                 m_Applications.add (appEl);
170                 m_ApplicationNodes.add (nodeItem);
171
172                 logger.debug (" Application Element name :" + appEl.getName ());
173                 logger.debug (" path :" + appEl.getPath ());
174                 logger.debug (" deployDir :" + appEl.getDeployDir ());
175                 logger.debug (" parent :" + appEl.getParent ());
176                 logger.debug (" autoStart :" + appEl.getAutoStart ());
177             }
178         }
179
180         logger.debug (" done");
181     }
182
183
184     /**
185      * Return the list of Applications Element
186      *
187      * @return (Vector) list of Application Elements
188      */

189     public Vector JavaDoc getApplications () {
190
191         return m_Applications;
192
193     }
194
195
196     /**
197      * Append a new <Application..> Element in the xml file
198      * Add it in the Application list
199      *
200      * @param (Application_Element) appEl
201      *
202      * @return (boolean) true if doesn't exist and added
203      */

204     public boolean appendApplication (Application_Element appEl) {
205
206         if (!isDeclared (appEl.getName ())) {
207
208             Element JavaDoc docElNode = (Element JavaDoc) m_XMLDocument.getDocumentElement ();
209
210             Element JavaDoc newNode = (Element JavaDoc) m_XMLDocument.createElement ("application");
211
212             XMLParser.setAttribute (newNode, "name", appEl.getName ());
213             XMLParser.setAttribute (newNode, "path", appEl.getPath ());
214             XMLParser.setAttribute (newNode, "deployment-directory", appEl.getDeployDir ());
215             XMLParser.setAttribute (newNode, "parent", appEl.getParent ());
216             XMLParser.setAttribute (newNode, "auto-start", appEl.getAutoStart ());
217
218             if (m_ApplicationNodes.size () > 0) {
219                 Node JavaDoc lastAppNode = (Node JavaDoc) m_ApplicationNodes.get (
220                         (m_ApplicationNodes.size () - 1));
221                 docElNode.insertBefore (newNode, lastAppNode);
222             } else {
223                 docElNode.appendChild (newNode);
224             }
225
226             m_Applications.add (appEl);
227             m_ApplicationNodes.add (newNode);
228             return true;
229         }
230
231         return false;
232     }
233
234
235     /**
236      * Check if an application already exists in the xml file
237      *
238      * @param (String) appName, application name
239      */

240     public boolean isDeclared (String JavaDoc name) {
241
242         Application_Element appEl = null;
243         int size = m_Applications.size ();
244         for (int i = 0; i < size; i++) {
245             appEl = (Application_Element) m_Applications.get (i);
246             if (appEl.getName ().equals (name)) {
247                 return true;
248             }
249         }
250
251         return false;
252     }
253
254
255     /**
256      * Remove an application Element
257      *
258      * @param (String) appName
259      */

260     public void removeApplication (String JavaDoc appName) {
261
262         Application_Element appEl = null;
263
264         int size = m_Applications.size ();
265         for (int i = 0; i < size; i++) {
266             appEl = (Application_Element) m_Applications.get (i);
267             if (appEl.getName ().equals (appName)) {
268
269                 m_Applications.remove (i);
270                 Element JavaDoc node = (Element JavaDoc) m_ApplicationNodes.get (i);
271                 Element JavaDoc docElNode = (Element JavaDoc) m_XMLDocument.getDocumentElement ();
272                 docElNode.removeChild (node);
273                 m_ApplicationNodes.remove (i);
274                 break;
275             }
276         }
277     }
278
279
280 } // end Server_Xml
281
Popular Tags