| Revision | 273 (tree) |
|---|---|
| Time | 2016-05-03 22:33:58 |
| Author | t_nakayama1971 |
(empty log message)
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2.base; |
| 18 | 19 | |
| 19 | 20 | import org.apache.commons.chain2.CatalogFactory; |
| @@ -49,34 +50,16 @@ | ||
| 49 | 50 | * silently ignored. Otherwise, a lookup failure will trigger an |
| 50 | 51 | * <code>IllegalArgumentException</code>.</p> |
| 51 | 52 | * |
| 53 | + * @version $Id$ | |
| 52 | 54 | * @param <K> the type of keys maintained by the context associated with this catalog |
| 53 | 55 | * @param <V> the type of mapped values |
| 54 | 56 | * @param <C> Type of the context associated with this command |
| 55 | 57 | * |
| 56 | - * @version $Id$ | |
| 57 | 58 | * @since Chain 1.1 |
| 58 | 59 | */ |
| 59 | 60 | public class DispatchLookupCommand<K, V, C extends Context<K, V>> |
| 60 | 61 | extends LookupCommand<K, V, C> { |
| 61 | 62 | |
| 62 | - // -------------------------------------------------------------- Constructors | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * Create an instance with an unspecified <code>catalogFactory</code> property. | |
| 66 | - * This property can be set later using <code>setProperty</code>, or if it is not set, | |
| 67 | - * the static singleton instance from <code>CatalogFactory.getInstance()</code> will be used. | |
| 68 | - */ | |
| 69 | - public DispatchLookupCommand() { super(); } | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * Create an instance and initialize the <code>catalogFactory</code> property | |
| 73 | - * to given <code>factory</code>. | |
| 74 | - * @param factory The Catalog Factory. | |
| 75 | - */ | |
| 76 | - public DispatchLookupCommand(CatalogFactory<K, V, C> factory) { | |
| 77 | - super(factory); | |
| 78 | - } | |
| 79 | - | |
| 80 | 63 | // ------------------------------------------------------- Static Variables |
| 81 | 64 | |
| 82 | 65 | /** |
| @@ -95,12 +78,32 @@ | ||
| 95 | 78 | |
| 96 | 79 | private String methodKey = null; |
| 97 | 80 | |
| 81 | + // -------------------------------------------------------------- Constructors | |
| 82 | + | |
| 98 | 83 | /** |
| 84 | + * Create an instance with an unspecified <code>catalogFactory</code> property. | |
| 85 | + * This property can be set later using <code>setProperty</code>, or if it is not set, | |
| 86 | + * the static singleton instance from <code>CatalogFactory.getInstance()</code> will be used. | |
| 87 | + */ | |
| 88 | + public DispatchLookupCommand() { | |
| 89 | + super(); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Create an instance and initialize the <code>catalogFactory</code> property | |
| 94 | + * to given <code>factory</code>. | |
| 95 | + * @param factory The Catalog Factory. | |
| 96 | + */ | |
| 97 | + public DispatchLookupCommand(final CatalogFactory<K, V, C> factory) { | |
| 98 | + super(factory); | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 99 | 102 | * Return the method name. |
| 100 | 103 | * @return The method name. |
| 101 | 104 | */ |
| 102 | 105 | public String getMethod() { |
| 103 | - return method; | |
| 106 | + return this.method; | |
| 104 | 107 | } |
| 105 | 108 | |
| 106 | 109 | /** |
| @@ -108,23 +111,23 @@ | ||
| 108 | 111 | * @return The Context key for the method name. |
| 109 | 112 | */ |
| 110 | 113 | public String getMethodKey() { |
| 111 | - return methodKey; | |
| 114 | + return this.methodKey; | |
| 112 | 115 | } |
| 113 | 116 | |
| 114 | 117 | /** |
| 115 | 118 | * Set the method name. |
| 116 | - * @param method The method name. | |
| 119 | + * @param val The method name. | |
| 117 | 120 | */ |
| 118 | - public void setMethod(String method) { | |
| 119 | - this.method = method; | |
| 121 | + public void setMethod(final String val) { | |
| 122 | + this.method = val; | |
| 120 | 123 | } |
| 121 | 124 | |
| 122 | 125 | /** |
| 123 | 126 | * Set the Context key for the method name. |
| 124 | - * @param methodKey The Context key for the method name. | |
| 127 | + * @param val The Context key for the method name. | |
| 125 | 128 | */ |
| 126 | - public void setMethodKey(String methodKey) { | |
| 127 | - this.methodKey = methodKey; | |
| 129 | + public void setMethodKey(final String val) { | |
| 130 | + this.methodKey = val; | |
| 128 | 131 | } |
| 129 | 132 | |
| 130 | 133 | // --------------------------------------------------------- Public Methods |
| @@ -140,9 +143,10 @@ | ||
| 140 | 143 | * <code>optional</code> property is set to <code>false</code> |
| 141 | 144 | */ |
| 142 | 145 | @Override |
| 143 | - public Processing execute(C context) { | |
| 146 | + public Processing execute(final C context) { | |
| 144 | 147 | if (this.getMethod() == null && this.getMethodKey() == null) { |
| 145 | - throw new IllegalStateException("Neither 'method' nor 'methodKey' properties are defined"); | |
| 148 | + throw new IllegalStateException( | |
| 149 | + "Neither 'method' nor 'methodKey' properties are defined"); | |
| 146 | 150 | } |
| 147 | 151 | |
| 148 | 152 | Command<K, V, C> command = getCommand(context); |
| @@ -152,19 +156,21 @@ | ||
| 152 | 156 | Method methodObject = extractMethod(command, context); |
| 153 | 157 | Object obj = methodObject.invoke(command, getArguments(context)); |
| 154 | 158 | |
| 155 | - if(obj instanceof Processing) { | |
| 159 | + if (obj instanceof Processing) { | |
| 156 | 160 | Processing result = (Processing) obj; |
| 157 | 161 | return result; |
| 158 | 162 | } |
| 159 | 163 | return Processing.CONTINUE; |
| 160 | 164 | |
| 161 | - } catch (NoSuchMethodException e) { | |
| 162 | - throw new DispatchException("Error extracting method from context", e, context, this); | |
| 163 | - } catch (IllegalAccessException e) { | |
| 165 | + } catch (final NoSuchMethodException e) { | |
| 166 | + throw new DispatchException( | |
| 167 | + "Error extracting method from context", e, context, this); | |
| 168 | + } catch (final IllegalAccessException e) { | |
| 164 | 169 | throw new DispatchException("Error accessing method", e, context, this); |
| 165 | - } catch (InvocationTargetException e) { | |
| 170 | + } catch (final InvocationTargetException e) { | |
| 166 | 171 | Throwable cause = e.getTargetException(); |
| 167 | - throw new DispatchException("Error in reflected dispatched command", cause, context, this); | |
| 172 | + throw new DispatchException( | |
| 173 | + "Error in reflected dispatched command", cause, context, this); | |
| 168 | 174 | } |
| 169 | 175 | } |
| 170 | 176 | return Processing.CONTINUE; |
| @@ -195,7 +201,7 @@ | ||
| 195 | 201 | * @param context The context associated with the request |
| 196 | 202 | * @return the method arguments to be used |
| 197 | 203 | */ |
| 198 | - protected Object[] getArguments(C context) { | |
| 204 | + protected Object[] getArguments(final C context) { | |
| 199 | 205 | return new Object[] {context}; |
| 200 | 206 | } |
| 201 | 207 |
| @@ -216,7 +222,8 @@ | ||
| 216 | 222 | * specified name. |
| 217 | 223 | * @throws NullPointerException if no methodName can be determined |
| 218 | 224 | */ |
| 219 | - private Method extractMethod(Command<K, V, C> command, C context) throws NoSuchMethodException { | |
| 225 | + private Method extractMethod(final Command<K, V, C> command, final C context) | |
| 226 | + throws NoSuchMethodException { | |
| 220 | 227 | String methodName = this.getMethod(); |
| 221 | 228 | |
| 222 | 229 | if (methodName == null) { |
| @@ -229,13 +236,12 @@ | ||
| 229 | 236 | |
| 230 | 237 | Method theMethod = null; |
| 231 | 238 | |
| 232 | - synchronized (methods) { | |
| 233 | - theMethod = methods.get(methodName); | |
| 239 | + synchronized (this.methods) { | |
| 240 | + theMethod = this.methods.get(methodName); | |
| 234 | 241 | |
| 235 | 242 | if (theMethod == null) { |
| 236 | - theMethod = command.getClass().getMethod(methodName, | |
| 237 | - getSignature()); | |
| 238 | - methods.put(methodName, theMethod); | |
| 243 | + theMethod = command.getClass().getMethod(methodName, getSignature()); | |
| 244 | + this.methods.put(methodName, theMethod); | |
| 239 | 245 | } |
| 240 | 246 | } |
| 241 | 247 |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2.base; |
| 18 | 19 | |
| 19 | 20 | import org.apache.commons.chain2.Catalog; |
| @@ -42,14 +43,22 @@ | ||
| 42 | 43 | * silently ignored. Otherwise, a lookup failure will trigger an |
| 43 | 44 | * <code>IllegalArgumentException</code>.</p> |
| 44 | 45 | * |
| 46 | + * @version $Id$ | |
| 47 | + * | |
| 45 | 48 | * @param <K> Context key type |
| 46 | 49 | * @param <V> Context value type |
| 47 | 50 | * @param <C> Type of the context associated with this command |
| 48 | - * | |
| 49 | - * @version $Id$ | |
| 50 | 51 | */ |
| 51 | 52 | public class LookupCommand<K, V, C extends Map<K, V>> implements Filter<K, V, C> { |
| 52 | 53 | |
| 54 | + private CatalogFactory<K, V, C> catalogFactory = null; | |
| 55 | + private String catalogName = null; | |
| 56 | + private String name = null; | |
| 57 | + private String nameKey = null; | |
| 58 | + private boolean optional = false; | |
| 59 | + private boolean ignoreExecuteResult = false; | |
| 60 | + private boolean ignorePostprocessResult = false; | |
| 61 | + | |
| 53 | 62 | // -------------------------------------------------------------- Constructors |
| 54 | 63 | |
| 55 | 64 | /** |
| @@ -70,24 +79,22 @@ | ||
| 70 | 79 | * |
| 71 | 80 | * @since Chain 1.1 |
| 72 | 81 | */ |
| 73 | - public LookupCommand(CatalogFactory<K, V, C> factory) { | |
| 82 | + public LookupCommand(final CatalogFactory<K, V, C> factory) { | |
| 74 | 83 | this.catalogFactory = factory; |
| 75 | 84 | } |
| 76 | 85 | |
| 77 | 86 | // -------------------------------------------------------------- Properties |
| 78 | 87 | |
| 79 | - private CatalogFactory<K, V, C> catalogFactory = null; | |
| 80 | - | |
| 81 | 88 | /** |
| 82 | 89 | * <p>Set the {@link CatalogFactoryBase} from which lookups will be |
| 83 | 90 | * performed.</p> |
| 84 | 91 | * |
| 85 | - * @param catalogFactory The Catalog Factory. | |
| 92 | + * @param factory The Catalog Factory. | |
| 86 | 93 | * |
| 87 | 94 | * @since Chain 1.1 |
| 88 | 95 | */ |
| 89 | - public void setCatalogFactory(CatalogFactory<K, V, C> catalogFactory) { | |
| 90 | - this.catalogFactory = catalogFactory; | |
| 96 | + public void setCatalogFactory(final CatalogFactory<K, V, C> factory) { | |
| 97 | + this.catalogFactory = factory; | |
| 91 | 98 | } |
| 92 | 99 | |
| 93 | 100 | /** |
| @@ -100,8 +107,6 @@ | ||
| 100 | 107 | return this.catalogFactory; |
| 101 | 108 | } |
| 102 | 109 | |
| 103 | - private String catalogName = null; | |
| 104 | - | |
| 105 | 110 | /** |
| 106 | 111 | * <p>Return the name of the {@link Catalog} to be searched, or |
| 107 | 112 | * <code>null</code> to search the default {@link Catalog}.</p> |
| @@ -115,14 +120,12 @@ | ||
| 115 | 120 | * <p>Set the name of the {@link Catalog} to be searched, or |
| 116 | 121 | * <code>null</code> to search the default {@link Catalog}.</p> |
| 117 | 122 | * |
| 118 | - * @param catalogName The new {@link Catalog} name or <code>null</code> | |
| 123 | + * @param val The new {@link Catalog} name or <code>null</code> | |
| 119 | 124 | */ |
| 120 | - public void setCatalogName(String catalogName) { | |
| 121 | - this.catalogName = catalogName; | |
| 125 | + public void setCatalogName(final String val) { | |
| 126 | + this.catalogName = val; | |
| 122 | 127 | } |
| 123 | 128 | |
| 124 | - private String name = null; | |
| 125 | - | |
| 126 | 129 | /** |
| 127 | 130 | * <p>Return the name of the {@link Command} that we will look up and |
| 128 | 131 | * delegate execution to.</p> |
| @@ -136,14 +139,12 @@ | ||
| 136 | 139 | * <p>Set the name of the {@link Command} that we will look up and |
| 137 | 140 | * delegate execution to.</p> |
| 138 | 141 | * |
| 139 | - * @param name The new command name | |
| 142 | + * @param val The new command name | |
| 140 | 143 | */ |
| 141 | - public void setName(String name) { | |
| 142 | - this.name = name; | |
| 144 | + public void setName(final String val) { | |
| 145 | + this.name = val; | |
| 143 | 146 | } |
| 144 | 147 | |
| 145 | - private String nameKey = null; | |
| 146 | - | |
| 147 | 148 | /** |
| 148 | 149 | * <p>Return the context attribute key under which the {@link Command} |
| 149 | 150 | * name is stored.</p> |
| @@ -157,14 +158,12 @@ | ||
| 157 | 158 | * <p>Set the context attribute key under which the {@link Command} |
| 158 | 159 | * name is stored.</p> |
| 159 | 160 | * |
| 160 | - * @param nameKey The new context attribute key | |
| 161 | + * @param val The new context attribute key | |
| 161 | 162 | */ |
| 162 | - public void setNameKey(String nameKey) { | |
| 163 | - this.nameKey = nameKey; | |
| 163 | + public void setNameKey(final String val) { | |
| 164 | + this.nameKey = val; | |
| 164 | 165 | } |
| 165 | 166 | |
| 166 | - private boolean optional = false; | |
| 167 | - | |
| 168 | 167 | /** |
| 169 | 168 | * <p>Return <code>true</code> if locating the specified command |
| 170 | 169 | * is optional.</p> |
| @@ -177,14 +176,12 @@ | ||
| 177 | 176 | /** |
| 178 | 177 | * <p>Set the optional flag for finding the specified command.</p> |
| 179 | 178 | * |
| 180 | - * @param optional The new optional flag | |
| 179 | + * @param val The new optional flag | |
| 181 | 180 | */ |
| 182 | - public void setOptional(boolean optional) { | |
| 183 | - this.optional = optional; | |
| 181 | + public void setOptional(final boolean val) { | |
| 182 | + this.optional = val; | |
| 184 | 183 | } |
| 185 | 184 | |
| 186 | - private boolean ignoreExecuteResult = false; | |
| 187 | - | |
| 188 | 185 | /** |
| 189 | 186 | * <p>Return <code>true</code> if this command should ignore |
| 190 | 187 | * the return value from executing the looked-up command. |
| @@ -197,7 +194,7 @@ | ||
| 197 | 194 | * @since Chain 1.1 |
| 198 | 195 | */ |
| 199 | 196 | public boolean isIgnoreExecuteResult() { |
| 200 | - return ignoreExecuteResult; | |
| 197 | + return this.ignoreExecuteResult; | |
| 201 | 198 | } |
| 202 | 199 | |
| 203 | 200 | /** |
| @@ -207,17 +204,15 @@ | ||
| 207 | 204 | * <p>If you are looking up a chain which may be "aborted" and |
| 208 | 205 | * you do not want this class to stop chain processing, then this |
| 209 | 206 | * value should be set to <code>true</code></p> |
| 210 | - * @param ignoreReturn <code>true</code> if result of the | |
| 207 | + * @param val <code>true</code> if result of the | |
| 211 | 208 | * looked up Command should be ignored. |
| 212 | 209 | * |
| 213 | 210 | * @since Chain 1.1 |
| 214 | 211 | */ |
| 215 | - public void setIgnoreExecuteResult(boolean ignoreReturn) { | |
| 216 | - this.ignoreExecuteResult = ignoreReturn; | |
| 212 | + public void setIgnoreExecuteResult(final boolean val) { | |
| 213 | + this.ignoreExecuteResult = val; | |
| 217 | 214 | } |
| 218 | 215 | |
| 219 | - private boolean ignorePostprocessResult = false; | |
| 220 | - | |
| 221 | 216 | /** |
| 222 | 217 | * <p>Return <code>true</code> if this command is a Filter and |
| 223 | 218 | * should ignore the return value from executing the looked-up Filter's |
| @@ -231,7 +226,7 @@ | ||
| 231 | 226 | * @since Chain 1.1 |
| 232 | 227 | */ |
| 233 | 228 | public boolean isIgnorePostprocessResult() { |
| 234 | - return ignorePostprocessResult; | |
| 229 | + return this.ignorePostprocessResult; | |
| 235 | 230 | } |
| 236 | 231 | |
| 237 | 232 | /** |
| @@ -241,13 +236,13 @@ | ||
| 241 | 236 | * <p>If you are looking up a Filter which may be "aborted" and |
| 242 | 237 | * you do not want this class to stop chain processing, then this |
| 243 | 238 | * value should be set to <code>true</code></p> |
| 244 | - * @param ignorePostprocessResult <code>true</code> if result of the | |
| 239 | + * @param val <code>true</code> if result of the | |
| 245 | 240 | * looked up Filter's <code>postprocess()</code> method should be ignored. |
| 246 | 241 | * |
| 247 | 242 | * @since Chain 1.1 |
| 248 | 243 | */ |
| 249 | - public void setIgnorePostprocessResult(boolean ignorePostprocessResult) { | |
| 250 | - this.ignorePostprocessResult = ignorePostprocessResult; | |
| 244 | + public void setIgnorePostprocessResult(final boolean val) { | |
| 245 | + this.ignorePostprocessResult = val; | |
| 251 | 246 | } |
| 252 | 247 | |
| 253 | 248 | // ---------------------------------------------------------- Filter Methods |
| @@ -269,10 +264,11 @@ | ||
| 269 | 264 | * <code>CONTINUE</code> if no command is found or if the command |
| 270 | 265 | * is found but the <code>ignoreExecuteResult</code> property of this |
| 271 | 266 | * instance is <code>true</code> |
| 272 | - * @throws org.apache.commons.chain2.ChainException if and error occurs in the looked-up Command. | |
| 267 | + * @throws org.apache.commons.chain2.ChainException | |
| 268 | + * if and error occurs in the looked-up Command. | |
| 273 | 269 | */ |
| 274 | 270 | @Override |
| 275 | - public Processing execute(C context) { | |
| 271 | + public Processing execute(final C context) { | |
| 276 | 272 | Command<K, V, C> command = getCommand(context); |
| 277 | 273 | if (command != null) { |
| 278 | 274 | Processing result = command.execute(context); |
| @@ -298,7 +294,7 @@ | ||
| 298 | 294 | * case <code>IllegalArgumentException</code> will be thrown. |
| 299 | 295 | */ |
| 300 | 296 | @Override |
| 301 | - public boolean postprocess(C context, Exception exception) { | |
| 297 | + public boolean postprocess(final C context, final Exception exception) { | |
| 302 | 298 | Command<K, V, C> command = getCommand(context); |
| 303 | 299 | if (command != null) { |
| 304 | 300 | if (command instanceof Filter) { |
| @@ -321,28 +317,26 @@ | ||
| 321 | 317 | * |
| 322 | 318 | * @since Chain 1.2 |
| 323 | 319 | */ |
| 324 | - protected Catalog<K, V, C> getCatalog(C context) { | |
| 320 | + protected Catalog<K, V, C> getCatalog(final C context) { | |
| 325 | 321 | CatalogFactory<K, V, C> lookupFactory = this.catalogFactory; |
| 326 | 322 | if (lookupFactory == null) { |
| 327 | 323 | lookupFactory = CatalogFactoryBase.getInstance(); |
| 328 | 324 | } |
| 329 | 325 | |
| 330 | - String catalogName = getCatalogName(); | |
| 326 | + String ctlg = getCatalogName(); | |
| 331 | 327 | Catalog<K, V, C> catalog = null; |
| 332 | - if (catalogName == null) { | |
| 328 | + if (ctlg == null) { | |
| 333 | 329 | // use default catalog |
| 334 | 330 | catalog = lookupFactory.getCatalog(); |
| 335 | 331 | } else { |
| 336 | - catalog = lookupFactory.getCatalog(catalogName); | |
| 332 | + catalog = lookupFactory.getCatalog(ctlg); | |
| 337 | 333 | } |
| 334 | + | |
| 338 | 335 | if (catalog == null) { |
| 339 | - if (catalogName == null) { | |
| 340 | - throw new IllegalArgumentException | |
| 341 | - ("Cannot find default catalog"); | |
| 336 | + if (ctlg == null) { | |
| 337 | + throw new IllegalArgumentException("Cannot find default catalog"); | |
| 342 | 338 | } |
| 343 | - throw new IllegalArgumentException | |
| 344 | - ("Cannot find catalog '" + catalogName + "'"); | |
| 345 | - | |
| 339 | + throw new IllegalArgumentException("Cannot find catalog '" + ctlg + "'"); | |
| 346 | 340 | } |
| 347 | 341 | |
| 348 | 342 | return catalog; |
| @@ -357,23 +351,20 @@ | ||
| 357 | 351 | * can be found and the <code>optional</code> property is set |
| 358 | 352 | * to <code>false</code> |
| 359 | 353 | */ |
| 360 | - protected Command<K, V, C> getCommand(C context) { | |
| 354 | + protected Command<K, V, C> getCommand(final C context) { | |
| 361 | 355 | Catalog<K, V, C> catalog = getCatalog(context); |
| 362 | 356 | |
| 363 | 357 | Command<K, V, C> command; |
| 364 | - String name = getCommandName(context); | |
| 365 | - if (name != null) { | |
| 366 | - command = catalog.getCommand(name); | |
| 358 | + String cmd = getCommandName(context); | |
| 359 | + if (cmd != null) { | |
| 360 | + command = catalog.getCommand(cmd); | |
| 367 | 361 | if (command == null && !isOptional()) { |
| 368 | - if (catalogName == null) { | |
| 369 | - throw new IllegalArgumentException | |
| 370 | - ("Cannot find command '" + name | |
| 371 | - + "' in default catalog"); | |
| 362 | + if (this.catalogName == null) { | |
| 363 | + throw new IllegalArgumentException( | |
| 364 | + "Cannot find command '" + cmd + "' in default catalog"); | |
| 372 | 365 | } |
| 373 | - throw new IllegalArgumentException | |
| 374 | - ("Cannot find command '" + name | |
| 375 | - + "' in catalog '" + catalogName + "'"); | |
| 376 | - | |
| 366 | + throw new IllegalArgumentException("Cannot find command '" + cmd | |
| 367 | + + "' in catalog '" + this.catalogName + "'"); | |
| 377 | 368 | } |
| 378 | 369 | return command; |
| 379 | 370 | } |
| @@ -388,12 +379,12 @@ | ||
| 388 | 379 | * |
| 389 | 380 | * @since Chain 1.2 |
| 390 | 381 | */ |
| 391 | - protected String getCommandName(C context) { | |
| 392 | - String name = getName(); | |
| 393 | - if (name == null) { | |
| 394 | - name = (String) context.get(getNameKey()); | |
| 382 | + protected String getCommandName(final C context) { | |
| 383 | + String ret = getName(); | |
| 384 | + if (ret == null) { | |
| 385 | + ret = (String) context.get(getNameKey()); | |
| 395 | 386 | } |
| 396 | - return name; | |
| 387 | + return ret; | |
| 397 | 388 | } |
| 398 | 389 | |
| 399 | 390 | } |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2.base; |
| 18 | 19 | |
| 19 | 20 | import java.util.Map; |
| @@ -25,11 +26,11 @@ | ||
| 25 | 26 | * <p>Copy a specified literal value, or a context attribute stored under |
| 26 | 27 | * the <code>fromKey</code> (if any), to the <code>toKey</code>.</p> |
| 27 | 28 | * |
| 29 | + * @version $Id$ | |
| 30 | + * | |
| 28 | 31 | * @param <K> the type of keys maintained by the context associated with this command |
| 29 | 32 | * @param <V> the type of mapped values |
| 30 | 33 | * @param <C> Type of the context associated with this command |
| 31 | - * | |
| 32 | - * @version $Id$ | |
| 33 | 34 | */ |
| 34 | 35 | public class CopyCommand<K, V, C extends Map<K, V>> implements Command<K, V, C> { |
| 35 | 36 |
| @@ -36,6 +37,7 @@ | ||
| 36 | 37 | // -------------------------------------------------------------- Properties |
| 37 | 38 | |
| 38 | 39 | private K fromKey = null; |
| 40 | + private K toKey = null; | |
| 39 | 41 | |
| 40 | 42 | /** |
| 41 | 43 | * <p>Return the context attribute key for the source attribute.</p> |
| @@ -48,14 +50,12 @@ | ||
| 48 | 50 | /** |
| 49 | 51 | * <p>Set the context attribute key for the source attribute.</p> |
| 50 | 52 | * |
| 51 | - * @param fromKey The new key | |
| 53 | + * @param val The new key | |
| 52 | 54 | */ |
| 53 | - public void setFromKey(K fromKey) { | |
| 54 | - this.fromKey = fromKey; | |
| 55 | + public void setFromKey(final K val) { | |
| 56 | + this.fromKey = val; | |
| 55 | 57 | } |
| 56 | 58 | |
| 57 | - private K toKey = null; | |
| 58 | - | |
| 59 | 59 | /** |
| 60 | 60 | * <p>Return the context attribute key for the destination attribute.</p> |
| 61 | 61 | * @return The destination attribute key. |
| @@ -67,10 +67,10 @@ | ||
| 67 | 67 | /** |
| 68 | 68 | * <p>Set the context attribute key for the destination attribute.</p> |
| 69 | 69 | * |
| 70 | - * @param toKey The new key | |
| 70 | + * @param val The new key | |
| 71 | 71 | */ |
| 72 | - public void setToKey(K toKey) { | |
| 73 | - this.toKey = toKey; | |
| 72 | + public void setToKey(final K val) { | |
| 73 | + this.toKey = val; | |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // ---------------------------------------------------------- Filter Methods |
| @@ -84,7 +84,7 @@ | ||
| 84 | 84 | * @throws org.apache.commons.chain2.ChainException in the if an error occurs during execution. |
| 85 | 85 | */ |
| 86 | 86 | @Override |
| 87 | - public Processing execute(C context) { | |
| 87 | + public Processing execute(final C context) { | |
| 88 | 88 | if (containsKeys(context)) { |
| 89 | 89 | V value = context.get(getFromKey()); |
| 90 | 90 | context.put(getToKey(), value); |
| @@ -92,7 +92,7 @@ | ||
| 92 | 92 | return Processing.CONTINUE; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - private boolean containsKeys(C context) { | |
| 95 | + private boolean containsKeys(final C context) { | |
| 96 | 96 | return context.containsKey(getFromKey()) && context.containsKey(getToKey()); |
| 97 | 97 | } |
| 98 | 98 |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2.base; |
| 18 | 19 | |
| 19 | 20 | import org.apache.commons.chain2.Command; |
| @@ -23,11 +24,11 @@ | ||
| 23 | 24 | /** |
| 24 | 25 | * <p>Remove any context attribute stored under the <code>fromKey</code>.</p> |
| 25 | 26 | * |
| 27 | + * @version $Id$ | |
| 28 | + * | |
| 26 | 29 | * @param <K> the type of keys maintained by the context associated with this catalog |
| 27 | 30 | * @param <V> the type of mapped values |
| 28 | 31 | * @param <C> Type of the context associated with this command |
| 29 | - * | |
| 30 | - * @version $Id$ | |
| 31 | 32 | */ |
| 32 | 33 | public class RemoveCommand<K, V, C extends Context<K, V>> implements Command<K, V, C> { |
| 33 | 34 |
| @@ -46,10 +47,10 @@ | ||
| 46 | 47 | /** |
| 47 | 48 | * <p>Set the context attribute key for the attribute.</p> |
| 48 | 49 | * |
| 49 | - * @param fromKey The new key | |
| 50 | + * @param val The new key | |
| 50 | 51 | */ |
| 51 | - public void setFromKey(K fromKey) { | |
| 52 | - this.fromKey = fromKey; | |
| 52 | + public void setFromKey(final K val) { | |
| 53 | + this.fromKey = val; | |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | 56 | // ---------------------------------------------------------- Filter Methods |
| @@ -63,7 +64,7 @@ | ||
| 63 | 64 | * @throws org.apache.commons.chain2.ChainException if and error occurs. |
| 64 | 65 | */ |
| 65 | 66 | @Override |
| 66 | - public Processing execute(C context) { | |
| 67 | + public Processing execute(final C context) { | |
| 67 | 68 | context.remove(getFromKey()); |
| 68 | 69 | return Processing.CONTINUE; |
| 69 | 70 | } |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2.base; |
| 18 | 19 | |
| 19 | 20 | import org.apache.commons.chain2.Command; |
| @@ -30,15 +31,21 @@ | ||
| 30 | 31 | * For use by developers who prefer to group related functionality into a single class |
| 31 | 32 | * rather than an inheritance family. |
| 32 | 33 | * |
| 34 | + * @version $Id$ | |
| 33 | 35 | * @param <K> the type of keys maintained by the context associated with this command |
| 34 | 36 | * @param <V> the type of mapped values |
| 35 | 37 | * @param <C> Type of the context associated with this command |
| 36 | 38 | * |
| 37 | 39 | * @since Chain 1.1 |
| 38 | - * @version $Id$ | |
| 39 | 40 | */ |
| 40 | 41 | public abstract class DispatchCommand<K, V, C extends Context<K, V>> implements Command<K, V, C> { |
| 41 | 42 | |
| 43 | + /** | |
| 44 | + * The base implementation expects dispatch methods to take a <code>Context</code> | |
| 45 | + * as their only argument. | |
| 46 | + */ | |
| 47 | + private static final Class<?>[] DEFAULT_SIGNATURE = new Class<?>[] {Context.class}; | |
| 48 | + | |
| 42 | 49 | /** Cache of methods */ |
| 43 | 50 | private final Map<String, Method> methods = new WeakHashMap<>(); |
| 44 | 51 |
| @@ -49,39 +56,36 @@ | ||
| 49 | 56 | private String methodKey = null; |
| 50 | 57 | |
| 51 | 58 | /** |
| 52 | - * The base implementation expects dispatch methods to take a <code>Context</code> | |
| 53 | - * as their only argument. | |
| 54 | - */ | |
| 55 | - static final Class<?>[] DEFAULT_SIGNATURE = new Class<?>[] {Context.class}; | |
| 56 | - /** | |
| 57 | 59 | * Look up the method specified by either "method" or "methodKey" and invoke it, |
| 58 | 60 | * returning a {@link Processing} value as interpreted by <code>evaluateResult</code>. |
| 59 | 61 | * @param context The Context to be processed by this Command. |
| 60 | 62 | * @return the result of method being dispatched to. |
| 61 | 63 | * @throws IllegalStateException if neither 'method' nor 'methodKey' properties are defined |
| 62 | - * @throws DispatchException if any is thrown by the invocation. Note that if invoking the method | |
| 64 | + * @throws DispatchException if any is thrown by the invocation. | |
| 65 | + * Note that if invoking the method | |
| 63 | 66 | * results in an InvocationTargetException, the cause of that exception is thrown instead of |
| 64 | - * the exception itself, unless the cause is an <code>Error</code> or other <code>Throwable</code> | |
| 65 | - * which is not an <code>Exception</code>. | |
| 67 | + * the exception itself, unless the cause is an <code>Error</code> | |
| 68 | + * or other <code>Throwable</code> which is not an <code>Exception</code>. | |
| 66 | 69 | */ |
| 67 | 70 | @Override |
| 68 | - public Processing execute(C context) { | |
| 71 | + public Processing execute(final C context) { | |
| 69 | 72 | if (this.getMethod() == null && this.getMethodKey() == null) { |
| 70 | - throw new IllegalStateException("Neither 'method' nor 'methodKey' properties are defined "); | |
| 73 | + throw new IllegalStateException( | |
| 74 | + "Neither 'method' nor 'methodKey' properties are defined "); | |
| 71 | 75 | } |
| 72 | 76 | |
| 73 | 77 | try { |
| 74 | 78 | Method methodObject = extractMethod(context); |
| 75 | - return evaluateResult(methodObject.invoke(this, | |
| 76 | - getArguments(context))); | |
| 79 | + return evaluateResult(methodObject.invoke(this, getArguments(context))); | |
| 77 | 80 | |
| 78 | - } catch (NoSuchMethodException e) { | |
| 81 | + } catch (final NoSuchMethodException e) { | |
| 79 | 82 | throw new DispatchException("Error extracting method from context", e, context, this); |
| 80 | - } catch (IllegalAccessException e) { | |
| 83 | + } catch (final IllegalAccessException e) { | |
| 81 | 84 | throw new DispatchException("Error accessing method", e, context, this); |
| 82 | - } catch (InvocationTargetException e) { | |
| 85 | + } catch (final InvocationTargetException e) { | |
| 83 | 86 | Throwable cause = e.getTargetException(); |
| 84 | - throw new DispatchException("Error in reflected dispatched command", cause, context, this); | |
| 87 | + throw new DispatchException( | |
| 88 | + "Error in reflected dispatched command", cause, context, this); | |
| 85 | 89 | } |
| 86 | 90 | } |
| 87 | 91 |
| @@ -95,13 +99,14 @@ | ||
| 95 | 99 | * @throws NoSuchMethodException if no method can be found under the specified name. |
| 96 | 100 | * @throws NullPointerException if no methodName cannot be determined |
| 97 | 101 | */ |
| 98 | - protected Method extractMethod(C context) throws NoSuchMethodException { | |
| 102 | + protected Method extractMethod(final C context) throws NoSuchMethodException { | |
| 99 | 103 | String methodName = this.getMethod(); |
| 100 | 104 | |
| 101 | 105 | if (methodName == null) { |
| 102 | 106 | Object methodContextObj = context.get(this.getMethodKey()); |
| 103 | 107 | if (methodContextObj == null) { |
| 104 | - throw new NullPointerException("No value found in context under " + this.getMethodKey()); | |
| 108 | + throw new NullPointerException( | |
| 109 | + "No value found in context under " + this.getMethodKey()); | |
| 105 | 110 | } |
| 106 | 111 | methodName = methodContextObj.toString(); |
| 107 | 112 | } |
| @@ -108,12 +113,12 @@ | ||
| 108 | 113 | |
| 109 | 114 | Method theMethod = null; |
| 110 | 115 | |
| 111 | - synchronized (methods) { | |
| 112 | - theMethod = methods.get(methodName); | |
| 116 | + synchronized (this.methods) { | |
| 117 | + theMethod = this.methods.get(methodName); | |
| 113 | 118 | |
| 114 | 119 | if (theMethod == null) { |
| 115 | 120 | theMethod = getClass().getMethod(methodName, getSignature()); |
| 116 | - methods.put(methodName, theMethod); | |
| 121 | + this.methods.put(methodName, theMethod); | |
| 117 | 122 | } |
| 118 | 123 | } |
| 119 | 124 |
| @@ -127,8 +132,8 @@ | ||
| 127 | 132 | * @param obj The result of the method execution |
| 128 | 133 | * @return The evaluated result/ |
| 129 | 134 | */ |
| 130 | - protected Processing evaluateResult(Object obj) { | |
| 131 | - if(obj instanceof Processing) { | |
| 135 | + protected Processing evaluateResult(final Object obj) { | |
| 136 | + if (obj instanceof Processing) { | |
| 132 | 137 | Processing result = (Processing) obj; |
| 133 | 138 | return result; |
| 134 | 139 | } |
| @@ -141,7 +146,7 @@ | ||
| 141 | 146 | * @return The method signature. |
| 142 | 147 | */ |
| 143 | 148 | protected Class<?>[] getSignature() { |
| 144 | - return DEFAULT_SIGNATURE; | |
| 149 | + return DEFAULT_SIGNATURE.clone(); | |
| 145 | 150 | } |
| 146 | 151 | |
| 147 | 152 | /** |
| @@ -153,7 +158,7 @@ | ||
| 153 | 158 | * @param context The Context being processed by this Command. |
| 154 | 159 | * @return The method arguments. |
| 155 | 160 | */ |
| 156 | - protected Object[] getArguments(C context) { | |
| 161 | + protected Object[] getArguments(final C context) { | |
| 157 | 162 | return new Object[] {context}; |
| 158 | 163 | } |
| 159 | 164 |
| @@ -162,7 +167,7 @@ | ||
| 162 | 167 | * @return The method name. |
| 163 | 168 | */ |
| 164 | 169 | public String getMethod() { |
| 165 | - return method; | |
| 170 | + return this.method; | |
| 166 | 171 | } |
| 167 | 172 | |
| 168 | 173 | /** |
| @@ -170,23 +175,23 @@ | ||
| 170 | 175 | * @return The Context key for the method name. |
| 171 | 176 | */ |
| 172 | 177 | public String getMethodKey() { |
| 173 | - return methodKey; | |
| 178 | + return this.methodKey; | |
| 174 | 179 | } |
| 175 | 180 | |
| 176 | 181 | /** |
| 177 | 182 | * Set the method name. |
| 178 | - * @param method The method name. | |
| 183 | + * @param val The method name. | |
| 179 | 184 | */ |
| 180 | - public void setMethod(String method) { | |
| 181 | - this.method = method; | |
| 185 | + public void setMethod(String val) { | |
| 186 | + this.method = val; | |
| 182 | 187 | } |
| 183 | 188 | |
| 184 | 189 | /** |
| 185 | 190 | * Set the Context key for the method name. |
| 186 | - * @param methodKey The Context key for the method name. | |
| 191 | + * @param val The Context key for the method name. | |
| 187 | 192 | */ |
| 188 | - public void setMethodKey(String methodKey) { | |
| 189 | - this.methodKey = methodKey; | |
| 193 | + public void setMethodKey(final String val) { | |
| 194 | + this.methodKey = val; | |
| 190 | 195 | } |
| 191 | 196 | |
| 192 | 197 | } |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * See the License for the specific language governing permissions and |
| 15 | 15 | * limitations under the License. |
| 16 | 16 | */ |
| 17 | + | |
| 17 | 18 | package org.apache.commons.chain2.base; |
| 18 | 19 | |
| 19 | 20 | import org.apache.commons.chain2.ChainException; |
| @@ -23,7 +24,8 @@ | ||
| 23 | 24 | |
| 24 | 25 | /** |
| 25 | 26 | * Runtime Exception that wraps an underlying exception thrown during the |
| 26 | - * execution of a {@link org.apache.commons.chain2.Command} or {@link org.apache.commons.chain2.Chain}. | |
| 27 | + * execution of a {@link org.apache.commons.chain2.Command} | |
| 28 | + * or {@link org.apache.commons.chain2.Chain}. | |
| 27 | 29 | * |
| 28 | 30 | * @version $Id$ |
| 29 | 31 | */ |
| @@ -31,19 +33,22 @@ | ||
| 31 | 33 | |
| 32 | 34 | /** serialVersionUID */ |
| 33 | 35 | private static final long serialVersionUID = 20120724L; |
| 36 | + | |
| 34 | 37 | /** |
| 35 | 38 | * @param message String |
| 36 | 39 | */ |
| 37 | - public DispatchException(String message) { | |
| 40 | + public DispatchException(final String message) { | |
| 38 | 41 | super(message); |
| 39 | 42 | } |
| 43 | + | |
| 40 | 44 | /** |
| 41 | 45 | * @param message String |
| 42 | 46 | * @param cause Throwable |
| 43 | 47 | */ |
| 44 | - public DispatchException(String message, Throwable cause) { | |
| 48 | + public DispatchException(final String message, final Throwable cause) { | |
| 45 | 49 | super(message, cause); |
| 46 | 50 | } |
| 51 | + | |
| 47 | 52 | /** |
| 48 | 53 | * @param <K> K |
| 49 | 54 | * @param <V> V |
| @@ -53,8 +58,8 @@ | ||
| 53 | 58 | * @param context C |
| 54 | 59 | * @param failedCommand Command |
| 55 | 60 | */ |
| 56 | - public <K, V, C extends Map<K, V>> DispatchException(String message, Throwable cause, | |
| 57 | - C context, Command<K, V, C> failedCommand) { | |
| 61 | + public <K, V, C extends Map<K, V>> DispatchException(final String message, | |
| 62 | + final Throwable cause, final C context, final Command<K, V, C> failedCommand) { | |
| 58 | 63 | super(message, cause, context, failedCommand); |
| 59 | 64 | } |
| 60 | 65 |
| @@ -26,11 +26,11 @@ | ||
| 26 | 26 | /** |
| 27 | 27 | * <p>Override any context attribute stored under the <code>key</code> with <code>value</code>.</p> |
| 28 | 28 | * |
| 29 | + * @version $Id$ | |
| 30 | + * | |
| 29 | 31 | * @param <K> the type of keys maintained by the context associated with this catalog |
| 30 | 32 | * @param <V> the type of mapped values |
| 31 | 33 | * @param <C> Type of the context associated with this command |
| 32 | - * | |
| 33 | - * @version $Id$ | |
| 34 | 34 | */ |
| 35 | 35 | public class OverrideCommand<K, V, C extends Map<K, V>> implements Command<K, V, C> { |
| 36 | 36 |
| @@ -44,16 +44,16 @@ | ||
| 44 | 44 | * @return The context attribute key. |
| 45 | 45 | */ |
| 46 | 46 | public K getKey() { |
| 47 | - return key; | |
| 47 | + return this.key; | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * <p>Set the context attribute key for the attribute to override.</p> |
| 52 | 52 | * |
| 53 | - * @param key The new key | |
| 53 | + * @param val The new key | |
| 54 | 54 | */ |
| 55 | - public void setKey(K key) { | |
| 56 | - this.key = key; | |
| 55 | + public void setKey(final K val) { | |
| 56 | + this.key = val; | |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| @@ -61,16 +61,16 @@ | ||
| 61 | 61 | * @return The value. |
| 62 | 62 | */ |
| 63 | 63 | public V getValue() { |
| 64 | - return value; | |
| 64 | + return this.value; | |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | 68 | * <p>Set the value that should override context attribute with key <code>key</code>.</p> |
| 69 | 69 | * |
| 70 | - * @param value The new value | |
| 70 | + * @param val The new value | |
| 71 | 71 | */ |
| 72 | - public void setValue(V value) { | |
| 73 | - this.value = value; | |
| 72 | + public void setValue(final V val) { | |
| 73 | + this.value = val; | |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // ---------------------------------------------------------- Filter Methods |
| @@ -83,7 +83,7 @@ | ||
| 83 | 83 | * @throws org.apache.commons.chain2.ChainException if and error occurs. |
| 84 | 84 | */ |
| 85 | 85 | @Override |
| 86 | - public Processing execute(C context) { | |
| 86 | + public Processing execute(final C context) { | |
| 87 | 87 | if (context.containsKey(getKey())) { |
| 88 | 88 | context.put(getKey(), getValue()); |
| 89 | 89 | } |