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