001    /*
002     * Copyright (C) 2010 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.Collections;
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.concurrent.TimeUnit;
026    
027    /**
028     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
029     * @version $Revision$
030     */
031    public abstract class PropertyDescriptor<T> {
032    
033      /** . */
034      private static final Map<String, PropertyDescriptor<?>> INTERNAL_ALL = new HashMap<String, PropertyDescriptor<?>>();
035    
036      /** . */
037      public static final Map<String, PropertyDescriptor<?>> ALL = Collections.unmodifiableMap(INTERNAL_ALL);
038    
039      /** . */
040      public static final PropertyDescriptor<Integer> SSH_PORT = new PropertyDescriptor<Integer>(Integer.class, "ssh.port", 2000, "The SSH port") {
041        @Override
042        public Integer doParse(String s) {
043          return Integer.parseInt(s);
044        }
045      };
046    
047      /** . */
048      public static final PropertyDescriptor<String> SSH_KEYPATH = new PropertyDescriptor<String>(String.class, "ssh.keypath", null, "The path to the key file") {
049        @Override
050        public String doParse(String s) {
051          return s;
052        }
053      };
054    
055      /** . */
056      public static final PropertyDescriptor<Integer> TELNET_PORT = new PropertyDescriptor<Integer>(Integer.class, "telnet.port", 5000, "The telnet port") {
057        @Override
058        public Integer doParse(String s) {
059          return Integer.parseInt(s);
060        }
061      };
062    
063      /** . */
064      public static final PropertyDescriptor<TimeUnit> VFS_REFRESH_UNIT = new PropertyDescriptor<TimeUnit>(TimeUnit.class, "vfs.refresh_unit", TimeUnit.SECONDS, "The refresh time unit") {
065        @Override
066        public TimeUnit doParse(String s) {
067          return TimeUnit.valueOf(s);
068        }
069      };
070    
071      /** . */
072      public static final PropertyDescriptor<Integer> VFS_REFRESH_PERIOD = new PropertyDescriptor<Integer>(Integer.class, "vfs.refresh_period", null, "The refresh rate period") {
073        @Override
074        public Integer doParse(String s) {
075          return Integer.parseInt(s);
076        }
077      };
078    
079      /** . */
080      public final Class<T> type;
081    
082      /** . */
083      public final String name;
084    
085      /** . */
086      public final T defaultValue;
087    
088      /** . */
089      public final String description;
090    
091      private PropertyDescriptor(Class<T> type, String name, T defaultValue) {
092        this(type, name, defaultValue, null);
093      }
094    
095      private PropertyDescriptor(Class<T> type, String name, T defaultValue, String description) {
096        if (name == null) {
097          throw new AssertionError();
098        }
099        this.type = type;
100        this.name = name;
101        this.defaultValue = defaultValue;
102        this.description = description;
103    
104        //
105        INTERNAL_ALL.put(name, this);
106      }
107    
108      public final T parse(String s) throws NullPointerException, IllegalArgumentException {
109        if (s == null) {
110          throw new NullPointerException("Cannot parse null property values");
111        }
112        try {
113          return doParse(s);
114        }
115        catch (Exception e) {
116          throw new IllegalArgumentException("Illegal property value " + s, e);
117        }
118      }
119    
120      public final Property<T> toProperty(String s) throws NullPointerException, IllegalArgumentException {
121        T value = parse(s);
122        return new Property<T>(this, value);
123      }
124    
125      protected abstract T doParse(String s);
126    }