View Javadoc
1   package org.apache.maven.plugin.eclipse;
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.DataOutputStream;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileNotFoundException;
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  import java.net.MalformedURLException;
30  import java.util.Properties;
31  
32  import org.apache.commons.io.FileUtils;
33  import org.apache.maven.plugin.eclipse.reader.ReadWorkspaceLocations;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.eclipse.core.internal.localstore.ILocalStoreConstants;
36  
37  public class TempEclipseWorkspace
38  {
39      private static int workspaceNumber = 0;
40      
41      /**
42       * @return RAD 7 workspace, JDK 14, includes projects: "direct-compile"
43       * @throws Exception
44       */
45      public static TempEclipseWorkspace getFixtureEclipseWorkspaceWithRad7Default14()
46          throws Exception
47      {
48          return new TempEclipseWorkspace( "rad7WithDefault14", new String[] { "direct-compile" } );
49      }
50  
51      /**
52       * @return Eclipse workspace, JDK 1.5, includes projects: "direct-compile".
53       * @throws Exception
54       */
55      public static TempEclipseWorkspace getFixtureEclipseWithDefault15()
56          throws Exception
57      {
58          return new TempEclipseWorkspace( "eclipseWithDefault15", new String[] { "direct-compile" } );
59      }
60  
61      /**
62       * @return Eclipse workspace, JDK 1.3, includes projects: "direct-compile"
63       * @throws Exception
64       */
65      public static TempEclipseWorkspace getFixtureEclipseWithDefault13()
66          throws Exception
67      {
68          return new TempEclipseWorkspace( "eclipseWithDefault13", new String[] { "direct-compile" } );
69      }
70  
71      /**
72       * @return Eclipse workspace, JDK 1.4, includes projects: "project-A/module-A1", "../project-O"
73       * @throws Exception
74       */
75      public static TempEclipseWorkspace getFixtureEclipseDynamicWorkspace()
76          throws Exception
77      {
78          return new TempEclipseWorkspace( "dynamicWorkspace", new String[] { "project-A/module-A1", "../project-O" } );
79      }
80  
81      public File workspaceLocation;
82  
83      public TempEclipseWorkspace( String testWorkspaceName, String[] projectsToLink )
84          throws Exception
85      {
86  
87          File tempWorkspace = new File( "target/tmp-workspace" + workspaceNumber++ );
88          FileUtils.deleteDirectory( tempWorkspace );
89          FileUtils.copyDirectoryToDirectory( new File( "src/test/resources/eclipse" ), tempWorkspace );
90  
91          File eclipseLocation = new File( tempWorkspace, "eclipse" ).getCanonicalFile();
92  
93          File jdkLocation = new File( eclipseLocation, "dummyJDK" );
94  
95          workspaceLocation = new File( eclipseLocation, testWorkspaceName + "/workspace" ).getCanonicalFile();
96  
97          File propertyfile =
98              new File( workspaceLocation,
99                        ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs" );
100 
101         preparePropertyFile( jdkLocation, propertyfile );
102 
103         if ( projectsToLink != null && projectsToLink.length != 0 )
104         {
105             for (String projectToLink : projectsToLink) {
106                 writeLocationFile(projectToLink);
107             }
108         }
109 
110     }
111 
112     /**
113      * Given the relative path from the workspace to the project to link use the basename as the project name and link
114      * this project to the fully qualified path anchored at workspaceLocation.
115      * 
116      * @param projectToLink The project to link
117      * @throws MalformedURLException
118      * @throws FileNotFoundException
119      * @throws IOException
120      */
121     private void writeLocationFile( String projectToLink )
122         throws IOException
123     {
124         File projectToLinkAsRelativeFile = new File( projectToLink );
125 
126         File projectWorkspaceDirectory =
127             new File( workspaceLocation, projectToLinkAsRelativeFile.getPath() ).getCanonicalFile();
128         String uriToProjectWorkspaceDirectory = "URI//" + projectWorkspaceDirectory.toURI().toURL().toString();
129 
130         File metaDataPlugins =
131             new File( workspaceLocation, ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RESOURCES_PROJECTS );
132         File projectMetaDataDirectory = new File( metaDataPlugins, projectToLinkAsRelativeFile.getName() );
133         File locationFile = new File( projectMetaDataDirectory, ReadWorkspaceLocations.BINARY_LOCATION_FILE );
134 
135         DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream( locationFile ) );
136 
137         dataOutputStream.write( ILocalStoreConstants.BEGIN_CHUNK );
138         dataOutputStream.writeUTF( uriToProjectWorkspaceDirectory );
139         dataOutputStream.write( ILocalStoreConstants.END_CHUNK );
140         IOUtil.close( dataOutputStream );
141     }
142 
143     private static void preparePropertyFile( File jdkLocation, File propertyfile )
144         throws IOException {
145         Properties properties = new Properties();
146         properties.load( new FileInputStream( propertyfile ) );
147         properties.setProperty(
148                                 "org.eclipse.jdt.launching.PREF_VM_XML",
149                                 properties.getProperty( "org.eclipse.jdt.launching.PREF_VM_XML" ).replaceAll(
150                                                                                                               "__replace_with_test_dir__",
151                                                                                                               jdkLocation.getCanonicalPath().replace(
152                                                                                                                                                       '\\',
153                                                                                                                                                       '/' ) ) );
154         properties.store( new FileOutputStream( propertyfile ), "" );
155     }
156 
157 }