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 * A descriptor for a configuration property. 029 * 030 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 031 * @version $Revision$ 032 */ 033 public abstract class PropertyDescriptor<T> { 034 035 public static PropertyDescriptor<String> create(String name, String defaultValue, String description) { 036 return new PropertyDescriptor<String>(String.class, name, defaultValue, description) { 037 @Override 038 protected String doParse(String s) throws Exception { 039 return s; 040 } 041 }; 042 } 043 044 public static PropertyDescriptor<Integer> create(String name, Integer defaultValue, String description) { 045 return new PropertyDescriptor<Integer>(Integer.class, name, defaultValue, description) { 046 @Override 047 protected Integer doParse(String s) throws Exception { 048 return Integer.parseInt(s); 049 } 050 }; 051 } 052 053 /** . */ 054 private static final Map<String, PropertyDescriptor<?>> INTERNAL_ALL = new HashMap<String, PropertyDescriptor<?>>(); 055 056 /** . */ 057 public static final Map<String, PropertyDescriptor<?>> ALL = Collections.unmodifiableMap(INTERNAL_ALL); 058 059 /** . */ 060 public static final PropertyDescriptor<TimeUnit> VFS_REFRESH_UNIT = new PropertyDescriptor<TimeUnit>(TimeUnit.class, "vfs.refresh_unit", TimeUnit.SECONDS, "The refresh time unit") { 061 @Override 062 public TimeUnit doParse(String s) { 063 return TimeUnit.valueOf(s); 064 } 065 }; 066 067 /** . */ 068 public static final PropertyDescriptor<Integer> VFS_REFRESH_PERIOD = PropertyDescriptor.create("vfs.refresh_period", (Integer)null, "The refresh rate period"); 069 070 /** . */ 071 public final Class<T> type; 072 073 /** . */ 074 public final String name; 075 076 /** . */ 077 public final T defaultValue; 078 079 /** . */ 080 public final String description; 081 082 /** 083 * Create a new property descriptor. 084 * 085 * @param type the property type 086 * @param name the property name 087 * @param defaultValue the default value 088 * @param description the description 089 * @throws NullPointerException if the type, name or description is null 090 */ 091 protected PropertyDescriptor(Class<T> type, String name, T defaultValue, String description) throws NullPointerException { 092 if (type == null) { 093 throw new NullPointerException("No null type accepted"); 094 } 095 if (name == null) { 096 throw new NullPointerException("No null name accepted"); 097 } 098 if (description == null) { 099 throw new NullPointerException("No null description accepted"); 100 } 101 102 this.type = type; 103 this.name = name; 104 this.defaultValue = defaultValue; 105 this.description = description; 106 107 // 108 INTERNAL_ALL.put(name, this); 109 } 110 111 public final String getName() { 112 return name; 113 } 114 115 public final String getDescription() { 116 return description; 117 } 118 119 public final Class<T> getType() { 120 return type; 121 } 122 123 public final T getDefaultValue() { 124 return defaultValue; 125 } 126 127 /** 128 * Parse a string representation of a value and returns the corresponding typed value. 129 * 130 * @param s the string to parse 131 * @return the corresponding value 132 * @throws NullPointerException if the argument is null 133 * @throws IllegalArgumentException if the string value cannot be parsed for some reason 134 */ 135 public final T parse(String s) throws NullPointerException, IllegalArgumentException { 136 if (s == null) { 137 throw new NullPointerException("Cannot parse null property values"); 138 } 139 try { 140 return doParse(s); 141 } 142 catch (Exception e) { 143 throw new IllegalArgumentException("Illegal property value " + s, e); 144 } 145 } 146 147 /** 148 * Parse a string representation of a value and returns the correspondig property value. 149 * 150 * @param s the string to parse 151 * @return the corresponding property 152 * @throws NullPointerException if the argument is null 153 * @throws IllegalArgumentException if the string value cannot be parsed for some reason 154 */ 155 public final Property<T> toProperty(String s) throws NullPointerException, IllegalArgumentException { 156 T value = parse(s); 157 return new Property<T>(this, value); 158 } 159 160 /** 161 * Implements the real parsing, the string argument must nto be null. The returned value must not be null 162 * instead an exception must be thrown. 163 * 164 * @param s the string to parse 165 * @return the related value 166 * @throws Exception any exception that would prevent parsing to hapen 167 */ 168 protected abstract T doParse(String s) throws Exception; 169 }