KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > Install


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: Install.java,v 1.2 2002/05/08 15:48:50 per_nyfelt Exp $
8

9 package org.ozoneDB.tools;
10
11 import java.io.*;
12 import java.util.*;
13 import org.ozoneDB.*;
14 import org.ozoneDB.core.*;
15
16 /**
17  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
18  * @author Per Nyfelt
19  */

20 public class Install extends Object JavaDoc {
21     
22     protected static String JavaDoc dirName;
23
24     /* our log message outputter, we set to console per default so users of
25      * static methods does not get a NPE
26      */

27     protected static PrintWriter log = new PrintWriter(System.out);
28     
29     
30     public static void main( String JavaDoc[] args ) throws Exception JavaDoc {
31         String JavaDoc dir = File.separator + "tmp" + File.separator + "db" + File.separator;
32         boolean help = false;
33         
34         for (int i = 0; i < args.length; i++) {
35             if (args[i].startsWith( "-d" )) {
36                 dir = args[i].substring( 2 ) + File.separator;
37             } else if (args[i].startsWith( "-h" )) {
38                 help = true;
39             } else {
40                 System.out.println( "illegal option: " + args[i] );
41                 help = true;
42             }
43         }
44         
45         if (args.length == 0 || help) {
46             System.out.println( "usage: ozoneInst -d<dir> [-D<property>=<value>]*" );
47             System.out.println( " -d<directory> database directory" );
48             // System.out.println (" -id<database id> id-number of the database");
49
System.out.println( " -h shows this help" );
50             System.out.println( "" );
51             
52             System.out.println( "where properties include:" );
53             Setup defaults = new Setup( null );
54             defaults.fillWithOzoneDefaults();
55             for (Enumeration e = defaults.propertyNames(); e.hasMoreElements();) {
56                 System.out.println( " " + (String JavaDoc)e.nextElement() );
57             }
58             System.exit( 0 );
59         }
60         
61         //
62
try {
63             File file = new File( dir );
64             if (file.list().length > 0) {
65                 System.out.print( "Do you want to recursively delete all files in " + dir + " ? [y/N]" );
66                 
67                 InputStreamReader is = new InputStreamReader( System.in );
68                 
69                 int c = is.read();
70                 if (c != 'y') {
71                     System.exit( 0 );
72                 }
73             }
74         } catch (Exception JavaDoc e) {
75         }
76         
77         System.out.println( "installing database in " + dir + " ..." );
78         
79         Setup defaults = new Setup( null );
80         defaults.addProperties( System.getProperties(), "ozoneDB." );
81         createDB( dir, defaults, new PrintWriter( System.out, true ) );
82         
83         // System.out.println ("");
84
System.out.println( "Edit the file " + dir + "config.properties to change settings." );
85         System.out.println( "Ready." );
86     }
87     
88     
89     public static void createDB( String JavaDoc _dirName, Setup _defaults, PrintWriter _log ) throws Exception JavaDoc {
90         log = _log;
91         dirName = _dirName + File.separator;
92         
93         mkDir();
94         makeDataDir();
95         makeIdTableDir();
96         
97         resetConfigFile( _defaults );
98         resetStateFile( _defaults );
99     }
100     
101     
102     /**
103      * Create the ozone database directory.
104      */

105     public static void mkDir() throws Exception JavaDoc {
106         // deleteDir (dirName + Env.DATA_DIR);
107
// deleteDir (dirName + Env.STATS_DIR);
108
deleteDir( dirName, true );
109         
110         File f = new File( dirName );
111         boolean creationSuccess = true;
112         if (!f.exists()) {
113             log.println( "making dir " + dirName + " ..." );
114             creationSuccess = f.mkdirs();
115         }
116         if (!creationSuccess) {
117           throw new IOException("Failed to create db dir " + f);
118         }
119     }
120     
121     
122     public static void resetStateFile( Setup config ) throws IOException {
123         log.println( "making " + dirName + Env.STATE_FILE + " ..." );
124         
125         Setup state = new Setup( null );
126         long dbID = config.longProperty( Setup.DB_ID, 0 );
127         
128         // keep 100 ids free for internal use
129
state.setLongProperty( Setup.XOID, (dbID << 40) + 100 );
130         log.println( " ID counter = " + dbID + " * 2^40" );
131         
132         OutputStream out = new PrintStream( new FileOutputStream( new File( dirName, Env.STATE_FILE ) ) );
133         state.store( out, "Ozone Server State File.\n#Do not edit!" );
134         out.close();
135     }
136     
137     
138     public static Setup resetConfigFile( Setup _defaults ) throws IOException {
139         log.println( "making " + dirName + Env.CONFIG_FILE + " ..." );
140         
141         Setup defaults = new Setup( null );
142         defaults.fillWithOzoneDefaults();
143         defaults.addProperties( _defaults, "ozoneDB." );
144         
145         OutputStream out = new FileOutputStream( new File( dirName, Env.CONFIG_FILE ) );
146         defaults.store( out,
147                 "Ozone Config File.\n" + "#\n" + "# Do not use comments. This file will be overwritten,\n"
148                 + "# if the config changes. See the ozone documentation\n"
149                 + "# for details about the properties and their values.\n" );
150         out.close();
151         
152         defaults.print( System.out, "", " " );
153         // System.out.println ("The server will use these default settings.");
154
return defaults;
155     }
156     
157     
158     /**
159      * Create the cluster directory.
160      */

161     public static void makeDataDir() throws IOException {
162         log.println( "making " + dirName + Env.DATA_DIR + " ..." );
163         
164         File f = new File( dirName + Env.DATA_DIR );
165         boolean creationSuccess = true;
166         if (!f.exists()) {
167             creationSuccess = f.mkdir();
168         }
169         if (!creationSuccess) {
170           throw new IOException("Failed to create data dir " + f);
171         }
172     }
173     
174     
175     public static void makeIdTableDir() throws IOException {
176         log.println( "making " + dirName + Env.OS_DIR + " ..." );
177         
178         File f = new File( dirName + Env.OS_DIR );
179         boolean creationSuccess = true;
180         if (!f.exists()) {
181             creationSuccess = f.mkdir();
182         }
183         if (!creationSuccess) {
184           throw new IOException("Failed to create IdTable dir " + f);
185         }
186     }
187     
188     
189     public static void deleteDir( String JavaDoc path, boolean recursive ) throws IOException {
190         log.println( "deleting " + path + " ..." );
191         File f = new File( path );
192         String JavaDoc[] files = f.list();
193         if (files == null) {
194             return;
195         }
196         
197         for (int i = 0; i < files.length; i++) {
198             File file = new File( path, files[i] );
199             if (file.isDirectory()) {
200                 if (recursive) {
201                     deleteDir( file.getPath(), recursive );
202                 }
203             } else {
204                 if (!file.delete()) {
205                     log.println( "Unable to delete " + file + "." );
206                 }
207             }
208         }
209     }
210
211     /* Tells whether a database has been installed in the dbDir directory or not
212      * by checking to see if we have a config file in the database directory.
213      * Useful for erver when starting up the ExternalDatabase and makes it possible to
214      * determine whether to create or open a LocalDatabase
215      */

216     public static boolean dbExists(String JavaDoc dbDir) {
217         File testFile = new File( dbDir, Env.CONFIG_FILE );
218         return testFile.exists();
219     }
220 }
221
Popular Tags