001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: Install.java,v 1.2 2002/05/08 15:48:50 per_nyfelt Exp $
008:
009: package org.ozoneDB.tools;
010:
011: import java.io.*;
012: import java.util.*;
013: import org.ozoneDB.*;
014: import org.ozoneDB.core.*;
015:
016: public class Install extends Object {
017:
018: protected static String dirName;
019:
020: /* our log message outputter, we set to console per default so users of
021: * static methods does not get a NPE
022: */
023: protected static PrintWriter log = new PrintWriter(System.out);
024:
025: public static void main(String[] args) throws Exception {
026: String dir = File.separator + "tmp" + File.separator + "db"
027: + File.separator;
028: boolean help = false;
029:
030: for (int i = 0; i < args.length; i++) {
031: if (args[i].startsWith("-d")) {
032: dir = args[i].substring(2) + File.separator;
033: } else if (args[i].startsWith("-h")) {
034: help = true;
035: } else {
036: System.out.println("illegal option: " + args[i]);
037: help = true;
038: }
039: }
040:
041: if (args.length == 0 || help) {
042: System.out
043: .println("usage: ozoneInst -d<dir> [-D<property>=<value>]*");
044: System.out
045: .println(" -d<directory> database directory");
046: // System.out.println (" -id<database id> id-number of the database");
047: System.out
048: .println(" -h shows this help");
049: System.out.println("");
050:
051: System.out.println("where properties include:");
052: Setup defaults = new Setup(null);
053: defaults.fillWithOzoneDefaults();
054: for (Enumeration e = defaults.propertyNames(); e
055: .hasMoreElements();) {
056: System.out.println(" " + (String) e.nextElement());
057: }
058: System.exit(0);
059: }
060:
061: //
062: try {
063: File file = new File(dir);
064: if (file.list().length > 0) {
065: System.out
066: .print("Do you want to recursively delete all files in "
067: + dir + " ? [y/N]");
068:
069: InputStreamReader is = new InputStreamReader(System.in);
070:
071: int c = is.read();
072: if (c != 'y') {
073: System.exit(0);
074: }
075: }
076: } catch (Exception e) {
077: }
078:
079: System.out.println("installing database in " + dir + " ...");
080:
081: Setup defaults = new Setup(null);
082: defaults.addProperties(System.getProperties(), "ozoneDB.");
083: createDB(dir, defaults, new PrintWriter(System.out, true));
084:
085: // System.out.println ("");
086: System.out.println("Edit the file " + dir
087: + "config.properties to change settings.");
088: System.out.println("Ready.");
089: }
090:
091: public static void createDB(String _dirName, Setup _defaults,
092: PrintWriter _log) throws Exception {
093: log = _log;
094: dirName = _dirName + File.separator;
095:
096: mkDir();
097: makeDataDir();
098: makeIdTableDir();
099:
100: resetConfigFile(_defaults);
101: resetStateFile(_defaults);
102: }
103:
104: /**
105: * Create the ozone database directory.
106: */
107: public static void mkDir() throws Exception {
108: // deleteDir (dirName + Env.DATA_DIR);
109: // deleteDir (dirName + Env.STATS_DIR);
110: deleteDir(dirName, true);
111:
112: File f = new File(dirName);
113: if (!f.exists()) {
114: log.println("making dir " + dirName + " ...");
115: f.mkdir();
116: }
117: }
118:
119: public static void resetStateFile(Setup config) throws IOException {
120: log.println("making " + dirName + Env.STATE_FILE + " ...");
121:
122: Setup state = new Setup(null);
123: long dbID = config.longProperty(Setup.DB_ID, 0);
124:
125: // keep 100 ids free for internal use
126: state.setLongProperty(Setup.XOID, (dbID << 40) + 100);
127: log.println(" ID counter = " + dbID + " * 2^40");
128:
129: OutputStream out = new PrintStream(new FileOutputStream(
130: new File(dirName, Env.STATE_FILE)));
131: state.save(out, "Ozone Server State File.\n#Do not edit!");
132: out.close();
133: }
134:
135: public static Setup resetConfigFile(Setup _defaults)
136: throws IOException {
137: log.println("making " + dirName + Env.CONFIG_FILE + " ...");
138:
139: Setup defaults = new Setup(null);
140: defaults.fillWithOzoneDefaults();
141: defaults.addProperties(_defaults, "ozoneDB.");
142:
143: OutputStream out = new FileOutputStream(new File(dirName,
144: Env.CONFIG_FILE));
145: defaults
146: .save(
147: out,
148: "Ozone Config File.\n"
149: + "#\n"
150: + "# Do not use comments. This file will be overwritten,\n"
151: + "# if the config changes. See the ozone documentation\n"
152: + "# for details about the properties and their values.\n");
153: out.close();
154:
155: defaults.print(System.out, "", " ");
156: // System.out.println ("The server will use these default settings.");
157: return defaults;
158: }
159:
160: /**
161: * Create the cluster directory.
162: */
163: public static void makeDataDir() throws IOException {
164: log.println("making " + dirName + Env.DATA_DIR + " ...");
165:
166: File f = new File(dirName + Env.DATA_DIR);
167: if (!f.exists()) {
168: f.mkdir();
169: }
170: }
171:
172: public static void makeIdTableDir() throws IOException {
173: log.println("making " + dirName + Env.OS_DIR + " ...");
174:
175: File f = new File(dirName + Env.OS_DIR);
176: if (!f.exists()) {
177: f.mkdir();
178: }
179: }
180:
181: public static void deleteDir(String path, boolean recursive)
182: throws IOException {
183: log.println("deleting " + path + " ...");
184: File f = new File(path);
185: String[] files = f.list();
186: if (files == null) {
187: return;
188: }
189:
190: for (int i = 0; i < files.length; i++) {
191: File file = new File(path, files[i]);
192: if (file.isDirectory()) {
193: if (recursive) {
194: deleteDir(file.getPath(), recursive);
195: }
196: } else {
197: if (!file.delete()) {
198: log.println("Unable to delete " + file + ".");
199: }
200: }
201: }
202: }
203:
204: /* Tells whether a database has been installed in the dbDir directory or not
205: * by checking to see if we have a config file in the database directory.
206: * Useful for erver when starting up the ExternalDatabase and makes it possible to
207: * determine whether to create or open a LocalDatabase
208: */
209: public static boolean dbExists(String dbDir) {
210: File testFile = new File(dbDir, Env.CONFIG_FILE);
211: return testFile.exists();
212: }
213: }
|