1 package org
.eclipse
.linuxtools
.lttng
.jni
.factory
;
2 /*******************************************************************************
3 * Copyright (c) 2009, 2011 Ericsson, MontaVista Software
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
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 *******************************************************************************/
17 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniException
;
18 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniTraceVersionException
;
21 * <b><u>JniTraceVersion</u></b>
23 * This class is responsible of returning the correct version number of a trace at a certain given path.<p>
25 * The class will call the C library to get the correct version number from the trace.<p>
27 * Lttv library loader (liblttvtraceread_loader.so) and the default Lttv library (liblttvtraceread.so) must be installed on system and available to java.
30 public class JniTraceVersion
{
32 private static final String LTTVTRACEREAD_LOADER_LIBNAME
= "lttvtraceread_loader";
34 // Native access functions
35 protected native void ltt_getTraceVersion(String tracepath
);
37 // Variables to store version number
38 private int majorNumber
= 0;
39 private int minorNumber
= 0;
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$
46 // Was the trace read already?
47 private boolean wasTraceRead
= false;
50 * Default constructor.<p>
52 * Do nothing, readVersionFromTrace(path) will need to be called by the user
54 public JniTraceVersion() {
59 * Constructor that takes a tracepath parameter.<p>
61 * This constructor read the version number from the trace, so it might throw.
63 * @param newTracepath The <b>directory</b> of the trace to read.
64 * @param traceLibPath The <b>directory</b> of the trace libraries.
66 * @exception JniException If the library can not be loaded,if the path is wrong or if something go wrong during the read.
68 public JniTraceVersion(String newTracepath
, String traceLibPath
) throws JniTraceVersionException
{
69 // Read the version number from the trace
70 readVersionFromTrace(newTracepath
, traceLibPath
);
76 * @param oldVersion A reference to the JniTraceVersion to copy.
78 public JniTraceVersion(JniTraceVersion oldVersion
) {
79 majorNumber
= oldVersion
.majorNumber
;
80 minorNumber
= oldVersion
.minorNumber
;
84 * Read the version from the (already set) tracepath.<p>
86 * This version is used internally and will silently dismiss any exceptions.
89 private void readVersionNumberNofail() {
91 readVersionFromTrace(tracepath
, traceLibPath
);
93 catch(JniTraceVersionException e
) {
94 // Yes, we do ignore exception.
99 * Read the version from the (already set) tracepath.<p>
101 * This function throw if the library can not be loaded, if the path is wrong or if something go wrong during the read.
104 public void readVersionNumber() throws JniTraceVersionException
{
105 readVersionFromTrace(tracepath
, traceLibPath
);
109 * Read the version from a given tracepath.<p>
110 * MajorVersion and MinorVersion should be set after a successful execution of this function.<br>
112 * This function throw if the library can not be loaded,if the path is wrong or if something go wrong during the read.
115 public void readVersionFromTrace(String newTracepath
, String newTraceLibPath
) throws JniTraceVersionException
{
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$
122 // Otherwise set the path in case it was changed
123 tracepath
= newTracepath
;
125 traceLibPath
= newTraceLibPath
;
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$
133 File loaderLib
= new File(newTraceLibPath
, System
.mapLibraryName(LTTVTRACEREAD_LOADER_LIBNAME
));
134 System
.load(loaderLib
.getCanonicalPath());
136 // Assuming the C library loaded correctly, call the JNI here.
137 ltt_getTraceVersion(tracepath
);
139 // We can now assume that the trace was read
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$
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$
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.
157 * @return major version
159 public int getMajor() {
160 if ( wasTraceRead
== false ) {
161 readVersionNumberNofail();
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.
171 * @return minor version
173 public int getMinor() {
174 if ( wasTraceRead
== false ) {
175 readVersionNumberNofail();
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.
185 * @return Full Version as float
187 public float getVersionAsFloat() {
188 if ( wasTraceRead
== false ) {
189 readVersionNumberNofail();
192 return ((float)majorNumber
+ ((float)minorNumber
)/10);
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.
199 * @return Full Version as string
201 public String
getVersionAsString() {
202 if ( wasTraceRead
== false ) {
203 readVersionNumberNofail();
206 return majorNumber
+ "." + minorNumber
; //$NON-NLS-1$
210 * Get for the current tracepath
212 * @return The tracepath was are currently using.
214 public String
getTracepath() {
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.
223 * @param newtracepath The net tracepath
225 public void setTracepath(String newtracepath
) {
228 wasTraceRead
= false;
229 tracepath
= newtracepath
;
231 // Call the read function. This will fill up all the number if it goes well.
232 readVersionNumberNofail();
236 * This function is be called from the C side to assign the version number the Java variable.
238 private void setTraceVersionFromC(int newMajor
, int newMinor
) {
239 majorNumber
= newMajor
;
240 minorNumber
= newMinor
;
245 @SuppressWarnings("nls")
246 public String
toString() {
247 return "JniTraceVersion [" + majorNumber
+ "." + minorNumber
+ "]";