code style improvements.
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / DatabaseStarter.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.support.persistence;
17
18 /**
19  * This class is used for starting the database as a main program.
20  */
21 public class DatabaseStarter {
22
23     /**
24      * Database class which encapsulates management of the database.
25      */
26     private Class databaseClass;
27
28     /**
29      * Execution as a main program. Commandline
30      * 
31      * <pre>
32      * 
33      *    DatabaseStarter &lt;databaseClassName&gt;
34      * 
35      * </pre>
36      * 
37      * where the database class name must be the name of a concrete subclass of
38      * {@link Database}.
39      * 
40      * @param aArgs
41      */
42     public static void main(String[] aArgs) throws Exception {
43         String clazz = aArgs[0];
44         try {
45             new DatabaseStarter(Class.forName(clazz)).start();
46         } catch (Exception e) {
47             e.printStackTrace();
48             System.out.println("\nUsage: ant dbClass ");
49         }
50     }
51
52     /**
53      * Constructs the database starter.
54      * 
55      * @param aClassName
56      *            Classname of the database class to use.
57      * @throws Exception
58      */
59     public DatabaseStarter(Class aClass) throws Exception {
60         if (!Database.class.isAssignableFrom(aClass)) {
61             throw new IllegalArgumentException("Class '" + aClass.getName() +
62                 "' is not a subclass of Database");
63         }
64         databaseClass = aClass;
65     }
66
67     /**
68      * Constructs a database starter with the derby database.
69      * 
70      * @throws Exception
71      */
72     public DatabaseStarter() throws Exception {
73         this(DerbyDatabase.class);
74     }
75
76     /**
77      * Starts the database.
78      * 
79      * @throws Exception
80      */
81     public void start() throws Exception {
82         Database lDatabase = (Database) databaseClass.newInstance();
83         lDatabase.start();
84         System.out.println("Database has been started. ");
85         System.out.println();
86         System.out
87             .println("=======================================================");
88         System.out.println("Connection details:");
89         // System.out.println( "  Driver class: "
90         // + lDatabase.getDriverClassName( ) );
91         System.out.println("  JDBC URL:     " + lDatabase.getExternalJdbcUrl());
92         System.out.println("  username:     '" + lDatabase.getUsername() + "'");
93         System.out.println("  password:     '" + lDatabase.getPassword() + "'");
94         System.out.println("Interrupt the program to stop the database.");
95         System.out
96             .println("=======================================================");
97         System.out
98             .println("You must now populate the database with a schema. Use 'ant help' for information.");
99         for (;;) {
100             Thread.sleep(1000);
101         }
102     }
103 }