View Javadoc

1   /*
2    * orangevolt.sf.net Copyright 2008, orangevolt.sf.net, and individual
3    * contributors as indicated by the @authors tag. See the copyright.txt in the
4    * distribution for a full listing of individual contributors. This is free
5    * software; you can redistribute it and/or modify it under the terms of the GNU
6    * Lesser General Public License as published by the Free Software Foundation;
7    * either version 3 of the License, or (at your option) any later version. This
8    * software is distributed in the hope that it will be useful, but WITHOUT ANY
9    * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10   * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11   * details. You should have received a copy of the GNU Lesser General Public
12   * License along with this software; if not, write to the Free Software
13   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or
14   * see the FSF site: http://www.fsf.org. 
15   * $Id: LnkFile.java 116 2008-10-12 20:01:00Z arnep $
16   */
17  package net.sf.ovanttasks.ovnative.win32;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.util.Properties;
25  
26  /**
27   * A helper class that loads the native lib (ov-native-VERSION.dll).
28   * Load the native lib *.dll. 1st ty it the java way (dll must be in the PATH on
29   * Windows - (LD_LIBRARY_PATH on *NIX) if it fails try it in the
30   * HomeDir/.ov-native - if this fails also try to copy the dll from the resource
31   * to HomeDir/.ov-native if this fails then it gives up. So you have to pack the
32   * dll with your app or lib. see the ov-naive-demo/pom.xml for a howto.
33   * 
34   * @author arnep@users.sf.net
35   */
36  class OvNativeLibLoader {
37  
38      static boolean loaded = false;
39      static String libName;
40  
41      public static String getLibName(boolean appendVersion) {
42          if (libName == null) {
43              Properties props = new Properties();
44              try {
45                  props.load(OvNativeLibLoader.class.getResourceAsStream("ov-native.properties"));
46                  libName = "ov-native-win32";
47                  if (appendVersion) {
48                      libName += "-" + props.getProperty("net.sf.ovanttasks.ovnative.libversion");
49                  }
50              } catch (IOException e) {
51                  throw new Win32Exception(
52                          "Unable to get libname fom properties", e);
53              }
54          }
55          return libName;
56  
57      }
58  
59      /**
60       * For Unittests in ov-native-win32 it will be without Version...
61       */
62      static synchronized void loadLibWithoutVersion() {
63          if (loaded) {
64              return;
65          }
66          libName = null;
67          System.loadLibrary(getLibName(false));
68          loaded = true;
69      }
70  
71      public static synchronized void loadLib() {
72          if (loaded) {
73              return;
74          }
75          libName = null;
76          try {
77              System.loadLibrary(getLibName(true));
78              loaded = true;
79          } catch (UnsatisfiedLinkError e) {
80              File file = new File(System.getProperty("user.home") + File.separator + ".ov-native" + File.separator + getLibName(true) + ".dll");
81              if (!file.exists()) {
82                  try {
83                      if (!file.getParentFile().exists()) {
84                          file.getParentFile().mkdir();
85                      }
86                      System.out.println("Install native library " + file.getAbsolutePath());
87                      String resName = "/" + getLibName(true) + ".dll";
88                      System.out.println("Dll in Jar Name:\"" + resName + "\"");
89                      InputStream in = OvNativeLibLoader.class.getResourceAsStream(resName);
90                      if (in == null) {
91                          throw new Win32Exception("Cant find dll in resource");
92  
93                      }
94                      OutputStream out = new FileOutputStream(file);
95                      byte buffer[] = new byte[8192];
96                      int read;
97                      int written = 0;
98                      while ((read = in.read(buffer)) > -1) {
99                          written += read;
100                         out.write(buffer, 0, read);
101                     }
102                     out.close();
103                     in.close();
104                     if (written == 0) {
105                         file.delete();
106                         loaded = true;
107                     } else {
108                         loaded = true;
109                     }
110                 } catch (Exception ex) {
111                     file.delete();
112                     ex.printStackTrace();
113                     throw new Win32Exception("Unable to copy " + file.getName() + " to user.home" + File.separator + ".ov-native.",
114                             ex);
115                 }
116             }
117             System.load(file.getAbsolutePath());
118         }
119 
120     }
121 }