crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/data/ErrorLibrary.java
changeset 0 5ad7ad99af01
equal deleted inserted replaced
-1:000000000000 0:5ad7ad99af01
       
     1 /*
       
     2 * Copyright (c) 2008 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 package com.nokia.s60tools.crashanalyser.data;
       
    19 
       
    20 import java.util.ArrayList;
       
    21 import java.util.List;
       
    22 import java.util.Map;
       
    23 import com.nokia.s60tools.crashanalyser.model.*;
       
    24 import com.nokia.s60tools.crashanalyser.containers.ErrorLibraryError;
       
    25 import com.nokia.s60tools.crashanalyser.interfaces.IErrorLibraryObserver;
       
    26 import org.eclipse.core.runtime.IProgressMonitor;
       
    27 import org.eclipse.core.runtime.IStatus;
       
    28 import org.eclipse.core.runtime.Status;
       
    29 import org.eclipse.core.runtime.jobs.ILock;
       
    30 import org.eclipse.core.runtime.jobs.Job;
       
    31 
       
    32 /**
       
    33  * ErrorLibrary class contains all error descriptions. It contains
       
    34  * a list of errors, panics and panic categories. 
       
    35  *
       
    36  */
       
    37 public final class ErrorLibrary extends Job {
       
    38 
       
    39 	private static ErrorLibrary contentProvider = null;
       
    40 	Map<String, ErrorLibraryError> errors = null;
       
    41 	Map<String, ErrorLibraryError> panics = null;
       
    42 	Map<String, ErrorLibraryError> categories = null;
       
    43 	List<IErrorLibraryObserver> observers = null;
       
    44 	boolean errorsRead = false;
       
    45 	boolean running = false;
       
    46 	ILock accessLock = null;
       
    47 	ILock queryLock = null;
       
    48 	
       
    49 	/**
       
    50 	 * a singleton constructor
       
    51 	 * @param observer 
       
    52 	 * @return a ErrorLibrary instance
       
    53 	 */
       
    54 	public static ErrorLibrary getInstance(IErrorLibraryObserver observer) {
       
    55 		if (contentProvider == null) {
       
    56 			contentProvider = new ErrorLibrary();
       
    57 		}
       
    58 		
       
    59 		contentProvider.run(observer);
       
    60 		
       
    61 		return contentProvider;
       
    62 	}
       
    63 	
       
    64 	/**
       
    65 	 * private constructor
       
    66 	 */
       
    67 	private ErrorLibrary() {
       
    68 		super("Errors container");
       
    69 		accessLock = Job.getJobManager().newLock();
       
    70 		queryLock = Job.getJobManager().newLock();
       
    71 		setPriority(Job.LONG);
       
    72 		setUser(false);
       
    73 	}
       
    74 	
       
    75 	/**
       
    76 	 * Starts the errors reading job
       
    77 	 * @param observer
       
    78 	 */
       
    79 	void run(IErrorLibraryObserver observer) {
       
    80 		accessLock.acquire();
       
    81 		addObserver(observer);
       
    82 		try {
       
    83 			if (running)
       
    84 				return;
       
    85 			else
       
    86 				running = true;
       
    87 		} finally {
       
    88 			accessLock.release();
       
    89 		}
       
    90 		schedule(100);		
       
    91 	}
       
    92 
       
    93 	public boolean isReady() {
       
    94 		return errorsRead;
       
    95 	}
       
    96 	
       
    97 	/**
       
    98 	 * Returns list of errors
       
    99 	 * @return list of errors
       
   100 	 */
       
   101 	public ErrorLibraryError[] getErrors() {
       
   102 		queryLock.acquire();
       
   103 		try {
       
   104 			if (errors == null)
       
   105 				return new ErrorLibraryError[0];
       
   106 	
       
   107 			return errors.values().toArray(new ErrorLibraryError[errors.size()]);
       
   108 		} finally {
       
   109 			queryLock.release();
       
   110 		}
       
   111 	}
       
   112 	
       
   113 	/**
       
   114 	 * Returns list of panics
       
   115 	 * @return list of panics
       
   116 	 */
       
   117 	public ErrorLibraryError[] getPanics() {
       
   118 		queryLock.acquire();
       
   119 		try {
       
   120 			if (panics == null)
       
   121 				return new ErrorLibraryError[0];
       
   122 			
       
   123 			return panics.values().toArray(new ErrorLibraryError[panics.size()]);
       
   124 		} finally {
       
   125 			queryLock.release();
       
   126 		}
       
   127 	}
       
   128 	
       
   129 	/**
       
   130 	 * Returns list of panic categories
       
   131 	 * @return list of panic categories
       
   132 	 */
       
   133 	public ErrorLibraryError[] getCategories() {
       
   134 		queryLock.release();
       
   135 		try {
       
   136 			if (categories == null)
       
   137 				return new ErrorLibraryError[0];
       
   138 	
       
   139 		return categories.values().toArray(new ErrorLibraryError[categories.size()]);
       
   140 		} finally {
       
   141 			queryLock.release();
       
   142 		}
       
   143 		
       
   144 	}
       
   145 	
       
   146 	/**
       
   147 	 * Searches a description for given panic code
       
   148 	 * @param category e.g. USER
       
   149 	 * @param id e.g. 46
       
   150 	 * @return description for given panic. "" if not found.
       
   151 	 */
       
   152 	public String getPanicDescription(String category, String id) {
       
   153 		queryLock.acquire();
       
   154 		try {
       
   155 			String retval = "";
       
   156 			
       
   157 			if (panics != null && panics.containsKey(category+id)) {
       
   158 				retval = panics.get(category+id).getDescription();
       
   159 			}
       
   160 			
       
   161 			return retval;
       
   162 		} finally {
       
   163 			queryLock.release();
       
   164 		}
       
   165 	}
       
   166 	
       
   167 	/**
       
   168 	 * Searches a description for given error code
       
   169 	 * @param value e.g. -1 or KErrNotFound etc.
       
   170 	 * @return description for given error. "" if not found.
       
   171 	 */
       
   172 	public String getErrorDescription(String value) {
       
   173 		queryLock.acquire();
       
   174 		try {
       
   175 			String retval = "";
       
   176 			
       
   177 			if (errors != null && errors.containsKey(value)) {
       
   178 				retval = errors.get(value).getDescription();
       
   179 			}
       
   180 			
       
   181 			return retval;
       
   182 		} finally {
       
   183 			queryLock.release();
       
   184 		}
       
   185 	}
       
   186 	
       
   187 	/**
       
   188 	 * Adds a new error library observer
       
   189 	 * @param observer
       
   190 	 */
       
   191 	void addObserver(IErrorLibraryObserver observer) {
       
   192 		try {
       
   193 			if (observers == null)
       
   194 				observers = new ArrayList<IErrorLibraryObserver>();
       
   195 			if (observer != null && !observers.contains(observer)) {
       
   196 				try {
       
   197 					observers.add(observer);
       
   198 				} catch (Exception e) {
       
   199 					e.printStackTrace();
       
   200 				}
       
   201 			}
       
   202 		} catch (Exception e) {
       
   203 			e.printStackTrace();
       
   204 		}
       
   205 	}
       
   206 	
       
   207 	/**
       
   208 	 * Notifies observers that error library is ready to be used
       
   209 	 */
       
   210 	void errorReaderReady() {
       
   211 		
       
   212 		try {
       
   213 			while (!observers.isEmpty()) {
       
   214 				IErrorLibraryObserver observer = observers.get(0);
       
   215 				observer.errorLibraryReady();
       
   216 				observers.remove(observer);
       
   217 			}
       
   218 		} catch (Exception e) {
       
   219 			e.printStackTrace();
       
   220 		}
       
   221 
       
   222 		running = false;
       
   223 	}
       
   224 	
       
   225 	@Override
       
   226 	protected IStatus run(IProgressMonitor arg0) {
       
   227 		try {
       
   228 			// if errors haven't already been read, read errors
       
   229 			if (!errorsRead) {
       
   230 				ErrorsXmlReader reader = new ErrorsXmlReader();
       
   231 				reader.readAll();
       
   232 				errors = reader.getErrorsOwnership();
       
   233 				panics = reader.getPanicsOwnership();
       
   234 				categories = reader.getCategoriesOwnership();
       
   235 				errorsRead = true;
       
   236 			}
       
   237 		} catch (Exception e) {
       
   238 			e.printStackTrace();
       
   239 		}
       
   240 
       
   241 		accessLock.acquire();
       
   242 		errorReaderReady();
       
   243 		accessLock.release();
       
   244 		
       
   245 		return Status.OK_STATUS;
       
   246 	}
       
   247 }