1 19 20 package com.sslexplorer.extensions.store; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.util.Properties ; 29 30 import com.sslexplorer.boot.ContextHolder; 31 import com.sslexplorer.boot.Util; 32 import com.sslexplorer.extensions.ExtensionBundle; 33 34 final class ExtensionStoreStatusManager { 35 private static final String DISABLED_EXTENSIONS_FILE = "disabledExtensions.properties"; 36 37 private ExtensionStoreStatusManager() { 38 } 39 40 static ExtensionBundle.ExtensionBundleStatus getExtensionStatus(String bundleId) throws IOException { 41 Properties properties = loadDisabledExtensionProperties(); 42 if(properties.containsKey(bundleId)) 43 { 44 boolean isSystem = Boolean.valueOf((String )properties.get(bundleId)); 45 return isSystem ? ExtensionBundle.ExtensionBundleStatus.SYSTEM_DISABLED : ExtensionBundle.ExtensionBundleStatus.DISABLED; 46 } 47 return ExtensionBundle.ExtensionBundleStatus.ENABLED; 48 } 49 50 55 static void systemDisableExtension(String bundleId) throws IOException { 56 disableExtension(bundleId, true); 57 } 58 59 64 static void disableExtension(String bundleId) throws IOException { 65 disableExtension(bundleId, false); 66 } 67 68 private static void disableExtension(String bundleId, boolean isSystem) throws IOException { 69 Properties properties = loadDisabledExtensionProperties(); 70 properties.put(bundleId, String.valueOf(isSystem)); 71 storeProperties(properties); 72 } 73 74 79 static void enableExtension(String bundleId) throws IOException { 80 Properties disabledExtensions = loadDisabledExtensionProperties(); 81 if(!disabledExtensions.containsKey(bundleId)) 82 return; 83 84 boolean isSystem = Boolean.valueOf((String )disabledExtensions.get(bundleId)); 85 if(isSystem) 86 throw new IllegalArgumentException ("System disabled extensions cannot be enabled"); 87 88 disabledExtensions.remove(bundleId); 89 storeProperties(disabledExtensions); 90 } 91 92 97 static void removeExtension(String bundleId) throws IOException { 98 Properties disabledExtensions = loadDisabledExtensionProperties(); 99 if(!disabledExtensions.containsKey(bundleId)) 100 return; 101 disabledExtensions.remove(bundleId); 102 storeProperties(disabledExtensions); 103 } 104 105 110 static void installExtension(String bundleId) throws IOException { 111 Properties disabledExtensions = loadDisabledExtensionProperties(); 112 disabledExtensions.remove(bundleId); 113 storeProperties(disabledExtensions); 114 } 115 116 private static Properties loadDisabledExtensionProperties() throws IOException { 117 InputStream inputStream = new FileInputStream (getDisabledExtensionFile()); 118 Properties properties = new Properties (); 119 properties.load(inputStream); 120 inputStream.close(); 121 return properties; 122 } 123 124 private static File getDisabledExtensionFile() throws IOException { 125 File confDirectory = ContextHolder.getContext().getConfDirectory(); 126 File file = new File (confDirectory, DISABLED_EXTENSIONS_FILE); 127 if(!file.exists() && !file.createNewFile()) 128 throw new IOException ("Failed to create disabled extensions file"); 129 return file; 130 } 131 132 private static void storeProperties(Properties properties) throws IOException { 133 OutputStream fileOutputStream = null; 134 try { 135 fileOutputStream = new FileOutputStream (getDisabledExtensionFile()); 136 properties.store(fileOutputStream, ""); 137 fileOutputStream.close(); 138 } finally { 139 Util.closeStream(fileOutputStream); 140 } 141 } 142 } 143 | Popular Tags |