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