1 16 package org.apache.commons.chain.generic; 17 18 19 import org.apache.commons.chain.Catalog; 20 import org.apache.commons.chain.CatalogFactory; 21 import org.apache.commons.chain.Command; 22 import org.apache.commons.chain.Context; 23 import org.apache.commons.chain.Filter; 24 25 26 44 45 public class LookupCommand implements Filter { 46 47 48 50 51 private String catalogName = null; 52 53 54 58 public String getCatalogName() { 59 60 return (this.catalogName); 61 62 } 63 64 65 71 public void setCatalogName(String catalogName) { 72 73 this.catalogName = catalogName; 74 75 } 76 77 78 private String name = null; 79 80 81 85 public String getName() { 86 87 return (this.name); 88 89 } 90 91 92 98 public void setName(String name) { 99 100 this.name = name; 101 102 } 103 104 105 private String nameKey = null; 106 107 108 112 public String getNameKey() { 113 114 return (this.nameKey); 115 116 } 117 118 119 125 public void setNameKey(String nameKey) { 126 127 this.nameKey = nameKey; 128 129 } 130 131 132 private boolean optional = false; 133 134 135 139 public boolean isOptional() { 140 141 return (this.optional); 142 143 } 144 145 146 151 public void setOptional(boolean optional) { 152 153 this.optional = optional; 154 155 } 156 157 158 159 161 162 171 public boolean execute(Context context) throws Exception { 172 173 Command command = getCommand(context); 174 if (command != null) { 175 return (command.execute(context)); 176 } else { 177 return (false); 178 } 179 180 } 181 182 183 192 public boolean postprocess(Context context, Exception exception) { 193 194 Command command = getCommand(context); 195 if (command != null) { 196 if (command instanceof Filter) { 197 return (((Filter) command).postprocess(context, exception)); 198 } 199 } 200 return (false); 201 202 } 203 204 205 207 208 217 private Command getCommand(Context context) { 218 219 CatalogFactory catalogFactory = CatalogFactory.getInstance(); 220 String catalogName = getCatalogName(); 221 Catalog catalog = null; 222 if (catalogName == null) { 223 catalog = catalogFactory.getCatalog(); 225 } else { 226 catalog = catalogFactory.getCatalog(catalogName); 227 } 228 if (catalog == null) { 229 if (catalogName == null) { 230 throw new IllegalArgumentException 231 ("Cannot find default catalog"); 232 } else { 233 throw new IllegalArgumentException 234 ("Cannot find catalog '" + catalogName + "'"); 235 } 236 } 237 238 Command command = null; 239 String name = getName(); 240 if (name == null) { 241 name = (String ) context.get(getNameKey()); 242 } 243 if (name != null) { 244 command = catalog.getCommand(name); 245 if ((command == null) && !isOptional()) { 246 if (catalogName == null) { 247 throw new IllegalArgumentException 248 ("Cannot find command '" + name 249 + "' in default catalog"); 250 } else { 251 throw new IllegalArgumentException 252 ("Cannot find command '" + name 253 + "' in catalog '" + catalogName + "'"); 254 } 255 } 256 return (command); 257 } else { 258 throw new IllegalArgumentException ("No command name"); 259 } 260 261 } 262 263 264 } 265 | Popular Tags |