Skip to content

Replace plexus logging by slf4j #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
<artifactId>plexus-component-annotations</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>

<!-- TEST -->
<dependency>
Expand All @@ -57,6 +62,12 @@
<version>3.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.simplify4u</groupId>
<artifactId>slf4j-mock</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@
* SOFTWARE.
*/

import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.resource.PlexusResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.List;

/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public abstract class AbstractResourceLoader
extends AbstractLogEnabled
implements ResourceLoader
public abstract class AbstractResourceLoader implements ResourceLoader
{
protected List<String> paths = new ArrayList<String>();
protected List<String> paths = new ArrayList<>();

public void addSearchPath( String path )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.resource.PlexusResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -39,6 +41,8 @@
public class JarResourceLoader
extends AbstractResourceLoader
{
private static final Logger LOGGER = LoggerFactory.getLogger( ResourceLoader.class );

public static final String ID = "jar";

/**
Expand All @@ -50,14 +54,14 @@ public class JarResourceLoader
* Maps JAR URLs to the actual JAR (key = the JAR URL, value = the JAR).
*/
private Map<String, JarHolder> jarfiles = new LinkedHashMap<String, JarHolder>( 89 );

private boolean initializeCalled;

public void initialize()
throws InitializationException
{
initializeCalled = true;

if ( paths != null )
{
for ( int i = 0; i < paths.size(); i++ )
Expand All @@ -69,21 +73,17 @@ public void initialize()

private void loadJar( String path )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "JarResourceLoader : trying to load \"" + path + "\"" );
}
LOGGER.debug( "JarResourceLoader : trying to load '{}'", path );

// Check path information
if ( path == null )
{
getLogger().error( "JarResourceLoader : can not load JAR - JAR path is null" );
LOGGER.error( "JarResourceLoader : can not load JAR - JAR path is null" );
return;
}
if ( !path.startsWith( "jar:" ) )
{
getLogger().error(
"JarResourceLoader : JAR path must start with jar: -> "
LOGGER.error( "JarResourceLoader : JAR path must start with jar: -> "
+ "see java.net.JarURLConnection for information" );
return;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ private void addEntries( Map entries )

/**
* Get an InputStream so that the Runtime can build a template with it.
*
*
* @param source name of template to get
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found in the file template path.
Expand All @@ -147,7 +147,7 @@ public PlexusResource getResource( String source )
throw new ResourceNotFoundException( e.getMessage(), e );
}
}

if ( source == null || source.length() == 0 )
{
throw new ResourceNotFoundException( "Need to have a resource!" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
* SOFTWARE.
*/

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.resource.PlexusResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.resource.PlexusResource;

/**
* @author Jason van Zyl
*/
Expand All @@ -42,9 +44,11 @@ public class URLResourceLoader
extends AbstractResourceLoader
{

private static final Logger LOGGER = LoggerFactory.getLogger( URLResourceLoader.class );

public static final String ID = "url";

protected Map<String, String> templateRoots = new HashMap<String, String>();
protected Map<String, String> templateRoots = new HashMap<>();

/**
* Get an InputStream so that the Runtime can build a template with it.
Expand All @@ -71,10 +75,7 @@ public PlexusResource getResource( String name )

if ( inputStream != null )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "URLResourceLoader: Found '" + name + "' at '" + path + "'" );
}
LOGGER.debug( "URLResourceLoader: Found '{}' at '{}'", name, path );

// save this root for later re-use
templateRoots.put( name, path );
Expand All @@ -98,19 +99,11 @@ public synchronized InputStream getInputStream()
}
catch( MalformedURLException mue )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "URLResourceLoader: No valid URL '" + path + name + '\'' );
}
LOGGER.debug( "URLResourceLoader: No valid URL '{}{}'", path, name );
}
catch ( IOException ioe )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug(
"URLResourceLoader: Exception when looking for '" + name + "' at '" + path + "'",
ioe );
}
LOGGER.debug( "URLResourceLoader: Exception when looking for '{}' at '{}'", name, path, ioe );
}
}

Expand Down Expand Up @@ -142,18 +135,12 @@ public synchronized InputStream getInputStream()
}
catch( MalformedURLException mue )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "URLResourceLoader: No valid URL '" + name + '\'' );
}
LOGGER.debug( "URLResourceLoader: No valid URL '{}'", name );
}
catch ( IOException ioe )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "URLResourceLoader: Exception when looking for '" + name + '\'', ioe );
}
}
LOGGER.debug( "URLResourceLoader: Exception when looking for '{}'", name, ioe );
}

// convert to a general Velocity ResourceNotFoundException
throw new ResourceNotFoundException( name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.File;

import org.codehaus.plexus.resource.PlexusResource;
import org.codehaus.plexus.resource.loader.AbstractResourceLoaderTest;

/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
package org.codehaus.plexus.resource.loader;

import static org.mockito.Mockito.when;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import junit.framework.TestCase;

import org.codehaus.plexus.logging.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;


@RunWith( MockitoJUnitRunner.class )
public class URLResourceLoaderTest
extends TestCase
{
@Mock
private Logger logger;

@InjectMocks
private ResourceLoader resourceLoader = new URLResourceLoader();

@Override
protected void setUp()
throws Exception
{
MockitoAnnotations.initMocks( this );
}

@Test
public void testMalformedURL()
throws Exception
{
when( logger.isDebugEnabled() ).thenReturn( true );

try
{
resourceLoader.getResource( "LICENSE.txt" );
fail();
}
catch ( ResourceNotFoundException e )
{
verify( logger ).isDebugEnabled();
verify( logger ).debug( "URLResourceLoader: No valid URL 'LICENSE.txt'" );
verify( logger ).debug( "URLResourceLoader: No valid URL '{}'", "LICENSE.txt" );
verifyNoMoreInteractions( logger );
assertEquals( "Could not find resource 'LICENSE.txt'.", e.getMessage() );
}
Expand Down