1   package org.apache.maven.plugin.eclipse;
2   
3   import java.io.DataOutputStream;
4   import java.io.File;
5   import java.io.FileInputStream;
6   import java.io.FileNotFoundException;
7   import java.io.FileOutputStream;
8   import java.io.IOException;
9   import java.net.MalformedURLException;
10  import java.util.Properties;
11  
12  import org.apache.maven.plugin.eclipse.reader.ReadWorkspaceLocations;
13  import org.codehaus.plexus.util.IOUtil;
14  import org.eclipse.core.internal.localstore.ILocalStoreConstants;
15  
16  public class TempEclipseWorkspace
17  {
18      private static TempEclipseWorkspace rad7WithDefault14;
19  
20      private static TempEclipseWorkspace eclipseWithDefault15;
21  
22      private static TempEclipseWorkspace eclipseWithDefault13;
23  
24      private static TempEclipseWorkspace dynamicWorkspace;
25  
26      /**
27       * @return RAD 7 workspace, JDK 14, includes projects: "direct-compile"
28       * @throws Exception
29       */
30      public static TempEclipseWorkspace getFixtureEclipseWorkspaceWithRad7Default14()
31          throws Exception
32      {
33          if ( rad7WithDefault14 == null )
34          {
35              rad7WithDefault14 = new TempEclipseWorkspace( "rad7WithDefault14", new String[] { "direct-compile" } );
36          }
37          return rad7WithDefault14;
38      }
39  
40      /**
41       * @return Eclipse workspace, JDK 1.5, includes projects: "direct-compile".
42       * @throws Exception
43       */
44      public static TempEclipseWorkspace getFixtureEclipseWithDefault15()
45          throws Exception
46      {
47          if ( eclipseWithDefault15 == null )
48          {
49              eclipseWithDefault15 = new TempEclipseWorkspace( "eclipseWithDefault15", new String[] { "direct-compile" } );
50          }
51          return eclipseWithDefault15;
52      }
53  
54      /**
55       * @return Eclipse workspace, JDK 1.3, includes projects: "direct-compile"
56       * @throws Exception
57       */
58      public static TempEclipseWorkspace getFixtureEclipseWithDefault13()
59          throws Exception
60      {
61          if ( eclipseWithDefault13 == null )
62          {
63              eclipseWithDefault13 = new TempEclipseWorkspace( "eclipseWithDefault13", new String[] { "direct-compile" } );
64          }
65          return eclipseWithDefault13;
66      }
67  
68      /**
69       * @return Eclipse workspace, JDK 1.4, includes projects: "project-A/module-A1", "../project-O"
70       * @throws Exception
71       */
72      public static TempEclipseWorkspace getFixtureEclipseDynamicWorkspace()
73          throws Exception
74      {
75          if ( dynamicWorkspace == null )
76          {
77              dynamicWorkspace =
78                  new TempEclipseWorkspace( "dynamicWorkspace", new String[] { "project-A/module-A1", "../project-O" } );
79          }
80          return dynamicWorkspace;
81      }
82  
83      public File workspaceLocation;
84  
85      public TempEclipseWorkspace( String testWorkspaceName, String[] projectsToLink )
86          throws Exception
87      {
88  
89          File eclipseLocation = new java.io.File( "target/test-classes/eclipse" ).getCanonicalFile();
90  
91          File jdkLocation = new File( eclipseLocation, "dummyJDK" );
92  
93          workspaceLocation = new File( eclipseLocation, testWorkspaceName + "/workspace" ).getCanonicalFile();
94  
95          File propertyfile =
96              new File( workspaceLocation,
97                        ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs" );
98  
99          preparePropertyFile( jdkLocation, propertyfile );
100 
101         if ( projectsToLink != null && projectsToLink.length != 0 )
102         {
103             for ( int i = 0; i < projectsToLink.length; i++ )
104             {
105                 String projectToLink = projectsToLink[i];
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
117      * @throws MalformedURLException
118      * @throws FileNotFoundException
119      * @throws IOException
120      */
121     private void writeLocationFile( String projectToLink )
122         throws MalformedURLException, FileNotFoundException, 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, FileNotFoundException
145     {
146         Properties properties = new Properties();
147         properties.load( new FileInputStream( propertyfile ) );
148         properties.setProperty(
149                                 "org.eclipse.jdt.launching.PREF_VM_XML",
150                                 properties.getProperty( "org.eclipse.jdt.launching.PREF_VM_XML" ).replaceAll(
151                                                                                                               "__replace_with_test_dir__",
152                                                                                                               jdkLocation.getCanonicalPath().replace(
153                                                                                                                                                       '\\',
154                                                                                                                                                       '/' ) ) );
155         properties.store( new FileOutputStream( propertyfile ), "" );
156     }
157 
158 }