Coverage Report - org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseWorkspaceWriter
0%
0/41
0%
0/6
2.5
 
 1  
 package org.apache.maven.plugin.eclipse.writers.workspace;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.FileNotFoundException;
 6  
 import java.io.FileOutputStream;
 7  
 import java.io.IOException;
 8  
 import java.io.OutputStream;
 9  
 import java.util.Properties;
 10  
 
 11  
 import org.apache.maven.plugin.MojoExecutionException;
 12  
 import org.apache.maven.plugin.eclipse.Messages;
 13  
 import org.apache.maven.plugin.eclipse.WorkspaceConfiguration;
 14  
 import org.apache.maven.plugin.logging.Log;
 15  
 import org.codehaus.plexus.util.IOUtil;
 16  
 
 17  0
 public class EclipseWorkspaceWriter
 18  
     implements WorkspaceWriter
 19  
 {
 20  
 
 21  
     /**
 22  
      * Path under Eclipse workspace where Eclipse Plugin metadata/config is stored.
 23  
      */
 24  
     public static final String ECLIPSE_PLUGINS_METADATA_DIR = ".metadata/.plugins"; //$NON-NLS-1$
 25  
 
 26  
     /**
 27  
      * Path under {@value #ECLIPSE_PLUGINS_METADATA_DIR } folder where Eclipse Workspace Runtime settings are stored.
 28  
      */
 29  
     public static final String ECLIPSE_CORE_RUNTIME_SETTINGS_DIR =
 30  
         ECLIPSE_PLUGINS_METADATA_DIR + "/org.eclipse.core.runtime/.settings"; //$NON-NLS-1$
 31  
     
 32  
     /**
 33  
      * Directory where workspace specific settings are written.
 34  
      */
 35  
     public static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$   
 36  
     
 37  
     /**
 38  
      * File that stores the Eclipse JDT Core preferences.
 39  
      */
 40  
     public static final String ECLIPSE_JDT_CORE_PREFS_FILE = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
 41  
 
 42  
     /**
 43  
      * Property constant under which Variable 'M2_REPO' is setup.
 44  
      */
 45  
     public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO"; //$NON-NLS-1$
 46  
 
 47  
     /**
 48  
      * File that stores the Eclipse JDT UI preferences.
 49  
      */
 50  
     public static final String ECLIPSE_JDT_UI_PREFS_FILE = "org.eclipse.jdt.ui.prefs"; //$NON-NLS-1$
 51  
 
 52  
     private WorkspaceConfiguration config;
 53  
 
 54  
     private Log logger;
 55  
 
 56  
     private File workDir;
 57  
 
 58  
     public WorkspaceWriter init( Log logger, WorkspaceConfiguration config )
 59  
     {
 60  0
         this.logger = logger;
 61  0
         this.config = config;
 62  
 
 63  0
         workDir = new File( config.getWorkspaceDirectory(), ECLIPSE_CORE_RUNTIME_SETTINGS_DIR );
 64  0
         workDir.mkdirs();
 65  
 
 66  0
         return this;
 67  
     }
 68  
 
 69  
     public void write()
 70  
         throws MojoExecutionException
 71  
     {
 72  0
         this.writeLocalRepositoryConfiguration();
 73  
 
 74  0
         if ( config.getCodeStylesURL() != null )
 75  
         {
 76  0
             this.writeCodeStyleConfiguration();
 77  
         }
 78  0
     }
 79  
 
 80  
     private void writeCodeStyleConfiguration()
 81  
         throws MojoExecutionException
 82  
     {
 83  0
         File f = new File( workDir, ECLIPSE_JDT_UI_PREFS_FILE );
 84  
 
 85  0
         Properties props = loadProperties( f );
 86  
 
 87  0
         EclipseCodeFormatterProfile codeFormatter =
 88  
             new EclipseCodeFormatterProfile().init( config.getCodeStylesURL(), config.getActiveStyleProfileName() );
 89  
 
 90  0
         if ( codeFormatter.getProfileName() != null )
 91  
         {
 92  0
             logger.info( "Set active code style profile name: " + codeFormatter.getProfileName() );
 93  0
             props.setProperty( "formatter_profile", "_" + codeFormatter.getProfileName() );
 94  
         }
 95  
 
 96  0
         props.setProperty( "org.eclipse.jdt.ui.formatterprofiles", codeFormatter.getContent() );
 97  
 
 98  0
         storeProperties( props, f );
 99  0
     }
 100  
 
 101  
     private void writeLocalRepositoryConfiguration()
 102  
         throws MojoExecutionException
 103  
     {
 104  0
         File f = new File( workDir, ECLIPSE_JDT_CORE_PREFS_FILE );
 105  
 
 106  0
         Properties props = loadProperties( f );
 107  
 
 108  0
         props.put( CLASSPATH_VARIABLE_M2_REPO, config.getLocalRepository().getBasedir() ); //$NON-NLS-1$  //$NON-NLS-2$
 109  
 
 110  0
         storeProperties( props, f );
 111  0
     }
 112  
 
 113  
     private static Properties loadProperties( File f )
 114  
         throws MojoExecutionException
 115  
     {
 116  0
         Properties props = new Properties();
 117  
 
 118  
         // preserve old settings
 119  0
         if ( f.exists() )
 120  
         {
 121  
             try
 122  
             {
 123  0
                 props.load( new FileInputStream( f ) );
 124  
             }
 125  0
             catch ( FileNotFoundException e )
 126  
             {
 127  0
                 throw new MojoExecutionException(
 128  
                                                   Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$
 129  
             }
 130  0
             catch ( IOException e )
 131  
             {
 132  0
                 throw new MojoExecutionException(
 133  
                                                   Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$
 134  0
             }
 135  
         }
 136  
 
 137  0
         return props;
 138  
     }
 139  
 
 140  
     private static void storeProperties( Properties props, File f )
 141  
         throws MojoExecutionException
 142  
     {
 143  0
         OutputStream os = null;
 144  
 
 145  
         try
 146  
         {
 147  0
             os = new FileOutputStream( f );
 148  0
             props.store( os, null );
 149  
         }
 150  0
         catch ( IOException ioe )
 151  
         {
 152  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", //$NON-NLS-1$
 153  
                                                                   f.getAbsolutePath() ) );
 154  
         }
 155  
         finally
 156  
         {
 157  0
             IOUtil.close( os );
 158  0
         }
 159  0
     }
 160  
 }