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
.plugins
.groovy
.config
;
18 import com
.intellij
.openapi
.module
.Module
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.openapi
.roots
.impl
.libraries
.ProjectLibraryTable
;
21 import com
.intellij
.openapi
.roots
.libraries
.Library
;
22 import com
.intellij
.openapi
.roots
.libraries
.LibraryTable
;
23 import com
.intellij
.openapi
.util
.Condition
;
24 import com
.intellij
.openapi
.vfs
.VirtualFile
;
25 import com
.intellij
.util
.ArrayUtil
;
26 import com
.intellij
.util
.Function
;
27 import com
.intellij
.util
.containers
.ContainerUtil
;
28 import org
.jetbrains
.annotations
.NonNls
;
29 import org
.jetbrains
.annotations
.NotNull
;
30 import org
.jetbrains
.annotations
.Nullable
;
31 import org
.jetbrains
.plugins
.groovy
.util
.GroovyUtils
;
32 import org
.jetbrains
.plugins
.groovy
.util
.LibrariesUtil
;
35 import java
.io
.InputStream
;
36 import java
.util
.Collection
;
37 import java
.util
.List
;
38 import java
.util
.jar
.Attributes
;
39 import java
.util
.jar
.JarEntry
;
40 import java
.util
.jar
.JarFile
;
41 import java
.util
.jar
.Manifest
;
42 import java
.util
.regex
.Matcher
;
43 import java
.util
.regex
.Pattern
;
48 public abstract class AbstractConfigUtils
{
50 // SDK-dependent entities
51 @NonNls protected String STARTER_SCRIPT_FILE_NAME
;
53 private final Condition
<Library
> LIB_SEARCH_CONDITION
= new Condition
<Library
>() {
54 public boolean value(Library library
) {
55 return isSDKLibrary(library
);
60 @NonNls public static final String UNDEFINED_VERSION
= "undefined";
61 @NonNls public static final String MANIFEST_PATH
= "META-INF/MANIFEST.MF";
65 * Define, whether given home is appropriate SDK home
70 public abstract boolean isSDKHome(final VirtualFile file
);
73 public abstract String
getSDKVersion(@NotNull String path
);
76 * Return value of Implementation-Version attribute in jar manifest
79 * @param jarPath directory containing jar file
80 * @param jarRegex filename pattern for jar file
81 * @param manifestPath path to manifest file in jar file
82 * @return value of Implementation-Version attribute, null if not found
85 public static String
getSDKJarVersion(String jarPath
, final String jarRegex
, String manifestPath
) {
87 File
[] jars
= GroovyUtils
.getFilesInDirectoryByPattern(jarPath
, jarRegex
);
88 if (jars
.length
!= 1) {
91 JarFile jarFile
= new JarFile(jars
[0]);
93 JarEntry jarEntry
= jarFile
.getJarEntry(manifestPath
);
94 if (jarEntry
== null) {
97 final InputStream inputStream
= jarFile
.getInputStream(jarEntry
);
100 manifest
= new Manifest(inputStream
);
105 final String version
= manifest
.getMainAttributes().getValue(Attributes
.Name
.IMPLEMENTATION_VERSION
);
106 if (version
!= null) {
110 final Matcher matcher
= Pattern
.compile(jarRegex
).matcher(jars
[0].getName());
111 if (matcher
.matches() && matcher
.groupCount() == 1) {
112 return matcher
.group(1);
120 catch (Exception e
) {
125 public Library
[] getProjectSDKLibraries(Project project
) {
126 if (project
== null || project
.isDisposed()) return new Library
[0];
127 final LibraryTable table
= ProjectLibraryTable
.getInstance(project
);
128 final List
<Library
> all
= ContainerUtil
.findAll(table
.getLibraries(), LIB_SEARCH_CONDITION
);
129 return all
.toArray(new Library
[all
.size()]);
132 public Library
[] getAllSDKLibraries(@Nullable Project project
) {
133 return ArrayUtil
.mergeArrays(getGlobalSDKLibraries(), getProjectSDKLibraries(project
), Library
.class);
136 public Library
[] getGlobalSDKLibraries() {
137 return LibrariesUtil
.getGlobalLibraries(LIB_SEARCH_CONDITION
);
140 public abstract boolean isSDKLibrary(Library library
);
142 public Library
[] getSDKLibrariesByModule(final Module module
) {
143 final Condition
<Library
> condition
= new Condition
<Library
>() {
144 public boolean value(Library library
) {
145 return isSDKLibrary(library
);
148 return LibrariesUtil
.getLibrariesByCondition(module
, condition
);