KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > impl > profile > ProfileProperties


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.profile;
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 properties that are stored
38  * against a either global or user profiles.
39  *
40  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
41  */

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

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

57     public ProfileProperties() throws IOException JavaDoc, JDOMException {
58         super(NAME, true);
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         if (!(key instanceof ProfilePropertyKey)) {
68             throw new IllegalArgumentException JavaDoc("Property key is not an instanceof ProfilePropertyKey");
69         }
70         PropertyDefinition def = getDefinition(key.getName());
71         ProfilePropertyKey profilesKey = (ProfilePropertyKey) key;
72         try {
73             String JavaDoc val = ProfilesFactory.getInstance().retrieveGenericProperty(profilesKey.getName(),
74                 profilesKey.isUserSpecific() ? profilesKey.getUsername() : "",
75                 String.valueOf(profilesKey.getProfile()),
76                 String.valueOf(profilesKey.getRealm()),
77                 "");
78             // If a username was supplied, then now try the global profiles
79
if(val == null && profilesKey.isUserSpecific()) {
80                 val = ProfilesFactory.getInstance().retrieveGenericProperty(profilesKey.getName(),
81                     "",
82                     String.valueOf(profilesKey.getProfile()),
83                     String.valueOf(profilesKey.getRealm()),
84                     "");
85             }
86             
87             // Fallback to defaults
88
if (val == null) {
89                 val = def.getDefaultValue();
90             } else {
91                 if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
92                     try {
93                         val = ContextHolder.getContext().deobfuscatePassword(val);
94                     } catch (Throwable JavaDoc t) {
95                         log.warn("Password property " + def.getName() + " could not be decoded. It has been result to the default.",
96                             t);
97                     }
98                 }
99             }
100             return val;
101         } catch (Exception JavaDoc e) {
102             log.error("Failed to retrieve property.", e);
103         }
104         return null;
105     }
106
107     public String JavaDoc storePropertyImpl(AbstractPropertyKey key, String JavaDoc value) throws IllegalArgumentException JavaDoc {
108         if (!(key instanceof ProfilePropertyKey)) {
109             throw new IllegalArgumentException JavaDoc("Property key is not an instanceof ProfilePropertyKey");
110         }
111         PropertyDefinition def = getDefinition(key.getName());
112         ProfilePropertyKey profilesKey = (ProfilePropertyKey) key;
113
114         String JavaDoc oldValue = retrieveProperty(key);
115
116         if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
117             try {
118                 value = ContextHolder.getContext().obfuscatePassword(value);
119             } catch (Throwable JavaDoc t) {
120                 log.warn("Password property " + def.getName() + " could not be encoded.", t);
121             }
122         }
123
124         // If the definitions shows a visibilitiy of CONTEXT_CONFIGURATION then
125
// set the property value in the context
126
try {
127             ProfilesFactory.getInstance().storeGenericProperty(profilesKey.getName(),
128                 profilesKey.isUserSpecific() ? profilesKey.getUsername() : "",
129                 String.valueOf(profilesKey.getProfile()),
130                 String.valueOf(profilesKey.getRealm()),
131                 "",
132                 value);
133         } catch (Exception JavaDoc e) {
134             log.error("Could not store properties in database.", e);
135         }
136         return oldValue;
137     }
138 }
139
Popular Tags