trace/traceanalyser/com.nokia.s60tools.traceanalyser/src/com/nokia/s60tools/traceanalyser/model/FailLogManager.java
changeset 9 14dc2103a631
equal deleted inserted replaced
8:15296fd0af4a 9:14dc2103a631
       
     1 /*
       
     2 * Copyright (c) 2009 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 "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 
       
    18 
       
    19 package com.nokia.s60tools.traceanalyser.model;
       
    20 
       
    21 import java.io.BufferedInputStream;
       
    22 import java.io.BufferedOutputStream;
       
    23 import java.io.FileInputStream;
       
    24 import java.io.FileOutputStream;
       
    25 import java.io.IOException;
       
    26 import java.io.InputStream;
       
    27 import java.io.ObjectInput;
       
    28 import java.io.ObjectInputStream;
       
    29 import java.io.ObjectOutput;
       
    30 import java.io.ObjectOutputStream;
       
    31 import java.io.OutputStream;
       
    32 import java.util.*;
       
    33 
       
    34 import org.eclipse.core.runtime.IPath;
       
    35 import org.eclipse.core.runtime.IProgressMonitor;
       
    36 import org.eclipse.core.runtime.IStatus;
       
    37 import org.eclipse.core.runtime.Platform;
       
    38 import org.eclipse.core.runtime.Status;
       
    39 import org.eclipse.core.runtime.jobs.Job;
       
    40 import org.eclipse.core.runtime.jobs.ILock;
       
    41 
       
    42 
       
    43 import com.nokia.s60tools.traceanalyser.interfaces.ITraceAnalyserFileObserver;
       
    44 import com.nokia.s60tools.traceanalyser.plugin.TraceAnalyserPlugin;
       
    45 import com.nokia.s60tools.traceanalyser.export.RuleEvent;
       
    46 
       
    47 
       
    48 /**
       
    49  * This class is responsible for providing Trace Analyser Fail Log items to MainView's content provider.
       
    50  */
       
    51 public class FailLogManager extends Job {
       
    52 	
       
    53 	/* file observer */
       
    54 	ITraceAnalyserFileObserver filesObserver = null;
       
    55 	
       
    56 	/* accesslock */
       
    57 	ILock accessLock = null;
       
    58 	
       
    59 	/* boolean value that is true when job is on-going */
       
    60 	boolean jobRunning = false;
       
    61 	
       
    62 	/* list of fails */
       
    63 	//ArrayList<FailLogItem> failLog;
       
    64 	ArrayList<RuleEvent> failLog;
       
    65 	
       
    66 	
       
    67 	/**
       
    68 	 * TraceAnalyserFileManager.
       
    69 	 * Constructor.
       
    70 	 * @param observer observer, which is notified when reading is finished.
       
    71 	 */
       
    72 	public FailLogManager( ITraceAnalyserFileObserver observer ) {
       
    73 		super("Trace Analyser - Reading Fail Log");
       
    74 		filesObserver = observer;
       
    75 		accessLock = Job.getJobManager().newLock();
       
    76 		failLog = null;
       
    77 	}
       
    78 
       
    79 
       
    80 	/*
       
    81 	 * (non-Javadoc)
       
    82 	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
       
    83 	 */
       
    84 	protected IStatus run(IProgressMonitor monitor) {
       
    85 
       
    86 		accessLock.acquire();
       
    87 		jobRunning = true;
       
    88 		
       
    89 		// if log has not yet been read, read it now.
       
    90 		if(failLog == null){
       
    91 			loadLogFromFile();
       
    92 		}
       
    93 		// notify main view that reading finished.
       
    94 		if(filesObserver != null){
       
    95 			filesObserver.failLogUpdated();
       
    96 		}
       
    97 		jobRunning = false;
       
    98 		accessLock.release();
       
    99 		
       
   100 		return Status.OK_STATUS;
       
   101 	}
       
   102 
       
   103 	
       
   104 	/**
       
   105 	 * getTraceAnalyserFailLog.
       
   106 	 * Method that returns read violations from fail log. 
       
   107 	 * If fails are not read method starts reading them.
       
   108 	 * @return array containing all fail events.
       
   109 	 */
       
   110 	public RuleEvent[] getTraceAnalyserFailLog() {
       
   111 		// files have not yet been read, start reading process
       
   112 		if (failLog == null ) {
       
   113 			if(!jobRunning){
       
   114 				jobRunning = true;
       
   115 				setPriority(Job.LONG);
       
   116 				setUser(false);
       
   117 				schedule(100);
       
   118 			}
       
   119 			RuleEvent[] cFiles = new RuleEvent[0];
       
   120 			
       
   121 			
       
   122 			return cFiles;
       
   123 		}
       
   124 		else{
       
   125 			return failLog.toArray(new RuleEvent[failLog.size()]);
       
   126 
       
   127 		}
       
   128 		
       
   129 	}
       
   130 	
       
   131 	/**
       
   132 	 * addItem.
       
   133 	 * adds one item to fail log.
       
   134 	 * @param newItem
       
   135 	 */
       
   136 	public void addItem(RuleEvent newItem){
       
   137 		if(failLog != null){
       
   138 			failLog.add(0,newItem);
       
   139 		}
       
   140 		if(filesObserver != null){
       
   141 			filesObserver.failLogUpdated();
       
   142 		}
       
   143 	}
       
   144 	
       
   145 	public void clearLog(){
       
   146 		failLog.clear();
       
   147 		if(filesObserver != null){
       
   148 			filesObserver.failLogUpdated();
       
   149 		}
       
   150 	}
       
   151 	
       
   152 	/**
       
   153 	 * refresh.
       
   154 	 * Refresh rule list.
       
   155 	 */
       
   156 	public void refresh() {
       
   157 		
       
   158 		accessLock.acquire();
       
   159 		try {
       
   160 		if (!jobRunning){
       
   161 			jobRunning = true;
       
   162 			setPriority(Job.LONG);
       
   163 			setUser(false);
       
   164 			schedule(100);
       
   165 			}
       
   166 		} catch (Exception e) {
       
   167 			e.printStackTrace();
       
   168 		} finally {
       
   169 			accessLock.release();
       
   170 		}
       
   171 	}
       
   172 
       
   173 	/**
       
   174 	 * getPluginWorkingLocation.
       
   175 	 * Returns a path where Rule plug-in can do various tasks (located under workspace).
       
   176 	 */	
       
   177 	private String getFailLogFileName() {
       
   178 		IPath location = Platform.getStateLocation( TraceAnalyserPlugin.getDefault().getBundle());
       
   179 		return location.toOSString() + "//FailLog.log";		
       
   180 	}
       
   181 	
       
   182 	/**
       
   183 	 * saveLogToFile.
       
   184 	 * Saves fail log into file system.
       
   185 	 */
       
   186 	public void saveLogToFile(){
       
   187 		 try {
       
   188 
       
   189 			OutputStream file = new FileOutputStream(getFailLogFileName());
       
   190 			OutputStream buffer = new BufferedOutputStream(file);
       
   191 			ObjectOutput output = new ObjectOutputStream(buffer);
       
   192 			try {
       
   193 				// serialize fail list.
       
   194 				output.writeObject(failLog);
       
   195 			} finally {
       
   196 				output.close();
       
   197 			}
       
   198 		} catch (IOException ex) {
       
   199 			ex.printStackTrace();
       
   200 		}
       
   201 	}
       
   202 	
       
   203 	@SuppressWarnings("unchecked")
       
   204 	/**
       
   205 	 * loadLogFromFile.
       
   206 	 * Loads fail list from file system.
       
   207 	 */
       
   208 	private void loadLogFromFile() {
       
   209 		try {
       
   210 			// use buffering
       
   211 			InputStream file = new FileInputStream(getFailLogFileName());
       
   212 			InputStream buffer = new BufferedInputStream(file);
       
   213 			ObjectInput input = new ObjectInputStream(buffer);
       
   214 			try {
       
   215 				// deserialize the List
       
   216 				failLog = (ArrayList<RuleEvent>) input.readObject();
       
   217 				
       
   218 			} finally {
       
   219 				input.close();
       
   220 				if(failLog == null){
       
   221 					failLog = new ArrayList<RuleEvent>();
       
   222 				}
       
   223 			}
       
   224 		} catch (ClassNotFoundException ex) {
       
   225 			ex.printStackTrace();
       
   226 			failLog = new ArrayList<RuleEvent>();
       
   227 
       
   228 		} catch (IOException ex) {
       
   229 			failLog = new ArrayList<RuleEvent>();
       
   230 
       
   231 		}
       
   232 	}
       
   233 	
       
   234 	/**
       
   235 	 * setObserver.
       
   236 	 * Sets observer.
       
   237 	 * @param filesObserver, new observer.
       
   238 	 */
       
   239 	public void setObserver(ITraceAnalyserFileObserver filesObserver){
       
   240 		this.filesObserver = filesObserver;
       
   241 	}
       
   242 
       
   243 }