KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > webapps_deployer > tomcat > Tomcat_Users_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
// NK 02.02.2001
14
//
15

16 package org.jahia.services.webapps_deployer.tomcat;
17
18 import org.jahia.data.xml.JahiaXmlDocument;
19 import org.jahia.exceptions.JahiaException;
20 import org.jahia.utils.xml.XMLParser;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 import java.util.Vector JavaDoc;
25
26 /**
27  * Holds Informations about the Tomcat tomcat-users.xml file
28  * We need to modify this file to add the "manager" role to
29  * use the Tomcat Management Application
30  * <tomcat-users>
31  * <user name="tomcat" password="tomcat" roles="tomcat,manager" />
32  * <user name="role1" password="tomcat" roles="role1" />
33  * <user name="both" password="tomcat" roles="tomcat,role1" />
34  * </tomcat-users>
35  *
36  * @author Khue ng
37  * @version 1.0
38  */

39 public class Tomcat_Users_Xml extends JahiaXmlDocument {
40
41     private static org.apache.log4j.Logger logger =
42             org.apache.log4j.Logger.getLogger (Tomcat_Users_Xml.class);
43
44     /** The list of Tomcat users Nodes * */
45     private Vector JavaDoc m_UserNodes = new Vector JavaDoc ();
46
47     /**
48      * Constructor
49      *
50      * @param docPath the full path to the tomcat-users.xml file
51      */

52     public Tomcat_Users_Xml (String JavaDoc docPath)
53             throws JahiaException {
54         super (docPath);
55         extractDocumentData ();
56
57     }
58
59     /**
60      * Extracts data from the Tomcat-users.xml file. Build the JahiaWebAppsWarPackage object
61      * to store extracted data
62      */

63     public void extractDocumentData ()
64             throws JahiaException {
65
66         logger.debug ("started ");
67
68         if (m_XMLDocument == null) {
69
70             throw new JahiaException ("Tomcat_Users_Xml",
71                     "Parsed Tomcat-users.xml document is null",
72                     JahiaException.ERROR_SEVERITY,
73                     JahiaException.SERVICE_ERROR);
74         }
75
76         if (!m_XMLDocument.hasChildNodes ()) {
77
78             throw new JahiaException ("Tomcat_Users_Xml",
79                     "Main document node has no children",
80                     JahiaException.ERROR_SEVERITY,
81                     JahiaException.SERVICE_ERROR);
82         }
83
84         // get <tomcat-users> node
85

86         Element JavaDoc docElNode = (Element JavaDoc) m_XMLDocument.getDocumentElement ();
87
88         if (!docElNode.getNodeName ().equalsIgnoreCase ("tomcat-users")) {
89
90             throw new JahiaException ("Invalid XML format",
91                     "tomcat-users tag is not present as starting tag in file",
92                     JahiaException.ERROR_SEVERITY,
93                     JahiaException.SERVICE_ERROR);
94         }
95
96         logger.debug ("Tomcat-users.xml file has tomcat-users element");
97
98         // get users elements
99
m_UserNodes = XMLParser.getChildNodes (docElNode, "user");
100
101         logger.debug ("done");
102     }
103
104     /**
105      * Return the list of Users Nodes
106      *
107      * @return (Vector) list of Users Nodes
108      */

109     public Vector JavaDoc getUserNodes () {
110
111         return m_UserNodes;
112
113     }
114
115     /**
116      * Append a new <user..> Element in the xml file
117      * Add it in the user nodes list
118      *
119      * @param (String) name, tomcat user
120      * @param (String) password
121      * @param (String) roles , ex: "tomcat,manager"
122      */

123     public void addUser (
124             String JavaDoc name,
125             String JavaDoc password,
126             String JavaDoc roles
127             ) {
128
129         Element JavaDoc docElNode = (Element JavaDoc) m_XMLDocument.getDocumentElement ();
130
131         Element JavaDoc newNode = (Element JavaDoc) m_XMLDocument.createElement ("user");
132
133         XMLParser.setAttribute (newNode, "name", name);
134         XMLParser.setAttribute (newNode, "password", password);
135         XMLParser.setAttribute (newNode, "roles", roles);
136
137         if (m_UserNodes.size () > 0) {
138             Node JavaDoc lastUserNode = (Node JavaDoc) m_UserNodes.get ((m_UserNodes.size () - 1));
139             docElNode.insertBefore (newNode, lastUserNode);
140         } else {
141             docElNode.appendChild (newNode);
142         }
143
144         m_UserNodes.add (newNode);
145
146     }
147
148     /**
149      * Change user password for a user with of a gived name and a having a gived role
150      *
151      * @param (String) name tomcat user
152      * @param (String) password
153      * @param (String) role
154      * @param (boolean) return false on error
155      */

156     public boolean updateUser (
157             String JavaDoc name,
158             String JavaDoc password,
159             String JavaDoc role
160             ) {
161
162         int size = m_UserNodes.size ();
163         for (int i = 0; i < size; i++) {
164             Element JavaDoc user = (Element JavaDoc) m_UserNodes.get (i);
165
166             if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, "name"))
167                     &&
168                     role.equalsIgnoreCase (XMLParser.getAttributeValue (user, "roles"))) {
169
170                 XMLParser.setAttribute (user, "password", password);
171                 return true;
172             } else if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user,
173                     "username"))
174                     &&
175                     role.equalsIgnoreCase (XMLParser.getAttributeValue (user,
176                             "roles"))) {
177                 XMLParser.setAttribute (user, "password", password);
178                 return true;
179
180             }
181         }
182
183         return false;
184     }
185
186     /**
187      * Check if a user exist
188      *
189      * @param (String) name, tomcat user
190      * @param (String) password,
191      */

192     public boolean checkUser (
193             String JavaDoc name,
194             String JavaDoc password
195             ) {
196
197         int size = m_UserNodes.size ();
198         for (int i = 0; i < size; i++) {
199             Element JavaDoc user = (Element JavaDoc) m_UserNodes.get (i);
200
201             if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, "name"))
202                     &&
203                     password.equalsIgnoreCase (XMLParser.getAttributeValue (user, "password"))) {
204                 return true;
205             } else if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user,
206                     "username"))
207                     &&
208                     password.equalsIgnoreCase (XMLParser.getAttributeValue (user,
209                             "password"))) {
210                 return true;
211             }
212         }
213
214         return false;
215     }
216
217     /**
218      * Return the password of a user with a gived name and a gived role
219      *
220      * @param (String) name, tomcat user
221      * @param (String) role,
222      */

223     public String JavaDoc getUserPassword (String JavaDoc name, String JavaDoc role) {
224
225         int size = m_UserNodes.size ();
226         for (int i = 0; i < size; i++) {
227             Element JavaDoc user = (Element JavaDoc) m_UserNodes.get (i);
228
229             if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user, "name")) &&
230                 (XMLParser.getAttributeValue (user, "roles") != null) &&
231                 (XMLParser.getAttributeValue (user, "roles").indexOf(role) != -1)) {
232                 return XMLParser.getAttributeValue (user, "password");
233
234             } else if (name.equalsIgnoreCase (XMLParser.getAttributeValue (user,
235                 "username")) &&
236                        (XMLParser.getAttributeValue (user, "roles") != null) &&
237                        (XMLParser.getAttributeValue (user, "roles").indexOf(role) != -1)) {
238                 return XMLParser.getAttributeValue (user, "password");
239             }
240         }
241
242         return null;
243     }
244
245 } // end Tomcat_Users_Xml
246
Popular Tags