themeinstaller/source/src/com/nokia/tools/themeinstaller/installationmanager/ResourceInstaller.java
branchRCL_3
changeset 17 fe49e33862e2
parent 16 b685c59de105
child 18 04b7640f6fb5
equal deleted inserted replaced
16:b685c59de105 17:fe49e33862e2
     1 /*
       
     2 * Copyright (c) 2007 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:  Class for installing theme resources
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.installationmanager;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 import java.util.ArrayList;
       
    24 import java.util.Enumeration;
       
    25 import java.util.Observable;
       
    26 import java.util.Observer;
       
    27 import java.util.Vector;
       
    28 
       
    29 import com.nokia.tools.themeinstaller.defrep.IDefinitionRepository;
       
    30 import com.nokia.tools.themeinstaller.defrep.operations.FileOperationEvent;
       
    31 import com.nokia.tools.themeinstaller.defrep.operations.FileOperationUtils;
       
    32 import com.nokia.tools.themeinstaller.mediaconverter.MediaConverter;
       
    33 import com.nokia.tools.themeinstaller.odtconverter.MimeTypeResolver;
       
    34 import com.nokia.tools.themeinstaller.odtconverter.ODTHeader;
       
    35 import com.nokia.tools.themeinstaller.odtconverter.ODTResource;
       
    36 import com.nokia.tools.themeinstaller.odtconverter.ThemeStatusResolver;
       
    37 
       
    38 /**
       
    39  * Class for installing theme resources.
       
    40  */
       
    41 public class ResourceInstaller implements IResourceInstaller
       
    42     {
       
    43 
       
    44     // CONSTANTS
       
    45     private static final String MBM_SUFFIX = "mbm";
       
    46     private static final char FILE_EXT_SEPARATOR = '.';
       
    47 
       
    48     // Definition repository for accessing the file storage services
       
    49     private IDefinitionRepository iDefRep;
       
    50 
       
    51     // File operation event for observing file operations
       
    52     private FileOperationEvent iEvent;
       
    53 
       
    54     // Destination root directory
       
    55     private File iDestinationDir;
       
    56 
       
    57     // Data directory
       
    58     private String iDataDir;
       
    59 
       
    60     // Media converter for converting resources
       
    61     private MediaConverter iMediaConverter;
       
    62 
       
    63     // Mime type resolver for resolving resource type
       
    64     private MimeTypeResolver iMimeResolver;
       
    65 
       
    66     // Observer for monitoring file operation completions
       
    67     private Observer iFileCopyObserver;
       
    68 
       
    69     // For storing temporary files.
       
    70     private Vector iTempFiles;
       
    71 
       
    72     // Lock for asynchronous operations.
       
    73     private Lock iLock;
       
    74 
       
    75     /**
       
    76      * Constructor.
       
    77      * @param aDefRep Definition Repository for accessing the file storage
       
    78      * @param aDestinationDir Destination root directory
       
    79      * @param aDataDir Data directory containing the theme sources
       
    80      * @param aNameSpace Theme name space
       
    81      * @throws IOException if Media Converter can not be created
       
    82      */
       
    83     public ResourceInstaller( IDefinitionRepository aDefRep,
       
    84                               File aDestinationDir,
       
    85                               String aDataDir ) throws IOException
       
    86         {
       
    87         iDefRep = aDefRep;
       
    88         iDestinationDir = aDestinationDir;
       
    89         iDataDir = aDataDir;
       
    90         iMediaConverter = new MediaConverter();
       
    91         iMimeResolver = new MimeTypeResolver();
       
    92         iTempFiles = new Vector();
       
    93         iLock = new Lock();
       
    94 
       
    95         // Create an observer for monitoring file copy operation completions
       
    96         iFileCopyObserver = new Observer()
       
    97             {
       
    98             public void update( Observable aFileOperation, Object aEvent )
       
    99                 {
       
   100                 // Store the event
       
   101                 iEvent = ( FileOperationEvent ) aEvent;
       
   102                 // Open the lock
       
   103                 iLock.unLock();
       
   104                 }
       
   105             };
       
   106         }
       
   107 
       
   108     /* (non-Javadoc)
       
   109      * @see com.nokia.tools.odtconverter.installationmanager.IResourceInstaller#installResources(java.util.Vector, com.nokia.tools.odtconverter.ODTHeader)
       
   110      */
       
   111     public Vector installResources(
       
   112             Vector aResources,
       
   113             ODTHeader aHeader ) throws IOException
       
   114         {
       
   115         Vector result = new Vector();
       
   116         Enumeration resources = aResources.elements();
       
   117 
       
   118         // Process all resource files
       
   119         while ( resources.hasMoreElements() )
       
   120             {
       
   121             result.add( installResource( ( ThemeResource ) resources.nextElement(), aHeader ) );
       
   122             }
       
   123 
       
   124         return result;
       
   125         }
       
   126 
       
   127     /* (non-Javadoc)
       
   128      * @see com.nokia.tools.odtconverter.installationmanager.IResourceInstaller#installResource(com.nokia.tools.odtconverter.installationmanager.ThemeResource, com.nokia.tools.odtconverter.ODTHeader)
       
   129      */
       
   130     public ODTResource installResource( ThemeResource aResource,
       
   131                                         ODTHeader aHeader ) throws IOException
       
   132         {
       
   133         // Create the ODT resource
       
   134         ODTResource res = createResource( iDataDir, aResource );
       
   135 
       
   136         // Copy file to correct location
       
   137         File f = new File(
       
   138                 iDataDir + ( String )res.get( ODTResource.TempFileName ) );
       
   139         iDefRep.copyResource(
       
   140                 f,
       
   141                 iDestinationDir,
       
   142                 aHeader,
       
   143                 iFileCopyObserver );
       
   144         // Wait for file copying
       
   145         iLock.lock();
       
   146 
       
   147         if ( iEvent.getErrorCode() == FileOperationEvent.OPERATION_SUCCESSFUL )
       
   148             {
       
   149             deleteTemporaryFile( iEvent.getFile().getName() );
       
   150             }
       
   151         else
       
   152             {
       
   153             throw new IOException( "Resource file copying failed: "
       
   154                     + f.getPath() );
       
   155             }
       
   156 
       
   157         // Set the actual resource location (in Symbian file system) to resource
       
   158         res.put( ODTResource.FileName, iEvent.getDestPath() );
       
   159         return res;
       
   160         }
       
   161 
       
   162     /**
       
   163      * Delete temporary file.
       
   164      *
       
   165      * @param aFileName The file name
       
   166      *
       
   167      * @throws IOException Signals that an I/O exception has occurred.
       
   168      */
       
   169     private void deleteTemporaryFile( String aFileName ) throws IOException
       
   170         {
       
   171         Enumeration fileObjects = iTempFiles.elements();
       
   172 
       
   173         while ( fileObjects.hasMoreElements() )
       
   174             {
       
   175             File temp = ( File ) fileObjects.nextElement();
       
   176             if ( temp.getName().equals( aFileName ) )
       
   177                 {
       
   178                 if ( !temp.delete() )
       
   179                     {
       
   180                     throw new IOException( "Temporary file deletion failed: "
       
   181                             + aFileName );
       
   182                     }
       
   183                 else
       
   184                     {
       
   185                     iTempFiles.remove( temp );
       
   186                     deleteTemporaryFile( aFileName );
       
   187                     }
       
   188                 }
       
   189             }
       
   190         }
       
   191 
       
   192 
       
   193     /* (non-Javadoc)
       
   194      * @see com.nokia.tools.odtconverter.installationmanager.IResourceInstaller#createODTResource(com.nokia.tools.odtconverter.ODTHeader)
       
   195      */
       
   196     public ODTResource createODTResource( ODTHeader aHeader, String aNameSpace )
       
   197         {
       
   198         ODTResource res = new ODTResource();
       
   199         String odtPath = null;
       
   200 
       
   201         // Create ODT file path
       
   202         odtPath = iDefRep.createODTPath( iDestinationDir, aHeader );
       
   203 
       
   204         // Convert the path to Symbian OS file system format
       
   205         odtPath = FileOperationUtils.parseSymbianFSPath( odtPath );
       
   206 
       
   207         res.put( ODTResource.FileName, odtPath );
       
   208         res.put( ODTResource.CacheType,
       
   209                 new Integer( ManifestFactory.CACHE_TYPE_CACHE_FILE ) );
       
   210 
       
   211         // If EXnThemeStatusLicenceeDefault flag is set,
       
   212         // locking policy is E_XN_LOCKED
       
   213         int flags = ( ( Integer )aHeader.get( ODTHeader.Flags ) ).intValue();
       
   214         if ( ( flags & ThemeStatusResolver.E_XN_THEME_STATUS_LICENCEE_DEFAULT ) != 0 )
       
   215             {
       
   216             res.put( ODTResource.LockingPolicy,
       
   217                     new Integer( ManifestFactory.E_XN_LOCKED ) );
       
   218             }
       
   219         else
       
   220             {
       
   221             res.put( ODTResource.LockingPolicy,
       
   222                     new Integer( ManifestFactory.E_XN_UNLOCKED ) );
       
   223             }
       
   224 
       
   225         // ODT file's mime type is "unknown"
       
   226         res.put( ODTResource.MimeType, MimeTypeResolver.UNKNOWN_MIME_TYPE );
       
   227         res.put( ODTResource.NameSpace, aNameSpace );
       
   228         res.put( ODTResource.ResourceID, aHeader
       
   229                         .get( ODTHeader.ThemeShortName ) );
       
   230         res.put( ODTResource.ResourceType, new Integer(
       
   231                 MimeTypeResolver.E_RESOURCEODT ) );
       
   232         return res;
       
   233         }
       
   234 
       
   235 
       
   236     /**
       
   237      * Creates ODT resource from given node
       
   238      * @param aItem DOM Node to be added
       
   239      * @param aDataDirectory Resource directory
       
   240      * @return new ODTResource object
       
   241      * @throws IOException if media file can not be converter
       
   242      */
       
   243     private ODTResource createResource( String aDataDirectory,
       
   244                                         ThemeResource aItem ) throws IOException
       
   245         {
       
   246         ODTResource res = new ODTResource();
       
   247 
       
   248         String tempFileName = null;
       
   249         String filename = aItem.getFileName();
       
   250         int cacheType = aItem.getCacheType();
       
   251 
       
   252         // Determine the need for media conversion
       
   253         if( cacheType == ManifestFactory.CACHE_TYPE_CACHE_FILE ||
       
   254             cacheType == ManifestFactory.CACHE_TYPE_CACHE_MEMORY )
       
   255             {
       
   256             // Convert the media file
       
   257             tempFileName = convertMedia( aDataDirectory + filename );
       
   258             }
       
   259         else
       
   260             {
       
   261             // No media conversion
       
   262             tempFileName = filename;
       
   263             }
       
   264 
       
   265         // Remove directories and separators from the file name
       
   266         int fileNameIndex = tempFileName.lastIndexOf( File.separatorChar );
       
   267         if( fileNameIndex > -1 )
       
   268             {
       
   269             tempFileName = tempFileName.substring( fileNameIndex + 1 );
       
   270             }
       
   271 
       
   272         // Update resource type after media conversion
       
   273         aItem.setResourceType( iMimeResolver.getResourceType( tempFileName ) );
       
   274         aItem.setMimeType( iMimeResolver.getMimeType( tempFileName ) );
       
   275 
       
   276         // Set temporary file name that is not externalized. It is required
       
   277         // only for copying the resource file in a case that media conversion
       
   278         // has changed the file name and extension.
       
   279         res.put( ODTResource.TempFileName, tempFileName );
       
   280 
       
   281         // Set properties that are externalized. The file name is set after the
       
   282         // resource file is copied to the correct location.
       
   283         res.put( ODTResource.CacheType, new Integer(cacheType ));
       
   284         res.put( ODTResource.ResourceID, filename );
       
   285         res.put( ODTResource.LockingPolicy, new Integer(aItem.getLockingPolicy() ));
       
   286         res.put( ODTResource.NameSpace, aItem.getNameSpace() );
       
   287         res.put( ODTResource.MimeType, aItem.getMimeType() );
       
   288         res.put( ODTResource.ResourceType, new Integer(aItem.getResourceType() ));
       
   289         return res;
       
   290         }
       
   291 
       
   292     /**
       
   293      * Uses MediaConverter to converts media to MBM
       
   294      * @param aFilename File to convert
       
   295      * @return Filename of converted resource
       
   296      * @throws IOException File conversion fails
       
   297      */
       
   298     private String convertMedia( String aFilename ) throws IOException
       
   299         {
       
   300         ArrayList files = new ArrayList();
       
   301         files.add( aFilename );
       
   302         int dotPosition = aFilename.lastIndexOf( FILE_EXT_SEPARATOR ) + 1;
       
   303         String destinationImage =
       
   304             aFilename.substring( 0, dotPosition ) + MBM_SUFFIX;
       
   305 
       
   306         if ( dotPosition == 0 )
       
   307             {
       
   308             destinationImage = aFilename + FILE_EXT_SEPARATOR + MBM_SUFFIX;
       
   309             }
       
   310 
       
   311         String filePrefix = destinationImage.substring( 0,
       
   312                 destinationImage.lastIndexOf( FILE_EXT_SEPARATOR ) );
       
   313         File destFile = new File( destinationImage );
       
   314         int index = 1;
       
   315 
       
   316         // create unique name to not overwrite any existing files
       
   317         while( destFile.exists() )
       
   318             {
       
   319             destinationImage =
       
   320                 filePrefix + index++ + FILE_EXT_SEPARATOR + MBM_SUFFIX;
       
   321             destFile = new File( destinationImage );
       
   322             }
       
   323 
       
   324         iMediaConverter.convertMedia( files, destinationImage );
       
   325         iTempFiles.add( destFile );
       
   326         return destinationImage;
       
   327         }
       
   328 
       
   329     }