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

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/systemtap/ui/ide/structures/TapsetLibrary.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
/*******************************************************************************
 
2
 * Copyright (c) 2006 IBM Corporation.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.systemtap.ui.ide.structures;
 
13
 
 
14
import java.io.File;
 
15
 
 
16
import org.eclipse.jface.dialogs.InputDialog;
 
17
import org.eclipse.jface.preference.IPreferenceStore;
 
18
import org.eclipse.linuxtools.internal.systemtap.ui.ide.IDEPlugin;
 
19
import org.eclipse.linuxtools.internal.systemtap.ui.ide.Localization;
 
20
import org.eclipse.linuxtools.internal.systemtap.ui.ide.preferences.IDEPreferenceConstants;
 
21
import org.eclipse.linuxtools.systemtap.ui.ide.IDESessionSettings;
 
22
import org.eclipse.linuxtools.systemtap.ui.structures.TreeNode;
 
23
import org.eclipse.linuxtools.systemtap.ui.structures.listeners.IUpdateListener;
 
24
import org.eclipse.linuxtools.systemtap.ui.systemtapgui.preferences.PreferenceConstants;
 
25
import org.eclipse.ui.PlatformUI;
 
26
 
 
27
 
 
28
 
 
29
/**
 
30
 * This class is used for obtaining all probes and functions from the tapsets.
 
31
 * It will initially try to obtain the list from the TreeSettings.xml file, but
 
32
 * if there is a problem doing that it will run the TapsetParser in order to
 
33
 * obtain everything that way.
 
34
 * @author Ryan Morse
 
35
 */
 
