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