1 18 package org.apache.geronimo.interop.properties; 19 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.geronimo.interop.util.FileUtil; 28 import org.apache.geronimo.interop.util.ListUtil; 29 import org.apache.geronimo.interop.util.StringUtil; 30 31 public class StringProperty extends PropertyType { 32 private String defaultValue = ""; 33 private List valueIsInList = null; 34 private Map displayValues = null; 35 private Class valueIsNameOf = null; 36 private boolean isDirName = false; 37 private boolean isFileName = false; 38 private boolean isList = false; 39 private boolean isReadOnly = false; 40 private boolean isRequired = false; 41 42 public StringProperty(Class componentClass, String propertyName) { 43 super(componentClass, propertyName); 44 } 45 46 public StringProperty displayName(String displayName) { 47 setDisplayName(displayName); 48 return this; 49 } 50 51 public StringProperty displayOnlyIf(PropertyType other, String value) { 52 setDisplayOnlyIf(other, value); 53 return this; 54 } 55 56 public StringProperty description(String description) { 57 setDescription(description); 58 return this; 59 } 60 61 public StringProperty consoleHelp(String consoleHelp) { 62 setConsoleHelp(consoleHelp); 63 return this; 64 } 65 66 public StringProperty sortOrder(int sortOrder) { 67 setSortOrder(sortOrder); 68 return this; 69 } 70 71 public StringProperty defaultValue(String defaultValue) { 72 this.defaultValue = defaultValue; 73 return this; 74 } 75 76 public StringProperty legalValues(Class valueIsNameOf) { 77 this.valueIsNameOf = valueIsNameOf; 78 return this; 79 } 80 81 public StringProperty legalValues(List valueIsInList) { 82 valueIsInList = Collections.unmodifiableList(valueIsInList); 83 return this; 84 } 85 86 public StringProperty legalValues(String valueIsInList) { 87 List list = ListUtil.getCommaSeparatedList(valueIsInList); 88 this.valueIsInList = new ArrayList (list.size()); 89 for (Iterator i = list.iterator(); i.hasNext();) { 90 String value = (String ) i.next(); 91 if (value.indexOf('=') != -1) { 92 String displayValue = StringUtil.afterFirst("=", value).trim(); 93 value = StringUtil.beforeFirst("=", value).trim(); 94 if (displayValues == null) { 95 displayValues = new HashMap (); 96 } 97 displayValues.put(value, displayValue); 98 } 99 this.valueIsInList.add(value); 100 } 101 return this; 102 } 103 104 public String getDisplayValue(String value) { 105 if (displayValues != null) { 106 String displayValue = (String ) displayValues.get(value); 107 if (displayValue != null) { 108 return displayValue; 109 } 110 } 111 return value; 112 } 113 114 public StringProperty isDirName() { 115 isDirName = true; 116 return this; 117 } 118 119 public StringProperty isFileName() { 120 isFileName = true; 121 return this; 122 } 123 124 public StringProperty list() { 125 isList = true; 126 return this; 127 } 128 129 public StringProperty readOnly() { 130 isReadOnly = true; 131 return this; 132 } 133 134 public StringProperty required() { 135 isRequired = true; 136 return this; 137 } 138 139 public boolean isList() { 140 return isList; 141 } 142 143 public boolean isReadOnly() { 144 return isReadOnly; 145 } 146 147 public boolean isRequired() { 148 return isRequired; 149 } 150 151 public String getDefaultValue() { 152 return defaultValue; 153 } 154 155 public String getDefaultValueAsString() { 156 return defaultValue; 157 } 158 159 public List getLegalValues() { 160 if (valueIsInList != null) { 161 return valueIsInList; 162 } else if (valueIsNameOf != null) { 163 return null; 165 } else { 166 return null; 167 } 168 } 169 170 public String getString() { 171 return getString(null, getComponentProperties()); 172 } 173 174 public String getString(String instanceName, PropertyMap props) { 175 boolean ok = true, usingDefaultValue = false; 176 String s = props.getProperty(getPropertyName(), defaultValue); 177 if (s != null && s.startsWith("${")) { 178 s = StringUtil.removePrefix(s, "${"); 180 s = StringUtil.removeSuffix(s, "}"); 181 StringProperty sp = new StringProperty(SystemProperties.class, s); 182 s = sp.getString(); 183 if (s == null || s.length() == 0) { 184 if (isRequired()) { 185 String message = getLog(instanceName).errorMissingValueForRequiredSystemProperty(sp.getPropertyName(), getPropertyName(), getContext(instanceName)); 186 throw new MissingRequiredPropertyException(message); 187 } 188 } 189 } 190 if (s == null && !isRequired()) { 191 s = ""; 192 } 193 List legalValues = getLegalValues(); 194 if (legalValues != null) { 195 ok = false; 196 for (Iterator i = legalValues.iterator(); i.hasNext();) { 197 String legalValue = (String ) i.next(); 198 if (s != null && s.equals(legalValue)) { 199 ok = true; 200 break; 201 } 202 } 203 if (!isRequired() && s.equals("")) { 204 ok = true; 205 } 206 } 207 if (!ok) { 208 badPropertyValue(instanceName, s, expectedValueInList(legalValues)); 209 } 210 if (isDirName || isFileName) { 211 s = FileUtil.expandHomeRelativePath(s); 212 s = FileUtil.pretty(s); 213 } 214 if (s == null || s.length() == 0) { 215 if (isRequired()) { 216 String message = getLog(instanceName).errorMissingValueForRequiredProperty(getPropertyName(), getContext(instanceName)); 217 throw new MissingRequiredPropertyException(message); 218 } 219 } 220 logPropertyValue(instanceName, s, s != null && s.equals(defaultValue)); 221 return s; 222 } 223 } 224 | Popular Tags |