diff -r 5b9d4d8641ce -r ae255c9aa552 sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/model/SessionHandler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/model/SessionHandler.java Wed Jun 23 15:05:09 2010 +0300 @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). + * All rights reserved. + * This component and the accompanying materials are made available + * under the terms of the License "Eclipse Public License v1.0" + * which accompanies this distribution, and is available + * at the URL "http://www.eclipse.org/legal/epl-v10.html". + * + * Initial Contributors: + * Nokia Corporation - initial contribution. + * + * Contributors: + * + * Description: + * + */ +package com.nokia.carbide.cpp.internal.pi.wizards.model; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.core.runtime.IPath; + +import com.nokia.carbide.cpp.pi.PiPlugin; +import com.nokia.carbide.cpp.pi.wizards.WizardsPlugin; + +public final class SessionHandler { + + private static final String SESSION_DATA_FILE_NAME = "PerformanceInvestigatorSession.bin"; //$NON-NLS-1$ + private static final IPath SESSION_DATA_FILE_PATH = PiPlugin.getDefault() + .getStateLocation().append(SESSION_DATA_FILE_NAME); + private static SessionHandler instance; + + public static SessionHandler getInstance() { + if (instance == null) { + instance = new SessionHandler(); + } + return instance; + } + + private SessionHandler() { + + } + + /** + * Saves given trace files. + * + * @param files + * @return true if trace files are saved otherwise + * false is returned + */ + public boolean saveTraceFiles(List files) { + FileOutputStream fos = null; + ObjectOutputStream out = null; + try { + fos = new FileOutputStream(SESSION_DATA_FILE_PATH.toFile()); + out = new ObjectOutputStream(fos); + out.writeObject(files); + out.flush(); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (out != null) { + try { + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * Load trace files from storage + * + * @return array of the trace files + */ + @SuppressWarnings("unchecked") + public TraceFile[] loadTraceFile() { + File file = SESSION_DATA_FILE_PATH.toFile(); + if (!file.exists()) { + return new TraceFile[0]; + } + FileInputStream fis = null; + ObjectInputStream in = null; + try { + fis = new FileInputStream(file); + in = new ObjectInputStream(fis); + List traceFiles = (List) in.readObject(); + return traceFiles.toArray(new TraceFile[0]); + } catch (Exception e) { + return new TraceFile[0]; + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * Removes given file. + * + * @param traceFile + * file to be removed + */ + public void removeTraceFile(TraceFile traceFile) { + if (traceFile == null) { + return; + } + List traceFiles = new ArrayList(); + traceFiles.addAll(Arrays.asList(loadTraceFile())); + boolean removed = traceFiles.remove(traceFile); + if (removed) { + WizardsPlugin.getDefault().getPreferenceStore().setValue( + traceFile.getTraceFilePath().toString(), false); + saveTraceFiles(traceFiles); + } + } + + /** + * Get TraceFile with given path. + * + * @param path + * @return instance of the TraceFile if instance is found with given path + * otherwise null is returned + */ + public TraceFile getTraceFile(IPath path) { + if (path == null) { + return null; + } + List traceFiles = new ArrayList(); + traceFiles.addAll(Arrays.asList(loadTraceFile())); + for (TraceFile traceFile : traceFiles) { + if (path.equals(traceFile.getTraceFilePath())) { + return traceFile; + } + + } + return null; + } + + /** + * Add given file. + * + * @param traceFile + * file to be added + */ + public void addTraceFile(TraceFile traceFile) { + List traceFileList = new ArrayList(); + traceFileList.addAll(Arrays.asList(loadTraceFile())); + traceFileList.add(traceFile); + saveTraceFiles(traceFileList); + } +}