View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.workspace;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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       * Path under Eclipse workspace where Eclipse Plugin metadata/config is stored.
43       */
44      public static final String ECLIPSE_PLUGINS_METADATA_DIR = ".metadata/.plugins"; //$NON-NLS-1$
45  
46      /**
47       * Path under {@value #ECLIPSE_PLUGINS_METADATA_DIR } folder where Eclipse Workspace Runtime settings are stored.
48       */
49      public static final String ECLIPSE_CORE_RUNTIME_SETTINGS_DIR =
50          ECLIPSE_PLUGINS_METADATA_DIR + "/org.eclipse.core.runtime/.settings"; //$NON-NLS-1$
51      
52      /**
53       * Directory where workspace specific settings are written.
54       */
55      public static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$   
56      
57      /**
58       * File that stores the Eclipse JDT Core preferences.
59       */
60      public static final String ECLIPSE_JDT_CORE_PREFS_FILE = "org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
61  
62      /**
63       * Property constant under which Variable 'M2_REPO' is setup.
64       */
65      public static final String CLASSPATH_VARIABLE_M2_REPO = "org.eclipse.jdt.core.classpathVariable.M2_REPO"; //$NON-NLS-1$
66  
67      /**
68       * File that stores the Eclipse JDT UI preferences.
69       */
70      public static final String ECLIPSE_JDT_UI_PREFS_FILE = "org.eclipse.jdt.ui.prefs"; //$NON-NLS-1$
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() ); //$NON-NLS-1$  //$NON-NLS-2$
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         // preserve old settings
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 ); //$NON-NLS-1$
149             }
150             catch ( IOException e )
151             {
152                 throw new MojoExecutionException(
153                                                   Messages.getString( "EclipsePlugin.cantreadfile", f.getAbsolutePath() ), e ); //$NON-NLS-1$
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", //$NON-NLS-1$
173                                                                   f.getAbsolutePath() ) );
174         }
175         finally
176         {
177             IOUtil.close( os );
178         }
179     }
180 }