mcconnell 2004/01/28 06:44:35
Added: merlin/logging/logkit/api/src/java/org/apache/avalon/logging/logkit
MissingIdException.java
UnknownLogTargetException.java
merlin/logging/logkit/impl/src/java/org/apache/avalon/logging/logkit/factory
PluginTargetFactory.java
merlin/logging/logkit/test/src/test/org/apache/avalon/logging/logkit/test
LoggingManagerHelper.java
Removed:
merlin/logging/logkit/impl/src/java/org/apache/avalon/logging/logkit/factory
DatagramTargetFactory.java
Log:
Addition of an initial log target plugin example.
Revision Changes Path
1.1
avalon/merlin/logging/logkit/api/src/java/org/apache/avalon/logging/logkit/MissingIdException.java
Index: MissingIdException.java
===================================================================
/*
* Copyright 2004 Apache Software Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avalon.logging.logkit;
import org.apache.avalon.logging.provider.LoggingException;
/**
* Exception to indicate that a log target configuration does not contain a target
id.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
* @version $Revision: 1.1 $ $Date: 2004/01/28 14:44:34 $
*/
public class MissingIdException
extends LogTargetException
{
/**
* Construct a new <code>MissingIdException</code> instance.
*
* @param message The detail message for this exception.
*/
public MissingIdException( final String message )
{
this( message, null );
}
/**
* Construct a new <code>MissingIdException</code> instance.
*
* @param message The detail message for this exception.
* @param throwable the root cause of the exception
*/
public MissingIdException( final String message, final Throwable throwable )
{
super( message, throwable );
}
}
1.1
avalon/merlin/logging/logkit/api/src/java/org/apache/avalon/logging/logkit/UnknownLogTargetException.java
Index: UnknownLogTargetException.java
===================================================================
/*
* Copyright 2004 Apache Software Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avalon.logging.logkit;
import org.apache.avalon.logging.provider.LoggingException;
/**
* Exception to indicate that a log target reference is unknown.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
* @version $Revision: 1.1 $ $Date: 2004/01/28 14:44:34 $
*/
public class UnknownLogTargetException
extends LogTargetException
{
/**
* Construct a new <code>UnknownLogTargetException</code> instance.
*
* @param message The detail message for this exception.
*/
public UnknownLogTargetException( final String message )
{
this( message, null );
}
/**
* Construct a new <code>UnknownLogTargetException</code> instance.
*
* @param message The detail message for this exception.
* @param throwable the root cause of the exception
*/
public UnknownLogTargetException( final String message, final Throwable
throwable )
{
super( message, throwable );
}
}
1.1
avalon/merlin/logging/logkit/impl/src/java/org/apache/avalon/logging/logkit/factory/PluginTargetFactory.java
Index: PluginTargetFactory.java
===================================================================
/*
* Copyright 2004 Apache Software Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avalon.logging.logkit.factory;
import java.io.File;
import java.util.Map;
import java.util.HashMap;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.avalon.logging.logkit.LogTargetException;
import org.apache.avalon.logging.logkit.LogTargetFactory;
import org.apache.avalon.logging.logkit.LogTargetManager;
import org.apache.avalon.repository.Artifact;
import org.apache.avalon.repository.provider.InitialContext;
import org.apache.avalon.repository.provider.Builder;
import org.apache.avalon.repository.provider.Factory;
import org.apache.excalibur.configuration.ConfigurationUtil;
import org.apache.log.LogTarget;
import org.apache.log.LogEvent;
import org.apache.log.format.ExtendedPatternFormatter;
import org.apache.log.format.Formatter;
import org.apache.log.format.PatternFormatter;
import org.apache.log.format.RawFormatter;
import org.apache.log.output.net.DatagramOutputTarget;
/**
*
*/
public class PluginTargetFactory implements LogTargetFactory
{
//--------------------------------------------------------------
// static
//--------------------------------------------------------------
private static final Resources REZ =
ResourceManager.getPackageResources( PluginTargetFactory.class );
//--------------------------------------------------------------
// immutable state
//--------------------------------------------------------------
private final ClassLoader m_classloader;
private final InitialContext m_context;
private final Logger m_logger;
private final File m_basedir;
//
// table of factories keyed by artifact spec
//
private final Map m_factories = new HashMap();
//--------------------------------------------------------------
// constructor
//--------------------------------------------------------------
public PluginTargetFactory(
InitialContext context, ClassLoader classloader,
Logger logger, File basedir )
{
m_context = context;
m_classloader = classloader;
m_logger = logger;
m_basedir = basedir;
}
//--------------------------------------------------------------
// LogTargetFactory
//--------------------------------------------------------------
/**
* Create a LogTarget based on a supplied configuration
* @param conf the target coonfiguration
* @return the log target
*/
public LogTarget createTarget( final Configuration config )
throws LogTargetException
{
String listing = ConfigurationUtil.list( config );
m_logger.info( "## CONFIG: " + listing );
final String spec = config.getAttribute( "artifact", null );
if( null == spec )
{
final String error =
REZ.getString( "plugin.error.missing-artifact" );
throw new LogTargetException( error );
}
LogTargetFactory factory = (LogTargetFactory) m_factories.get( spec );
if( null != factory)
{
return factory.createTarget( config );
}
//
// otherwise we need to construct the factory, register it
// under the spec key and return it to the client
//
try
{
final String uri = "artifact:" + spec;
Artifact artifact = Artifact.createArtifact( uri );
Builder builder = m_context.newBuilder( m_classloader, artifact );
factory = (LogTargetFactory) builder.getFactory();
//
// to do - add factory parameterization
//
m_factories.put( spec, factory );
return factory.createTarget( config );
}
catch( Throwable e )
{
final String error =
REZ.getString( "plugin.error.build", spec );
throw new LogTargetException( error, e );
}
}
}
1.1
avalon/merlin/logging/logkit/test/src/test/org/apache/avalon/logging/logkit/test/LoggingManagerHelper.java
Index: LoggingManagerHelper.java
===================================================================
/*
* Copyright 2004 Apache Software Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avalon.logging.logkit.test;
import java.io.File;
import java.net.URL;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.logging.provider.LoggingManager;
import org.apache.avalon.repository.Artifact;
import org.apache.avalon.repository.provider.Factory;
import org.apache.avalon.repository.provider.InitialContext;
import org.apache.avalon.repository.provider.Builder;
import org.apache.avalon.repository.main.DefaultInitialContext;
import org.apache.avalon.repository.main.DefaultBuilder;
import org.apache.avalon.util.env.Env;
import org.apache.avalon.util.exception.ExceptionHelper;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephen McConnell</a>
* @author $Author: mcconnell $
* @version $Revision: 1.1 $
*/
public class LoggingManagerHelper
{
//-------------------------------------------------------------------
// utilities
//-------------------------------------------------------------------
/**
* Setup the logging system.
* @param filename the name of a file in the test/conf directory
* @param bootstrap the boostrap logger logging level
* @return the logging manager
*/
public static LoggingManager setUpLoggingManager( String filename ) throws
Exception
{
InitialContext context =
new DefaultInitialContext(
getMavenRepositoryDirectory() );
//
// FIX ME - remove hard reference (get from a property)
//
Artifact artifact = Artifact.createArtifact(
"avalon-logging", "avalon-logkit-impl", "1.0-SNAPSHOT" );
Builder builder = context.newBuilder( artifact );
Factory factory = builder.getFactory();
Map criteria = factory.createDefaultCriteria();
//
// customize the criteria
//
File basedir = getBaseDir();
File target = new File( basedir, "target" );
File conf = new File( basedir, "conf" );
File file = new File( conf, filename );
//criteria.put( "avalon.logging.bootstrap", "debug" );
criteria.put( "avalon.logging.configuration", file );
criteria.put( "avalon.logging.basedir", target );
//
// create the logging manager
//
return (LoggingManager) factory.create( criteria );
}
//-------------------------------------------------------------------
// utilities
//-------------------------------------------------------------------
private static File getMavenRepositoryDirectory()
{
return new File( getMavenHomeDirectory(), "repository" );
}
private static File getMavenHomeDirectory()
{
return new File( getMavenHome() );
}
private static String getMavenHome()
{
try
{
String local =
System.getProperty(
"maven.home.local",
Env.getEnvVariable( "MAVEN_HOME_LOCAL" ) );
if( null != local ) return local;
return System.getProperty( "user.home" ) + File.separator + ".maven";
}
catch( Throwable e )
{
final String error =
"Internal error while attempting to access environment.";
final String message =
ExceptionHelper.packException( error, e, true );
throw new RuntimeException( message );
}
}
protected static File getBaseDir()
{
return new File( System.getProperty( "basedir" ) );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]