001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.tools;
025:
026: import java.io.*;
027: import java.util.*;
028:
029: public class MyOodbInstall {
030: private static final org.myoodb.util.Logger LOGGER = org.myoodb.util.Logger
031: .getLogger(MyOodbInstall.class);
032:
033: public static void main(String[] args) throws Exception {
034: String dir = "db" + File.separator;
035: boolean help = false;
036:
037: for (int i = 0; i < args.length; i++) {
038: if (args[i].startsWith("-d")) {
039: dir = args[i].substring(2) + File.separator;
040: } else if (args[i].startsWith("-h")) {
041: help = true;
042: } else {
043: LOGGER.error("illegal option: " + args[i]);
044: help = true;
045: }
046: }
047:
048: if (args.length == 0 || help) {
049: if (LOGGER.isInfoEnabled() == true) {
050: LOGGER.info("usage: install -d<directory>");
051: LOGGER
052: .info(" -d<directory> database directory");
053: LOGGER
054: .info(" -h shows this help");
055: LOGGER.info("");
056: }
057:
058: System.exit(0);
059: }
060:
061: createDatabase(dir);
062: }
063:
064: public static void createDatabase(String dirName) throws Exception {
065: if (LOGGER.isInfoEnabled() == true) {
066: LOGGER.info("Installing Database in " + dirName + " ...");
067: }
068:
069: makeDirectory(dirName);
070: makeDirectory(dirName, org.myoodb.core.MyOodbManager.ROOT_DIR);
071: makeDirectory(dirName, org.myoodb.core.MyOodbManager.OBJECT_DIR);
072: resetPropertyFiles(dirName);
073:
074: if (LOGGER.isInfoEnabled() == true) {
075: LOGGER.info("Installation Complete.");
076: }
077: }
078:
079: public static void makeDirectory(String dirName) throws Exception {
080: org.myoodb.core.FileHelper.delete(dirName);
081:
082: File dirFile = new File(dirName);
083:
084: if (dirFile.exists() == false) {
085: if (LOGGER.isDebugEnabled() == true) {
086: LOGGER.debug("making dir " + dirName + " ...");
087: }
088:
089: if (dirFile.mkdirs() == false) {
090: throw new IOException("Failed to create db dir "
091: + dirFile);
092: }
093: }
094: }
095:
096: public static void makeDirectory(String dirName, String component)
097: throws IOException {
098: if (LOGGER.isDebugEnabled() == true) {
099: LOGGER.debug("making " + dirName + File.separator
100: + component + " ...");
101: }
102:
103: File dirFile = new File(dirName + File.separator + component);
104:
105: if (dirFile.exists() == false) {
106: if (LOGGER.isDebugEnabled() == true) {
107: LOGGER.debug("making " + component + " dir " + dirName
108: + File.separator + component + " ...");
109: }
110:
111: if (dirFile.mkdirs() == false) {
112: throw new IOException("Failed to create " + component
113: + " db dir " + dirFile);
114: }
115: }
116: }
117:
118: public static void resetPropertyFiles(String dirName)
119: throws IOException {
120: if (LOGGER.isDebugEnabled() == true) {
121: LOGGER.debug("making " + dirName + File.separator
122: + org.myoodb.core.MyOodbManager.ID_FILE + " ...");
123: LOGGER.debug("making " + dirName + File.separator
124: + org.myoodb.core.MyOodbManager.USER_FILE + " ...");
125: }
126:
127: org.myoodb.core.Properties properties = new org.myoodb.core.Properties(
128: null);
129: properties.setLongProperty(
130: org.myoodb.core.IdentifierManager.XID, 0);
131: OutputStream out = new PrintStream(
132: new FileOutputStream(new File(dirName,
133: org.myoodb.core.MyOodbManager.ID_FILE)));
134: properties.store(out, "Identifier Property File.\n");
135: out.close();
136:
137: properties = new org.myoodb.core.Properties(null);
138: out = new PrintStream(new FileOutputStream(new File(dirName,
139: org.myoodb.core.MyOodbManager.USER_FILE)));
140: properties.store(out, "User Property File.\n");
141: out.close();
142: }
143:
144: public static boolean dbExists(String dirName) {
145: File testFile = new File(dirName,
146: org.myoodb.core.MyOodbManager.ID_FILE);
147: return testFile.exists();
148: }
149: }
|