1 2 23 package com.geinuke.servlet; 24 25 26 import java.io.BufferedReader ; 27 import java.io.BufferedWriter ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.FileWriter ; 31 import java.io.IOException ; 32 import java.io.PrintWriter ; 33 import java.io.StringReader ; 34 import java.sql.Connection ; 35 import java.sql.DriverManager ; 36 import java.sql.SQLException ; 37 38 import javax.naming.Context ; 39 import javax.naming.InitialContext ; 40 import javax.servlet.http.HttpServlet ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import javax.sql.DataSource ; 44 45 import com.geinuke.util.TextUtil; 46 import com.ibatis.common.jdbc.ScriptRunner; 47 48 public class Configurator extends HttpServlet { 49 50 String basePath=null; 51 52 public void service(HttpServletRequest req, HttpServletResponse res){ 53 this.basePath=this.getServletContext().getRealPath("/"); 54 basePath=basePath.substring(0,basePath.length()-1); 55 String mod=req.getParameter("db"); 56 if(this.mayGo()){ 57 this.serviceLOG4J(req,res); 58 if(mod==null || mod.equals("hsqldb")){ 59 this.serviceHSQLDB(req,res); 60 }else if(mod!=null && mod.equals("mysql")){ 61 this.serviceMySQL(req,res); 62 }else if(mod!=null && mod.equals("mysqlJNDI")){ 63 this.serviceMySQLJNDI(req,res); 64 }else if(mod!=null && mod.equals("no")){ 65 this.serviceClientDSM(req,res); 66 } 67 } 68 try{ 69 this.printOnFile("/WEB-INF/cinfigured.lck","configured"); 70 res.sendRedirect("geinuke.jhtm"); 71 }catch(Exception e){ 72 e.printStackTrace(); 73 } 74 } 75 76 protected boolean mayGo(){ 77 boolean flag=true; 78 File f=new File (this.basePath+"/WEB-INF/cinfigured.lck"); 79 flag=!f.isFile(); 80 return flag; 81 } 82 83 protected void printOnFile(String filePath,String s) throws IOException { 84 PrintWriter out=null; 85 out= new PrintWriter (new BufferedWriter (new FileWriter (basePath+filePath))); 86 out.print(s); 87 out.close(); 88 } 89 90 protected String getStringFromFile(String filePath) throws Exception { 91 String res=null; 92 BufferedReader in1=null; 93 in1= new BufferedReader (new FileReader (basePath+filePath)); 94 res=TextUtil.getString(in1); 95 in1.close(); 96 return res; 97 } 98 99 public void serviceClientDSM(HttpServletRequest req, HttpServletResponse res){ 100 101 BufferedReader in1=null; 102 PrintWriter out=null; 103 104 String fileS=null; 105 106 System.out.println("Configurator A "+basePath); 107 108 String tempPath="/WEB-INF/classes/"; 109 110 try{ 111 System.out.println("reading ... "+tempPath+"remote/BizLogicREMOTE.xml"); 112 fileS=this.getStringFromFile(tempPath+"remote/BizLogicREMOTE.xml"); 113 String remoteHost=null; 114 String port=null; 115 String servlet=null; 116 117 remoteHost=req.getParameter("remoteHost"); 118 port=req.getParameter("port"); 119 servlet=req.getParameter("servlet"); 120 121 basePath=TextUtil.normIfWin(basePath); 122 123 fileS=fileS.replaceAll("ReMoTeHoSt",remoteHost); 124 fileS=fileS.replaceAll("8080",port); 125 fileS=fileS.replaceAll("HeSsIaN",servlet); 126 127 128 129 System.out.println("writing on ... "+"/WEB-INF/classes/BizLogic.xml \n"+fileS); 130 this.printOnFile("/WEB-INF/classes/BizLogic.xml",fileS); 131 132 tempPath="/WEB-INF/templates/config/hsqldb/"; 133 fileS=this.getStringFromFile(tempPath+"velocity.properties"); 134 135 136 basePath=TextUtil.normIfWin(basePath); 137 138 System.out.println("\n"+fileS+"\n"); 139 fileS=fileS.replaceAll("bAsEpAtH",basePath); 140 System.out.println("\n"+fileS+"\n"); 141 142 this.printOnFile("/WEB-INF/velocity.properties",fileS); 143 144 145 }catch(Throwable e){ 146 e.printStackTrace(); 147 } 148 } 149 150 151 152 public void serviceHSQLDB(HttpServletRequest req, HttpServletResponse res){ 153 154 BufferedReader in1=null; 155 PrintWriter out=null; 156 157 String fileS=null; 158 159 System.out.println("Configurator A "+basePath); 160 161 String tempPath="/WEB-INF/templates/config/hsqldb/"; 162 163 try{ 164 165 fileS=this.getStringFromFile(tempPath+"velocity.properties"); 166 167 168 basePath=TextUtil.normIfWin(basePath); 169 170 System.out.println("\n"+fileS+"\n"); 171 fileS=fileS.replaceAll("bAsEpAtH",basePath); 172 System.out.println("\n"+fileS+"\n"); 173 174 this.printOnFile("/WEB-INF/velocity.properties",fileS); 175 176 177 fileS=this.getStringFromFile(tempPath+"SqlMapConfig.xml"); 178 179 fileS=fileS.replaceAll("bAsEpAtH",basePath); 180 this.printOnFile("/WEB-INF/classes/SqlMapConfig.xml",fileS); 181 182 183 }catch(Throwable e){ 184 e.printStackTrace(); 185 } 186 } 187 188 189 public void serviceLOG4J(HttpServletRequest req, HttpServletResponse res){ 190 191 BufferedReader in1=null; 192 PrintWriter out=null; 193 194 String fileS=null; 195 196 197 198 String tempPath="/WEB-INF/templates/config/"; 199 200 try{ 201 202 fileS=this.getStringFromFile(tempPath+"Geilog.properties"); 203 204 205 basePath=TextUtil.normIfWin(basePath); 206 207 System.out.println("\n"+fileS+"\n"); 208 fileS=fileS.replaceAll("fIlE",this.basePath+"/"+"WEB-INF"+"/"+"geilog.log"); 209 System.out.println("\n"+fileS+"\n"); 210 211 this.printOnFile("/WEB-INF/classes/Geilog.properties",fileS); 212 213 214 215 216 }catch(Throwable e){ 217 e.printStackTrace(); 218 } 219 } 220 221 222 223 224 protected Connection getMySqlConnection(HttpServletRequest req) throws ClassNotFoundException , SQLException { 225 Connection con=null; 226 Class.forName("org.gjt.mm.mysql.Driver"); 227 String host=req.getParameter("host"); 228 String port=req.getParameter("port"); 229 String db=req.getParameter("database"); 230 String user=req.getParameter("user"); 231 String pass=req.getParameter("pass"); 232 con = DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/test",user,pass); 233 con.setAutoCommit (false); 234 return con; 235 } 236 237 protected Connection getMySqlJNDIConnection(HttpServletRequest req) throws ClassNotFoundException , SQLException { 238 Connection con=null; 239 String connS=req.getParameter("jndi"); 240 try{ 241 Context ctx=new InitialContext (); 242 DataSource ds=(DataSource )ctx.lookup(connS); 243 con=ds.getConnection(); 244 }catch(Exception e){ 245 e.printStackTrace(); 246 } 247 256 return con; 257 } 258 259 public void serviceMySQL(HttpServletRequest req, HttpServletResponse res){ 260 261 BufferedReader in1=null; 262 PrintWriter out=null; 263 264 String fileS=null; 265 266 System.out.println("Configurator A "+basePath); 267 268 String tempPath="/WEB-INF/templates/config/mysql/"; 269 270 try{ 271 272 fileS=this.getStringFromFile(tempPath+"velocity.properties"); 273 274 275 basePath=TextUtil.normIfWin(basePath); 276 System.out.println("\n"+fileS+"\n"); 278 279 280 281 fileS=fileS.replaceAll("bAsEpAtH",basePath); 282 System.out.println("\n"+fileS+"\n"); 283 284 285 this.printOnFile("/WEB-INF/velocity.properties",fileS); 286 287 fileS=this.getStringFromFile(tempPath+"SqlMapConfig.xml"); 288 289 290 String host=req.getParameter("host"); 291 String port=req.getParameter("port"); 292 String db=req.getParameter("database"); 293 String user=req.getParameter("user"); 294 String pass=req.getParameter("pass"); 295 296 fileS=fileS.replaceAll("HoSt",host); 297 fileS=fileS.replaceAll("PoRt",port); 298 fileS=fileS.replaceAll("DaTaBaSe",db); 299 fileS=fileS.replaceAll("UsEr",user); 300 fileS=fileS.replaceAll("PaSs",pass); 301 302 Connection con=this.getMySqlConnection(req); 304 ScriptRunner sr=null; 305 sr=new ScriptRunner(); 306 307 String dataB=this.getStringFromFile( "/WEB-INF/templates/config/mysql/dbGei-InnoDB.sql" ); 308 dataB=dataB.replaceAll("DaTaBaSe",db); 309 310 311 sr.runScript(con, new StringReader (dataB ) ); 313 con.close(); 314 315 this.printOnFile("/WEB-INF/classes/SqlMapConfig.xml",fileS); 316 317 318 }catch(Throwable e){ 319 e.printStackTrace(); 320 } 321 } 322 323 324 public void serviceMySQLJNDI(HttpServletRequest req, HttpServletResponse res){ 325 326 BufferedReader in1=null; 327 PrintWriter out=null; 328 329 String fileS=null; 330 331 System.out.println("Configurator A "+basePath); 332 333 String tempPath="/WEB-INF/templates/config/mysql/"; 334 335 try{ 336 337 fileS=this.getStringFromFile(tempPath+"velocity.properties"); 338 339 340 basePath=TextUtil.normIfWin(basePath); 341 System.out.println("\n"+fileS+"\n"); 343 344 345 346 fileS=fileS.replaceAll("bAsEpAtH",basePath); 347 System.out.println("\n"+fileS+"\n"); 348 349 350 this.printOnFile("/WEB-INF/velocity.properties",fileS); 351 352 fileS=this.getStringFromFile(tempPath+"jndi/SqlMapConfig.xml"); 353 354 355 String jndi=req.getParameter("jndi"); 356 357 String db=req.getParameter("database"); 358 359 360 fileS=fileS.replaceAll("JnDi",jndi); 361 362 364 365 367 ScriptRunner sr=null; 368 sr=new ScriptRunner(); 369 String scriptSQL=null; 370 scriptSQL=fileS=this.getStringFromFile("/WEB-INF/templates/config/mysql/dbGei-InnoDB.sql"); 371 if(TextUtil.isEmpty(db)){ 372 scriptSQL=scriptSQL.substring( scriptSQL.indexOf("--SECOND START") ); 373 } 374 Connection con=this.getMySqlJNDIConnection(req); 377 sr.runScript(con, new StringReader (scriptSQL) ); 378 379 con.close(); 380 381 this.printOnFile("/WEB-INF/classes/SqlMapConfig.xml",fileS); 382 383 384 }catch(Throwable e){ 385 e.printStackTrace(); 386 } 387 } 388 389 } 390 391 | Popular Tags |