1 19 package org.jahia.utils.modifier; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.FileWriter ; 25 import java.util.Vector ; 26 27 import org.jahia.utils.keygenerator.JahiaKeyGen; 28 import org.jahia.utils.properties.PropertiesManager; 29 30 31 32 39 public class TomcatUsersModifier 40 { 41 42 43 44 48 public TomcatUsersModifier() 49 { 50 } 53 54 55 59 public static boolean ensureValidity( String 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 StringBuffer tomXMLPath = new StringBuffer ( pm.getProperty("serverHomeDiskPath") ); 70 tomXMLPath.append( "conf" ); 71 tomXMLPath.append( File.separator ); 72 tomXMLPath.append( "tomcat-users.xml" ); 73 74 String newPwd = JahiaKeyGen.getKey( 10 ); 76 77 FileReader fileReader = new FileReader ( tomXMLPath.toString() ); 79 BufferedReader bufferedReader = new BufferedReader ( fileReader ); 80 String lineReaded = null; 81 Vector newLines = new Vector (); 82 83 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 newLines.add( lineReaded ); 103 } 104 } 105 106 if(!jahiaIsFound) { 108 fileReader = new FileReader ( tomXMLPath.toString() ); 109 bufferedReader = new BufferedReader ( fileReader ); 110 newLines = new Vector (); 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 FileWriter fileWriter = new FileWriter ( tomXMLPath.toString() ); 123 StringBuffer outputBuffer = new StringBuffer (); 124 for(int i=0; i < newLines.size(); i++) { 125 outputBuffer.append((String ) newLines.get(i)); 126 outputBuffer.append("\n"); 127 } 128 fileWriter.write( outputBuffer.toString() ); 129 130 bufferedReader.close(); 132 fileReader.close(); 133 fileWriter.close(); 134 135 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 e) { 148 } 149 return pwdChanged; 150 } 152 153 } | Popular Tags |