Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ba8ef39c47121bc697fbeffbd1c6fa621e2f444 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.internal.launch;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourcePathComputer;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;

import org.codehaus.plexus.util.DirectoryScanner;

import org.apache.maven.artifact.Artifact;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.embedder.IMavenLauncherConfiguration;
import org.eclipse.m2e.core.internal.launch.AbstractMavenRuntime;
import org.eclipse.m2e.core.project.IMavenProjectFacade;


/**
 * Computes Maven launch configuration default source lookup path. Default source lookup includes Maven core libraries
 * only. It does not (and cannot) include entries for any Maven plugins which are loaded dynamically at runtime.
 * 
 * @author Eugene Kuleshov
 */
@SuppressWarnings({"restriction", "deprecation"})
public class MavenSourcePathComputer implements ISourcePathComputer {

  public MavenSourcePathComputer() {
  }

  public String getId() {
    return "org.eclipse.m2e.launching.MavenSourceComputer"; //$NON-NLS-1$
  }

  public ISourceContainer[] computeSourceContainers(ILaunchConfiguration configuration, IProgressMonitor monitor)
      throws CoreException {
    final List<IRuntimeClasspathEntry> entries = new ArrayList<IRuntimeClasspathEntry>();

    IRuntimeClasspathEntry jreEntry = JavaRuntime.computeJREEntry(configuration);
    if(jreEntry != null) {
      entries.add(jreEntry);
    }

    AbstractMavenRuntime runtime = MavenLaunchUtils.getMavenRuntime(configuration);
    IMavenLauncherConfiguration collector = new IMavenLauncherConfiguration() {
      public void addArchiveEntry(String entry) throws CoreException {
        addArchiveRuntimeClasspathEntry(entries, entry);
      }

      public void addProjectEntry(IMavenProjectFacade facade) {
        IJavaProject javaProject = JavaCore.create(facade.getProject());
        if(javaProject != null) {
          entries.add(JavaRuntime.newDefaultProjectClasspathEntry(javaProject));
        }
      }

      public void addRealm(String world) {
      }

      public void setMainType(String type, String world) {
      }
    };

    collector.addArchiveEntry(MavenLaunchUtils.getCliResolver(runtime));
    runtime.createLauncherConfiguration(collector, monitor);

    IRuntimeClasspathEntry[] resolved = JavaRuntime.resolveSourceLookupPath( //
        entries.toArray(new IRuntimeClasspathEntry[entries.size()]), configuration);
    return JavaRuntime.getSourceContainers(resolved);
  }

  protected void addArchiveRuntimeClasspathEntry(List<IRuntimeClasspathEntry> entries, String entryPath)
      throws CoreException {
    File entryFile = new File(entryPath);

    if(!entryFile.exists()) {
      return;
    }

    if(entryFile.isDirectory()) {
      DirectoryScanner ds = new DirectoryScanner();
      ds.setBasedir(entryFile);
      ds.setIncludes(new String[] {"META-INF/maven/*/*/pom.properties", //$NON-NLS-1$
      });
      ds.scan();
      String[] files = ds.getIncludedFiles();
      for(String file : files) {
        try {
          BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(entryFile, file)));
          try {
            addArchiveRuntimeClasspathEntry(entries, entryPath, is);
          } finally {
            is.close();
          }
        } catch(IOException e) {
          // ignore it
        }

      }
    } else {
      try {
        JarFile jar = new JarFile(entryFile);
        try {
          Enumeration<JarEntry> zes = jar.entries();
          while(zes.hasMoreElements()) {
            JarEntry ze = zes.nextElement();
            String name = ze.getName();
            if(!ze.isDirectory() && name.startsWith("META-INF/maven/") && name.endsWith("pom.properties")) { //$NON-NLS-1$ //$NON-NLS-2$
              addArchiveRuntimeClasspathEntry(entries, entryPath, jar.getInputStream(ze));
            }
          }
        } finally {
          jar.close();
        }
      } catch(IOException e) {
        // ignore it
      }
    }

  }

  private void addArchiveRuntimeClasspathEntry(List<IRuntimeClasspathEntry> entries, String entryPath, InputStream is)
      throws IOException, CoreException {
    Properties p = new Properties();
    p.load(is);

    String groupId = p.getProperty("groupId"); //$NON-NLS-1$
    String artifactId = p.getProperty("artifactId"); //$NON-NLS-1$
    String version = p.getProperty("version"); //$NON-NLS-1$

    File sourcesJar = getSourcesJar(groupId, artifactId, version);
    if(sourcesJar != null) {
      IRuntimeClasspathEntry entry = null;
      entry = JavaRuntime.newArchiveRuntimeClasspathEntry(Path.fromOSString(entryPath));
      entry.setSourceAttachmentPath(Path.fromOSString(sourcesJar.getAbsolutePath()));
      entries.add(entry);
    }
  }

  private File getSourcesJar(String groupId, String artifactId, String version) {
    if(groupId != null && artifactId != null && version != null) {
      IMaven maven = MavenPlugin.getMaven();

      try {
        Artifact artifact = maven.resolve(groupId, artifactId, version, "jar", "sources", null, null); //$NON-NLS-1$ //$NON-NLS-2$
        File file = artifact.getFile();

        if(file != null && file.exists() && file.canRead()) {
          return file;
        }
      } catch(CoreException ex) {
        // artifact not found, most likely... 
        // TODO add special status code so it is possible to know for sure 
      }
    }

    return null;
  }

}

Back to the top