KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > servlet > Configurator


1
2  /*
3  -- GeiNuke --
4 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
5  http://www.hostingjava.it/-geinuke/
6
7 This file is part of GeiNuke.
8
9     GeiNuke is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     GeiNuke is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with GeiNuke; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */

23 package com.geinuke.servlet;
24
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.io.StringReader JavaDoc;
34 import java.sql.Connection JavaDoc;
35 import java.sql.DriverManager JavaDoc;
36 import java.sql.SQLException JavaDoc;
37
38 import javax.naming.Context JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.servlet.http.HttpServlet JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import javax.sql.DataSource JavaDoc;
44
45 import com.geinuke.util.TextUtil;
46 import com.ibatis.common.jdbc.ScriptRunner;
47
48 public class Configurator extends HttpServlet JavaDoc{
49     
50     String JavaDoc basePath=null;
51     
52     public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res){
53         this.basePath=this.getServletContext().getRealPath("/");
54         basePath=basePath.substring(0,basePath.length()-1);
55         String JavaDoc 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 JavaDoc e){
72             e.printStackTrace();
73         }
74     }
75     
76     protected boolean mayGo(){
77         boolean flag=true;
78         File JavaDoc f=new File JavaDoc(this.basePath+"/WEB-INF/cinfigured.lck");
79         flag=!f.isFile();
80         return flag;
81     }
82     
83     protected void printOnFile(String JavaDoc filePath,String JavaDoc s) throws IOException JavaDoc{
84         PrintWriter JavaDoc out=null;
85         out= new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(basePath+filePath)));
86         out.print(s);
87         out.close();
88     }
89     
90     protected String JavaDoc getStringFromFile(String JavaDoc filePath) throws Exception JavaDoc{
91         String JavaDoc res=null;
92         BufferedReader JavaDoc in1=null;
93         in1= new BufferedReader JavaDoc(new FileReader JavaDoc(basePath+filePath));
94         res=TextUtil.getString(in1);
95         in1.close();
96         return res;
97     }
98     
99     public void serviceClientDSM(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res){
100         
101         BufferedReader JavaDoc in1=null;
102         PrintWriter JavaDoc out=null;
103            
104         String JavaDoc fileS=null;
105         
106         System.out.println("Configurator A "+basePath);
107         
108         String JavaDoc 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 JavaDoc remoteHost=null;
114             String JavaDoc port=null;
115             String JavaDoc 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 JavaDoc e){
146             e.printStackTrace();
147         }
148     }
149     
150     
151     
152     public void serviceHSQLDB(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res){
153         
154         BufferedReader JavaDoc in1=null;
155         PrintWriter JavaDoc out=null;
156            
157         String JavaDoc fileS=null;
158         
159         System.out.println("Configurator A "+basePath);
160         
161         String JavaDoc 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 JavaDoc e){
184             e.printStackTrace();
185         }
186     }
187     
188     
189     public void serviceLOG4J(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res){
190         
191         BufferedReader JavaDoc in1=null;
192         PrintWriter JavaDoc out=null;
193            
194         String JavaDoc fileS=null;
195         
196         
197         
198         String JavaDoc 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 JavaDoc e){
217             e.printStackTrace();
218         }
219     }
220     
221     
222     
223     
224     protected Connection JavaDoc getMySqlConnection(HttpServletRequest JavaDoc req) throws ClassNotFoundException JavaDoc, SQLException JavaDoc{
225         Connection JavaDoc con=null;
226         Class.forName("org.gjt.mm.mysql.Driver");
227         String JavaDoc host=req.getParameter("host");
228         String JavaDoc port=req.getParameter("port");
229         String JavaDoc db=req.getParameter("database");
230         String JavaDoc user=req.getParameter("user");
231         String JavaDoc 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 JavaDoc getMySqlJNDIConnection(HttpServletRequest JavaDoc req) throws ClassNotFoundException JavaDoc, SQLException JavaDoc{
238         Connection JavaDoc con=null;
239         String JavaDoc connS=req.getParameter("jndi");
240         try{
241             Context JavaDoc ctx=new InitialContext JavaDoc();
242             DataSource JavaDoc ds=(DataSource JavaDoc)ctx.lookup(connS);
243             con=ds.getConnection();
244         }catch(Exception JavaDoc e){
245             e.printStackTrace();
246         }
247         /*
248         String host=req.getParameter("host");
249         String port=req.getParameter("port");
250         String db=req.getParameter("database");
251         String user=req.getParameter("user");
252         String pass=req.getParameter("pass");
253         con = DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/test",user,pass);
254         con.setAutoCommit (false);
255         */

256         return con;
257     }
258     
259     public void serviceMySQL(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res){
260         
261         BufferedReader JavaDoc in1=null;
262         PrintWriter JavaDoc out=null;
263            
264         String JavaDoc fileS=null;
265         
266         System.out.println("Configurator A "+basePath);
267         
268         String JavaDoc 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"+basePath+"\n");
277
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 JavaDoc host=req.getParameter("host");
291             String JavaDoc port=req.getParameter("port");
292             String JavaDoc db=req.getParameter("database");
293             String JavaDoc user=req.getParameter("user");
294             String JavaDoc 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             //fileS=fileS.replaceAll("bAsEpAtH",basePath);
303
Connection JavaDoc con=this.getMySqlConnection(req);
304             ScriptRunner sr=null;
305             sr=new ScriptRunner();
306             
307             String JavaDoc dataB=this.getStringFromFile( "/WEB-INF/templates/config/mysql/dbGei-InnoDB.sql" );
308             dataB=dataB.replaceAll("DaTaBaSe",db);
309             
310             
311             //sr.runScript(con, new FileReader(this.basePath+ "/WEB-INF/templates/config/mysql/dbGei-InnoDB.sql" ) );
312
sr.runScript(con, new StringReader JavaDoc(dataB ) );
313             con.close();
314             
315             this.printOnFile("/WEB-INF/classes/SqlMapConfig.xml",fileS);
316             
317             
318         }catch(Throwable JavaDoc e){
319             e.printStackTrace();
320         }
321     }
322     
323     
324     public void serviceMySQLJNDI(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res){
325         
326         BufferedReader JavaDoc in1=null;
327         PrintWriter JavaDoc out=null;
328            
329         String JavaDoc fileS=null;
330         
331         System.out.println("Configurator A "+basePath);
332         
333         String JavaDoc 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"+basePath+"\n");
342
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 JavaDoc jndi=req.getParameter("jndi");
356             
357             String JavaDoc db=req.getParameter("database");
358             
359                         
360             fileS=fileS.replaceAll("JnDi",jndi);
361             
362             //fileS=fileS.replaceAll("DaTaBaSe",db);
363

364             
365             //fileS=fileS.replaceAll("bAsEpAtH",basePath);
366

367             ScriptRunner sr=null;
368             sr=new ScriptRunner();
369             String JavaDoc 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             //System.out.println("\n\n"+scriptSQL);
375
//this.printOnFile("/WEB-INF/trace.log",scriptSQL);
376
Connection JavaDoc con=this.getMySqlJNDIConnection(req);
377             sr.runScript(con, new StringReader JavaDoc(scriptSQL) );
378             
379             con.close();
380             
381             this.printOnFile("/WEB-INF/classes/SqlMapConfig.xml",fileS);
382             
383             
384         }catch(Throwable JavaDoc e){
385             e.printStackTrace();
386         }
387     }
388     
389 }
390
391
Popular Tags