1 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 ; 37 import javax.servlet.http.HttpServletResponse ; 38 import java.util.Map ; 39 import java.util.Properties ; 40 import java.io.File ; 41 import java.io.FileOutputStream ; 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 45 public class SetupDatabase implements SetupHandler { 46 public String getName() { 47 return "database"; 48 } 49 50 53 public Map setup(HttpServletRequest request, HttpServletResponse response, Configuration config, Map errors) { 54 String 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 fileStore = new File (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 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 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 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 JDBCApplicationStorage.createStorage(); 98 JDBCSnipStorage.createStorage(); 99 JDBCVersionStorage.createStorage(); 100 JDBCUserStorage.createStorage(); 101 102 } catch (Exception e) { 103 ConnectionManager.removeInstance(); 104 if (e instanceof RuntimeException ) { 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 configFile = new File (config.getWebInfDir(), "application.conf"); 118 try { 119 config.storeGlobals(new FileOutputStream (configFile)); 120 } catch (IOException 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 , SQLException { 129 System.err.println("creating internal database"); 130 File dbDir = new File (config.getWebInfDir(), "mckoidb"); 132 dbDir.mkdir(); 133 File dbConfFile = new File (config.getWebInfDir(), "mckoidb.conf"); 135 Properties dbConf = new Properties (); 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 (dbConfFile), "SnipSnap Database configuration"); 139 } 140 141 147 private boolean checkPath(String path) { 148 File pathFile = new File (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 |