Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 55d55615211b973e887b998a4cffcd8f6eaa4e47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *******************************************************************************/

package org.eclipse.linuxtools.internal.systemtap.ui.ide.structures;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.structures.nodedata.StapTreeDataFactory;
import org.eclipse.linuxtools.systemtap.structures.TreeDefinitionNode;
import org.eclipse.linuxtools.systemtap.structures.TreeNode;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;


/**
 * Handles access to the cached stap tapset library tree, including reading the cache back from disk
 * on startup, writing the cache to disk when the cache is initially generated, checking to make sure
 * that the cache is up-to-date, and providing accessor methods to the rest of the IDE that allow other
 * classes to use the cached tree data.
 * @author Ryan Morse
 */
public final class TreeSettings {
    private static final String FILE_NAME = "TreeSettings"; //$NON-NLS-1$
    private static final String FILE_DIRECTORY = ".systemtapgui"; //$NON-NLS-1$

    private static final String M_DISP = "display"; //$NON-NLS-1$
    private static final String M_DATA = "data"; //$NON-NLS-1$
    private static final String M_DATATYPE = "datatype"; //$NON-NLS-1$
    private static final String M_DEFINITON = "definition"; //$NON-NLS-1$
    private static final String M_CLICKABLE = "clickable"; //$NON-NLS-1$
    private static final String M_NULL = "<null>"; //$NON-NLS-1$
    private static final String M_ITEM = "item"; //$NON-NLS-1$

    private static final String T_FUNCTIONS = "functionTree"; //$NON-NLS-1$
    private static final String T_PROBES = "probeTree"; //$NON-NLS-1$
    private static final String T_DATE = "modifiedDate"; //$NON-NLS-1$
    private static final String T_VERSION = "version"; //$NON-NLS-1$
    private static final String VERSION_NUMBER = "3.0"; //$NON-NLS-1$

    private static TreeNode cachedFunctions;
    private static TreeNode cachedProbes;
    private static File settingsFile = null;

    private TreeSettings() {}

    /**
     * Deletes the Function and Probe Alias trees that have been saved to the filesystem
     * as an {@link IMemento}.
     * @return <code>true</code> if the delete attempt succeeded, <code>false</code> otherwise.
     */
    static boolean deleteTrees() {
        boolean deleted;
        try {
            deleted = settingsFile.delete();
        } catch (SecurityException e) {
            deleted = false;
        }
        if (deleted) {
            clearCachedTrees();
        }
        return deleted;
    }

    /**
     * Saves the provided Function and Probe Alias trees into an {@link IMemento} on
     * the filesystem. <p>
     * Note: Both trees must be saved at the same time to better ensure that they
     * are both obtained from the same tapset state.
     * @param functions The Function tree to store in cache.
     * @param probes The Probe Alias tree to store in cache.
     * @return <code>true</code> if the caching is successful.
     */
    public static synchronized boolean setTrees(TreeNode functions, TreeNode probes) {
        if (functions == null || probes == null || !isTreeFileAvailable()) {
            return false;
        }

        XMLMemento data = XMLMemento.createWriteRoot(FILE_NAME);
        writeTree(data, T_FUNCTIONS, functions);
        writeTree(data, T_PROBES, probes);

        data.createChild(T_DATE)
            .putTextData((Long.valueOf(Calendar.getInstance().getTimeInMillis())).toString());

        data.createChild(T_VERSION).putTextData(VERSION_NUMBER);

        try (FileWriter writer = new FileWriter(settingsFile)) {
            data.save(writer);
        } catch (IOException e) {
            return false;
        }

        clearCachedTrees();
        return true;
    }

    /**
     * Writes the tree passed in to the {@link IMemento} argument.
     * @param data The {@link IMemento} to store the tree to.
     * @param name The name to give to the <code>parent</code> node.
     * @param tree The {@link TreeNode} to store.
     */
    private static void writeTree(IMemento data, String name, TreeNode tree) {
        IMemento child = data.createChild(name);
        child.putString(M_DISP, tree.toString());
        Object treeData = tree.getData();
        if (treeData != null) {
            child.putString(M_DATA, treeData.toString());
            child.putString(M_DATATYPE, StapTreeDataFactory.getDataObjectID(treeData));
        }
        if (tree instanceof TreeDefinitionNode) {
            child.putString(M_DEFINITON,
                    getStringFromValue(((TreeDefinitionNode) tree).getDefinition()));
        }
        child.putBoolean(M_CLICKABLE, tree.isClickable());
        for (int i = 0, n = tree.getChildCount(); i < n; i++) {
            writeTree(child, M_ITEM, tree.getChildAt(i));
        }
    }

