~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng.jni/src/org/eclipse/linuxtools/lttng/jni/factory/JniTraceVersion.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

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