Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b5d3dc25ac5dbb0e798c00c71f2bddb95d71ebcf (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
/*******************************************************************************
 * 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.systemtap.ui.ide.structures;

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

import org.eclipse.linuxtools.systemtap.ui.structures.TreeDefinitionNode;
import org.eclipse.linuxtools.systemtap.ui.structures.TreeNode;
import org.eclipse.linuxtools.systemtap.ui.systemtapgui.SystemTapGUISettings;
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 TreeSettings() {
	}
	/**
	 * Returns the modification date for the Tree File. Used to make sure that the cache is not out of
	 * date.
	 * @return The datestamp for the Tree file.
	 */
	public static long getTreeFileDate() {
		if(!readData()) return -1;
		return treeFileDate;
	}

	/**
	 * Allows access to the Tapset Function tree, which contains information about all
	 * functions stored in the tapset library.
	 * @return The <code>TreeNode</code> root of the Function tree.
	 */
	public static TreeNode getFunctionTree() {
		if(!readData()) return null;
		return functions;
	}

	/**
	 * Allows access to the Tapset Probe Alias tree, which contains a list of all probe aliases
	 * in the tapset library.
	 * @return The <code>TreeNode</code> root of the Probe Alias tree.
	 */
	public static TreeNode getProbeTree() {
		if(!readData()) return null;
		return probes;
	}

	/**
	 * Sets the Probe Alias and Function trees that are being cached to the trees given as arguments.
	 * @param func The Function tree to store in cache.
	 * @param probe The Probe Alias tree to store in cache.
	 * @return True if the caching is successful.
	 */
	public static boolean setTrees(TreeNode func, TreeNode probe) {
		if(null == func || null == probe) return false;
		functions = func;
		probes = probe;
		return writeData();
	}

	/**
	 * Reads the contents of the cache file into memory.
	 * @return True if the read is successful.
	 */
	private static boolean readData() {
		if(null == settingsFile && !openFile())
			return false;

		try {
			FileReader reader = new FileReader(settingsFile);

			if(!reader.ready()) {
				reader.close();
				return false;
			}

			XMLMemento data = XMLMemento.createReadRoot(reader, "TreeSettings"); //$NON-NLS-1$

			IMemento child = data.getChild("functionTree"); //$NON-NLS-1$
			String s = child.getString("string"); //$NON-NLS-1$
			if("<null>".equals(s)) //$NON-NLS-1$
				s = null;
			String d = child.getString("data"); //$NON-NLS-1$
			if("<null>".equals(d)) //$NON-NLS-1$
				d = null;

			functions = new TreeNode(d, s, false);
			readTree(child, functions, 0);

			child = data.getChild("probeTree"); //$NON-NLS-1$
			s = child.getString("string"); //$NON-NLS-1$
			if("<null>".equals(s)) //$NON-NLS-1$
				s = null;
			d = child.getString("data"); //$NON-NLS-1$
			if("<null>".equals(d)) //$NON-NLS-1$
				d = null;
			probes = new TreeNode(d, s, false);
			readTree(child, probes, 0);

			child = data.getChild("modifiedDate"); //$NON-NLS-1$
			treeFileDate = Long.parseLong(child.getString("date")); //$NON-NLS-1$
		} catch(FileNotFoundException fnfe) {
			return false;
		} catch(WorkbenchException we) {
			return false;
		} catch(IOException e) {
			return false;
		}

		return true;
	}

	/**
	 * Writes the tree data currently stored by this class to disk for later access.
	 * @return True if the write is successful.
	 */
	private static boolean writeData() {
		if(null == settingsFile && !openFile())
			return false;

		try {
			XMLMemento data = XMLMemento.createWriteRoot("TreeSettings"); //$NON-NLS-1$

			IMemento child = data.createChild("functionTree"); //$NON-NLS-1$
			writeTree(child, functions, 0);

			child = data.createChild("probeTree"); //$NON-NLS-1$
			writeTree(child, probes, 0);

			child = data.createChild("modifiedDate"); //$NON-NLS-1$
			child.putString("date", (Long.valueOf(Calendar.getInstance().getTimeInMillis())).toString()); //$NON-NLS-1$

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

		return true;
	}

	/**
	 * Writes the tree passed in to the <code>IMemento</code> argument, up to the specified depth.
	 * @param child The <code>IMemento</code> to store the tree to.
	 * @param tree The <code>TreeNode</code> to store.
	 * @param depth The maximum depth level to write out.
	 */
	private static void writeTree(IMemento child, TreeNode tree, int depth) {
		if(null == tree.toString())
			child.putString("string", "<null>"); //$NON-NLS-1$ //$NON-NLS-2$
		else
			child.putString("string", tree.toString()); //$NON-NLS-1$

		if(null == tree.getData())
			child.putString("data","<null>"); //$NON-NLS-1$ //$NON-NLS-2$
		else
			child.putString("data", tree.getData().toString()); //$NON-NLS-1$

		if(tree instanceof TreeDefinitionNode) {
			if(null == ((TreeDefinitionNode)tree).getDefinition())
				child.putString("definition","<null>"); //$NON-NLS-1$ //$NON-NLS-2$
			else
				child.putString("definition", ((TreeDefinitionNode)tree).getDefinition().toString()); //$NON-NLS-1$
		}

		child.putInteger("click", (tree.isClickable()?1:0)); //$NON-NLS-1$
		for(int i=0; i<tree.getChildCount(); i++) {
			writeTree(child.createChild("level" + depth), tree.getChildAt(i), depth+1); //$NON-NLS-1$
		}
	}

	/**
	 * Opposite action as writeTree. Reads the <code>IMemento</code> passed in into the <code>TreeNode</code>
	 * up to the requested maximum depth.
	 * @param data The <code>IMemento</code> to read the tree out of.
	 * @param parent The <code>TreeNode</code> to store the tree in.
	 * @param depth The maximum depth to read.
	 */
	private static void readTree(IMemento data, TreeNode parent, int depth) {
		IMemento[] children = data.getChildren("level" + depth); //$NON-NLS-1$

		try {
			if(null != children) {
				for(int i=0; i<children.length; i++) {
					String s = children[i].getString("string"); //$NON-NLS-1$
					String d = children[i].getString("data"); //$NON-NLS-1$
					String def = children[i].getString("definition"); //$NON-NLS-1$

					boolean c = ((0==children[i].getInteger("click").intValue())?false:true); //$NON-NLS-1$

					if("<null>".equals(s)) //$NON-NLS-1$
						s = null;
					if("<null>".equals(d)) //$NON-NLS-1$
						d = null;

					TreeNode t;
					if(null == def) {
						t = new TreeNode(d, s, c);
					} else {
						if("<null>".equals(def)) //$NON-NLS-1$
							def = null;

						t = new TreeDefinitionNode(d, s, def, c);
					}
					parent.add(t);

					readTree(children[i], t, depth+1);
				}
			}
		} catch(NullPointerException e) {
		}
	}

	private static boolean openFile() {
		settingsFile = new File(SystemTapGUISettings.settingsFolder.getAbsolutePath() + fileName);

		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 long treeFileDate;
	private static TreeNode functions;
	private static TreeNode probes;
	private static final String fileName = "/TreeSettings.xml"; //$NON-NLS-1$
	private static File settingsFile = null;
}

Back to the top