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
1 package org.eclipse.linuxtools.lttng.jni.factory;
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 *******************************************************************************/
13
14 import org.eclipse.linuxtools.lttng.jni.exception.JniException;
15 import org.eclipse.linuxtools.lttng.jni.exception.JniTraceVersionException;
16
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 */
27 public class JniTraceVersion {
28
29 // Native access functions
30 protected native void ltt_getTraceVersion(String tracepath);
31
32 // Variables to store version number
33 private int majorNumber = 0;
34 private int minorNumber = 0;
35
36 // To store the given tracepath
37 private String tracepath = "";
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 */
47 public JniTraceVersion() {
48 // Nothing to do
49 }
50
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 */
60 public JniTraceVersion(String newTracepath) throws JniTraceVersionException {
61 // Read the version number from the trace
62 readVersionFromTrace(newTracepath);
63 }
64
65 /**
66 * Copy constructor.
67 *
68 * @param oldVersion A reference to the JniTraceVersion to copy.
69 */
70 public JniTraceVersion(JniTraceVersion oldVersion) {
71 majorNumber = oldVersion.majorNumber;
72 minorNumber = oldVersion.minorNumber;
73 }
74
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 }
85 catch(JniTraceVersionException e) {
86 // Yes, we do ignore exception.
87 }
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 */
96 public void readVersionNumber() throws JniTraceVersionException {
97 readVersionFromTrace(tracepath);
98 }
99
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 {
108
109 // Verify that the tracepath isn't obliviously wrong (null or empty)
110 if ( (newTracepath == null) || (newTracepath.equals("") ) ) {
111 throw new JniTraceVersionException("ERROR : Tracepath is null or empty! (readVersionNumber)");
112 }
113 else {
114 // Otherwise set the path in case it was changed
115 tracepath = newTracepath;
116 }
117
118 try {
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.
124 ltt_getTraceVersion(tracepath);
125
126 // We can now assume that the trace was read
127 wasTraceRead = true;
128 }
129 // The library was unable to load -> Lttv not installed or bad version of it?
130 catch (java.lang.UnsatisfiedLinkError e) {
131 throw new JniTraceVersionException("\nERROR : Could not get trace version. Is the library missing?" +
132 "\nMake sure your \"LD_LIBRARY_PATH\" is setted correctly (readVersionNumber)\n");
133 }
134 // Something else failed -> Possibly a bad tracepath was given
135 catch (Exception e) {
136 throw new JniTraceVersionException("\nERROR : Call to ltt_getTraceVersion failed. (readVersionNumber)\n");
137 }
138 }
139
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 */
146 public int getMajor() {
147 if ( wasTraceRead == false ) {
148 readVersionNumberNofail();
149 }
150
151 return majorNumber;
152 }
153
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 */
160 public int getMinor() {
161 if ( wasTraceRead == false ) {
162 readVersionNumberNofail();
163 }
164
165 return minorNumber;
166 }
167
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 */
174 public float getVersionAsFloat() {
175 if ( wasTraceRead == false ) {
176 readVersionNumberNofail();
177 }
178
179 return ((float)majorNumber + ((float)minorNumber)/10);
180 }
181
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 */
201 public String getTracepath() {
202 return tracepath;
203 }
204
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 */
212 public void setTracepath(String newtracepath) {
213 majorNumber = 0;
214 minorNumber = 0;
215 wasTraceRead = false;
216 tracepath = newtracepath;
217
218 // Call the read function. This will fill up all the number if it goes well.
219 readVersionNumberNofail();
220 }
221
222 /*
223 * This function is be called from the C side to assign the version number the Java variable.
224 */
225 private void setTraceVersionFromC(int newMajor, int newMinor) {
226 majorNumber = newMajor;
227 minorNumber = newMinor;
228 }
229
230
231 @Override
232 @SuppressWarnings("nls")
233 public String toString() {
234 return "JniTraceVersion [" + majorNumber + "." + minorNumber + "]";
235 }
236
237 }
This page took 0.035117 seconds and 5 git commands to generate.