36
public final class TapsetLibrary {
 
37
        public static TreeNode getProbes() {
 
38
                return probeTree;
 
39
        }
 
40
        
 
41
        public static TreeNode getFunctions() {
 
42
                return functionTree;
 
43
        }
 
44
        
 
45
        /**
 
46
         * This mthod will attempt to get the most up-to-date information.
 
47
         * However, if the TapsetParser is running already it will quit, 
 
48
         * assuming that new information will be avilable soon.  By registering
 
49
         * a listener at that point the class can be notified when an update is
 
50
         * available.
 
51
         */
 
52
        public static void init() {
 
53
                if(null != stpp && stpp.isRunning())
 
54
                        return;
 
55
                
 
56
                if(IDEPlugin.getDefault().getPreferenceStore()
 
57
                                .getBoolean(IDEPreferenceConstants.P_STORED_TREE) && 
 
58
                                isTreeFileCurrent())
 
59
                        readTreeFile();
 
60
                else
 
61
                        runStapParser();
 
62
        }
 
63
        
 
64
        /**
 
65
         * This method will create a new instance of the TapsetParser in order
 
66
         * to get the information directly from the files.
 
67
         */
 
68
        private static void runStapParser() {
 
69
                String[] tapsets = IDEPlugin.getDefault().getPreferenceStore()
 
70
                                                                .getString(IDEPreferenceConstants.P_TAPSETS).split(File.pathSeparator);
 
71
                
 
72
                stpp = new TapsetParser(tapsets);
 
73
                stpp.start();
 
74
                stpp.addListener(completionListener);
 
75
                functionTree = stpp.getFunctions();
 
76
                probeTree = stpp.getProbes();
 
77
        }
 
78
        
 
79
        /**
 
80
         * This method will get all of the tree information from 
 
81
         * the TreeSettings xml file.
 
82
         */
 
83
        private static void readTreeFile() {
 
84
                functionTree = TreeSettings.getFunctionTree();
 
85
                probeTree = TreeSettings.getProbeTree();
 
86
        }
 
87
        
 
88
        /**
 
89
         * This method checks to see if the tapsets have changed
 
90
         * at all since the TreeSettings.xml file was created.
 
91
         * @return boolean indecating whether or not the TreeSettings.xml file has the most up-to-date version
 
92
         */
 
93
        private static boolean isTreeFileCurrent() {
 
94
                long treesDate = TreeSettings.getTreeFileDate();
 
95
 
 
96
                IPreferenceStore p = IDEPlugin.getDefault().getPreferenceStore();
 
97
                String[] tapsets = p.getString(IDEPreferenceConstants.P_TAPSETS).split(File.pathSeparator);
 
98
 
 
99
                File f = getTapsetLocation(p);
 
100
                
 
101
                if(!checkIsCurrentFolder(treesDate, f))
 
102
                        return false;
 
103
                
 
104
                if(null != tapsets) {
 
105
                        for(int i=0; i<tapsets.length; i++) {
 
106
                                f = new File(tapsets[i]);
 
107
                                if(f.lastModified() > treesDate)
 
108
                                        return false;
 
109
                                if(f.canRead() && !checkIsCurrentFolder(treesDate, f))
 
110
                                        return false;
 
111
                        }
 
112
                }
 
113
                return true;
 
114
        }
 
115
        
 
116
        /**
 
117
         * This method attempts to locate the default tapset directory.
 
118
         * @param p Preference store where the tapset location might be stored
 
119
         * @return File representing the default tapset location.
 
120
         */
 
121
        public static File getTapsetLocation(IPreferenceStore p) {
 
122
                File f;
 
123
                String path = p.getString(PreferenceConstants.P_ENV[2][0]);
 
124
                if(path.trim().equals("")) {
 
125
                        f = new File("/usr/share/systemtap/tapset");
 
126
                        if(!f.exists()) {
 
127
                                f = new File("/usr/local/share/systemtap/tapset");
 
128
                                if(!f.exists()) {
 
129
                                        InputDialog i = new InputDialog(
 
130
                                                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 
 
131
                                                        Localization.getString("TapsetBrowserView.TapsetLocation"), Localization.getString("TapsetBrowserView.WhereDefaultTapset"), "", null);
 
132
                                        i.open();
 
133
                                        p.setValue(PreferenceConstants.P_ENV[2][0], i.getValue());
 
134
                                        f = new File( i.getValue() );
 
135
                                }
 
136
                        }
 
137
                } else {
 
138
                        f = new File( p.getString(path) );
 
139
                }
 
140
                IDESessionSettings.tapsetLocation = f.getAbsolutePath();
 
141
                return f;
 
142
        }
 
143
        
 
144
        /**
 
145
         * This method checks the provided time stap against the folders
 
146
         * time stamp.  This is to see if the folder may have new data in it
 
147
         * @param time The current time stamp
 
148
         * @param folder The folder to check if it is newer the then time stamp
 
149
         * @return boolean indicating whther the time stamp is newer then the folder
 
150
         */
 
151
        private static boolean checkIsCurrentFolder(long time, File folder) {
 
152
                File[] fs = folder.listFiles();
 
153
 
 
154
                for(int i=0; i<fs.length; i++) {
 
155
                        if(fs[i].lastModified() > time)
 
156
                                return false;
 
157
 
 
158
                        if(fs[i].isDirectory() && fs[i].canRead())
 
159
                                if(!checkIsCurrentFolder(time, fs[i]))
 
160
                                        return false;
 
161
                }
 
162
                return true;
 
163
        }
 
164
        
 
165
        /**
 
166
         * Adds a new listener to the TapsetParser
 
167
         * @param listener the listener to be added
 
168
         * @return boolean indacating whether or not the listener was added
 
169
         */
 
170
        public static boolean addListener(IUpdateListener listener) {
 
171
                if(null == stpp)
 
172
                        return false;
 
173
                
 
174
                stpp.addListener(listener);
 
175
                return true;
 
176
        }
 
177
        
 
178
        /**
 
179
         * Removes the provided listener from the tapsetParser.
 
180
         * @param listener The listener to be removed from the tapsetParser
 
181
         */
 
182
        public static void removeUpdateListener(IUpdateListener listener) {
 
183
                stpp.removeListener(listener);
 
184
        }
 
185
        
 
186
        /**
 
187
         * This class handles saving the results of the TapsetParser to 
 
188
         * the TreeSettings.xml file.
 
189
         */
 
190
        private static final IUpdateListener completionListener = new IUpdateListener() {
 
191
                public void handleUpdateEvent() {
 
192
                        functionTree = stpp.getFunctions();
 
193
                        probeTree = stpp.getProbes();
 
194
                        if(stpp.isFinishSuccessful())
 
195
                                TreeSettings.setTrees(functionTree, probeTree);
 
196
                }
 
197
        };
 
198
        
 
199
        private static TreeNode functionTree = null;
 
200
        private static TreeNode probeTree = null;
 
201
        private static TapsetParser stpp = null;
 
202
}