    private static void clearCachedTrees() {
        if (cachedFunctions != null) {
            cachedFunctions.dispose();
            cachedFunctions = null;
        }
        if (cachedProbes != null) {
            cachedProbes.dispose();
            cachedProbes = null;
        }
    }

    /**
     * Allows access to the Tapset Function tree, which contains information about all
     * functions stored in the tapset library.
     * @return The {@link TreeNode} root of the Function tree.
     * @since 2.0
     */
    public static synchronized TreeNode getFunctionTree() {
        if (cachedFunctions == null) {
            cachedFunctions = readData(T_FUNCTIONS);
        }
        return cachedFunctions;
    }

    /**
     * Allows access to the Tapset Probe Alias tree, which contains a list of all probe aliases
     * in the tapset library.
     * @return The {@link TreeNode} root of the Probe Alias tree.
     * @since 2.0
     */
    public synchronized static TreeNode getProbeTree() {
        if (cachedProbes == null) {
            cachedProbes = readData(T_PROBES);
        }
        return cachedProbes;
    }

    /**
     * Reads the contents of the cached memento to recreate the stored trees.
     * @return True if the read is successful.
     */
    private static TreeNode readData(String section) {
        IMemento data = getTreeFileMemento();
        if (data == null) {
            return null;
        }
        return readTree(data.getChild(section));
    }

    /**
     * Opposite action as writeTree. Reconstruct a tree from a previously-saved {@link IMemento}.
     * @param data The {@link IMemento} to read the tree out of.
     * @return The reconstructed {@link TreeNode}.
     */
    private static TreeNode readTree(IMemento data) {
        String disp = data.getString(M_DISP);
        String def = data.getString(M_DEFINITON);
        boolean c = data.getBoolean(M_CLICKABLE);
        Object d = StapTreeDataFactory.createObjectFromString(data.getString(M_DATA), data.getString(M_DATATYPE));

        TreeNode parent;
        if (def == null) {
            parent = new TreeNode(d, disp, c);
        } else {
            parent = new TreeDefinitionNode(d, disp, getValueFromString(def), c);
        }
        for (IMemento child : data.getChildren()) {
            parent.add(readTree(child));
        }
        return parent;
    }

    /**
     * Returns the modification date for the tree file.
     * Use this to make sure that the cache is not out of date.
     * @return The datestamp for the Tree file.
     */
    public synchronized static long getTreeFileDate() {
        IMemento data = getTreeFileMemento();
        if (data != null) {
            IMemento child = data.getChild(T_DATE);
            try {
                return Long.parseLong(child.getTextData());
            } catch (NumberFormatException e) {}
        }
        return -1;
    }

    private static IMemento getTreeFileMemento() {
        if (!isTreeFileAvailable()) {
            return null;
        }

        try (FileReader reader = new FileReader(settingsFile)) {
            IMemento data = XMLMemento.createReadRoot(reader, FILE_NAME);
            IMemento versionChild = data.getChild(T_VERSION);
            if (versionChild != null && versionChild.getTextData().equals(VERSION_NUMBER)) {
                return data;
            }
            return null;
        } catch (IOException | WorkbenchException fnfe) {
            return null;
        }
    }

    private static boolean isTreeFileAvailable() {
        if (settingsFile != null) {
            return true;
        }

        IPath path = new Path(System.getenv("HOME")). //$NON-NLS-1$
                append(FILE_DIRECTORY).append(FILE_NAME).
                addFileExtension("xml"); //$NON-NLS-1$
        settingsFile = path.toFile();

        try {
            if (!settingsFile.exists()){
                // Create a new settings file-and its parent
                // directories- if one does not exist.
                settingsFile.getParentFile().mkdirs();
                settingsFile.createNewFile();
            }
        } catch (IOException ioe) {
            return false;
        }

        return true;
    }

    private static String getStringFromValue(String val) {
        return val == null ? M_NULL : val;
    }

    private static String getValueFromString(String string) {
        return M_NULL.equals(string) ? null : string;
    }
}

Back to the top