View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kew.api.WorkflowRuntimeException;
20  import org.kuali.rice.kew.api.validation.RuleValidationContext;
21  import org.kuali.rice.kew.api.validation.ValidationResults;
22  import org.kuali.rice.kew.framework.KewFrameworkServiceLocator;
23  import org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService;
24  import org.kuali.rice.kew.rule.RuleValidationAttribute;
25  
26  import java.lang.reflect.InvocationHandler;
27  import java.lang.reflect.Method;
28  import java.lang.reflect.Proxy;
29  
30  /**
31   * RuleValidationAttributeResolver reference impl.  Returns a proxy which delegates to the appropriate
32   * RuleValidationExporterService.
33   *
34   * @see org.kuali.rice.kew.rule.RuleValidationAttribute
35   * @see org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class RuleValidationAttributeResolverImpl implements RuleValidationAttributeResolver {
39      @Override
40      public RuleValidationAttribute resolveRuleValidationAttribute(final String attributeName, String applicationId) throws Exception {
41          final RuleValidationAttributeExporterService service = findRuleValidationAttributeExporterService(applicationId);
42          return (RuleValidationAttribute) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[] { RuleValidationAttribute.class }, new RuleValidationAttributeInvocationHandler() {
43              @Override
44              protected ValidationResults invokeValidate(RuleValidationContext context) throws Exception {
45                  return service.validate(attributeName, context);
46              }
47          });
48      }
49  
50      protected RuleValidationAttributeExporterService findRuleValidationAttributeExporterService(String applicationId) {
51          RuleValidationAttributeExporterService service = KewFrameworkServiceLocator.getRuleValidationAttributeExporterService(applicationId);
52          if (service == null) {
53              throw new WorkflowRuntimeException("Failed to locate RuleValidationAttributeExporterService for applicationId: " + applicationId);
54          }
55          return service;
56      }
57  
58      protected static abstract class RuleValidationAttributeInvocationHandler implements InvocationHandler {
59          @Override
60          public ValidationResults invoke(Object o, Method method, Object[] objects) throws Throwable {
61              if (!StringUtils.equals(method.getName(), "validate")) {
62                  throw new UnsupportedOperationException("RuleValidationAttribute only supports 'validate'");
63              }
64              return invokeValidate((RuleValidationContext) objects[0]);
65          }
66          protected abstract ValidationResults invokeValidate(RuleValidationContext context) throws Exception;
67      }
68  }