KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > store > ExtensionStoreStatusManager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.extensions.store;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Properties JavaDoc;
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 JavaDoc DISABLED_EXTENSIONS_FILE = "disabledExtensions.properties";
36
37     private ExtensionStoreStatusManager() {
38     }
39     
40     static ExtensionBundle.ExtensionBundleStatus getExtensionStatus(String JavaDoc bundleId) throws IOException JavaDoc {
41         Properties JavaDoc properties = loadDisabledExtensionProperties();
42         if(properties.containsKey(bundleId))
43         {
44             boolean isSystem = Boolean.valueOf((String JavaDoc)properties.get(bundleId));
45             return isSystem ? ExtensionBundle.ExtensionBundleStatus.SYSTEM_DISABLED : ExtensionBundle.ExtensionBundleStatus.DISABLED;
46         }
47         return ExtensionBundle.ExtensionBundleStatus.ENABLED;
48     }
49     
50     /**
51      * Disables the extension, using this mode means it can't be re-enabled
52      * @param bundleId
53      * @throws IOException
54      */

55     static void systemDisableExtension(String JavaDoc bundleId) throws IOException JavaDoc {
56         disableExtension(bundleId, true);
57     }
58     
59     /**
60      * Disables the extension
61      * @param bundleId
62      * @throws IOException
63      */

64     static void disableExtension(String JavaDoc bundleId) throws IOException JavaDoc {
65         disableExtension(bundleId, false);
66     }
67
68     private static void disableExtension(String JavaDoc bundleId, boolean isSystem) throws IOException JavaDoc {
69         Properties JavaDoc properties = loadDisabledExtensionProperties();
70         properties.put(bundleId, String.valueOf(isSystem));
71         storeProperties(properties);
72     }
73     
74     /**
75      * Enables the extension
76      * @param bundleId
77      * @throws IOException
78      */

79     static void enableExtension(String JavaDoc bundleId) throws IOException JavaDoc {
80         Properties JavaDoc disabledExtensions = loadDisabledExtensionProperties();
81         if(!disabledExtensions.containsKey(bundleId))
82             return;
83         
84         boolean isSystem = Boolean.valueOf((String JavaDoc)disabledExtensions.get(bundleId));
85         if(isSystem)
86             throw new IllegalArgumentException JavaDoc("System disabled extensions cannot be enabled");
87         
88         disabledExtensions.remove(bundleId);
89         storeProperties(disabledExtensions);
90     }
91     
92     /**
93      * Remove the extension
94      * @param bundleId
95      * @throws IOException
96      */

97     static void removeExtension(String JavaDoc bundleId) throws IOException JavaDoc {
98         Properties JavaDoc disabledExtensions = loadDisabledExtensionProperties();
99         if(!disabledExtensions.containsKey(bundleId))
100             return;
101         disabledExtensions.remove(bundleId);
102         storeProperties(disabledExtensions);
103     }
104     
105     /**
106      *
107      * @param bundleId
108      * @throws IOException
109      */

110     static void installExtension(String JavaDoc bundleId) throws IOException JavaDoc {
111         Properties JavaDoc disabledExtensions = loadDisabledExtensionProperties();
112         disabledExtensions.remove(bundleId);
113         storeProperties(disabledExtensions);
114     }
115
116     private static Properties JavaDoc loadDisabledExtensionProperties() throws IOException JavaDoc {
117         InputStream JavaDoc inputStream = new FileInputStream JavaDoc(getDisabledExtensionFile());
118         Properties JavaDoc properties = new Properties JavaDoc();
119         properties.load(inputStream);
120         inputStream.close();
121         return properties;
122     }
123
124     private static File JavaDoc getDisabledExtensionFile() throws IOException JavaDoc {
125         File JavaDoc confDirectory = ContextHolder.getContext().getConfDirectory();
126         File JavaDoc file = new File JavaDoc(confDirectory, DISABLED_EXTENSIONS_FILE);
127         if(!file.exists() && !file.createNewFile())
128             throw new IOException JavaDoc("Failed to create disabled extensions file");
129         return file;
130     }
131
132     private static void storeProperties(Properties JavaDoc properties) throws IOException JavaDoc {
133         OutputStream JavaDoc fileOutputStream = null;
134         try {
135             fileOutputStream = new FileOutputStream JavaDoc(getDisabledExtensionFile());
136             properties.store(fileOutputStream, "");
137             fileOutputStream.close();
138         } finally {
139             Util.closeStream(fileOutputStream);
140         }
141     }
142 }
143
Popular Tags