2fe0683afa73acb0636d581a7eec12fe62c63ab0
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / DatabaseStarter.java
1 /*
2  * Copyright 2005 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 /**
20  * This class is used for starting the database from ant.
21  */
22 public class DatabaseStarter {
23
24     /**
25      * Database class which encapsulates management of the database.
26      */
27     private Class databaseClass;
28
29     /**
30      * Execution as a main program. Commandline
31      * 
32      * <pre>
33      * 
34      *    DatabaseStarter &lt;databaseClassName&gt;
35      *  
36      * </pre>
37      * 
38      * where the database class name must be the name of a concrete subclass of
39      * {@link Database}.
40      * 
41      * @param args
42      */
43     public static void main( String[] args ) throws Exception {
44         String clazz = args[0];
45         try {
46             new DatabaseStarter( Class.forName( clazz ) ).start( );
47         } catch ( Exception e ) {
48             e.printStackTrace( );
49             System.out
50                     .println( "\nUsage: ant dbClass ");
51         }
52     }
53
54     /**
55      * Constructs the database starter.
56      * 
57      * @param aClassName
58      *            Classname of the database class to use.
59      * @throws Exception
60      */
61     public DatabaseStarter( Class aClass ) throws Exception {
62         if ( !Database.class.isAssignableFrom( aClass ) ) {
63             throw new IllegalArgumentException( "Class '"
64                     + aClass.getName( )
65                     + "' is not a subclass of Database" );
66         }
67         databaseClass = aClass;
68     }
69
70     /**
71      * Constructs a database starter with the derby database.
72      * 
73      * @throws Exception
74      */
75     public DatabaseStarter( ) throws Exception {
76         this( DerbyDatabase.class );
77     }
78
79     /**
80      * Starts the database.
81      * 
82      * @throws Exception
83      */
84     public void start( ) throws Exception {
85         Database lDatabase = (Database) databaseClass.newInstance( );
86         lDatabase.start( );
87         System.out.println( "Database has been started. " );
88         System.out.println( );
89         System.out.println("=======================================================");
90         System.out.println( "Connection details:" );
91        // System.out.println( "  Driver class: "
92        //         + lDatabase.getDriverClassName( ) );
93         System.out.println( "  JDBC URL:     "
94                 + lDatabase.getExternalJdbcUrl( ) );
95         System.out.println( "  username:     '" + lDatabase.getUsername( )
96                 + "'" );
97         System.out.println( "  password:     '" + lDatabase.getPassword( )
98                 + "'" );
99         System.out.println( "Interrupt the program to stop the database." );
100         System.out.println("=======================================================");
101         System.out.println("You must now populate the database with a schema. Use 'ant help' for information.");
102         for ( ;; ) {
103             Thread.sleep( 1000 );
104         }
105     }
106 }