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
|
/*******************************************************************************
* 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.core.internal.project;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.QualifiedName;
import org.apache.maven.project.MavenProject;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.internal.project.registry.MavenProjectManager;
import org.eclipse.m2e.core.project.IMavenProjectChangedListener;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenProjectChangedEvent;
/**
* Maintains map file of maven artifacts present in workspace.
*/
public class WorkspaceStateWriter implements IMavenProjectChangedListener {
private static QualifiedName PPROP_EXTENSION = new QualifiedName(WorkspaceStateWriter.class.getName(), "extension"); //$NON-NLS-1$
private static final Logger log = LoggerFactory.getLogger(WorkspaceStateWriter.class);
private MavenProjectManager projectManager;
public WorkspaceStateWriter(MavenProjectManager projectManager) {
this.projectManager = projectManager;
}
public void mavenProjectChanged(MavenProjectChangedEvent[] events, IProgressMonitor monitor) {
try {
Properties state = new Properties();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
for(IMavenProjectFacade projectFacade : projectManager.getProjects()) {
IProject project = projectFacade.getProject();
if(!project.isAccessible()) {
log.debug("Project registry contains closed project {}", project);
// this is actually a bug somewhere in registry refresh logic, closed projects should not be there
continue;
}
try {
ArtifactKey artifact = projectFacade.getArtifactKey();
IFile pomFile = projectFacade.getPom();
IPath location = pomFile.getLocation();
if(location != null) {
File pom = location.toFile();
if(pom.canRead()) {
String key = artifact.getGroupId() + ":" + artifact.getArtifactId() + ":pom:" + artifact.getVersion(); //$NON-NLS-1$ //$NON-NLS-2$
state.put(key, pom.getCanonicalPath());
}
}
IResource outputLocation = root.findMember(projectFacade.getOutputLocation());
if(!"pom".equals(projectFacade.getPackaging()) && outputLocation != null && outputLocation.exists()) { //$NON-NLS-1$
// three cases to consider
// 1. facade has cached MavenProject instance, i.e. it was refreshed during this eclipse session
// 2. project has persistent PPROP_EXTENSION
// 3. neither cached MavenProject instance nor PPROP_EXTENSION are present
String extension;
MavenProject mavenProject = projectFacade.getMavenProject();
if(mavenProject != null) {
extension = getAndPersistArtifactExtension(project, mavenProject);
} else {
extension = project.getPersistentProperty(PPROP_EXTENSION);
}
if(extension == null && mavenProject == null) {
// force loading of MavenProject
extension = getAndPersistArtifactExtension(project, projectFacade.getMavenProject(monitor));
}
if(extension != null) {
String key = artifact.getGroupId()
+ ":" + artifact.getArtifactId() + ":" + extension + ":" + artifact.getVersion(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
state.put(key, outputLocation.getLocation().toFile().getCanonicalPath());
} else {
log.warn("Could not determine project {} main artifact extension.", project);
}
}
} catch(CoreException ex) {
log.error("Error writing workspace state file", ex);
}
}
OutputStream buf = new BufferedOutputStream(new FileOutputStream(projectManager.getWorkspaceStateFile()));
try {
state.store(buf, null);
} finally {
buf.close();
}
} catch(IOException ex) {
log.error("Error writing workspace state file", ex);
}
}
private String getAndPersistArtifactExtension(IProject project, MavenProject mavenProject) throws CoreException {
String extension = mavenProject.getArtifact().getArtifactHandler().getExtension();
project.setPersistentProperty(PPROP_EXTENSION, extension);
return extension;
}
}
|