KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > ContextConfig


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.boot;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.jdom.JDOMException;
33
34
35 /**
36  * {@link PropertyClass} implementation suitable for context configuration
37  * properties
38  *
39  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
40  */

41 public class ContextConfig extends AbstractXMLDefinedPropertyClass {
42
43     final static Log log = LogFactory.getLog(ContextConfig.class);
44
45     /**
46      * Constant for name
47      */

48     public final static String JavaDoc NAME = "contextConfig";
49
50     // Private instance variables
51

52     private Properties JavaDoc contextProperties = new Properties JavaDoc();
53
54     /**
55      * Constructor.
56      *
57      * @throws IOException
58      * @throws JDOMException
59      */

60     public ContextConfig() throws IOException JavaDoc, JDOMException {
61         super(NAME, false);
62         File JavaDoc contextFile = new File JavaDoc(ContextHolder.getContext().getConfDirectory(), "webserver.properties");
63         if (contextFile.exists()) {
64             contextProperties.load(new FileInputStream JavaDoc(contextFile));
65         }
66     }
67
68     /*
69      * (non-Javadoc)
70      *
71      * @see com.sslexplorer.properties.PropertyType#retrieve(com.sslexplorer.properties.PropertyKey)
72      */

73     public String JavaDoc retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
74         PropertyDefinition def = getDefinition(key.getName());
75         try {
76             String JavaDoc val = contextProperties.getProperty(key.getName(), def.getDefaultValue());
77             if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
78                 try {
79                     val = ContextHolder.getContext().deobfuscatePassword(val);
80                 } catch (Throwable JavaDoc t) {
81                     log.warn("Password property " + def.getName() + " could not be decoded. It has been result to the default.", t);
82                 }
83             }
84             return val;
85         } catch (Exception JavaDoc e) {
86             log.error("Failed to retrieve property.", e);
87         }
88         return null;
89     }
90
91     public String JavaDoc storePropertyImpl(AbstractPropertyKey key, String JavaDoc value) throws IllegalArgumentException JavaDoc {
92         PropertyDefinition def = getDefinition(key.getName());
93         String JavaDoc oldValue = retrieveProperty(key);
94         if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
95             try {
96                 value = ContextHolder.getContext().obfuscatePassword(value);
97             } catch (Throwable JavaDoc t) {
98                 log.warn("Password property " + def.getName() + " could not be encoded.", t);
99             }
100         }
101         contextProperties.put(key.getName(), value);
102         try {
103             OutputStream JavaDoc out = new FileOutputStream JavaDoc(new File JavaDoc(ContextHolder.getContext().getConfDirectory(), "webserver.properties"));
104             contextProperties.store(out, "SSL-Explorer webserver properties");
105             Util.closeStream(out);
106
107         } catch (IOException JavaDoc ex) {
108             log.error("Failed to save web server properties! Your server may not initialize correctly", ex);
109         }
110         return oldValue;
111     }
112 }
113
Popular Tags