KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > impl > resource > ResourceProperties


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.properties.impl.resource;
21
22 import java.io.IOException JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.jdom.JDOMException;
27
28 import com.sslexplorer.boot.AbstractPropertyKey;
29 import com.sslexplorer.boot.AbstractXMLDefinedPropertyClass;
30 import com.sslexplorer.boot.ContextHolder;
31 import com.sslexplorer.boot.PropertyClass;
32 import com.sslexplorer.boot.PropertyDefinition;
33 import com.sslexplorer.core.CoreServlet;
34 import com.sslexplorer.properties.ProfilesFactory;
35
36 /**
37  * {@link PropertyClass} implementation suitable for system configuration
38  * properties
39  *
40  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
41  */

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

49     public final static String JavaDoc NAME = "resourceProperties";
50
51     /**
52      * Constructor.
53      *
54      * @throws IOException
55      * @throws JDOMException
56      */

57     public ResourceProperties() throws IOException JavaDoc, JDOMException {
58         super(NAME, false);
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see com.sslexplorer.properties.PropertyType#retrieve(com.sslexplorer.properties.PropertyKey)
65      */

66     public String JavaDoc retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
67         PropertyDefinition def = getDefinition(key.getName());
68         ResourceKey resourceKey = (ResourceKey)key;
69         try {
70             String JavaDoc val = ProfilesFactory.getInstance().retrieveGenericProperty(
71                 resourceKey.getName(), String.valueOf(resourceKey.getResourceType().getResourceTypeId()), String.valueOf(resourceKey.getResourceId()), "", "");
72             if (val == null) {
73                 val = def.getDefaultValue();
74             } else {
75                 if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
76                     try {
77                         val = ContextHolder.getContext().deobfuscatePassword(val);
78                     } catch (Throwable JavaDoc t) {
79                         log.warn(
80                             "Password property " + def.getName() + " could not be decoded. It has been result to the default.", t);
81                     }
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         ResourceKey resourceKey = (ResourceKey)key;
94         String JavaDoc oldValue = retrieveProperty(key);
95
96         if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
97             try {
98                 value = ContextHolder.getContext().obfuscatePassword(value);
99             } catch (Throwable JavaDoc t) {
100                 log.warn("Password property " + def.getName() + " could not be encoded.", t);
101             }
102         }
103         try {
104             ProfilesFactory.getInstance().storeGenericProperty(
105                 resourceKey.getName(), String.valueOf(resourceKey.getResourceType().getResourceTypeId()), String.valueOf(resourceKey.getResourceId()), "", "", value);
106         } catch (Exception JavaDoc e) {
107             log.error("Could not store properties in database.");
108         }
109         return oldValue;
110     }
111 }
112
Popular Tags