KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > examples > jdbcdatasource > Main


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.avalon.examples.jdbcdatasource;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26 import org.apache.avalon.excalibur.component.DefaultRoleManager;
27 import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
28 import org.apache.avalon.excalibur.logger.DefaultLogKitManager;
29 import org.apache.avalon.framework.configuration.Configuration;
30 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
31 import org.apache.avalon.framework.context.DefaultContext;
32 import org.apache.log.Hierarchy;
33 import org.apache.log.Logger;
34 import org.apache.log.Priority;
35
36 /**
37  * This example application creates a conmponent which makes use of a JdbcDataSource to
38  * connect to a Hypersonic SQL database. It then adds a row to a table that it creates
39  * displaying a list of all the rows in the table.
40  *
41  * Note, this code ignores exceptions to keep the code simple.
42  *
43  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
44  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:28 $
45  * @since 4.1
46  */

47 public class Main
48 {
49     /*---------------------------------------------------------------
50      * Constructors
51      *-------------------------------------------------------------*/

52     private Main() {}
53
54     /*---------------------------------------------------------------
55      * Methods
56      *-------------------------------------------------------------*/

57     /**
58      * Creates and initializes the component m_manager using config files.
59      *
60      * @return the ECM for use.
61      *
62      * @throws Exception if we cannot build the ECM
63      */

64     private static ExcaliburComponentManager createComponentManager()
65         throws Exception JavaDoc
66     {
67         // Create a context to use.
68
DefaultContext context = new DefaultContext();
69         // Add any context variables here.
70
context.makeReadOnly();
71
72         // Create a ConfigurationBuilder to parse the config files.
73
DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
74
75         // Load in the configuration files
76
Configuration logKitConfig = builder.build( "../conf/logkit.xml" );
77         Configuration rolesConfig = builder.build( "../conf/roles.xml" );
78         Configuration componentsConfig = builder.build( "../conf/components.xml" );
79
80         // Setup the LogKitManager
81
DefaultLogKitManager logManager = new DefaultLogKitManager();
82         Logger lmLogger = Hierarchy.getDefaultHierarchy().
83             getLoggerFor( logKitConfig.getAttribute( "logger", "lm" ) );
84         lmLogger.setPriority(
85             Priority.getPriorityForName( logKitConfig.getAttribute( "log-level", "INFO" ) ) );
86         logManager.setLogger( lmLogger );
87         logManager.contextualize( context );
88         logManager.configure( logKitConfig );
89
90         // Setup the RoleManager
91
DefaultRoleManager roleManager = new DefaultRoleManager();
92         roleManager.setLogger(
93             logManager.getLogger( rolesConfig.getAttribute( "logger", "rm" ) ) );
94         roleManager.configure( rolesConfig );
95
96         // Set up the ComponentManager
97
ExcaliburComponentManager manager = new ExcaliburComponentManager();
98         manager.setLogger(
99             logManager.getLogger( componentsConfig.getAttribute( "logger", "cm" ) ) );
100         manager.setLogKitManager( logManager );
101         manager.contextualize( context );
102         manager.setRoleManager( roleManager );
103         manager.configure( componentsConfig );
104         manager.initialize();
105
106         return manager;
107     }
108
109     /**
110      * Loop and handle requests from the user.
111      *
112      * @param helloDB The HelloDBService we are using to handle our requests
113      */

114     private static void handleRequests( HelloDBService helloDB )
115     {
116         System.out.println();
117         System.out.println( "Please enter a title to be added to the database" );
118         System.out.println( " (RESET deletes all titles, LIST lists all titles, QUIT or EXIT to quit)" );
119
120         BufferedReader JavaDoc in = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( System.in ) );
121         String JavaDoc title;
122         boolean quit = false;
123         do
124         {
125             System.out.print ( ": " );
126             try
127             {
128                 title = in.readLine();
129             }
130             catch (IOException JavaDoc e)
131             {
132                 title = "";
133             }
134
135             if ( title.length() > 0 )
136             {
137                 if ( title.equalsIgnoreCase( "RESET" ) )
138                 {
139                     System.out.println( "Deleting all titles currently in the database..." );
140                     helloDB.deleteRows();
141                 }
142                 else if ( title.equalsIgnoreCase( "LIST" ) )
143                 {
144                     System.out.println( "Listing all titles currently in the database..." );
145                     helloDB.logRows();
146                 }
147                 else if ( title.equalsIgnoreCase( "QUIT" ) || title.equalsIgnoreCase( "EXIT" ) )
148                 {
149                     quit = true;
150                 }
151                 else
152                 {
153                     System.out.println( "Adding title '" + title + "' to the database..." );
154                     helloDB.addRow( title );
155                 }
156             }
157         }
158         while ( !quit );
159
160         System.out.println();
161     }
162
163     /*---------------------------------------------------------------
164      * Main method
165      *-------------------------------------------------------------*/

166     /**
167      * All of the guts of this example exist in the main method.
168      *
169      * @param args The command line arguments
170      *
171      * @throws Exception if there is any problem running this example
172      */

173     public static void main( String JavaDoc[] args )
174         throws Exception JavaDoc
175     {
176         System.out.println( "Running the JdbcDataSource Example Application" );
177
178         // Create the ComponentManager
179
ExcaliburComponentManager manager = createComponentManager();
180         try
181         {
182             // Obtain a reference to the HelloDBService instance
183
HelloDBService helloDB = (HelloDBService)manager.lookup( HelloDBService.ROLE );
184             try
185             {
186                 handleRequests( helloDB );
187             }
188             finally
189             {
190                 // Release the HelloDBService instance
191
manager.release( helloDB );
192                 helloDB = null;
193             }
194         }
195         finally
196         {
197             // Dispose the ComponentManager
198
manager.dispose();
199         }
200
201         System.out.println();
202         System.out.println( "Exiting..." );
203         System.exit(0);
204     }
205 }
206
207
Popular Tags