Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Isolate generation of the NATIVE path for the jar file.
[simgrid.git] / src / bindings / java / org / simgrid / msg / Msg.java
1 /* JNI interface to C code for MSG. */
2
3 /* Copyright (c) 2006-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 package org.simgrid.msg;
10
11 import java.io.FileOutputStream;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.io.File;
15
16
17 public final class Msg {
18         /* Statically load the library which contains all native functions used in here */
19         static private boolean isNativeInited = false;
20         public static void nativeInit() {
21                 if (isNativeInited)
22                         return;
23                 try {
24                         /* prefer the version on disk, if existing */
25                         System.loadLibrary("simgrid-java");
26                 } catch (UnsatisfiedLinkError e) {
27                         /* If not found, unpack the one bundled into the jar file and use it */
28                         loadLib("simgrid");
29                         loadLib("simgrid-java");
30                 }
31                 isNativeInited = true;
32         }
33         static {
34                 nativeInit();
35         }
36         private static void loadLib (String name) {
37                 String Path = NativeLib.getPath();
38
39                 String filename=name;
40                 InputStream in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
41                 
42                 if (in == null) {
43                         filename = "lib"+name+".so";
44                         in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
45                 } 
46                 if (in == null) {
47                         filename = name+".dll";
48                         in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
49                 }  
50                 if (in == null) {
51                         filename = "lib"+name+".dll";
52                         in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
53                 }  
54                 if (in == null) {
55                         filename = "lib"+name+".dylib";
56                         in = Msg.class.getClassLoader().getResourceAsStream(Path+filename);
57                 }  
58                 if (in == null) {
59                         throw new RuntimeException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine.");
60                 }
61 // Caching the file on disk: desactivated because it could fool the users               
62 //              if (new File(filename).isFile()) {
63 //                      // file was already unpacked -- use it directly
64 //                      System.load(new File(".").getAbsolutePath()+File.separator+filename);
65 //                      return;
66 //              }
67                 try {
68                         // We must write the lib onto the disk before loading it -- stupid operating systems
69                         File fileOut = new File(filename);
70 //                      if (!new File(".").canWrite()) {
71 //                              System.out.println("Cannot write in ."+File.separator+filename+"; unpacking the library into a temporary file instead");
72                                 fileOut = File.createTempFile("simgrid-", ".tmp");
73                                 // don't leak the file on disk, but remove it on JVM shutdown
74                                 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(fileOut.getAbsolutePath())));
75 //                      }
76 //                      System.out.println("Unpacking SimGrid native library to " + fileOut.getAbsolutePath());
77                         OutputStream out = new FileOutputStream(fileOut);
78                         
79                         /* copy the library in position */  
80                         byte[] buffer = new byte[4096]; 
81                         int bytes_read; 
82                         while ((bytes_read = in.read(buffer)) != -1)     // Read until EOF
83                                 out.write(buffer, 0, bytes_read); 
84                       
85                         /* close all file descriptors, and load that shit */
86                         in.close();
87                         out.close();
88                         System.load(fileOut.getAbsolutePath());
89                 } catch (Exception e) {
90                         System.err.println("Cannot load the bindings to the simgrid library: ");
91                         e.printStackTrace();
92                         System.err.println("This jar file does not seem to fit your system, sorry");
93                         System.exit(1);
94                 }
95         }               
96         /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */
97         private static class FileCleaner implements Runnable {
98                 private String target;
99                 public FileCleaner(String name) {
100                         target = name;
101                 }
102         public void run() {
103             try {
104                 new File(target).delete();
105             } catch(Exception e) {
106                 System.out.println("Unable to clean temporary file "+target+" during shutdown.");
107                 e.printStackTrace();
108             }
109         }    
110         }
111
112     /** Retrieve the simulation time
113      * @return The simulation time.
114      */
115         public final static native double getClock();
116         /**
117          * Issue a debug logging message.
118          * @param s message to log.
119          */
120         public final static native void debug(String s);
121         /**
122          * Issue an verbose logging message.
123          * @param s message to log.
124          */
125         public final static native void verb(String s);
126
127         /** Issue an information logging message
128      * @param s
129      */
130         public final static native void info(String s);
131         /**
132          * Issue an warning logging message.
133          * @param s message to log.
134          */
135         public final static native void warn(String s);
136         /**
137          * Issue an error logging message.
138          * @param s message to log.
139          */
140         public final static native void error(String s);
141         /**
142          * Issue an critical logging message.
143          * @param s message to log.
144          */
145         public final static native void critical(String s);
146
147         /*********************************************************************************
148          * Deployment and initialization related functions                               *
149          *********************************************************************************/
150
151         /**
152          * The natively implemented method to initialize a MSG simulation.
153          *
154          * @param args            The arguments of the command line of the simulation.
155          */
156         public final static native void init(String[]args);
157
158         /**
159          * Run the MSG simulation.
160          *
161          * The simulation is not cleaned afterward (see  
162          * {@link #clean()} if you really insist on cleaning the C side), so you can freely 
163          * retrieve the informations that you want from the simulation. In particular, retrieving the status 
164          * of a process or the current date is perfectly ok. 
165          */
166         public final static native void run() ;
167         
168         /** This function is useless nowadays, just stop calling it. */
169         @Deprecated
170         public final static void clean(){}
171
172         /**
173          * The native implemented method to create the environment of the simulation.
174          *
175          * @param platformFile    The XML file which contains the description of the environment of the simulation
176          *
177          */
178         public final static native void createEnvironment(String platformFile);
179
180         public final static native As environmentGetRoutingRoot();
181
182         /**
183          * The method to deploy the simulation.
184          *
185      *
186      * @param deploymentFile
187      */
188         public final static native void deployApplication(String deploymentFile);
189
190     /** Example launcher. You can use it or provide your own launcher, as you wish
191      * @param args
192      * @throws MsgException
193      */
194         static public void main(String[]args) throws MsgException {
195                 /* initialize the MSG simulation. Must be done before anything else (even logging). */
196                 Msg.init(args);
197
198                 if (args.length < 2) {
199                         Msg.info("Usage: Msg platform_file deployment_file");
200                         System.exit(1);
201                 }
202
203                 /* Load the platform and deploy the application */
204                 Msg.createEnvironment(args[0]);
205                 Msg.deployApplication(args[1]);
206                 /* Execute the simulation */
207                 Msg.run();
208         }
209 }