Skip to content

Commit

Permalink
[JENKINS-46577] More robust resource loading strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille Le Clerc committed Dec 10, 2017
1 parent a57a33b commit 76e6db8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.jdbcx.JdbcConnectionPool; 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.RuntimeIoException;
import org.jenkinsci.plugins.pipeline.maven.util.RuntimeSqlException; import org.jenkinsci.plugins.pipeline.maven.util.RuntimeSqlException;


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
Expand Down Expand Up @@ -334,7 +334,7 @@ protected synchronized void initializeDatabase() {
while (true) { while (true) {
idx++; idx++;
String sqlScriptPath = "sql/h2/" + numberFormat.format(idx) + "_migration.sql"; String sqlScriptPath = "sql/h2/" + numberFormat.format(idx) + "_migration.sql";
InputStream sqlScriptInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(sqlScriptPath); InputStream sqlScriptInputStream = ClassUtils.getResourceAsStream(sqlScriptPath);
if (sqlScriptInputStream == null) { if (sqlScriptInputStream == null) {
break; break;
} else { } else {
Expand All @@ -354,7 +354,9 @@ protected synchronized void initializeDatabase() {
// https://issues.jenkins-ci.org/browse/JENKINS-46577 // https://issues.jenkins-ci.org/browse/JENKINS-46577
throw new IllegalStateException("Failure to load database DDL files. " + throw new IllegalStateException("Failure to load database DDL files. " +
"Files 'sql/h2/xxx_migration.sql' NOT found in the Thread Context Class Loader. " + "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) { } else if (newSchemaVersion == initialSchemaVersion) {
// no migration was needed // no migration was needed
} else { } else {
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -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

jglick Dec 11, 2017

Member

Do not even bother checking this.

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;
}

}
Original file line number Original file line Diff line number Diff line change
@@ -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.