1 19 20 package com.sslexplorer.properties; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import com.sslexplorer.boot.AbstractPropertyKey; 26 import com.sslexplorer.boot.PropertyClass; 27 import com.sslexplorer.boot.PropertyClassManager; 28 import com.sslexplorer.boot.PropertyDefinition; 29 import com.sslexplorer.boot.PropertyList; 30 import com.sslexplorer.core.CoreEventConstants; 31 import com.sslexplorer.core.CoreServlet; 32 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey; 33 import com.sslexplorer.security.SessionInfo; 34 35 41 public class Property { 42 43 final static Log log = LogFactory.getLog(Property.class); 44 45 48 private Property() { 49 } 50 51 59 public static PropertyDefinition getDefinition(AbstractPropertyKey key) { 60 PropertyClassManager mgr = PropertyClassManager.getInstance(); 61 PropertyClass propertyClass = mgr.getPropertyClass(key.getPropertyClassName()); 62 if (propertyClass == null) { 63 throw new IllegalArgumentException ("Invalid property class " + key.getPropertyClassName() + "."); 64 } 65 return propertyClass.getDefinition(key.getName()); 66 } 67 68 78 public static String setProperty(AbstractPropertyKey key, String newValue, SessionInfo sessionInfo) { 79 80 PropertyDefinition def = getDefinition(key); 81 PropertyProfile p = null; 82 try { 83 PropertyClass t = def.getPropertyClass(); 84 String oldVal = t.storeProperty(key, newValue); 85 if ( ( oldVal == null && newValue != null ) || !oldVal.equals(newValue)) { 86 if (key instanceof ProfilePropertyKey) { 87 p = ProfilesFactory.getInstance().getPropertyProfile(((ProfilePropertyKey) key).getProfile()); 88 } 89 CoreServlet.getServlet().fireCoreEvent(new PropertyChangeEvent(def, 90 CoreEventConstants.PROPERTY_CHANGED, 91 def, 92 sessionInfo, 93 p, 94 oldVal, 95 newValue, 96 PropertyChangeEvent.STATE_SUCCESSFUL)); 97 } 98 return oldVal; 99 } catch (Exception e) { 100 log.error("Failed to set property.", e); 101 CoreServlet.getServlet().fireCoreEvent(new PropertyChangeEvent(def, 102 CoreEventConstants.PROPERTY_CHANGED, 103 def, 104 sessionInfo, 105 p, 106 null, 107 newValue, 108 PropertyChangeEvent.STATE_UNSUCCESSFUL)); 109 } 110 return null; 111 } 112 113 123 public static boolean setProperty(AbstractPropertyKey key, boolean newValue, SessionInfo sessionInfo) { 124 String oldVal = setProperty(key, String.valueOf(newValue), sessionInfo); 125 assert oldVal != null; return Boolean.parseBoolean(oldVal); 127 } 128 129 140 public static PropertyList setProperty(AbstractPropertyKey key, PropertyList newValue, SessionInfo sessionInfo) { 141 String oldVal = setProperty(key, newValue.getAsPropertyText(), sessionInfo); 142 return oldVal == null ? null : new PropertyList(oldVal); 143 144 } 145 146 157 public static int setProperty(AbstractPropertyKey key, int newValue, SessionInfo sessionInfo) { 158 String oldVal = setProperty(key, String.valueOf(newValue), sessionInfo); 159 return oldVal == null ? -1 : Integer.parseInt(oldVal); 160 } 161 162 173 public static long setProperty(AbstractPropertyKey key, long newValue, SessionInfo sessionInfo) { 174 String oldVal = setProperty(key, String.valueOf(newValue), sessionInfo); 175 return oldVal == null ? -1 : Long.parseLong(oldVal); 176 } 177 178 188 public static String getProperty(AbstractPropertyKey key) throws IllegalArgumentException { 189 PropertyDefinition def = getDefinition(key); 190 if (def == null) { 191 throw new IllegalArgumentException ("Invalid key. " + key); 192 } 193 PropertyClass t = def.getPropertyClass(); 194 return t.retrieveProperty(key); 195 } 196 197 208 public static int getPropertyInt(AbstractPropertyKey key) throws IllegalArgumentException { 209 return Integer.parseInt(getProperty(key)); 210 } 211 212 223 public static long getPropertyLong(AbstractPropertyKey key) throws IllegalArgumentException { 224 return Long.parseLong(getProperty(key)); 225 } 226 227 240 public static boolean getPropertyBoolean(AbstractPropertyKey key) throws IllegalArgumentException { 241 return Boolean.parseBoolean(getProperty(key)); 242 } 243 244 255 public static PropertyList getPropertyList(AbstractPropertyKey key) throws IllegalArgumentException { 256 return new PropertyList(getProperty(key)); 257 } 258 259 } 260 | Popular Tags |