Skip to content
Permalink
Browse files

[JENKINS-46577] More robust resource loading strategy

  • Loading branch information...
cyrille-leclerc committed Dec 10, 2017
1 parent a57a33b commit 76e6db899cc954f42e120a5e31c2e747f2e3fb16
@@ -29,14 +29,14 @@
import org.apache.commons.io.IOUtils;
import org.h2.api.ErrorCode;
import org.h2.jdbcx.JdbcConnectionPool;
import org.jenkinsci.plugins.pipeline.maven.util.ClassUtils;
import org.jenkinsci.plugins.pipeline.maven.util.RuntimeIoException;
import org.jenkinsci.plugins.pipeline.maven.util.RuntimeSqlException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -334,7 +334,7 @@ protected synchronized void initializeDatabase() {
while (true) {
idx++;
String sqlScriptPath = "sql/h2/" + numberFormat.format(idx) + "_migration.sql";
InputStream sqlScriptInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(sqlScriptPath);
InputStream sqlScriptInputStream = ClassUtils.getResourceAsStream(sqlScriptPath);
if (sqlScriptInputStream == null) {
break;
} else {
@@ -354,7 +354,9 @@ protected synchronized void initializeDatabase() {
// https://issues.jenkins-ci.org/browse/JENKINS-46577
throw new IllegalStateException("Failure to load database DDL files. " +
"Files 'sql/h2/xxx_migration.sql' NOT found in the Thread Context Class Loader. " +
" Pipeline Maven Plugin may be installed in an unsupported manner");
" Pipeline Maven Plugin may be installed in an unsupported manner " +
"(thread.contextClassLoader: " + Thread.currentThread().getContextClassLoader() + ", "
+ "classLoader: " + ClassUtils.class.getClassLoader() + ")");
} else if (newSchemaVersion == initialSchemaVersion) {
// no migration was needed
} else {
@@ -0,0 +1,22 @@
package org.jenkinsci.plugins.pipeline.maven.util;

import java.io.InputStream;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class ClassUtils {

@Nullable
public static InputStream getResourceAsStream(@Nonnull String resourcePath) {
InputStream result = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);

This comment has been minimized.

Copy link
@jglick
if(result == null) {
result = ClassUtils.class.getClassLoader().getResourceAsStream(resourcePath);

This comment has been minimized.

Copy link
@jglick

jglick Dec 11, 2017

Member

Just inline this method and delete the helper class.

}
return result;
}

}
@@ -0,0 +1,18 @@
package org.jenkinsci.plugins.pipeline.maven.util;

import org.junit.Test;

import java.io.InputStream;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class ClassUtilsTest {

@Test
public void testGetResource(){
InputStream in = ClassUtils.getResourceAsStream("org/jenkinsci/plugins/pipeline/maven/util/classutils-test-1.txt");


}
}

0 comments on commit 76e6db8

Please sign in to comment.
You can’t perform that action at this time.