1 8 package org.apache.avalon.excalibur.logger.factory; 9 10 11 12 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 16 import javax.sql.DataSource ; 17 import org.apache.avalon.excalibur.logger.LogTargetFactory; 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.log.LogTarget; 22 import org.apache.log.output.db.ColumnInfo; 23 import org.apache.log.output.db.ColumnType; 24 import org.apache.log.output.db.DefaultJDBCTarget; 25 import org.apache.log.output.db.NormalizedJDBCTarget; 26 27 54 public class JDBCTargetFactory implements LogTargetFactory 55 { 56 public LogTarget createTarget(Configuration configuration) 57 throws ConfigurationException 58 { 59 final String dataSourceName = 60 configuration.getChild( "datasource", true ).getValue(); 61 62 final boolean normalized = 63 configuration.getChild( "normalized", true ).getValueAsBoolean( false ); 64 65 final Configuration tableConfiguration = 66 configuration.getChild( "table" ); 67 68 final String table = tableConfiguration.getAttribute( "name" ); 69 70 final Configuration[] conf = tableConfiguration.getChildren(); 71 final ColumnInfo[] columns = new ColumnInfo[ conf.length ]; 72 73 for (int i = 0; i < conf.length; i++) 74 { 75 final String name = conf[i].getValue(); 76 final int type = ColumnType.getTypeIdFor( conf[i].getName() ); 77 final String aux = conf[i].getAttribute( "aux", null ); 78 79 columns[i] = new ColumnInfo( name, type, aux ); 80 } 81 82 final DataSource dataSource; 83 84 try 85 { 86 Context ctx = new InitialContext (); 87 dataSource = (DataSource )ctx.lookup( dataSourceName ); 88 } 89 catch( final NamingException ne ) 90 { 91 throw new ConfigurationException( "Cannot lookup data source", ne ); 92 } 93 94 final LogTarget logTarget; 95 if( normalized ) 96 { 97 logTarget = new NormalizedJDBCTarget(dataSource, table, columns); 98 } 99 else 100 { 101 logTarget = new DefaultJDBCTarget( dataSource, table, columns ); 102 } 103 104 return logTarget; 105 } 106 } 107 | Popular Tags |