KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > SetupDatabase


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.net.admin;
27
28 import org.snipsnap.config.Configuration;
29 import org.snipsnap.config.Globals;
30 import org.snipsnap.app.JDBCApplicationStorage;
31 import org.snipsnap.snip.storage.JDBCSnipStorage;
32 import org.snipsnap.snip.storage.JDBCUserStorage;
33 import org.snipsnap.versioning.JDBCVersionStorage;
34 import org.snipsnap.util.ConnectionManager;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44
45 public class SetupDatabase implements SetupHandler {
46   public String JavaDoc getName() {
47     return "database";
48   }
49
50   /**
51    * Set up the database which is the central data store
52    */

53   public Map JavaDoc setup(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Configuration config, Map JavaDoc errors) {
54     String JavaDoc database = request.getParameter(Configuration.APP_DATABASE);
55     config.setDatabase(database);
56
57     if ("file".equals(database)) {
58       config.setFileStore(request.getParameter(Globals.APP_FILE_STORE));
59       File JavaDoc fileStore = new File JavaDoc(config.getGlobal(Globals.APP_FILE_STORE));
60       if (checkPath(config.getGlobal(Globals.APP_FILE_STORE))) {
61         fileStore.mkdirs();
62       } else {
63         errors.put(Globals.APP_FILE_STORE, Globals.APP_FILE_STORE);
64       }
65     } else if (database.startsWith("jdbc")) {
66       boolean internalDatabase = "jdbc.mckoi".equals(database);
67       if (internalDatabase) {
68         config.setJdbcDriver(config.getGlobalDefault(Globals.APP_JDBC_DRIVER));
69         config.setJdbcUrl(config.getGlobalDefault(Globals.APP_JDBC_URL));
70         config.setJdbcUser("snipsnap");
71         config.setJdbcPassword("snipsnap");
72       } else {
73         String JavaDoc jdbcDriver = request.getParameter(Globals.APP_JDBC_DRIVER);
74         config.setJdbcDriver(jdbcDriver != null ? jdbcDriver : "");
75         config.setJdbcUrl(request.getParameter(Globals.APP_JDBC_URL));
76         config.setJdbcUser(request.getParameter(Globals.APP_JDBC_USER));
77         String JavaDoc passwd = request.getParameter(Globals.APP_JDBC_PASSWORD);
78         if (null != passwd) {
79           config.setJdbcPassword(passwd);
80         }
81       }
82
83       try {
84         Class.forName(config.getJdbcDriver());
85       } catch (ClassNotFoundException JavaDoc e) {
86         errors.put(Globals.APP_JDBC_DRIVER, Globals.APP_JDBC_DRIVER);
87         return errors;
88       }
89
90       try {
91         if (internalDatabase) {
92           createInternalDatabase(config);
93         }
94
95         // initialize storages
96
// TODO: make generic or check for type of storage
97
JDBCApplicationStorage.createStorage();
98         JDBCSnipStorage.createStorage();
99         JDBCVersionStorage.createStorage();
100         JDBCUserStorage.createStorage();
101
102       } catch (Exception JavaDoc e) {
103         ConnectionManager.removeInstance();
104         if (e instanceof RuntimeException JavaDoc) {
105           errors.put(Globals.APP_JDBC_URL, Globals.APP_JDBC_URL);
106           errors.put(Globals.APP_JDBC_USER, Globals.APP_JDBC_USER);
107           errors.put(Globals.APP_JDBC_PASSWORD, Globals.APP_JDBC_PASSWORD);
108         } else {
109           errors.put("fatal", "fatal");
110         }
111         e.printStackTrace();
112         return errors;
113       }
114     }
115
116     config.setInstalled("true");
117     File JavaDoc configFile = new File JavaDoc(config.getWebInfDir(), "application.conf");
118     try {
119       config.storeGlobals(new FileOutputStream JavaDoc(configFile));
120     } catch (IOException JavaDoc e) {
121       errors.put("fatal", "fatal");
122       e.printStackTrace();
123     }
124
125     return errors;
126   }
127
128   private static void createInternalDatabase(Globals config) throws IOException JavaDoc, SQLException JavaDoc {
129     System.err.println("creating internal database");
130 // create directories
131
File JavaDoc dbDir = new File JavaDoc(config.getWebInfDir(), "mckoidb");
132     dbDir.mkdir();
133 // store default configurationn file
134
File JavaDoc dbConfFile = new File JavaDoc(config.getWebInfDir(), "mckoidb.conf");
135     Properties JavaDoc dbConf = new Properties JavaDoc();
136     System.err.println("Creating internal database config file: " + dbConfFile.toString());
137     dbConf.load(ConfigureServlet.class.getResourceAsStream("/defaults/mckoidb.conf"));
138     dbConf.store(new FileOutputStream JavaDoc(dbConfFile), "SnipSnap Database configuration");
139   }
140
141   /**
142    * Check a path if it is writable. Returns true if one of the parents is writable and
143    * false if one of the parents is not writable.
144    * @param path the path to check
145    * @return whether the path if writable or creatable
146    */

147   private boolean checkPath(String JavaDoc path) {
148     File JavaDoc pathFile = new File JavaDoc(path);
149     while (pathFile.getParentFile() != null && !pathFile.exists()) {
150       pathFile = pathFile.getParentFile();
151     }
152     return pathFile.exists() && pathFile.canWrite();
153   }
154
155 }
156
Popular Tags