themeinstaller/source/src/com/nokia/tools/themeinstaller/ui/ThemeInstaller.java
branchRCL_3
changeset 32 fe49e33862e2
parent 31 b685c59de105
child 33 04b7640f6fb5
equal deleted inserted replaced
31:b685c59de105 32: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:  Standalone UI
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.ui;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 import com.nokia.tools.themeinstaller.installationmanager.IInstallationListener;
       
    24 import com.nokia.tools.themeinstaller.installationmanager.InstallationManager;
       
    25 import com.nokia.tools.themeinstaller.installationmanager.InstallationParameters;
       
    26 import com.nokia.tools.themeinstaller.installationmanager.Lock;
       
    27 import com.nokia.tools.themeinstaller.installationmanager.ProgressEvent;
       
    28 import com.nokia.tools.themeinstaller.logger.LogWriter;
       
    29 
       
    30 public class ThemeInstaller
       
    31     {
       
    32 
       
    33     // CONSTANTS
       
    34     // Error message prefix
       
    35     private final static String ERROR_PREFIX = "*** ERROR: ";
       
    36 
       
    37     // Usage instructions
       
    38     private final static String USAGE =
       
    39         "\nUSAGE: ThemeInstaller manifest_file [destination_dir] [-option_1] [-option_n]\n" +
       
    40         "\nOptions:" +
       
    41         "\n    -loc:<loc_settings_filename.xml>   Enable localisation enhancements" +
       
    42         "\n    -prop:<properties_filename.prop>   Use external properties file instead of the default one" +
       
    43         "\n    -log:<path>                        Path for log file" +
       
    44         "\n    -log                               If no path given, the log is printed to console\n"; 
       
    45         //"\n    -fixDTD                            Use if tool should attempt to fix errors in dtd\n";
       
    46     
       
    47 
       
    48     // Command line options
       
    49     private final static String OPTION_PREFIX = "-";
       
    50     private final static String LOC_OPTION = "loc:";
       
    51     private final static String PROP_OPTION = "prop:";
       
    52     private final static String LOG_PATH_OPTION = "log:";
       
    53     private final static String LOG_PATH_OPTION_NO_PATH = "log";
       
    54     private final static String FIX_INVALID_DTD_PARAMS = "fixDTD";
       
    55 
       
    56     // Lock object for waiting the install to complete
       
    57     private Lock iLock;
       
    58 
       
    59     // Manifest file
       
    60     protected String iManifest;
       
    61 
       
    62     // Destination directory
       
    63     protected String iDestination;
       
    64 
       
    65     // Localisation settings file
       
    66     protected String iLocSettings;
       
    67 
       
    68     // External properties file
       
    69     protected String iPropertiesFile;
       
    70 
       
    71     // Logging directory
       
    72     protected String iLogDir;
       
    73 
       
    74     /**
       
    75      * Application entry point
       
    76      * @param aArgs application arguments
       
    77      */
       
    78     public static void main( String[] aArgs )
       
    79         {
       
    80         ThemeInstaller instance = new ThemeInstaller();
       
    81         instance.install( aArgs );
       
    82         }
       
    83 
       
    84     /**
       
    85      * Constructor.
       
    86      */
       
    87     protected ThemeInstaller()
       
    88         {
       
    89         iLock = new Lock();
       
    90         }
       
    91 
       
    92     /**
       
    93      * Starts the installation process. Parses the arguments before
       
    94      * staring the installation.
       
    95      * @param aArgs Application arguments
       
    96      */
       
    97     protected void install( String[] aArgs )
       
    98         {
       
    99         try
       
   100             {
       
   101             parseArgs( aArgs );
       
   102             }
       
   103         catch( IllegalArgumentException iae )
       
   104             {
       
   105             //System.out.println( ERROR_PREFIX + iae.getMessage() ); 
       
   106         	System.out.println("ThemeInstaller version 2.0.0");
       
   107         	System.out.println("Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved");
       
   108             System.out.println("");
       
   109         	System.out.println( USAGE );
       
   110             return;
       
   111             }
       
   112         catch ( IOException e )
       
   113             {
       
   114             System.out.println( ERROR_PREFIX + e.getMessage() );
       
   115             }
       
   116 
       
   117         try
       
   118             {
       
   119             doInstall();
       
   120             }
       
   121         catch ( Exception e )
       
   122             {
       
   123             String error = ERROR_PREFIX + "Installation failed: "
       
   124                     + e.getMessage();
       
   125             if ( !LogWriter.getInstance().inConsoleMode() )
       
   126                 {
       
   127                 System.out.println( error );
       
   128                 }
       
   129             LogWriter.getInstance().logSevere( error );
       
   130             }
       
   131         finally
       
   132             {
       
   133             LogWriter.closeInstance();
       
   134             }
       
   135         }
       
   136 
       
   137     /**
       
   138      * Execute the installation
       
   139      * @param aManifest theme manifest file name
       
   140      * @param aDestination destination directory
       
   141      * @throws Exception if installation fails
       
   142      */
       
   143     protected void doInstall() throws Exception
       
   144         {
       
   145         InstallationParameters params = new InstallationParameters(
       
   146                 new File( iManifest ), new File( iDestination ) );
       
   147 
       
   148         LogWriter.getInstance().logInfo( "Starting Installer" );
       
   149         LogWriter.getInstance().logInfo( "Manifest file : " + iManifest );
       
   150         LogWriter.getInstance().logInfo( "Destination directory : " + iDestination );
       
   151 
       
   152         if( iLocSettings != null )
       
   153             {
       
   154             params.setLocSettings( new File( iLocSettings ) );
       
   155             LogWriter.getInstance().logInfo( "Settings file : " + params.getLocSettings().getPath() );
       
   156             }
       
   157         if( iPropertiesFile != null )
       
   158             {
       
   159             params.setPropFile( new File ( iPropertiesFile ) );
       
   160             LogWriter.getInstance().logInfo( "Properties file : " + params.getPropFile().getPath() );
       
   161             }
       
   162 
       
   163         IInstallationListener listener = new IInstallationListener()
       
   164             {
       
   165             public void installationCompleted( ProgressEvent aEvent )
       
   166                 {
       
   167                 if( aEvent.getError() == IInstallationListener.NO_ERROR )
       
   168                     {
       
   169                     if ( !LogWriter.getInstance().inConsoleMode() )
       
   170                         {
       
   171                         System.out.println( "Installation done." );
       
   172                         }
       
   173                     LogWriter.getInstance().logInfo( "Installation done" );
       
   174                     }
       
   175                 else
       
   176                     {
       
   177                     String error = ERROR_PREFIX
       
   178                                 + "Installation failed, error: "
       
   179                                 + aEvent.getError() + ", "
       
   180                                 + aEvent.getMessage();
       
   181                     if ( !LogWriter.getInstance().inConsoleMode() )
       
   182                             {
       
   183                             System.out.println( error );
       
   184                             }
       
   185                         LogWriter.getInstance().logSevere( error );
       
   186                         }
       
   187 
       
   188                 // release lock
       
   189                 iLock.unLock();
       
   190                 }
       
   191 
       
   192             public void installationProgress( ProgressEvent aEvent )
       
   193                 {
       
   194                 if( aEvent.getState() == IInstallationListener.STATE_PARSED )
       
   195                     {
       
   196                 		String message = "Resource files parsing done,";
       
   197                 		if( aEvent.getError() != IInstallationListener.NO_ERROR ) {
       
   198                 			message += " error: " + aEvent.getError() + ",";
       
   199                 		}
       
   200                 		message +=  " theme: "
       
   201                         + aEvent.getName() + ", language: "
       
   202                         + aEvent.getLanguage();
       
   203                     
       
   204                                 
       
   205                     if ( !LogWriter.getInstance().inConsoleMode() )
       
   206                         {
       
   207                         System.out.println( message );
       
   208                         }
       
   209                     LogWriter.getInstance().logInfo( message );
       
   210                     }
       
   211                 else if ( aEvent.getState() == IInstallationListener.STATE_WRITED )
       
   212                     {
       
   213                     String message = "Installed ODT: "
       
   214                             + aEvent.getFileName();
       
   215                     if ( !LogWriter.getInstance().inConsoleMode() )
       
   216                         {
       
   217                         System.out.println( message );
       
   218                         }
       
   219                     LogWriter.getInstance().logInfo( message );
       
   220                     }
       
   221                 }
       
   222             };
       
   223 
       
   224         InstallationManager i = new InstallationManager( params, listener );
       
   225         i.startInstallation();
       
   226 
       
   227         // wait for finalization
       
   228         iLock.lock();
       
   229         }
       
   230 
       
   231     /**
       
   232      * Parse command line arguments.
       
   233      * @param aArgs Command line arguments
       
   234      * @throws IllegalArgumentException if mandatory command line parameters
       
   235      * are not defined
       
   236      * @throws IOException Signals that an I/O exception has occurred.
       
   237      */
       
   238     protected void parseArgs( String[] aArgs ) throws IllegalArgumentException,
       
   239             IOException
       
   240         {
       
   241         // Print usage information if mandatory arguments were not specified
       
   242         if( aArgs.length == 0 )
       
   243             {
       
   244             throw new IllegalArgumentException( "No command line arguments" );
       
   245             }
       
   246 
       
   247         // Resolve manifest file name and destination directory
       
   248         iManifest = aArgs[ 0 ];
       
   249         if( aArgs.length > 1 &&
       
   250             !aArgs[ 1 ].startsWith( OPTION_PREFIX ) )
       
   251             {
       
   252             iDestination = aArgs[ 1 ];
       
   253             }
       
   254         else
       
   255             {
       
   256             // If no destination directory is specified, use the current dir
       
   257             iDestination = System.getProperty( "user.dir" );
       
   258             }
       
   259 
       
   260         // Parse the options
       
   261         for( int i = 1; i < aArgs.length; i++ )
       
   262             {
       
   263         	//fix DTD
       
   264         	String opt = OPTION_PREFIX + FIX_INVALID_DTD_PARAMS;
       
   265         	if ( aArgs[ i ].toLowerCase().startsWith( opt.toLowerCase() )) {
       
   266         		InstallationManager.setFixDTD(true);
       
   267         	}
       
   268         	
       
   269             // Localisation settings
       
   270             opt = OPTION_PREFIX + LOC_OPTION;
       
   271             if( aArgs[ i ].startsWith( opt ) )
       
   272                 {
       
   273                 iLocSettings = aArgs[ i ].substring( opt.length() );
       
   274                 if( iLocSettings.length() == 0 )
       
   275                     {
       
   276                     throw new IllegalArgumentException( "No localisation settings file defined with the option" );
       
   277                     }
       
   278                 }
       
   279             // External properties file
       
   280             opt = OPTION_PREFIX + PROP_OPTION;
       
   281             if( aArgs[ i ].startsWith( opt ) )
       
   282                 {
       
   283                 iPropertiesFile = aArgs[ i ].substring( opt.length() );
       
   284                 if( iPropertiesFile.length() == 0 )
       
   285                     {
       
   286                     throw new IllegalArgumentException( "No external properties file name defined with the option" );
       
   287                     }
       
   288                 }
       
   289             // Logging without path (option "-log")
       
   290             opt = OPTION_PREFIX + LOG_PATH_OPTION_NO_PATH;
       
   291             if ( aArgs[ i ].equals( opt ) )
       
   292                 {
       
   293                 // No logging file defined
       
   294                 LogWriter.initialize( null );
       
   295                 }
       
   296             // Logging with path or with option "-log:"
       
   297             opt = OPTION_PREFIX + LOG_PATH_OPTION;
       
   298             if ( aArgs[ i ].startsWith( opt ) )
       
   299                 {
       
   300                 iLogDir = aArgs[ i ].substring( opt.length() );
       
   301                 if ( iLogDir.length() == 0 )
       
   302                     {
       
   303                     // No logging file defined
       
   304                     LogWriter.initialize( null );
       
   305                     }
       
   306                 else
       
   307                     {
       
   308                     LogWriter.initialize( new File( iLogDir ) );
       
   309                     }
       
   310                 }
       
   311             }
       
   312         }
       
   313     }