View | Details | Raw Unified | Return to bug 192647 | Differences between
and this patch

Collapse All | Expand All

(-)maven/src/org/netbeans/modules/maven/queries/RepositoryMavenCPProvider.java (+213 lines)
Line 0    Link Here 
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.maven.queries;
43
44
import java.io.File;
45
import java.util.ArrayList;
46
import java.util.Collections;
47
import java.util.List;
48
import java.util.logging.Level;
49
import java.util.logging.Logger;
50
import org.apache.maven.artifact.Artifact;
51
import org.apache.maven.artifact.repository.ArtifactRepository;
52
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
53
import org.apache.maven.artifact.repository.MavenArtifactRepository;
54
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
55
import org.apache.maven.model.building.ModelBuildingRequest;
56
import org.apache.maven.project.DefaultProjectBuildingRequest;
57
import org.apache.maven.project.MavenProject;
58
import org.apache.maven.project.ProjectBuildingException;
59
import org.apache.maven.project.ProjectBuildingResult;
60
import org.netbeans.api.java.classpath.ClassPath;
61
import org.netbeans.api.java.platform.JavaPlatformManager;
62
import org.netbeans.modules.maven.embedder.EmbedderFactory;
63
import org.netbeans.modules.maven.embedder.MavenEmbedder;
64
import org.netbeans.modules.maven.indexer.api.RepositoryInfo;
65
import org.netbeans.modules.maven.indexer.api.RepositoryPreferences;
66
import org.netbeans.spi.java.classpath.ClassPathFactory;
67
import org.netbeans.spi.java.classpath.ClassPathImplementation;
68
import org.netbeans.spi.java.classpath.ClassPathProvider;
69
import org.netbeans.spi.java.classpath.PathResourceImplementation;
70
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
71
import org.openide.filesystems.FileObject;
72
import org.openide.filesystems.FileUtil;
73
import org.openide.util.lookup.ServiceProvider;
74
75
/**
76
 *
77
 * @author mkleint
78
 */
