From b0699d42d861733ea64f07091c7888e1c9363b50 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Tue, 24 Mar 2009 14:10:38 +0000 Subject: [PATCH] refactor: move method loadNativeLibraryFromResource() from FileUtilities to NativeLibraryUtilities SVN: 10345 --- .../cisd/common/filesystem/FileUtilities.java | 110 ------------------ .../java/ch/systemsx/cisd/common/os/Unix.java | 4 +- .../utilities/NativeLibraryUtilities.java | 76 ++++++++++++ .../common/utilities/ResourceUtilities.java | 101 ++++++++++++++++ .../common/filesystem/FileUtilitiesTest.java | 64 ---------- .../utilities/ResourceUtilitiesTest.java | 103 ++++++++++++++++ 6 files changed, 282 insertions(+), 176 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/NativeLibraryUtilities.java create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/ResourceUtilities.java create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/utilities/ResourceUtilitiesTest.java diff --git a/common/source/java/ch/systemsx/cisd/common/filesystem/FileUtilities.java b/common/source/java/ch/systemsx/cisd/common/filesystem/FileUtilities.java index 21a18abaa9c..84561c7460d 100644 --- a/common/source/java/ch/systemsx/cisd/common/filesystem/FileUtilities.java +++ b/common/source/java/ch/systemsx/cisd/common/filesystem/FileUtilities.java @@ -24,8 +24,6 @@ import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URISyntaxException; @@ -59,7 +57,6 @@ import ch.systemsx.cisd.common.logging.ISimpleLogger; import ch.systemsx.cisd.common.logging.LogLevel; import ch.systemsx.cisd.common.parser.filter.AlwaysAcceptLineFilter; import ch.systemsx.cisd.common.parser.filter.ILineFilter; -import ch.systemsx.cisd.common.utilities.OSUtilities; import ch.systemsx.cisd.common.utilities.StringUtilities; /** @@ -1208,113 +1205,6 @@ public final class FileUtilities } } - /** - * Copies the resource with the given name to a temporary file. - * - * @param resource The name of the resource to copy. - * @param prefix The prefix to use for the temporary name. - * @param postfix The postfix to use for the temporary name. - * @return The name of the temporary file. - * @throws IllegalArgumentException If the resource cannot be found in the class path. - * @throws WrappedIOException If an {@link IOException} occurs. - */ - public final static String copyResourceToTempFile(final String resource, final String prefix, - final String postfix) throws WrappedIOException - { - final InputStream resourceStream = FileUtilities.class.getResourceAsStream(resource); - if (resourceStream == null) - { - throw new IllegalArgumentException("Resource '" + resource + "' not found."); - } - try - { - final File tempFile = File.createTempFile(prefix, postfix); - tempFile.deleteOnExit(); - final OutputStream fileStream = new FileOutputStream(tempFile); - try - { - IOUtils.copy(resourceStream, fileStream); - } finally - { - IOUtils.closeQuietly(fileStream); - } - return tempFile.getAbsolutePath(); - } catch (final IOException ex) - { - throw CheckedExceptionTunnel.wrapIfNecessary(ex); - } finally - { - IOUtils.closeQuietly(resourceStream); - } - } - - /** - * Tries to copy a native library which is available as a resource to a temporary file. It will - * use the following naming schema to locate the resource containing the native library: - * <p> - * <code>/native/<libname>/<platform_id>/<libname>.so</code>. - * - * @param libraryName The name of the library. - * @return The name of the temporary file, or <code>null</code>, if the resource could not be - * copied. - */ - public final static String tryCopyNativeLibraryToTempFile(final String libraryName) - { - return tryCopyResourceToTempFile(String.format("/native/%s/%s/%s.so", libraryName, - OSUtilities.getCompatibleComputerPlatform(), libraryName), libraryName, ".so"); - } - - /** - * Loads the native library <var>libraryName</var> from a Java resource that follows tha naming - * convention described in {@link #tryCopyNativeLibraryToTempFile(String)}. - * - * @return <code>true</code> if the library has been loaded successfully and - * <code>false</code> otherwise. - */ - public final static boolean loadNativeLibraryFromResource(final String libraryName) - { - final String filename = FileUtilities.tryCopyNativeLibraryToTempFile(libraryName); - - if (filename != null) - { - final File linkLib = new File(filename); - if (linkLib.exists() && linkLib.canRead() && linkLib.isFile()) - { - try - { - System.load(filename); - return true; - } catch (final Throwable err) - { - System.err.printf("Native library '%s' failed to load:\n", filename); - err.printStackTrace(); - } - } - } - return false; - } - - /** - * Tries to copy the resource with the given name to a temporary file. - * - * @param resource The name of the resource to copy. - * @param prefix The prefix to use for the temporary name. - * @param postfix The postfix to use for the temporary name. - * @return The name of the temporary file, or <code>null</code>, if the resource could not be - * copied. - */ - public final static String tryCopyResourceToTempFile(final String resource, - final String prefix, final String postfix) - { - try - { - return copyResourceToTempFile(resource, prefix, postfix); - } catch (final Exception ex) - { - return null; - } - } - /** * Lists files of given <var>directory</var>. * <p> diff --git a/common/source/java/ch/systemsx/cisd/common/os/Unix.java b/common/source/java/ch/systemsx/cisd/common/os/Unix.java index fe5231beb12..f233b6354f1 100644 --- a/common/source/java/ch/systemsx/cisd/common/os/Unix.java +++ b/common/source/java/ch/systemsx/cisd/common/os/Unix.java @@ -22,7 +22,7 @@ import java.io.IOException; import ch.rinn.restrictions.Private; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.exceptions.WrappedIOException; -import ch.systemsx.cisd.common.filesystem.FileUtilities; +import ch.systemsx.cisd.common.utilities.NativeLibraryUtilities; /** * A utility class that provides access to common Unix system calls. Obviously, this will only work @@ -47,7 +47,7 @@ public class Unix static { - operational = FileUtilities.loadNativeLibraryFromResource("unix"); + operational = NativeLibraryUtilities.loadNativeLibraryFromResource("unix"); if (operational) { init(); diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/NativeLibraryUtilities.java b/common/source/java/ch/systemsx/cisd/common/utilities/NativeLibraryUtilities.java new file mode 100644 index 00000000000..d7d1a3946ac --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/NativeLibraryUtilities.java @@ -0,0 +1,76 @@ +/* + * Copyright 2009 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.utilities; + +import java.io.File; + +/** + * A library for loading native libraries from jar files. + * + * @author Bernd Rinn + */ +public final class NativeLibraryUtilities +{ + + /** + * Loads the native library <var>libraryName</var> from a Java resource that follows a naming + * convention described in {@link #tryCopyNativeLibraryToTempFile(String)}. + * + * @return <code>true</code> if the library has been loaded successfully and <code>false</code> + * otherwise. + */ + public static boolean loadNativeLibraryFromResource(final String libraryName) + { + final String filename = tryCopyNativeLibraryToTempFile(libraryName); + + if (filename != null) + { + final File linkLib = new File(filename); + if (linkLib.exists() && linkLib.canRead() && linkLib.isFile()) + { + try + { + System.load(filename); + return true; + } catch (final Throwable err) + { + System.err.printf("Native library '%s' failed to load:\n", filename); + err.printStackTrace(); + } + } + } + return false; + } + + /** + * Tries to copy a native library which is available as a resource to a temporary file. It will + * use the following naming schema to locate the resource containing the native library: + * <p> + * <code>/native/<libname>/<platform_id>/<libname>.so</code>. + * + * @param libraryName The name of the library. + * @return The name of the temporary file, or <code>null</code>, if the resource could not be + * copied. + */ + public static String tryCopyNativeLibraryToTempFile(final String libraryName) + { + return ResourceUtilities.tryCopyResourceToTempFile(String.format("/native/%s/%s/%s.so", + libraryName, OSUtilities.getCompatibleComputerPlatform(), libraryName), + libraryName, ".so"); + } + +} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ResourceUtilities.java b/common/source/java/ch/systemsx/cisd/common/utilities/ResourceUtilities.java new file mode 100644 index 00000000000..40f4079f7ac --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ResourceUtilities.java @@ -0,0 +1,101 @@ +/* + * Copyright 2009 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.utilities; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.commons.io.IOUtils; + +import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; +import ch.systemsx.cisd.common.exceptions.WrappedIOException; + +/** + * Utilities for handling Java resources. + * + * @author Bernd Rinn + */ +public class ResourceUtilities +{ + + /** + * Tries to copy the resource with the given name to a temporary file. + * + * @param resource The name of the resource to copy. + * @param prefix The prefix to use for the temporary name. + * @param postfix The postfix to use for the temporary name. + * @return The name of the temporary file, or <code>null</code>, if the resource could not be + * copied. + */ + public static String tryCopyResourceToTempFile(final String resource, final String prefix, + final String postfix) + { + try + { + return copyResourceToTempFile(resource, prefix, postfix); + } catch (final Exception ex) + { + return null; + } + } + + /** + * Copies the resource with the given name to a temporary file. The file will be deleted on + * program exit. + * + * @param resource The name of the resource to copy. + * @param prefix The prefix to use for the temporary name. + * @param postfix The postfix to use for the temporary name. + * @return The name of the temporary file. + * @throws IllegalArgumentException If the resource cannot be found in the class path. + * @throws WrappedIOException If an {@link IOException} occurs. + */ + public static String copyResourceToTempFile(final String resource, final String prefix, + final String postfix) throws WrappedIOException + { + final InputStream resourceStream = ResourceUtilities.class.getResourceAsStream(resource); + if (resourceStream == null) + { + throw new IllegalArgumentException("Resource '" + resource + "' not found."); + } + try + { + final File tempFile = File.createTempFile(prefix, postfix); + tempFile.deleteOnExit(); + final OutputStream fileStream = new FileOutputStream(tempFile); + try + { + IOUtils.copy(resourceStream, fileStream); + fileStream.close(); + } finally + { + IOUtils.closeQuietly(fileStream); + } + return tempFile.getAbsolutePath(); + } catch (final IOException ex) + { + throw CheckedExceptionTunnel.wrapIfNecessary(ex); + } finally + { + IOUtils.closeQuietly(resourceStream); + } + } + +} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/FileUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/FileUtilitiesTest.java index f698bc5aec7..a787495d458 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/FileUtilitiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/FileUtilitiesTest.java @@ -24,17 +24,14 @@ import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; import java.io.File; -import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.regex.Pattern; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.testng.annotations.Test; import ch.systemsx.cisd.common.Constants; @@ -374,67 +371,6 @@ public final class FileUtilitiesTest extends AbstractFileSystemTestCase assertEquals("hello", relativeFile); } - @Test(expectedExceptions = IllegalArgumentException.class) - public final void testCopyResourceToTempFileIllegalResource() - { - FileUtilities.copyResourceToTempFile("nonexistent", "pre", "post"); - } - - @Test - public final void testCopyResourceToTempFile() - { - final String resourceName = - "/" + FileUtilities.class.getCanonicalName().replaceAll("\\.", "/") + ".class"; - final String absoluteTempFileName = - FileUtilities.copyResourceToTempFile(resourceName, "pre", "post"); - assertNotNull(absoluteTempFileName); - final File tempFile = new File(absoluteTempFileName); - final String tempFileName = tempFile.getName(); - assertTrue(tempFile.exists()); - assertTrue(tempFile.length() > 0); - assertTrue(tempFileName.startsWith("pre")); - assertTrue(tempFileName.endsWith("post")); - assertTrue(Arrays.equals(resourceToByteArray(resourceName), - fileToByteArray(absoluteTempFileName))); - - } - - private byte[] resourceToByteArray(String resourcename) - { - final InputStream is = FileUtilitiesTest.class.getResourceAsStream(resourcename); - if (is == null) - { - return null; - } - try - { - return IOUtils.toByteArray(is); - } catch (IOException ex) - { - return null; - } finally - { - IOUtils.closeQuietly(is); - } - } - - private byte[] fileToByteArray(String filename) - { - InputStream is = null; - try - { - is = new FileInputStream(filename); - return IOUtils.toByteArray(is); - } catch (IOException ex) - { - return null; - } finally - { - IOUtils.closeQuietly(is); - } - - } - private class CountingActivityObserver implements IActivityObserver { int count = 0; diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ResourceUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ResourceUtilitiesTest.java new file mode 100644 index 00000000000..728205909da --- /dev/null +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ResourceUtilitiesTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.utilities; + +import static org.testng.AssertJUnit.assertNotNull; +import static org.testng.AssertJUnit.assertTrue; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +import org.apache.commons.io.IOUtils; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.filesystem.AbstractFileSystemTestCase; +import ch.systemsx.cisd.common.filesystem.FileUtilities; + +/** + * Test cases for the {@link FileUtilities}. + * + * @author Bernd Rinn + */ +public final class ResourceUtilitiesTest extends AbstractFileSystemTestCase +{ + + @Test(expectedExceptions = IllegalArgumentException.class) + public final void testCopyResourceToTempFileIllegalResource() + { + ResourceUtilities.copyResourceToTempFile("nonexistent", "pre", "post"); + } + + @Test + public final void testCopyResourceToTempFile() + { + final String resourceName = + "/" + FileUtilities.class.getCanonicalName().replaceAll("\\.", "/") + ".class"; + final String absoluteTempFileName = + ResourceUtilities.copyResourceToTempFile(resourceName, "pre", "post"); + assertNotNull(absoluteTempFileName); + final File tempFile = new File(absoluteTempFileName); + final String tempFileName = tempFile.getName(); + assertTrue(tempFile.exists()); + assertTrue(tempFile.length() > 0); + assertTrue(tempFileName.startsWith("pre")); + assertTrue(tempFileName.endsWith("post")); + assertTrue(Arrays.equals(resourceToByteArray(resourceName), + fileToByteArray(absoluteTempFileName))); + + } + + private byte[] resourceToByteArray(String resourcename) + { + final InputStream is = ResourceUtilitiesTest.class.getResourceAsStream(resourcename); + if (is == null) + { + return null; + } + try + { + return IOUtils.toByteArray(is); + } catch (IOException ex) + { + return null; + } finally + { + IOUtils.closeQuietly(is); + } + } + + private byte[] fileToByteArray(String filename) + { + InputStream is = null; + try + { + is = new FileInputStream(filename); + return IOUtils.toByteArray(is); + } catch (IOException ex) + { + return null; + } finally + { + IOUtils.closeQuietly(is); + } + + } + +} -- GitLab