KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > tools > MyOodbInstall


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.tools;
25
26 import java.io.*;
27 import java.util.*;
28
29 public class MyOodbInstall
30 {
31     private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MyOodbInstall.class);
32
33     public static void main(String JavaDoc[] args) throws Exception JavaDoc
34     {
35         String JavaDoc dir = "db" + File.separator;
36         boolean help = false;
37         
38         for (int i = 0; i < args.length; i++)
39         {
40             if (args[i].startsWith("-d"))
41             {
42                 dir = args[i].substring(2) + File.separator;
43             }
44             else if (args[i].startsWith("-h"))
45             {
46                 help = true;
47             }
48             else
49             {
50                 LOGGER.error("illegal option: " + args[i]);
51                 help = true;
52             }
53         }
54         
55         if (args.length == 0 || help)
56         {
57             LOGGER.info("usage: install -d<directory>");
58             LOGGER.info(" -d<directory> database directory");
59             LOGGER.info(" -h shows this help");
60             LOGGER.info("");
61
62             System.exit(0);
63         }
64         
65         createDatabase(dir);
66     }
67     
68     public static void createDatabase(String JavaDoc dirName) throws Exception JavaDoc
69     {
70         LOGGER.info("Installing Database in " + dirName + " ...");
71
72         makeDirectory(dirName);
73         makeDirectory(dirName, org.myoodb.core.MyOodbManager.ROOT_DIR);
74         makeDirectory(dirName, org.myoodb.core.MyOodbManager.OBJECT_DIR);
75         resetPropertyFiles(dirName);
76
77         LOGGER.info("Installation Complete.");
78     }
79     
80     public static void makeDirectory(String JavaDoc dirName) throws Exception JavaDoc
81     {
82         org.myoodb.core.FileHelper.delete(dirName);
83
84         File dirFile = new File(dirName);
85
86         if (dirFile.exists() == false)
87         {
88             if (LOGGER.isDebugEnabled() == true)
89             {
90                 LOGGER.debug("making dir " + dirName + " ...");
91             }
92
93             if (dirFile.mkdirs() == false)
94             {
95                 throw new IOException("Failed to create db dir " + dirFile);
96             }
97         }
98     }
99     
100     public static void resetPropertyFiles(String JavaDoc dirName) throws IOException
101     {
102         if (LOGGER.isDebugEnabled() == true)
103         {
104             LOGGER.debug("making " + dirName + File.separator + org.myoodb.core.MyOodbManager.ID_FILE + " ...");
105             LOGGER.debug("making " + dirName + File.separator + org.myoodb.core.MyOodbManager.USER_FILE + " ...");
106         }
107         
108         org.myoodb.core.Properties properties = new org.myoodb.core.Properties(null);
109         properties.setLongProperty(org.myoodb.core.IdentifierManager.XID, 0);
110         OutputStream out = new PrintStream(new FileOutputStream(new File(dirName, org.myoodb.core.MyOodbManager.ID_FILE)));
111         properties.store(out, "Identifier Property File.\n");
112         out.close();
113
114         properties = new org.myoodb.core.Properties(null);
115         out = new PrintStream(new FileOutputStream(new File(dirName, org.myoodb.core.MyOodbManager.USER_FILE)));
116         properties.store(out, "User Property File.\n");
117         out.close();
118     }
119     
120     public static void makeDirectory(String JavaDoc dirName, String JavaDoc component) throws IOException
121     {
122         if (LOGGER.isDebugEnabled() == true)
123         {
124             LOGGER.debug("making " + dirName + File.separator + component + " ...");
125         }
126         
127         File dirFile = new File(dirName + File.separator + component);
128
129         if (dirFile.exists() == false)
130         {
131             if (LOGGER.isDebugEnabled() == true)
132             {
133                 LOGGER.debug("making " + component + " dir " + dirName + File.separator + component + " ...");
134             }
135
136             if (dirFile.mkdirs() == false)
137             {
138                 throw new IOException("Failed to create " + component + " db dir " + dirFile);
139             }
140         }
141     }
142     
143     public static boolean dbExists(String JavaDoc dirName)
144     {
145         File testFile = new File(dirName, org.myoodb.core.MyOodbManager.ID_FILE);
146         return testFile.exists();
147     }
148 }
149
Popular Tags