Java Reference

Java Reference

Loader.java
Go to the documentation of this file.
1// Copyright 2010-2018 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package com.google.ortools;
15
16import com.sun.jna.Platform;
17import java.io.IOException;
18import java.net.URI;
19import java.net.URISyntaxException;
20import java.net.URL;
21import java.nio.file.FileSystem;
22import java.nio.file.FileSystemAlreadyExistsException;
23import java.nio.file.FileSystems;
24import java.nio.file.FileVisitResult;
25import java.nio.file.Files;
26import java.nio.file.Path;
27import java.nio.file.SimpleFileVisitor;
28import java.nio.file.attribute.BasicFileAttributes;
29import java.util.Collections;
30import java.util.Objects;
31
33public class Loader {
35 private static URI getNativeResourceURI() throws IOException {
36 ClassLoader loader = Loader.class.getClassLoader();
37 String resource = Platform.RESOURCE_PREFIX + "/";
38 URL resourceURL = loader.getResource(resource);
39 Objects.requireNonNull(resourceURL,
40 String.format("Resource %s was not found in ClassLoader %s", resource, loader));
41
42 URI resourceURI;
43 try {
44 resourceURI = resourceURL.toURI();
45 } catch (URISyntaxException e) {
46 throw new IOException(e);
47 }
48 return resourceURI;
49 }
50
51 @FunctionalInterface
52 private interface PathConsumer<T extends IOException> {
53 void accept(Path path) throws T;
54 }
55
61 private static Path unpackNativeResources(URI resourceURI) throws IOException {
62 Path tempPath;
63 tempPath = Files.createTempDirectory("ortools-java");
64 tempPath.toFile().deleteOnExit();
65
66 PathConsumer<?> visitor;
67 visitor = (Path sourcePath) -> Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
68 @Override
69 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
70 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(file).toString());
71 Files.copy(file, newPath);
72 newPath.toFile().deleteOnExit();
73 return FileVisitResult.CONTINUE;
74 }
75
76 @Override
77 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
78 throws IOException {
79 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(dir).toString());
80 Files.copy(dir, newPath);
81 newPath.toFile().deleteOnExit();
82 return FileVisitResult.CONTINUE;
83 }
84 });
85
86 FileSystem fs;
87 try {
88 fs = FileSystems.newFileSystem(resourceURI, Collections.emptyMap());
89 } catch (FileSystemAlreadyExistsException e) {
90 fs = FileSystems.getFileSystem(resourceURI);
91 if (fs == null) {
92 throw new IllegalArgumentException();
93 }
94 }
95 Path p = fs.provider().getPath(resourceURI);
96 visitor.accept(p);
97 return tempPath;
98 }
99
101 private static boolean loaded = false;
102 public static void loadNativeLibraries() {
103 if (!loaded) {
104 try {
105 URI resourceURI = getNativeResourceURI();
106 Path tempPath = unpackNativeResources(resourceURI);
107 // Load the native library
108 System.load(tempPath.resolve(Platform.RESOURCE_PREFIX)
109 .resolve(System.mapLibraryName("jniortools"))
110 .toString());
111 loaded = true;
112 } catch (IOException e) {
113 throw new RuntimeException(e);
114 }
115 }
116 }
117}
Load native libraries needed for using ortools-java.
Definition: Loader.java:33
static void loadNativeLibraries()
Definition: Loader.java:102