1 package org
.eclipse
.linuxtools
.lttng
.jni
.factory
;
2 /*******************************************************************************
3 * Copyright (c) 2009 Ericsson
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 *******************************************************************************/
14 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniException
;
15 import org
.eclipse
.linuxtools
.lttng
.jni
.exception
.JniTraceVersionException
;
18 * <b><u>JniTraceVersion</u></b>
20 * This class is responsible of returning the correct version number of a trace at a certain given path.<p>
22 * The class will call the C library to get the correct version number from the trace.<p>
24 * Lttv library loader (liblttvtraceread_loader.so) and the default Lttv library (liblttvtraceread.so) must be installed on system and available to java.
27 public class JniTraceVersion
{
29 // Native access functions
30 protected native void ltt_getTraceVersion(String tracepath
);
32 // Variables to store version number
33 private int majorNumber
= 0;
34 private int minorNumber
= 0;
36 // To store the given tracepath
37 private String tracepath
= "";
39 // Was the trace read already?
40 private boolean wasTraceRead
= false;
43 * Default constructor.<p>
45 * Do nothing, readVersionFromTrace(path) will need to be called by the user
47 public JniTraceVersion() {
52 * Constructor that takes a tracepath parameter.<p>
54 * This constructor read the version number from the trace, so it might throw.
56 * @param newTracepath The <b>directory</b> of the trace to read.
58 * @exception JniException If the library can not be loaded,if the path is wrong or if something go wrong during the read.
60 public JniTraceVersion(String newTracepath
) throws JniTraceVersionException
{
61 // Read the version number from the trace
62 readVersionFromTrace(newTracepath
);
68 * @param oldVersion A reference to the JniTraceVersion to copy.
70 public JniTraceVersion(JniTraceVersion oldVersion
) {
71 majorNumber
= oldVersion
.majorNumber
;
72 minorNumber
= oldVersion
.minorNumber
;
76 * Read the version from the (already set) tracepath.<p>
78 * This version is used internally and will silently dismiss any exceptions.
81 private void readVersionNumberNofail() {
83 readVersionFromTrace(tracepath
);
85 catch(JniTraceVersionException e
) {
86 // Yes, we do ignore exception.
91 * Read the version from the (already set) tracepath.<p>
93 * This function throw if the library can not be loaded, if the path is wrong or if something go wrong during the read.
96 public void readVersionNumber() throws JniTraceVersionException
{
97 readVersionFromTrace(tracepath
);
101 * Read the version from a given tracepath.<p>
102 * MajorVersion and MinorVersion should be set after a successful execution of this function.<br>
104 * This function throw if the library can not be loaded,if the path is wrong or if something go wrong during the read.
107 public void readVersionFromTrace(String newTracepath
) throws JniTraceVersionException
{
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)");
114 // Otherwise set the path in case it was changed
115 tracepath
= newTracepath
;
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");
123 // Assuming the C library loaded correctly, call the JNI here.
124 ltt_getTraceVersion(tracepath
);
126 // We can now assume that the trace was read
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");
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");
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.
144 * @return major version
146 public int getMajor() {
147 if ( wasTraceRead
== false ) {
148 readVersionNumberNofail();
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.
158 * @return minor version
160 public int getMinor() {
161 if ( wasTraceRead
== false ) {
162 readVersionNumberNofail();
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.
172 * @return Full Version as float
174 public float getVersionAsFloat() {
175 if ( wasTraceRead
== false ) {
176 readVersionNumberNofail();
179 return ((float)majorNumber
+ ((float)minorNumber
)/10);
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.
186 * @return Full Version as string
188 public String
getVersionAsString() {
189 if ( wasTraceRead
== false ) {
190 readVersionNumberNofail();
193 return majorNumber
+ "." + minorNumber
;
197 * Get for the current tracepath
199 * @return The tracepath was are currently using.
201 public String
getTracepath() {
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.
210 * @param newtracepath The net tracepath
212 public void setTracepath(String newtracepath
) {
215 wasTraceRead
= false;
216 tracepath
= newtracepath
;
218 // Call the read function. This will fill up all the number if it goes well.
219 readVersionNumberNofail();
223 * This function is be called from the C side to assign the version number the Java variable.
225 private void setTraceVersionFromC(int newMajor
, int newMinor
) {
226 majorNumber
= newMajor
;
227 minorNumber
= newMinor
;
232 @SuppressWarnings("nls")
233 public String
toString() {
234 return "JniTraceVersion [" + majorNumber
+ "." + minorNumber
+ "]";