1 19 20 package org.apache.avalon.examples.jdbcdatasource; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 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 47 public class Main 48 { 49 52 private Main() {} 53 54 57 64 private static ExcaliburComponentManager createComponentManager() 65 throws Exception 66 { 67 DefaultContext context = new DefaultContext(); 69 context.makeReadOnly(); 71 72 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 74 75 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 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 DefaultRoleManager roleManager = new DefaultRoleManager(); 92 roleManager.setLogger( 93 logManager.getLogger( rolesConfig.getAttribute( "logger", "rm" ) ) ); 94 roleManager.configure( rolesConfig ); 95 96 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 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 in = new BufferedReader ( new InputStreamReader ( System.in ) ); 121 String title; 122 boolean quit = false; 123 do 124 { 125 System.out.print ( ": " ); 126 try 127 { 128 title = in.readLine(); 129 } 130 catch (IOException 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 166 173 public static void main( String [] args ) 174 throws Exception 175 { 176 System.out.println( "Running the JdbcDataSource Example Application" ); 177 178 ExcaliburComponentManager manager = createComponentManager(); 180 try 181 { 182 HelloDBService helloDB = (HelloDBService)manager.lookup( HelloDBService.ROLE ); 184 try 185 { 186 handleRequests( helloDB ); 187 } 188 finally 189 { 190 manager.release( helloDB ); 192 helloDB = null; 193 } 194 } 195 finally 196 { 197 manager.dispose(); 199 } 200 201 System.out.println(); 202 System.out.println( "Exiting..." ); 203 System.exit(0); 204 } 205 } 206 207 | Popular Tags |