001/* 002 * Copyright (C) 2012 eXo Platform SAS. 003 * 004 * This is free software; you can redistribute it and/or modify it 005 * under the terms of the GNU Lesser General Public License as 006 * published by the Free Software Foundation; either version 2.1 of 007 * the License, or (at your option) any later version. 008 * 009 * This software is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public 015 * License along with this software; if not, write to the Free 016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 018 */ 019 020package org.crsh.plugin; 021 022import java.util.HashMap; 023import java.util.Map; 024import java.util.logging.Level; 025import java.util.logging.Logger; 026 027/** @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> */ 028class PropertyManager { 029 030 /** . */ 031 private static final Logger log = Logger.getLogger(PropertyManager.class.getName()); 032 033 /** . */ 034 private final Map<String, Property<?>> properties; 035 036 PropertyManager() { 037 this.properties = new HashMap<String, Property<?>>(); 038 } 039 040 /** 041 * Returns a context property or null if it cannot be found. 042 * 043 * @param desc the property descriptor 044 * @param <T> the property parameter type 045 * @return the property value 046 * @throws NullPointerException if the descriptor argument is null 047 */ 048 public <T> Property<T> getProperty(PropertyDescriptor<T> desc) throws NullPointerException { 049 if (desc == null) { 050 throw new NullPointerException(); 051 } 052 return getProperty(desc.getName(), desc.getType()); 053 } 054 055 /** 056 * Returns a context property or null if it cannot be found. 057 * 058 * @param propertyName the name of the property 059 * @param type the property type 060 * @param <T> the property parameter type 061 * @return the property value 062 * @throws NullPointerException if the descriptor argument is null 063 */ 064 <T> Property<T> getProperty(String propertyName, Class<T> type) throws NullPointerException { 065 if (propertyName == null) { 066 throw new NullPointerException("No null property name accepted"); 067 } 068 if (type == null) { 069 throw new NullPointerException("No null property type accepted"); 070 } 071 Property<?> property = properties.get(propertyName); 072 if (property != null) { 073 PropertyDescriptor<?> descriptor = property.getDescriptor(); 074 if (type.equals(descriptor.getType())) { 075 return (Property<T>)property; 076 } 077 } 078 return null; 079 } 080 081 /** 082 * Returns a context property value or null if it cannot be found. 083 * 084 * @param desc the property descriptor 085 * @param <T> the property parameter type 086 * @return the property value 087 * @throws NullPointerException if the descriptor argument is null 088 */ 089 public <T> T resolvePropertyValue(PropertyDescriptor<T> desc) throws NullPointerException { 090 if (desc == null) { 091 throw new NullPointerException(); 092 } 093 return resolvePropertyValue(desc.getName(), desc.getType()); 094 } 095 096 /** 097 * Returns a context property value or null if it cannot be found. 098 * 099 * @param propertyName the name of the property 100 * @param type the property type 101 * @param <T> the property parameter type 102 * @return the property value 103 * @throws NullPointerException if the descriptor argument is null 104 */ 105 <T> T resolvePropertyValue(String propertyName, Class<T> type) throws NullPointerException { 106 if (propertyName == null) { 107 throw new NullPointerException("No null property name accepted"); 108 } 109 if (type == null) { 110 throw new NullPointerException("No null property type accepted"); 111 } 112 Property<? extends T> property = resolveProperty(propertyName, type); 113 if (property != null) { 114 return property.getValue(); 115 } 116 return null; 117 } 118 119 /** 120 * Resolve a context property or null if it cannot be resolved. 121 * 122 * @param desc the property descriptor 123 * @param <T> the property parameter type 124 * @return the property value 125 * @throws NullPointerException if the descriptor argument is null 126 */ 127 public <T> Property<? extends T> resolveProperty(PropertyDescriptor<T> desc) throws NullPointerException { 128 if (desc == null) { 129 throw new NullPointerException(); 130 } 131 return getProperty(desc.getName(), desc.getType()); 132 } 133 134 /** 135 * Resolve a context property or null if it cannot be resolved. 136 * 137 * @param propertyName the name of the property 138 * @param type the property type 139 * @param <T> the property parameter type 140 * @return the property value 141 * @throws NullPointerException if the descriptor argument is null 142 */ 143 <T> Property<? extends T> resolveProperty(String propertyName, Class<T> type) throws NullPointerException { 144 if (propertyName == null) { 145 throw new NullPointerException("No null property name accepted"); 146 } 147 if (type == null) { 148 throw new NullPointerException("No null property type accepted"); 149 } 150 Property<?> property = properties.get(propertyName); 151 if (property != null) { 152 PropertyDescriptor<?> descriptor = property.getDescriptor(); 153 if (type.isAssignableFrom(descriptor.getType())) { 154 return (Property<? extends T>)property; 155 } 156 } 157 return null; 158 } 159 160 /** 161 * Set a context property to a new value. If the provided value is null, then the property is removed. 162 * 163 * @param desc the property descriptor 164 * @param value the property value 165 * @param <T> the property parameter type 166 * @throws NullPointerException if the descriptor argument is null 167 */ 168 <T> void setProperty(PropertyDescriptor<T> desc, T value) throws NullPointerException { 169 if (desc == null) { 170 throw new NullPointerException("No null descriptor allowed"); 171 } 172 if (value == null) { 173 log.log(Level.FINE, "Removing property " + desc.name); 174 properties.remove(desc.getName()); 175 } else { 176 Property<T> property = new Property<T>(desc, value); 177 log.log(Level.FINE, "Setting property " + desc.name + " to value " + property.getValue()); 178 properties.put(desc.getName(), property); 179 } 180 } 181 182 /** 183 * Set a context property to a new value. 184 * 185 * @param desc the property descriptor 186 * @param value the property value 187 * @param <T> the property parameter type 188 * @throws NullPointerException if the descriptor argument or the value is null 189 * @throws IllegalArgumentException if the string value cannot be converted to the property type 190 */ 191 <T> void parseProperty(PropertyDescriptor<T> desc, String value) throws NullPointerException, IllegalArgumentException { 192 if (desc == null) { 193 throw new NullPointerException("No null descriptor allowed"); 194 } 195 if (value == null) { 196 throw new NullPointerException("No null value accepted"); 197 } else { 198 Property<T> property = desc.toProperty(value); 199 log.log(Level.FINE, "Setting property " + desc.name + " to value " + property.getValue()); 200 properties.put(desc.getName(), property); 201 } 202 } 203}