KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > modifier > TomcatUsersModifier


1 // $Id: TomcatUsersModifier.java 3567 2003-06-30 22:32:03Z shuber $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// TomcatUsersModifier
15
//
16
// 17.05.2001 AK added in jahia.
17
//
18

19 package org.jahia.utils.modifier;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.jahia.utils.keygenerator.JahiaKeyGen;
28 import org.jahia.utils.properties.PropertiesManager;
29
30
31
32 /**
33  * Copyright: Copyright (c) 2002
34  * Company: Jahia Ltd
35  *
36  * @author Alexandre Kraft
37  * @version 1.0
38  */

39 public class TomcatUsersModifier
40 {
41
42
43
44     /**
45      * Default constructor.
46      * @author Alexandre Kraft
47      */

48     public TomcatUsersModifier()
49     {
50         // do nothing :o)
51
} // end constructor
52

53
54
55     /**
56      * Load a complete properties file in memory by its filename.
57      * @author Alexandre Kraft
58      */

59     public static boolean ensureValidity( String JavaDoc pathJahiaProperties )
60     {
61         boolean pwdChanged = false;
62
63         try {
64             PropertiesManager pm = new PropertiesManager( pathJahiaProperties );
65
66             if(pm.getProperty("server").indexOf("Tomcat")>-1)
67             {
68                 // get tomcat-users.xml path...
69
StringBuffer JavaDoc tomXMLPath = new StringBuffer JavaDoc( pm.getProperty("serverHomeDiskPath") );
70                 tomXMLPath.append( "conf" );
71                 tomXMLPath.append( File.separator );
72                 tomXMLPath.append( "tomcat-users.xml" );
73
74                 // calculate new password...
75
String JavaDoc newPwd = JahiaKeyGen.getKey( 10 );
76
77                 // open tomcat-users.xml file...
78
FileReader JavaDoc fileReader = new FileReader JavaDoc( tomXMLPath.toString() );
79                 BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc( fileReader );
80                 String JavaDoc lineReaded = null;
81                 Vector JavaDoc newLines = new Vector JavaDoc();
82     
83                 // parse the file...
84
boolean jahiaIsFound = false;
85                 while((lineReaded = bufferedReader.readLine()) != null)
86                 {
87                     if(lineReaded.indexOf("Jahia")>-1) {
88                         jahiaIsFound = true;
89                         int pos = lineReaded.indexOf("password");
90                         if(pos>-1) {
91                             int start = pos+10;
92                             int stop = lineReaded.indexOf("\"", start);
93                             if(lineReaded.substring(start,stop).equals("Jahia") || lineReaded.substring(start,stop).equals("")) {
94                                 pwdChanged = true;
95                                 newLines.add( lineReaded.substring(0,start) + newPwd + lineReaded.substring(stop,lineReaded.length()));
96                             } else {
97                                 newLines.add( lineReaded );
98                             }
99                         }
100                     } else {
101                         // add line to vector. no changes...
102
newLines.add( lineReaded );
103                     }
104                 }
105
106                 // add a new line if user jahia isn't found...
107
if(!jahiaIsFound) {
108                     fileReader = new FileReader JavaDoc( tomXMLPath.toString() );
109                     bufferedReader = new BufferedReader JavaDoc( fileReader );
110                     newLines = new Vector JavaDoc();
111                     while((lineReaded = bufferedReader.readLine()) != null)
112                     {
113                         newLines.add( lineReaded );
114                         if(lineReaded.indexOf("<tomcat-users>")>-1) {
115                             newLines.add( "<user name=\"Jahia\" password=\""+newPwd+"\" roles=\"manager\"></user>" );
116                             pwdChanged = true;
117                         }
118                     }
119                 }
120
121                 // write the file...
122
FileWriter JavaDoc fileWriter = new FileWriter JavaDoc( tomXMLPath.toString() );
123                 StringBuffer JavaDoc outputBuffer = new StringBuffer JavaDoc();
124                 for(int i=0; i < newLines.size(); i++) {
125                     outputBuffer.append((String JavaDoc) newLines.get(i));
126                     outputBuffer.append("\n");
127                 }
128                 fileWriter.write( outputBuffer.toString() );
129     
130                 // close streams...
131
bufferedReader.close();
132                 fileReader.close();
133                 fileWriter.close();
134         
135                 // performance issue, force garbage collector...
136
newPwd = null;
137                 lineReaded = null;
138                 outputBuffer = null;
139                 newLines = null;
140                 bufferedReader = null;
141                 fileWriter = null;
142                 fileReader = null;
143                 tomXMLPath = null;
144             }
145             pm = null;
146
147         } catch (Exception JavaDoc e) {
148         }
149     return pwdChanged;
150     } // end ensureValidity
151

152
153 } // end TomcatUsersModifier
Popular Tags