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 com.intellij.openapi.diagnostic.Logger; 020 import org.jetbrains.annotations.NotNull; 021 import org.jetbrains.annotations.Nullable; 022 023 import java.io.File; 024 import java.io.IOException; 025 import java.io.InputStream; 026 import java.util.Properties; 027 import java.util.jar.Attributes; 028 import java.util.jar.JarFile; 029 import java.util.jar.Manifest; 030 031 public class LibraryUtils { 032 private static final Logger LOG = Logger.getInstance(LibraryUtils.class); 033 034 public static final String TITLE_KOTLIN_JVM_RUNTIME_AND_STDLIB; 035 public static final String TITLE_KOTLIN_JAVASCRIPT_STDLIB; 036 037 static { 038 String jsStdLib = ""; 039 String jvmStdLib = ""; 040 041 InputStream manifestProperties = LibraryUtils.class.getResourceAsStream("/manifest.properties"); 042 if (manifestProperties != null) { 043 try { 044 Properties properties = new Properties(); 045 properties.load(manifestProperties); 046 jvmStdLib = properties.getProperty("manifest.impl.title.kotlin.jvm.runtime.and.stdlib"); 047 jsStdLib = properties.getProperty("manifest.impl.title.kotlin.javascript.stdlib"); 048 } 049 catch (IOException e) { 050 LOG.error(e); 051 } 052 } 053 else { 054 LOG.error("Resource 'manifest.properties' not found."); 055 } 056 057 TITLE_KOTLIN_JVM_RUNTIME_AND_STDLIB = jvmStdLib; 058 TITLE_KOTLIN_JAVASCRIPT_STDLIB = jsStdLib; 059 } 060 061 private LibraryUtils() {} 062 063 @Nullable 064 public static Manifest getManifestFromJar(@NotNull File library) { 065 if (!library.canRead()) return null; 066 067 try { 068 JarFile jarFile = new JarFile(library); 069 try { 070 return jarFile.getManifest(); 071 } 072 finally { 073 jarFile.close(); 074 } 075 } 076 catch (IOException ignored) { 077 return null; 078 } 079 } 080 081 @Nullable 082 public static Attributes getManifestMainAttributesFromJar(@NotNull File library) { 083 Manifest manifest = getManifestFromJar(library); 084 return manifest != null ? manifest.getMainAttributes() : null; 085 } 086 087 private static boolean checkImplTitle(@NotNull File library, String expected) { 088 Attributes attributes = getManifestMainAttributesFromJar(library); 089 if (attributes == null) return false; 090 091 String title = attributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE); 092 return title != null && title.equals(expected); 093 } 094 095 public static boolean isJsRuntimeLibrary(@NotNull File library) { 096 return checkImplTitle(library, TITLE_KOTLIN_JAVASCRIPT_STDLIB); 097 } 098 099 public static boolean isJvmRuntimeLibrary(@NotNull File library) { 100 return checkImplTitle(library, TITLE_KOTLIN_JVM_RUNTIME_AND_STDLIB); 101 } 102 }