Commit | Line | Data |
---|---|---|
0152140d | 1 | package org.eclipse.linuxtools.lttng.jni.factory; |
c357e4b6 | 2 | /******************************************************************************* |
a3767fd9 | 3 | * Copyright (c) 2009, 2011 Ericsson, MontaVista Software |
84b2b1f0 | 4 | * |
c357e4b6 WB |
5 | * All rights reserved. This program and the accompanying materials are |
6 | * made available under the terms of the Eclipse Public License v1.0 which | |
7 | * accompanies this distribution, and is available at | |
8 | * http://www.eclipse.org/legal/epl-v10.html | |
84b2b1f0 | 9 | * |
c357e4b6 WB |
10 | * Contributors: |
11 | * William Bourque (wbourque@gmail.com) - Initial API and implementation | |
a3767fd9 | 12 | * Yufen Kuo (ykuo@mvista.com) - add support to allow user specify trace library path |
c357e4b6 | 13 | *******************************************************************************/ |
0152140d | 14 | |
a3767fd9 FC |
15 | import java.io.File; |
16 | ||
ce38c104 | 17 | import org.eclipse.linuxtools.internal.lttng.jni.exception.JniTraceVersionException; |
0152140d | 18 | |
c357e4b6 | 19 | /** |
c357e4b6 | 20 | * This class is responsible of returning the correct version number of a trace at a certain given path.<p> |
84b2b1f0 | 21 | * |
c357e4b6 | 22 | * The class will call the C library to get the correct version number from the trace.<p> |
84b2b1f0 | 23 | * |
c357e4b6 | 24 | * Lttv library loader (liblttvtraceread_loader.so) and the default Lttv library (liblttvtraceread.so) must be installed on system and available to java. |
84b2b1f0 | 25 | * |
d37aaa7f FC |
26 | * @version 0.1 |
27 | * @author William Bourque | |
c357e4b6 | 28 | */ |
0152140d | 29 | public class JniTraceVersion { |
84b2b1f0 | 30 | |
81ec645c | 31 | private static final String LTTVTRACEREAD_LOADER_LIBNAME = "lttvtraceread_loader"; //$NON-NLS-1$ |
a3767fd9 | 32 | |
c357e4b6 | 33 | // Native access functions |
0152140d | 34 | protected native void ltt_getTraceVersion(String tracepath); |
84b2b1f0 | 35 | |
c357e4b6 | 36 | // Variables to store version number |
a9fcdd8d WB |
37 | private int majorNumber = 0; |
38 | private int minorNumber = 0; | |
84b2b1f0 | 39 | |
c357e4b6 | 40 | // To store the given tracepath |
9c4eb5f7 | 41 | private String tracepath = ""; //$NON-NLS-1$ |
a3767fd9 FC |
42 | // To store the given trace lib path |
43 | private String traceLibPath = ""; //$NON-NLS-1$ | |
84b2b1f0 | 44 | |
c357e4b6 WB |
45 | // Was the trace read already? |
46 | private boolean wasTraceRead = false; | |
84b2b1f0 | 47 | |
c357e4b6 WB |
48 | /** |
49 | * Default constructor.<p> | |
84b2b1f0 | 50 | * |
c357e4b6 WB |
51 | * Do nothing, readVersionFromTrace(path) will need to be called by the user |
52 | */ | |
687cc7e7 | 53 | public JniTraceVersion() { |
84b2b1f0 | 54 | // Nothing to do |
0152140d | 55 | } |
84b2b1f0 AM |
56 | |
57 | /** | |
58 | * Constructor that takes a tracepath parameter. | |
59 | * <p> | |
60 | * | |
61 | * This constructor read the version number from the trace, so it might | |
62 | * throw. | |
63 | * | |
64 | * @param newTracepath | |
65 | * The <b>directory</b> of the trace to read. | |
66 | * @param traceLibPath | |
67 | * The <b>directory</b> of the trace libraries. | |
68 | * | |
69 | * @exception JniTraceVersionException | |
70 | * If the library can not be loaded,if the path is wrong or | |
71 | * if something go wrong during the read. | |
c357e4b6 | 72 | */ |
a3767fd9 | 73 | public JniTraceVersion(String newTracepath, String traceLibPath) throws JniTraceVersionException { |
84b2b1f0 | 74 | // Read the version number from the trace |
a3767fd9 | 75 | readVersionFromTrace(newTracepath, traceLibPath); |
687cc7e7 | 76 | } |
84b2b1f0 | 77 | |
c357e4b6 WB |
78 | /** |
79 | * Copy constructor. | |
84b2b1f0 AM |
80 | * |
81 | * @param oldVersion A reference to the JniTraceVersion to copy. | |
c357e4b6 | 82 | */ |
687cc7e7 WB |
83 | public JniTraceVersion(JniTraceVersion oldVersion) { |
84 | majorNumber = oldVersion.majorNumber; | |
85 | minorNumber = oldVersion.minorNumber; | |
86 | } | |
84b2b1f0 | 87 | |
c357e4b6 WB |
88 | /* |
89 | * Read the version from the (already set) tracepath.<p> | |
84b2b1f0 | 90 | * |
c357e4b6 | 91 | * This version is used internally and will silently dismiss any exceptions. |
84b2b1f0 | 92 | * |
c357e4b6 WB |
93 | */ |
94 | private void readVersionNumberNofail() { | |
95 | try { | |
a3767fd9 | 96 | readVersionFromTrace(tracepath, traceLibPath); |
c357e4b6 | 97 | } |
84b2b1f0 | 98 | catch(JniTraceVersionException e) { |
e0ea56dd WB |
99 | // Yes, we do ignore exception. |
100 | } | |
c357e4b6 | 101 | } |
84b2b1f0 AM |
102 | |
103 | /** | |
104 | * Read the version from the (already set) trace path. | |
105 | * <p> | |
106 | * | |
107 | * @throws JniTraceVersionException | |
108 | * If the library can not be loaded, if the path is wrong or if | |
109 | * something go wrong during the read. | |
110 | * | |
111 | */ | |
687cc7e7 | 112 | public void readVersionNumber() throws JniTraceVersionException { |
a3767fd9 | 113 | readVersionFromTrace(tracepath, traceLibPath); |
0152140d | 114 | } |
84b2b1f0 AM |
115 | |
116 | /** | |
117 | * Read the version from a given tracepath. | |
118 | * <p> | |
119 | * MajorVersion and MinorVersion should be set after a successful execution | |
120 | * of this function.<br> | |
121 | * | |
122 | * @param newTracepath | |
123 | * The path of the new trace | |
124 | * @param newTraceLibPath | |
125 | * The path to the trace reading library to use | |
126 | * @throws JniTraceVersionException | |
127 | * If the library can not be loaded,if the path is wrong or if | |
128 | * something go wrong during the read. | |
129 | * | |
130 | */ | |
a3767fd9 | 131 | public void readVersionFromTrace(String newTracepath, String newTraceLibPath) throws JniTraceVersionException { |
84b2b1f0 | 132 | |
687cc7e7 | 133 | // Verify that the tracepath isn't obliviously wrong (null or empty) |
9c4eb5f7 FC |
134 | if ( (newTracepath == null) || (newTracepath.equals("") ) ) { //$NON-NLS-1$ |
135 | throw new JniTraceVersionException("ERROR : Tracepath is null or empty! (readVersionNumber)"); //$NON-NLS-1$ | |
687cc7e7 | 136 | } |
84b2b1f0 AM |
137 | // Otherwise set the path in case it was changed |
138 | tracepath = newTracepath; | |
a3767fd9 | 139 | traceLibPath = newTraceLibPath; |
84b2b1f0 | 140 | |
0152140d | 141 | try { |
a3767fd9 FC |
142 | if (newTraceLibPath == null || newTraceLibPath.isEmpty()) { |
143 | // Load the C library here. | |
144 | // If LD_LIBRARY_PATH is not set correctly this will raise a java.lang.UnsatisfiedLinkError | |
84b2b1f0 | 145 | System.loadLibrary(LTTVTRACEREAD_LOADER_LIBNAME); |
a3767fd9 FC |
146 | } else { |
147 | File loaderLib = new File(newTraceLibPath, System.mapLibraryName(LTTVTRACEREAD_LOADER_LIBNAME)); | |
148 | System.load(loaderLib.getCanonicalPath()); | |
149 | } | |
687cc7e7 | 150 | // Assuming the C library loaded correctly, call the JNI here. |
0152140d | 151 | ltt_getTraceVersion(tracepath); |
a3767fd9 | 152 | |
c357e4b6 WB |
153 | // We can now assume that the trace was read |
154 | wasTraceRead = true; | |
0152140d | 155 | } |
84b2b1f0 | 156 | // The library was unable to load -> Lttv not installed or bad version of it? |
0152140d | 157 | catch (java.lang.UnsatisfiedLinkError e) { |
9c4eb5f7 FC |
158 | throw new JniTraceVersionException("\nERROR : Could not get trace version. Is the library missing?" + //$NON-NLS-1$ |
159 | "\nMake sure your \"LD_LIBRARY_PATH\" is setted correctly (readVersionNumber)\n"); //$NON-NLS-1$ | |
0152140d | 160 | } |
84b2b1f0 | 161 | // Something else failed -> Possibly a bad tracepath was given |
0152140d | 162 | catch (Exception e) { |
9c4eb5f7 | 163 | throw new JniTraceVersionException("\nERROR : Call to ltt_getTraceVersion failed. (readVersionNumber)\n"); //$NON-NLS-1$ |
0152140d ASL |
164 | } |
165 | } | |
84b2b1f0 | 166 | |
c357e4b6 WB |
167 | /** |
168 | * Get major version number of the trace.<p> | |
169 | * Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored. | |
84b2b1f0 | 170 | * |
c357e4b6 WB |
171 | * @return major version |
172 | */ | |
0152140d | 173 | public int getMajor() { |
c357e4b6 WB |
174 | if ( wasTraceRead == false ) { |
175 | readVersionNumberNofail(); | |
176 | } | |
84b2b1f0 | 177 | |
0152140d ASL |
178 | return majorNumber; |
179 | } | |
84b2b1f0 | 180 | |
c357e4b6 WB |
181 | /** |
182 | * Get minor version number of the trace.<p> | |
183 | * Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored. | |
84b2b1f0 | 184 | * |
c357e4b6 WB |
185 | * @return minor version |
186 | */ | |
0152140d | 187 | public int getMinor() { |
c357e4b6 WB |
188 | if ( wasTraceRead == false ) { |
189 | readVersionNumberNofail(); | |
190 | } | |
84b2b1f0 | 191 | |
0152140d ASL |
192 | return minorNumber; |
193 | } | |
84b2b1f0 | 194 | |
c357e4b6 WB |
195 | /** |
196 | * Get full version number of the trace.<p> | |
197 | * Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored. | |
84b2b1f0 | 198 | * |
c357e4b6 WB |
199 | * @return Full Version as float |
200 | */ | |
687cc7e7 | 201 | public float getVersionAsFloat() { |
c357e4b6 WB |
202 | if ( wasTraceRead == false ) { |
203 | readVersionNumberNofail(); | |
204 | } | |
84b2b1f0 AM |
205 | |
206 | return (majorNumber + ((float)minorNumber)/10); | |
687cc7e7 | 207 | } |
84b2b1f0 | 208 | |
c357e4b6 WB |
209 | /** |
210 | * Get full version number of the trace.<p> | |
211 | * Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored. | |
84b2b1f0 | 212 | * |
c357e4b6 WB |
213 | * @return Full Version as string |
214 | */ | |
215 | public String getVersionAsString() { | |
216 | if ( wasTraceRead == false ) { | |
217 | readVersionNumberNofail(); | |
218 | } | |
84b2b1f0 | 219 | |
9c4eb5f7 | 220 | return majorNumber + "." + minorNumber; //$NON-NLS-1$ |
c357e4b6 | 221 | } |
84b2b1f0 | 222 | |
c357e4b6 WB |
223 | /** |
224 | * Get for the current tracepath | |
84b2b1f0 | 225 | * |
c357e4b6 WB |
226 | * @return The tracepath was are currently using. |
227 | */ | |
687cc7e7 WB |
228 | public String getTracepath() { |
229 | return tracepath; | |
230 | } | |
84b2b1f0 | 231 | |
c357e4b6 WB |
232 | /** |
233 | * Set for the tracepath.<p> | |
234 | * NOTE : Changing this will reset the version number currently loaded. | |
235 | * NOTE2 : readVersionFromTrace() will be called but exception will be silently ignored. | |
84b2b1f0 | 236 | * |
c357e4b6 WB |
237 | * @param newtracepath The net tracepath |
238 | */ | |
687cc7e7 | 239 | public void setTracepath(String newtracepath) { |
c357e4b6 WB |
240 | majorNumber = 0; |
241 | minorNumber = 0; | |
242 | wasTraceRead = false; | |
687cc7e7 | 243 | tracepath = newtracepath; |
84b2b1f0 | 244 | |
c357e4b6 WB |
245 | // Call the read function. This will fill up all the number if it goes well. |
246 | readVersionNumberNofail(); | |
687cc7e7 | 247 | } |
84b2b1f0 | 248 | |
c357e4b6 WB |
249 | /* |
250 | * This function is be called from the C side to assign the version number the Java variable. | |
251 | */ | |
687cc7e7 | 252 | private void setTraceVersionFromC(int newMajor, int newMinor) { |
0152140d ASL |
253 | majorNumber = newMajor; |
254 | minorNumber = newMinor; | |
255 | } | |
84b2b1f0 AM |
256 | |
257 | ||
b9fb2d51 | 258 | @Override |
3b38ea61 | 259 | @SuppressWarnings("nls") |
0152140d | 260 | public String toString() { |
c357e4b6 | 261 | return "JniTraceVersion [" + majorNumber + "." + minorNumber + "]"; |
0152140d | 262 | } |
84b2b1f0 | 263 | |
0152140d | 264 | } |