001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.utils;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    
022    import java.io.File;
023    import java.io.IOException;
024    import java.util.jar.Attributes;
025    import java.util.jar.JarFile;
026    import java.util.jar.Manifest;
027    
028    public class LibraryUtils {
029        public static final String VENDOR_JETBRAINS = "JetBrains";
030    
031        public static final String TITLE_KOTLIN_RUNTIME_AND_STDLIB = "Kotlin Compiler Runtime + StdLib";
032        public static final String TITLE_KOTLIN_RUNTIME_AND_STDLIB_SOURCES = "Kotlin Compiler Runtime + StdLib Sources";
033        public static final String TITLE_KOTLIN_JAVASCRIPT_STDLIB = "Kotlin JavaScript StdLib";
034    
035        @Nullable
036        public static Manifest getManifestFromJar(@NotNull File library) {
037            if (!library.canRead()) return null;
038    
039            try {
040                JarFile jarFile = new JarFile(library);
041                try {
042                    return jarFile.getManifest();
043                }
044                finally {
045                    jarFile.close();
046                }
047            }
048            catch (IOException ignored) { }
049    
050            return null;
051        }
052    
053        @Nullable
054        public static Attributes getManifestMainAttributesFromJar(@NotNull File library) {
055            Manifest manifest = getManifestFromJar(library);
056            return manifest != null ? manifest.getMainAttributes() : null;
057        }
058    
059        private static boolean checkImplTitle(@NotNull File library, String expected) {
060            Attributes attributes = getManifestMainAttributesFromJar(library);
061            if (attributes == null) return false;
062    
063            String title = attributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
064            return title != null && title.equals(expected);
065        }
066    
067        public static boolean isJsRuntimeLibrary(@NotNull File library) {
068            return checkImplTitle(library, TITLE_KOTLIN_JAVASCRIPT_STDLIB);
069        }
070    
071        public static boolean isJvmRuntimeLibrary(@NotNull File library) {
072            return checkImplTitle(library, TITLE_KOTLIN_RUNTIME_AND_STDLIB);
073        }
074    }