ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / embedder / CustomArtifact.java
blobd474223b4a0b750a342079a7af6bca82091e5c52
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.maven.embedder;
18 import com.intellij.openapi.util.io.FileUtil;
19 import gnu.trove.THashMap;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.artifact.handler.ArtifactHandler;
22 import org.apache.maven.artifact.metadata.ArtifactMetadata;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
25 import org.apache.maven.artifact.versioning.ArtifactVersion;
26 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
27 import org.apache.maven.artifact.versioning.VersionRange;
28 import org.jetbrains.idea.maven.utils.MavenLog;
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.concurrent.locks.Lock;
38 import java.util.concurrent.locks.ReentrantReadWriteLock;
40 public class CustomArtifact implements Artifact {
41 private static final Map<String, File> ourStubCache = new THashMap<String, File>();
42 private static final ReentrantReadWriteLock ourCacheLock = new ReentrantReadWriteLock();
43 private static final Lock ourCacheReadLock = ourCacheLock.readLock();
44 private static final Lock ourCacheWriteLock = ourCacheLock.writeLock();
46 private final Artifact myWrapee;
47 private volatile boolean isStub;
49 public CustomArtifact(Artifact a) {
50 myWrapee = a;
53 public String getGroupId() {
54 return myWrapee.getGroupId();
57 public String getArtifactId() {
58 return myWrapee.getArtifactId();
61 public String getVersion() {
62 return myWrapee.getVersion();
65 public void setVersion(String version) {
66 myWrapee.setVersion(version);
69 public String getScope() {
70 return myWrapee.getScope();
73 public String getType() {
74 return myWrapee.getType();
77 public String getClassifier() {
78 return myWrapee.getClassifier();
81 public boolean hasClassifier() {
82 return myWrapee.hasClassifier();
85 public File getFile() {
86 update();
87 return myWrapee.getFile();
90 private void update() {
91 if ("pom".equals(getType()) && isResolved()) {
92 ensurePomFileExists();
96 private void ensurePomFileExists() {
97 File f = myWrapee.getFile();
98 if (f == null || f.exists()) return;
100 isStub = true;
102 ourCacheReadLock.lock();
103 try {
104 f = ourStubCache.get(getId());
106 finally {
107 ourCacheReadLock.unlock();
110 if (f != null) {
111 myWrapee.setFile(f);
112 return;
115 ourCacheWriteLock.lock();
116 try {
117 f = ourStubCache.get(getId());
118 if (f != null) {
119 myWrapee.setFile(f);
120 return;
123 f = FileUtil.createTempFile("idea.maven.stub", ".pom");
124 f.deleteOnExit();
126 FileOutputStream s = new FileOutputStream(f);
127 try {
128 PrintWriter w = new PrintWriter(s);
129 w.println("<project>");
130 w.println("<modelVersion>4.0.0</modelVersion>");
131 w.println("<packaging>pom</packaging>");
132 w.println("<groupId>" + getGroupId() + "</groupId>");
133 w.println("<artifactId>" + getArtifactId() + "</artifactId>");
134 w.println("<version>" + getVersion() + "</version>");
135 w.println("</project>");
136 w.flush();
138 finally {
139 s.close();
142 myWrapee.setFile(f);
143 ourStubCache.put(getId(), f);
145 catch (IOException e) {
146 MavenLog.LOG.warn(e);
148 finally {
149 ourCacheWriteLock.unlock();
153 public void setFile(File destination) {
154 myWrapee.setFile(destination);
157 public boolean isStub() {
158 return isStub;
161 public String getBaseVersion() {
162 return myWrapee.getBaseVersion();
165 public void setBaseVersion(String baseVersion) {
166 myWrapee.setBaseVersion(baseVersion);
169 public String getId() {
170 return myWrapee.getId();
173 public String getDependencyConflictId() {
174 return myWrapee.getDependencyConflictId();
177 public ArtifactMetadata getMetadata(Class<?> metadataClass) {
178 return myWrapee.getMetadata(metadataClass);
181 public void addMetadata(ArtifactMetadata metadata) {
182 myWrapee.addMetadata(metadata);
185 public Collection<ArtifactMetadata> getMetadataList() {
186 return myWrapee.getMetadataList();
189 public void setRepository(ArtifactRepository remoteRepository) {
190 myWrapee.setRepository(remoteRepository);
193 public ArtifactRepository getRepository() {
194 return myWrapee.getRepository();
197 public void updateVersion(String version, ArtifactRepository localRepository) {
198 myWrapee.updateVersion(version, localRepository);
201 public String getDownloadUrl() {
202 return myWrapee.getDownloadUrl();
205 public void setDownloadUrl(String downloadUrl) {
206 myWrapee.setDownloadUrl(downloadUrl);
209 public ArtifactFilter getDependencyFilter() {
210 return myWrapee.getDependencyFilter();
213 public void setDependencyFilter(ArtifactFilter artifactFilter) {
214 myWrapee.setDependencyFilter(artifactFilter);
217 public ArtifactHandler getArtifactHandler() {
218 return myWrapee.getArtifactHandler();
221 public List<String> getDependencyTrail() {
222 return myWrapee.getDependencyTrail();
225 public void setDependencyTrail(List<String> dependencyTrail) {
226 myWrapee.setDependencyTrail(dependencyTrail);
229 public void setScope(String scope) {
230 myWrapee.setScope(scope);
233 public VersionRange getVersionRange() {
234 return myWrapee.getVersionRange();
237 public void setVersionRange(VersionRange newRange) {
238 myWrapee.setVersionRange(newRange);
241 public void selectVersion(String version) {
242 myWrapee.selectVersion(version);
245 public void setGroupId(String groupId) {
246 myWrapee.setGroupId(groupId);
249 public void setArtifactId(String artifactId) {
250 myWrapee.setArtifactId(artifactId);
253 public boolean isSnapshot() {
254 return myWrapee.isSnapshot();
257 public void setResolved(boolean resolved) {
258 myWrapee.setResolved(resolved);
261 public boolean isResolved() {
262 return myWrapee.isResolved();
265 public void setResolvedVersion(String version) {
266 myWrapee.setResolvedVersion(version);
269 public void setArtifactHandler(ArtifactHandler handler) {
270 myWrapee.setArtifactHandler(handler);
273 public boolean isRelease() {
274 return myWrapee.isRelease();
277 public void setRelease(boolean release) {
278 myWrapee.setRelease(release);
281 public List<ArtifactVersion> getAvailableVersions() {
282 return myWrapee.getAvailableVersions();
285 public void setAvailableVersions(List<ArtifactVersion> versions) {
286 myWrapee.setAvailableVersions(versions);
289 public boolean isOptional() {
290 return myWrapee.isOptional();
293 public void setOptional(boolean optional) {
294 myWrapee.setOptional(optional);
297 public ArtifactVersion getSelectedVersion() throws OverConstrainedVersionException {
298 return myWrapee.getSelectedVersion();
301 public boolean isSelectedVersionKnown() throws OverConstrainedVersionException {
302 return myWrapee.isSelectedVersionKnown();
305 public int compareTo(Artifact o) {
306 return myWrapee.compareTo(o);
309 @Override
310 public String toString() {
311 return myWrapee.toString();
314 @Override
315 public boolean equals(Object obj) {
316 if (obj instanceof CustomArtifact) obj = ((CustomArtifact)obj).myWrapee;
317 return myWrapee.equals(obj);
320 @Override
321 public int hashCode() {
322 return myWrapee.hashCode();