ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / utils / MavenArtifactUtil.java
blobdfbc2a11ef04f590cc08ebd19b7e2a28f77233fd
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.utils;
18 import com.intellij.openapi.util.io.FileUtil;
19 import com.intellij.openapi.util.text.StringUtil;
20 import com.intellij.openapi.vfs.CharsetToolkit;
21 import gnu.trove.THashMap;
22 import org.jetbrains.annotations.Nullable;
23 import org.jetbrains.idea.maven.indices.IndicesBundle;
24 import org.jetbrains.idea.maven.project.MavenId;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipFile;
36 public class MavenArtifactUtil {
37 public static final String[] DEFAULT_GROUPS = new String[]{"org.apache.maven.plugins", "org.codehaus.mojo"};
38 public static final String MAVEN_PLUGIN_DESCRIPTOR = "META-INF/maven/plugin.xml";
40 private static final Map<File, MavenPluginInfo> ourPluginInfoCache = Collections.synchronizedMap(new THashMap<File, MavenPluginInfo>());
42 @Nullable
43 public static MavenPluginInfo readPluginInfo(File localRepository, MavenId mavenId) {
44 File file = getArtifactFile(localRepository, mavenId.getGroupId(), mavenId.getArtifactId(), mavenId.getVersion(), "jar");
46 MavenPluginInfo result = ourPluginInfoCache.get(file);
47 if (result == null) {
48 result = createPluginDocument(file);
49 ourPluginInfoCache.put(file, result);
51 return result;
54 public static boolean hasArtifactFile(File localRepository, MavenId id) {
55 return hasArtifactFile(localRepository, id, "jar");
58 public static boolean hasArtifactFile(File localRepository, MavenId id, String type) {
59 return getArtifactFile(localRepository, id, type).exists();
62 public static File getArtifactFile(File localRepostiory, MavenId id, String type) {
63 return getArtifactFile(localRepostiory, id.getGroupId(), id.getArtifactId(), id.getVersion(), type);
66 public static File getArtifactFile(File localRepostiory, String groupId, String artifactId, String version, String type) {
67 File dir = null;
68 if (StringUtil.isEmpty(groupId)) {
69 for (String each : DEFAULT_GROUPS) {
70 dir = getArtifactDirectory(localRepostiory, each, artifactId);
71 if (dir.exists()) break;
74 else {
75 dir = getArtifactDirectory(localRepostiory, groupId, artifactId);
78 if (StringUtil.isEmpty(version)) version = resolveVersion(dir);
79 return new File(dir, version + File.separator + artifactId + "-" + version + "." + type);
82 private static File getArtifactDirectory(File localRepository,
83 String groupId,
84 String artifactId) {
85 String relativePath = StringUtil.replace(groupId, ".", File.separator) + File.separator + artifactId;
86 return new File(localRepository, relativePath);
89 private static String resolveVersion(File pluginDir) {
90 List<String> versions = new ArrayList<String>();
92 File[] children;
93 try {
94 children = pluginDir.listFiles();
95 if (children == null) return "";
97 catch (Exception e) {
98 MavenLog.LOG.warn(e);
99 return "";
102 for (File version : children) {
103 if (version.isDirectory()) {
104 versions.add(version.getName());
108 if (versions.isEmpty()) return "";
110 Collections.sort(versions);
111 return versions.get(versions.size() - 1);
114 @Nullable
115 private static MavenPluginInfo createPluginDocument(File file) {
116 try {
117 if (!file.exists()) return null;
119 ZipFile jar = new ZipFile(file);
120 ZipEntry entry = jar.getEntry(MAVEN_PLUGIN_DESCRIPTOR);
122 if (entry == null) {
123 MavenLog.LOG.info(IndicesBundle.message("repository.plugin.corrupt", file));
124 return null;
127 InputStream is = jar.getInputStream(entry);
128 try {
129 byte[] bytes = FileUtil.loadBytes(is);
130 return new MavenPluginInfo(bytes);
132 finally {
133 is.close();
134 jar.close();
137 catch (IOException e) {
138 MavenLog.LOG.info(e);
139 return null;