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