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    
020    package org.crsh.plugin;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    import java.util.logging.Level;
025    import java.util.logging.Logger;
026    
027    /** @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> */
028    class 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> 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> 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 (descriptor.getType().isAssignableFrom(type)) {
075            return type.cast(property.getValue());
076          }
077        }
078        return null;
079      }
080    
081      /**
082       * Set a context property to a new value. If the provided value is null, then the property is removed.
083       *
084       * @param desc the property descriptor
085       * @param value the property value
086       * @param <T> the property parameter type
087       * @throws NullPointerException if the descriptor argument is null
088       */
089      <T> void setProperty(PropertyDescriptor<T> desc, T value) throws NullPointerException {
090        if (desc == null) {
091          throw new NullPointerException();
092        }
093        if (value == null) {
094          log.log(Level.FINE, "Removing property " + desc.name);
095          properties.remove(desc.getName());
096        } else {
097          Property<T> property = new Property<T>(desc, value);
098          log.log(Level.FINE, "Setting property " + desc.name + " to value " + property.getValue());
099          properties.put(desc.getName(), property);
100        }
101      }
102    
103      /**
104       * Set a context property to a new value. If the provided value is null, then the property is removed.
105       *
106       * @param desc the property descriptor
107       * @param value the property value
108       * @param <T> the property parameter type
109       * @throws NullPointerException if the descriptor argument is null
110       * @throws IllegalArgumentException if the string value cannot be converted to the property type
111       */
112      <T> void setProperty(PropertyDescriptor<T> desc, String value) throws NullPointerException, IllegalArgumentException {
113        if (desc == null) {
114          throw new NullPointerException();
115        }
116        if (value == null) {
117          log.log(Level.FINE, "Removing property " + desc.name);
118          properties.remove(desc.getName());
119        } else {
120          Property<T> property = desc.toProperty(value);
121          log.log(Level.FINE, "Setting property " + desc.name + " to value " + property.getValue());
122          properties.put(desc.getName(), property);
123        }
124      }
125    }