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 property value 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 getPropertyValue(PropertyDescriptor<T> desc) throws NullPointerException {
049        if (desc == null) {
050          throw new NullPointerException();
051        }
052        Property<T> property = getProperty(desc.getName(), desc.getType());
053        return property != null ? property.getValue() : null;
054      }
055    
056      /**
057       * Returns a property or null if it cannot be found.
058       *
059       * @param desc the property descriptor
060       * @param <T> the property parameter type
061       * @return the property object
062       * @throws NullPointerException if the descriptor argument is null
063       */
064      public <T> Property<T> getProperty(PropertyDescriptor<T> desc) throws NullPointerException {
065        if (desc == null) {
066          throw new NullPointerException();
067        }
068        return getProperty(desc.getName(), desc.getType());
069      }
070    
071      /**
072       * Returns a property or null if it cannot be found.
073       *
074       * @param propertyName the name of the property
075       * @param type the property type
076       * @param <T> the property parameter type
077       * @return the property object
078       * @throws NullPointerException if any argument is null
079       */
080      private <T> Property<T> getProperty(String propertyName, Class<T> type) throws NullPointerException {
081        if (propertyName == null) {
082          throw new NullPointerException("No null property name accepted");
083        }
084        if (type == null) {
085          throw new NullPointerException("No null property type accepted");
086        }
087        Property<?> property = properties.get(propertyName);
088        if (property != null) {
089          PropertyDescriptor<?> descriptor = property.getDescriptor();
090          if (type.equals(descriptor.getType())) {
091            return (Property<T>)property;
092          }
093        }
094        return null;
095      }
096    
097      /**
098       * Set a context property to a new value. If the provided value is null, then the property is removed.
099       *
100       * @param desc the property descriptor
101       * @param value the property value
102       * @param <T> the property parameter type
103       * @throws NullPointerException if the descriptor argument is null
104       */
105      <T> void setProperty(PropertyDescriptor<T> desc, T value) throws NullPointerException {
106        if (desc == null) {
107          throw new NullPointerException("No null descriptor allowed");
108        }
109        if (value == null) {
110          log.log(Level.FINE, "Removing property " + desc.name);
111          properties.remove(desc.getName());
112        } else {
113          Property<T> property = new Property<T>(desc, value);
114          log.log(Level.FINE, "Setting property " + desc.name + " to value " + property.getValue());
115          properties.put(desc.getName(), property);
116        }
117      }
118    
119      /**
120       * Set a context property to a new value.
121       *
122       * @param desc the property descriptor
123       * @param value the property value
124       * @param <T> the property parameter type
125       * @throws NullPointerException if the descriptor argument or the value is null
126       * @throws IllegalArgumentException if the string value cannot be converted to the property type
127       */
128      <T> void parseProperty(PropertyDescriptor<T> desc, String value) throws NullPointerException, IllegalArgumentException {
129        if (desc == null) {
130          throw new NullPointerException("No null descriptor allowed");
131        }
132        if (value == null) {
133          throw new NullPointerException("No null value accepted");
134        } else {
135          Property<T> property = desc.toProperty(value);
136          log.log(Level.FINE, "Setting property " + desc.name + " to value " + property.getValue());
137          properties.put(desc.getName(), property);
138        }
139      }
140    }