79
@ServiceProvider (service=ClassPathProvider.class, position=11)
80
public class RepositoryMavenCPProvider implements ClassPathProvider {
81
    private static final Logger LOG = Logger.getLogger(RepositoryMavenCPProvider.class.getName());
82
    
83
    @Override
84
    public ClassPath findClassPath(FileObject file, String type) {
85
        FileObject archive = FileUtil.getArchiveFile(file);
86
        if (archive != null && archive.getNameExt().endsWith("-sources.jar")) { //first simple check
87
            File sourceFile = FileUtil.toFile(archive);
88
            if (sourceFile != null) {
89
//                String name = jarFile.getName();
90
                File parent = sourceFile.getParentFile();
91
                if (parent != null) {
92
                    File parentParent = parent.getParentFile();
93
                    if (parentParent != null) {
94
                        // each repository artifact should have this structure
95
                        String artifact = parentParent.getName();
96
                        String version = parent.getName();
97
                        //TODO is there a need for generified extension lookup or is just .jar files ok?
98
                          //TODO can the .jar extension be hardwired? on CP..
99
                        File bin = new File(parent, artifact + "-" + version + ".jar"); //NOI18N
100
                        File pom = new File(parent, artifact + "-" + version + ".pom"); //NOI18N
101
                        
102
                        FileObject localRepoRoot = FileUtil.toFileObject(new File(EmbedderFactory.getProjectEmbedder().getLocalRepository().getBasedir()));
103
                        if (FileUtil.isParentOf(localRepoRoot, archive)) {
104
                            String groupId = FileUtil.getRelativePath(localRepoRoot, FileUtil.toFileObject(parentParent.getParentFile()));
105
                            if (groupId != null && !groupId.equals("")) {
106
                                groupId = groupId.replace("/", ".");
107
                                if (ClassPath.SOURCE.equals(type)) {
108
                                    return ClassPathFactory.createClassPath(createSourceCPI(sourceFile));
109
                                }
110
                                if (ClassPath.BOOT.equals(type)) {
111
                                    return ClassPathFactory.createClassPath(createBootCPI());
112
                                }
113
                                if (ClassPath.COMPILE.equals(type)) {
114
                                    MavenProject mp = loadMavenProject(pom, groupId, artifact, version);
115
                                    return ClassPathFactory.createClassPath(createCompileCPI(mp, bin));
116
                                }
117
                                if (ClassPath.EXECUTE.equals(type)) {
118
                                    MavenProject mp = loadMavenProject(pom, groupId, artifact, version);
119
                                    return ClassPathFactory.createClassPath(createExecuteCPI(mp, bin));
120
                                }
121
                            } else {
122
                                //some sort of weird groupId?
123
                            }
124
                        }
125
                            
126
                    }
127
                }
128
            }
129
            
130
            
131
        }
132
        return null;
133
    }
134
    
135
    private MavenProject loadMavenProject(File pom, String groupId, String artifactId, String version) {
136
        MavenEmbedder embedder = EmbedderFactory.getOnlineEmbedder();
137
        Artifact projectArtifact = embedder.createArtifact(groupId, artifactId, version,  "jar");
138
        try {
139
            DefaultProjectBuildingRequest dpbr = new DefaultProjectBuildingRequest();
140
            dpbr.setLocalRepository(embedder.getLocalRepository());
141
            dpbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
142
            dpbr.setSystemProperties(embedder.getSystemProperties());
143
            
144
            dpbr.setProcessPlugins(false);
145
            dpbr.setResolveDependencies(true);
146
            ArrayList<ArtifactRepository> remoteRepos = new ArrayList<ArtifactRepository>();
147
            for (RepositoryInfo info : RepositoryPreferences.getInstance().getRepositoryInfos()) {
148
                if (!info.isLocal()) {
149
                    remoteRepos.add(new MavenArtifactRepository(info.getId(), info.getRepositoryUrl(), new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy()));
150
                }
151
            }
152
            dpbr.setRemoteRepositories(remoteRepos);
153
            
154
            ProjectBuildingResult res = embedder.buildProject(projectArtifact, dpbr);
155
            if (res.getProject() != null) {
156
                return res.getProject();
157
            } else {
158
                LOG.log(Level.INFO, "No project model from repository for {0}: {1}", new Object[] {projectArtifact, res.getProblems()});
159
            }
160
        } catch (ProjectBuildingException ex) {
161
            LOG.log(Level.FINER, "Failed to load project model from repository for {0}: {1}", new Object[] {projectArtifact, ex});
162
        } catch (Exception exception) {
163
            LOG.log(Level.FINER, "Failed to load project model from repository for " + projectArtifact, exception);
164
        }
165
        return null;
166
    }
167
    
168
    
169
    
170
    private ClassPathImplementation createCompileCPI(MavenProject project, File binary) {
171
        List<PathResourceImplementation> items = new ArrayList<PathResourceImplementation>();
172
        if (binary.exists()) {
173
            items.add(ClassPathSupport.createResource(FileUtil.urlForArchiveOrDir(binary)));
174
        }
175
        if (project != null) {
176
            for (Artifact s : project.getCompileArtifacts()) {
177
                if (s.getFile() == null) continue;
178
                File f = FileUtil.normalizeFile(s.getFile());
179
                items.add(ClassPathSupport.createResource(FileUtil.urlForArchiveOrDir(f)));
180
            }
181
        }
182
        return ClassPathSupport.createClassPathImplementation(items);
183
    } 
184
    
185
    private ClassPathImplementation createExecuteCPI(MavenProject project, File binary) {
186
        List<PathResourceImplementation> items = new ArrayList<PathResourceImplementation>();
187
        if (binary.exists()) {
188
            items.add(ClassPathSupport.createResource(FileUtil.urlForArchiveOrDir(binary)));
189
        }
190
        if (project != null) {
191
            for (Artifact s : project.getRuntimeArtifacts()) {
192
                if (s.getFile() == null) continue;
193
                File f = FileUtil.normalizeFile(s.getFile());
194
                items.add(ClassPathSupport.createResource(FileUtil.urlForArchiveOrDir(f)));
195
            }
196
        }
197
        return ClassPathSupport.createClassPathImplementation(items);
198
    }
199
    
200
    private ClassPathImplementation createSourceCPI(File sourceFile) {
201
        return ClassPathSupport.createClassPathImplementation(Collections.singletonList(ClassPathSupport.createResource(FileUtil.urlForArchiveOrDir(sourceFile))));
202
    }
203
    
204
    private ClassPathImplementation createBootCPI() {
205
        List<PathResourceImplementation> result = new ArrayList<PathResourceImplementation> ();
206
        for (ClassPath.Entry entry : JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries().entries()) {
207
            result.add(ClassPathSupport.createResource(entry.getURL()));
208
        }
209
        return ClassPathSupport.createClassPathImplementation(result);
210
    }
211
    
212
    
213
}

Return to bug 192647
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo