sysperfana/memspyext/com.nokia.s60tools.memspy/src/com/nokia/s60tools/memspy/model/MemSpyFileBundle.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     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.memspy.model;
       
    20 
       
    21 
       
    22 import java.io.File;
       
    23 import java.util.Date;
       
    24 
       
    25 import com.nokia.s60tools.memspy.files.HeapDumpFile;
       
    26 import com.nokia.s60tools.memspy.files.SWMTFile;
       
    27 
       
    28 /**
       
    29  * MemSpyBundle class bundles up one folder under MemSpy plugin's folder. I.e. one 
       
    30  * MemSpyFileBundle is one row in MainView. A Bundle can contain an Heap Dump file or
       
    31  * 
       
    32  * SWMT-log file. All of the file listed above are found e.g. from
       
    33  * c:\my_carbide_workspace\.metadata\.plugins\com.nokia.s60tools.memspy\ImportedFiles[bundle folder]
       
    34  * 
       
    35  * Bundle can also be a waiting bundle, so that 'Loading files. Please wait' row can be shown in MainView.
       
    36  *
       
    37  */
       
    38 public class MemSpyFileBundle {
       
    39 
       
    40 	public static final int INDEX_FILE_TYPE = 0;
       
    41 	public static final int INDEX_FILE_NAME = 1;
       
    42 	public static final int INDEX_TIME = 2;
       
    43 
       
    44 	
       
    45 	/* 
       
    46 	 * Heap Dump file
       
    47 	 */
       
    48 	private HeapDumpFile heapDumpFile = null;
       
    49 	
       
    50 	/*
       
    51 	 * SWMT-log file
       
    52 	 */
       
    53 	private SWMTFile swmtFile = null; 
       
    54 		
       
    55 	/**
       
    56 	 * Bundle name for an empty or waiting bundle  
       
    57 	 */
       
    58 	private String bundleName = "";
       
    59 	
       
    60 	/**
       
    61 	 * Folder where all bundle's files can be found from
       
    62 	 */
       
    63 	private String bundleFolder = "";
       
    64 	
       
    65 	/**
       
    66 	 * If true, bundle is an empty or waiting bundle
       
    67 	 */
       
    68 	private boolean emptyFile = false;
       
    69 	
       
    70 	/**
       
    71 	 * Used for creating an empty or waiting bundle
       
    72 	 * @param empty If <code>true</code>, an empty bundle is created. If <code>false</code>, a waiting bundle is created. 
       
    73 	 */
       
    74 	public MemSpyFileBundle(boolean empty) {
       
    75 		if (empty)
       
    76 			bundleName = "No Crash Files Found.";
       
    77 		else
       
    78 			bundleName = "Loading files. Please wait.";
       
    79 		emptyFile = true;
       
    80 	}
       
    81 	
       
    82 	/**
       
    83 	 * Creates a bundle from folder
       
    84 	 * @param folder Bundle folder. Bundle's file will be read from here.
       
    85 	 */
       
    86 	public MemSpyFileBundle( String folder ) {
       
    87 		bundleFolder = MemSpyFileOperations.addSlashToEnd(folder);
       
    88 		
       
    89 		heapDumpFile = HeapDumpFile.read( folder );
       
    90 		swmtFile = SWMTFile.read( folder );
       
    91 		
       
    92 	}
       
    93 	
       
    94 	/**
       
    95 	 * Create dummy bundle.
       
    96 	 * @param folder where files are searched.
       
    97 	 * @return returns new bundle.
       
    98 	 */
       
    99 	public static MemSpyFileBundle createDummyBundle(String folder) {
       
   100 		return new MemSpyFileBundle(folder);
       
   101 	}
       
   102 	
       
   103 	/**
       
   104 	 * MainView can use this to get description for each column in the grid
       
   105 	 * @param index index of the column
       
   106 	 * @return value for asked column
       
   107 	 */
       
   108 	public String getText(int index) {
       
   109 		String retval = "";
       
   110 		switch (index) {
       
   111 			case INDEX_TIME:
       
   112 				retval = this.getTime();
       
   113 				break;
       
   114 				
       
   115 			case INDEX_FILE_NAME:
       
   116 				retval = this.getFileName();
       
   117 				break;
       
   118 			
       
   119 			case INDEX_FILE_TYPE:
       
   120 				retval = this.getFileType();
       
   121 				break;
       
   122 				
       
   123 			default:
       
   124 				break;
       
   125 		}
       
   126 		
       
   127 		return retval;
       
   128 	}
       
   129 	
       
   130 	/**
       
   131 	 * Get file type.
       
   132 	 * @return type of file.
       
   133 	 */
       
   134 	public String getFileType() {
       
   135 		if (emptyFile)
       
   136 			return bundleName;
       
   137 		
       
   138 		String retval = "";
       
   139 		
       
   140 		if ( this.heapDumpFile != null) {
       
   141 			retval = heapDumpFile.getFileType();
       
   142 		}
       
   143 		
       
   144 		if( swmtFile != null ){
       
   145 			retval = this.swmtFile.getFileType();
       
   146 		}
       
   147 		
       
   148 		return retval;
       
   149 	}
       
   150 	
       
   151 	
       
   152 	/**
       
   153 	 * Returns the file name for this bundle. File name depends on what types
       
   154 	 * of files this bundle contains (or if this bundle is an empty or waiting bundle).
       
   155 	 *   
       
   156 	 * @return the file name for this bundle.
       
   157 	 */
       
   158 	public String getFileName() {
       
   159 		if (emptyFile)
       
   160 			return bundleName;
       
   161 		
       
   162 		String retval = "";
       
   163 		
       
   164 		if ( this.heapDumpFile != null) {
       
   165 			retval = heapDumpFile.getFileName();
       
   166 		}
       
   167 		else if( swmtFile != null ){
       
   168 			retval = this.swmtFile.getFileName();
       
   169 		}
       
   170 		
       
   171 		return retval;
       
   172 	}
       
   173 	
       
   174 	/**
       
   175 	 * Get the Cycle number from FileName.
       
   176 	 * E.g. if file name is: "MemSpy SWMT-log 12.08.2009 08-40-22 Cycle 3" 
       
   177 	 * will return '3'. 
       
   178 	 *   
       
   179 	 * @return the cycle number from file name for this bundle, or -1 if can't found.
       
   180 	 */
       
   181 	public int getCycleNumberFromFileName() {
       
   182 		int retval = -1;
       
   183 		if (emptyFile){
       
   184 			return retval;
       
   185 		}
       
   186 				
       
   187 		String fileName = "";
       
   188 		
       
   189 		if ( this.heapDumpFile != null) {
       
   190 			fileName = heapDumpFile.getFileName();
       
   191 		}
       
   192 		else if( swmtFile != null ){
       
   193 			fileName = this.swmtFile.getFileName();
       
   194 		}
       
   195 		
       
   196 		if(fileName != null && fileName.indexOf(MemSpyFileOperations.CYCLE) != -1){
       
   197 			int index = fileName.indexOf(MemSpyFileOperations.CYCLE);
       
   198 			String cycle = fileName.substring((index + MemSpyFileOperations.CYCLE.length())).trim();
       
   199 			cycle = cycle.substring(0, cycle.indexOf('.'));
       
   200 			try {
       
   201 				retval = Integer.parseInt(cycle);
       
   202 			} catch (NumberFormatException e) {
       
   203 				e.printStackTrace();
       
   204 				retval = -1;
       
   205 			}
       
   206 		}
       
   207 		
       
   208 		return retval;
       
   209 	}
       
   210 	
       
   211 	/**  
       
   212 	 * Get Time
       
   213 	 * @return time of file creation.
       
   214 	 */
       
   215 	public String getTime() {
       
   216 		if (emptyFile)
       
   217 			return "";
       
   218 		String retval = "";
       
   219 
       
   220 		if( heapDumpFile != null ){
       
   221 			retval = this.heapDumpFile.getTime();
       
   222 		}		
       
   223 		else if( swmtFile != null ){
       
   224 			retval = this.swmtFile.getTime();
       
   225 		}
       
   226 	
       
   227 	
       
   228 		return retval;
       
   229 	}
       
   230 	
       
   231 	/**
       
   232 	 * Get time as long
       
   233 	 * @return time of file creation as long.
       
   234 	 */
       
   235 	public long getTimeAsLong(){
       
   236 		long time = -1;
       
   237 		Date date = null;
       
   238 		if( heapDumpFile != null ){
       
   239 			date = this.heapDumpFile.getDateTime();
       
   240 		}		
       
   241 		else if( swmtFile != null ){
       
   242 			date = this.swmtFile.getDateTime();
       
   243 		}
       
   244 		
       
   245 		if(date != null){
       
   246 			time = date.getTime();
       
   247 		}
       
   248 		
       
   249 		return time;
       
   250 	}
       
   251 	
       
   252 	
       
   253 	/**
       
   254 	 * Get XML file.
       
   255 	 * @return XML-files path
       
   256 	 */
       
   257 	public String getXMLFilePath() {
       
   258 		if( heapDumpFile != null ){
       
   259 			return this.heapDumpFile.getXmlPath();
       
   260 		}
       
   261 		else{
       
   262 			return null;
       
   263 		}
       
   264 	}
       
   265 	
       
   266 	/**
       
   267 	 * Get file path.
       
   268 	 * @return Files complete path.
       
   269 	 */
       
   270 	public String getFilePath(){
       
   271 		if (emptyFile)
       
   272 			return "";
       
   273 		String retval = "";
       
   274 
       
   275 		if( heapDumpFile != null ){
       
   276 			retval = this.heapDumpFile.getFilePath();
       
   277 		}
       
   278 		
       
   279 		if( swmtFile != null ){
       
   280 			retval = this.swmtFile.getFilePath();
       
   281 		}
       
   282 	
       
   283 	
       
   284 		return retval;
       
   285 	}
       
   286 	
       
   287 	
       
   288 	/**
       
   289 	 * Returns whether this is an empty or waiting bundle.
       
   290 	 * @return true if bundle is empty or waiting, false if not.
       
   291 	 */
       
   292 	public boolean isEmpty() {
       
   293 		return emptyFile;
       
   294 	}
       
   295 	
       
   296 	
       
   297 	
       
   298 	/**
       
   299 	 * Returns whether this bundle contains any files.
       
   300 	 * @return true if bundle contains files, false if not
       
   301 	 */
       
   302 	public boolean hasFiles() {
       
   303 		if ( heapDumpFile != null || swmtFile != null )
       
   304 			return true;
       
   305 		
       
   306 		return false;
       
   307 	}
       
   308 	
       
   309 	
       
   310 	
       
   311 	/**
       
   312 	 * Tests whether this bundle still exists in the drive.
       
   313 	 * If bundle is empty or waiting, true is always returned. 
       
   314 	 * 
       
   315 	 * @return <code>true</code> if bundle exists, <code>false</code> if not.
       
   316 	 */
       
   317 	public boolean exists() {
       
   318 		if (isEmpty())
       
   319 			return true;
       
   320 		
       
   321 		try {
       
   322 			File f = new File(bundleFolder);
       
   323 			if (f.isDirectory() && f.exists())
       
   324 				return true;
       
   325 		} catch (Exception e) {
       
   326 			return false;
       
   327 		}
       
   328 		
       
   329 		return false;
       
   330 	}
       
   331 	
       
   332 	
       
   333 	
       
   334 	/**
       
   335 	 * Returns this bundle's folder
       
   336 	 * @return bundle's folder or empty
       
   337 	 */
       
   338 	protected String getBundleFolder() {
       
   339 		return bundleFolder;
       
   340 	}
       
   341 	
       
   342 	
       
   343 	/**
       
   344 	 * Has this bundle a SWMT log file.
       
   345 	 * @return <code>true</code> if this bundle has SWMT-log <code>false</code> otherwise.
       
   346 	 */
       
   347 	public boolean hasSWMTLogFile(){
       
   348 		if( swmtFile != null ){
       
   349 			return true;
       
   350 		}
       
   351 		return false;
       
   352 	}
       
   353 	
       
   354 	/**
       
   355 	 * Has this bundle a heap dump file
       
   356 	 * @return <code>true</code> if this bundle has Heap Dump <code>false</code> otherwise.
       
   357 	 */
       
   358 	public boolean hasHeapDumpFile(){
       
   359 		
       
   360 		if( heapDumpFile != null ){
       
   361 			return true;
       
   362 			
       
   363 		}
       
   364 		return false;
       
   365 
       
   366 	}
       
   367 	
       
   368 	/**
       
   369 	 * Deletes this bundle. I.e deletes all files under this
       
   370 	 * bundle's folder and finally deletes the bundle folder.
       
   371 	 */
       
   372 	public boolean delete() {
       
   373 		if (!"".equals(bundleFolder)) {
       
   374 			return !MemSpyFileOperations.deleteDir( new File( bundleFolder ) );
       
   375 		}
       
   376 		else{
       
   377 			return false;
       
   378 		}
       
   379 		
       
   380 	}
       
   381 
       
   382 	/*
       
   383 	 * (non-Javadoc)
       
   384 	 * @see java.lang.Object#toString()
       
   385 	 */	
       
   386 	public String toString(){
       
   387 		if(swmtFile != null){
       
   388 			return swmtFile.getFilePath();
       
   389 		}
       
   390 		else{
       
   391 			return null;
       
   392 		}
       
   393 	}
       
   394 	
       
   395 	/**
       
   396 	 * Checks if bundles are equal. Two bundles are equal if
       
   397 	 * their bundleFolder is the same.
       
   398 	 */
       
   399 	public boolean equals(Object other) {
       
   400 		if (this == other)
       
   401 			return true;
       
   402 		
       
   403 		if (!(other instanceof MemSpyFileBundle))
       
   404 			return false;
       
   405 		
       
   406 		MemSpyFileBundle othr = (MemSpyFileBundle)other;
       
   407 		if (bundleFolder.compareToIgnoreCase(othr.getBundleFolder()) == 0)
       
   408 			return true;
       
   409 		return false;
       
   410 	}
       
   411 	
       
   412 	
       
   413 	
       
   414 }