|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package com.nokia.carbide.cpp.internal.pi.wizards.model; |
|
18 |
|
19 import java.io.File; |
|
20 import java.io.FileInputStream; |
|
21 import java.io.FileOutputStream; |
|
22 import java.io.IOException; |
|
23 import java.io.ObjectInputStream; |
|
24 import java.io.ObjectOutputStream; |
|
25 import java.util.ArrayList; |
|
26 import java.util.Arrays; |
|
27 import java.util.List; |
|
28 |
|
29 import org.eclipse.core.runtime.IPath; |
|
30 |
|
31 import com.nokia.carbide.cpp.pi.PiPlugin; |
|
32 import com.nokia.carbide.cpp.pi.wizards.WizardsPlugin; |
|
33 |
|
34 public final class SessionHandler { |
|
35 |
|
36 private static final String SESSION_DATA_FILE_NAME = "PerformanceInvestigatorSession.bin"; //$NON-NLS-1$ |
|
37 private static final IPath SESSION_DATA_FILE_PATH = PiPlugin.getDefault() |
|
38 .getStateLocation().append(SESSION_DATA_FILE_NAME); |
|
39 private static SessionHandler instance; |
|
40 |
|
41 public static SessionHandler getInstance() { |
|
42 if (instance == null) { |
|
43 instance = new SessionHandler(); |
|
44 } |
|
45 return instance; |
|
46 } |
|
47 |
|
48 private SessionHandler() { |
|
49 |
|
50 } |
|
51 |
|
52 /** |
|
53 * Saves given trace files. |
|
54 * |
|
55 * @param files |
|
56 * @return <code>true</code> if trace files are saved otherwise |
|
57 * <code>false</code> is returned |
|
58 */ |
|
59 public boolean saveTraceFiles(List<TraceFile> files) { |
|
60 FileOutputStream fos = null; |
|
61 ObjectOutputStream out = null; |
|
62 try { |
|
63 fos = new FileOutputStream(SESSION_DATA_FILE_PATH.toFile()); |
|
64 out = new ObjectOutputStream(fos); |
|
65 out.writeObject(files); |
|
66 out.flush(); |
|
67 return true; |
|
68 } catch (Exception e) { |
|
69 e.printStackTrace(); |
|
70 return false; |
|
71 } finally { |
|
72 if (fos != null) { |
|
73 try { |
|
74 fos.close(); |
|
75 } catch (IOException e) { |
|
76 e.printStackTrace(); |
|
77 } |
|
78 } |
|
79 if (out != null) { |
|
80 try { |
|
81 out.close(); |
|
82 } catch (IOException e) { |
|
83 e.printStackTrace(); |
|
84 } |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 /** |
|
90 * Load trace files from storage |
|
91 * |
|
92 * @return array of the trace files |
|
93 */ |
|
94 @SuppressWarnings("unchecked") |
|
95 public TraceFile[] loadTraceFile() { |
|
96 File file = SESSION_DATA_FILE_PATH.toFile(); |
|
97 if (!file.exists()) { |
|
98 return new TraceFile[0]; |
|
99 } |
|
100 FileInputStream fis = null; |
|
101 ObjectInputStream in = null; |
|
102 try { |
|
103 fis = new FileInputStream(file); |
|
104 in = new ObjectInputStream(fis); |
|
105 List<TraceFile> traceFiles = (List<TraceFile>) in.readObject(); |
|
106 return traceFiles.toArray(new TraceFile[0]); |
|
107 } catch (Exception e) { |
|
108 return new TraceFile[0]; |
|
109 } finally { |
|
110 if (fis != null) { |
|
111 try { |
|
112 fis.close(); |
|
113 } catch (IOException e) { |
|
114 e.printStackTrace(); |
|
115 } |
|
116 } |
|
117 if (in != null) { |
|
118 try { |
|
119 in.close(); |
|
120 } catch (IOException e) { |
|
121 e.printStackTrace(); |
|
122 } |
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * Removes given file. |
|
129 * |
|
130 * @param traceFile |
|
131 * file to be removed |
|
132 */ |
|
133 public void removeTraceFile(TraceFile traceFile) { |
|
134 if (traceFile == null) { |
|
135 return; |
|
136 } |
|
137 List<TraceFile> traceFiles = new ArrayList<TraceFile>(); |
|
138 traceFiles.addAll(Arrays.asList(loadTraceFile())); |
|
139 boolean removed = traceFiles.remove(traceFile); |
|
140 if (removed) { |
|
141 WizardsPlugin.getDefault().getPreferenceStore().setValue( |
|
142 traceFile.getTraceFilePath().toString(), false); |
|
143 saveTraceFiles(traceFiles); |
|
144 } |
|
145 } |
|
146 |
|
147 /** |
|
148 * Get TraceFile with given path. |
|
149 * |
|
150 * @param path |
|
151 * @return instance of the TraceFile if instance is found with given path |
|
152 * otherwise null is returned |
|
153 */ |
|
154 public TraceFile getTraceFile(IPath path) { |
|
155 if (path == null) { |
|
156 return null; |
|
157 } |
|
158 List<TraceFile> traceFiles = new ArrayList<TraceFile>(); |
|
159 traceFiles.addAll(Arrays.asList(loadTraceFile())); |
|
160 for (TraceFile traceFile : traceFiles) { |
|
161 if (path.equals(traceFile.getTraceFilePath())) { |
|
162 return traceFile; |
|
163 } |
|
164 |
|
165 } |
|
166 return null; |
|
167 } |
|
168 |
|
169 /** |
|
170 * Add given file. |
|
171 * |
|
172 * @param traceFile |
|
173 * file to be added |
|
174 */ |
|
175 public void addTraceFile(TraceFile traceFile) { |
|
176 List<TraceFile> traceFileList = new ArrayList<TraceFile>(); |
|
177 traceFileList.addAll(Arrays.asList(loadTraceFile())); |
|
178 traceFileList.add(traceFile); |
|
179 saveTraceFiles(traceFileList); |
|
180 } |
|
181 } |