BowlerKernel
NativeResource.java
Go to the documentation of this file.
1 package com.neuronrobotics.imageprovider;
2 
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 
9 public class NativeResource {
10 
11  private boolean loaded = false;
12  public synchronized void load(String libraryName) throws NativeResourceException {
13  if(loaded)
14  return;
15  loaded = true;
16  if(System.getProperty(libraryName + ".userlib") != null) {
17  try {
18  if(System.getProperty(libraryName + ".userlib").equalsIgnoreCase("sys")) {
19  System.loadLibrary(libraryName);
20  } else {
21  System.load(System.getProperty(libraryName + ".userlib"));
22  }
23  return;
24  } catch (Exception e){
25  e.printStackTrace();
26  throw new NativeResourceException("Unable to load native resource from given path.\n" + e.getLocalizedMessage());
27  }
28  }
29  loadLib(libraryName);
30  }
31 
32 
33 
34  private void inJarLoad(String name)throws UnsatisfiedLinkError, NativeResourceException{
35  //start by assuming the library can be loaded from the jar
36  InputStream resourceSource = locateResource(name);
37  File resourceLocation;
38  try {
39  resourceLocation = inJarLoad(resourceSource,name);
40  loadResource(resourceLocation);
42  } catch (IOException e) {
43  // TODO Auto-generated catch block
44  e.printStackTrace();
45  }
46 
47  }
48  @SuppressWarnings("rawtypes")
49  public static File inJarLoad(Class inputClass, String name) throws IOException{
50  InputStream resourceSource = inputClass.getResourceAsStream(name);
51  File resourceLocation = prepResourceLocation(name);
52  //System.out.println("Resource selected "+resourceSource);
53  //System.out.println("Resource target "+resourceLocation);
54 
55  copyResource(resourceSource, resourceLocation);
56  return resourceLocation;
57  }
58  public static File inJarLoad(InputStream inputStream, String name) throws IOException{
59  InputStream resourceSource = inputStream;
60  File resourceLocation = prepResourceLocation(name);
61  //System.out.println("Resource selected "+resourceSource);
62  //System.out.println("Resource target "+resourceLocation);
63 
64  copyResource(resourceSource, resourceLocation);
65  return resourceLocation;
66  }
67  private void loadLib(String name) throws NativeResourceException {
68 
69 
70  inJarLoad(name);
71 
72  return;
73 
74  }
75 
76  private void testNativeCode()throws UnsatisfiedLinkError {
77 
78  }
79 
80  private InputStream locateResource(String name) {
81  name += getExtension();
82  String file="";
83  if( isOSX()) {
84  file="/native/osx/" + name;
85  }else if( isWindows()) {
86  if( is64Bit()){
87  file="/native/windows/x86_64/" + name;
88  }else {
89  file="/native/windows/x86_32/" + name;
90  }
91  }else if( isLinux()) {
92  if( isARM()) {
93  file = "/native/linux/ARM/" + name;
94  }else {
95  if( is64Bit()) {
96  file="/native/linux/x86_64/" + name;
97  }else {
98  file="/native/linux/x86_32/" + name;
99  }
100  }
101  }else{
102  System.err.println("Can't load native file: "+name+" for os arch: "+ getOsArch());
103  return null;
104  }
105  //System.out.println("Loading "+file);
106  return getClass().getResourceAsStream(file);
107  }
108 
109  private void loadResource(File resource) {
110  if(!resource.canRead())
111  throw new RuntimeException("Cant open JNI file: "+resource.getAbsolutePath());
112  //System.out.println("Loading: "+resource.getAbsolutePath());
113  System.load(resource.getAbsolutePath());
114  }
115 
116  public static void copyResource(InputStream io, File file) throws IOException {
117  FileOutputStream fos = new FileOutputStream(file);
118 
119 
120  byte[] buf = new byte[256];
121  int read = 0;
122  while ((read = io.read(buf)) > 0) {
123  fos.write(buf, 0, read);
124  }
125  fos.close();
126  io.close();
127  }
128 
129  public static File prepResourceLocation(String fileName) throws NativeResourceException {
130  String tmpDir = System.getProperty("java.io.tmpdir");
131  //String tmpDir = "M:\\";
132  if ((tmpDir == null) || (tmpDir.length() == 0)) {
133  tmpDir = "tmp";
134  }
135 
136  String displayName = new File(fileName).getName().split("\\.")[0];
137 
138  String user = System.getProperty("user.name");
139 
140  File fd = null;
141  File dir = null;
142 
143  for(int i = 0; i < 10; i++) {
144  dir = new File(tmpDir, displayName + "_" + user + "_" + (i));
145  if (dir.exists()) {
146  if (!dir.isDirectory()) {
147  continue;
148  }
149 
150  try {
151  File[] files = dir.listFiles();
152  for (int j = 0; j < files.length; j++) {
153  if (!files[j].delete()) {
154  continue;
155  }
156  }
157  } catch (Throwable e) {
158 
159  }
160  }
161 
162  if ((!dir.exists()) && (!dir.mkdirs())) {
163  continue;
164  }
165 
166  try {
167  dir.deleteOnExit();
168  } catch (Throwable e) {
169  // Java 1.1 or J9
170  }
171 
172  fd = new File(dir, fileName + getExtension());
173  if ((fd.exists()) && (!fd.delete())) {
174  continue;
175  }
176 
177  try {
178  if (!fd.createNewFile()) {
179  continue;
180  }
181  } catch (IOException e) {
182  continue;
183  } catch (Throwable e) {
184  // Java 1.1 or J9
185  }
186 
187  break;
188  }
189 
190  if(fd == null || !fd.canRead()) {
191  throw new NativeResourceException("Unable to deploy native resource");
192  }
193  //System.out.println("Local file: "+fd.getAbsolutePath());
194  return fd;
195  }
196 
197 
198  public static boolean is64Bit() {
200  return getOsArch().startsWith("x86_64") || getOsArch().startsWith("amd64");
201  }
202  public static boolean isARM() {
203  return getOsArch().startsWith("arm");
204  }
205  public static boolean isCortexA8(){
206  if(isARM()){
207  //TODO check for cortex a8 vs arm9 generic
208  return true;
209  }
210  return false;
211  }
212  public static boolean isWindows() {
214  return getOsName().toLowerCase().startsWith("windows") ||getOsName().toLowerCase().startsWith("microsoft") || getOsName().toLowerCase().startsWith("ms");
215  }
216 
217  public static boolean isLinux() {
218  return getOsName().toLowerCase().startsWith("linux");
219  }
220 
221  public static boolean isOSX() {
222  return getOsName().toLowerCase().startsWith("mac");
223  }
224 
225  public static String getExtension() {
226  return "";
227  }
228 
229  public static String getOsName() {
230  return System.getProperty("os.name");
231  }
232 
233  public static String getOsArch() {
234  return System.getProperty("os.arch");
235  }
236 
237  @SuppressWarnings("unused")
238  public static String getIdentifier() {
239  return getOsName() + " : " + getOsArch();
240  }
241 
242 
243 }
synchronized void load(String libraryName)
static void copyResource(InputStream io, File file)
static File inJarLoad(InputStream inputStream, String name)
static File prepResourceLocation(String fileName)