KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > impl > policyattributes > PolicyAttributes


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.policyattributes;
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.ContextHolder;
30 import com.sslexplorer.boot.PropertyClass;
31 import com.sslexplorer.boot.PropertyDefinition;
32 import com.sslexplorer.core.CoreServlet;
33 import com.sslexplorer.properties.ProfilesFactory;
34 import com.sslexplorer.properties.attributes.AbstractXMLDefinedAttributesPropertyClass;
35 import com.sslexplorer.properties.attributes.AttributeDefinition;
36 import com.sslexplorer.properties.attributes.DefaultAttributeDefinition;
37
38 /**
39  * {@link PropertyClass} implementation for policy attributes.
40  *
41  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  */

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

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

58     public PolicyAttributes() throws IOException JavaDoc, JDOMException {
59         super(NAME, false, "properties", false);
60     }
61
62     public String JavaDoc retrievePropertyImpl(AbstractPropertyKey key)
63             throws IllegalArgumentException JavaDoc {
64         AttributeDefinition def = (AttributeDefinition) getDefinition(key
65                 .getName());
66         PolicyAttributeKey policyAttrKey = (PolicyAttributeKey) key;
67         try {
68             String JavaDoc val = ProfilesFactory.getInstance()
69                     .retrieveAttributeValue(policyAttrKey);
70
71             // Decrypt obfuscated password if of password type
72
if (def.getType() == PropertyDefinition.TYPE_PASSWORD
73                     && val != null) {
74                 try {
75                     val = ContextHolder.getContext().deobfuscatePassword(val);
76                 } catch (Throwable JavaDoc t) {
77                     log
78                             .warn(
79                                     "Password property "
80                                             + def.getName()
81                                             + " could not be decoded. It has been result to the default.",
82                                     t);
83                 }
84             }
85             //
86
return val == null ? def.getDefaultValue() : val;
87         } catch (Exception JavaDoc e) {
88             log.error("Failed to retrieve property.", e);
89         }
90         return null;
91     }
92
93     public String JavaDoc storePropertyImpl(AbstractPropertyKey key, String JavaDoc value)
94             throws IllegalArgumentException JavaDoc {
95         AttributeDefinition def = (AttributeDefinition) getDefinition(key
96                 .getName());
97         PolicyAttributeKey policyAttrKey = (PolicyAttributeKey) key;
98         String JavaDoc oldValue = retrieveProperty(key);
99         if (def.getDefaultValue().equals(value)) {
100             value = null;
101         }
102
103         if ((oldValue == null && value != null)
104                 || (oldValue != null && value == null)
105                 || !oldValue.equals(value)) {
106
107             // Obfuscate the password for storing in the database
108

109             if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
110                 try {
111                     value = ContextHolder.getContext().obfuscatePassword(value);
112                 } catch (Throwable JavaDoc t) {
113                     log.warn("Password property " + def.getName()
114                             + " could not be encoded.", t);
115                 }
116             }
117
118             // Store to the database
119
try {
120                 ProfilesFactory.getInstance()
121                         .storeAttributeValue(policyAttrKey, value);
122             } catch (Exception JavaDoc e) {
123                 log.error("Failed to update user attributes.", e);
124             }
125         }
126         return oldValue;
127     }
128
129     public AttributeDefinition createAttributeDefinition(int type, String JavaDoc name,
130             String JavaDoc typeMeta, int category, String JavaDoc categoryLabel,
131             String JavaDoc defaultValue, int visibility, int sortOrder,
132             String JavaDoc messageResourcesKey, boolean hidden, String JavaDoc label,
133             String JavaDoc description, boolean system, boolean replaceable,
134             String JavaDoc validationString) {
135         AttributeDefinition def = new DefaultAttributeDefinition(type, name, typeMeta, category,
136                 categoryLabel, defaultValue, visibility, sortOrder,
137                 messageResourcesKey, hidden, label, description, system,
138                 replaceable, validationString);
139         def.init(this);
140         return def;
141     }
142 }
143
Popular Tags