001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kew.validation; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.rice.kew.api.WorkflowRuntimeException; 020 import org.kuali.rice.kew.api.validation.RuleValidationContext; 021 import org.kuali.rice.kew.api.validation.ValidationResults; 022 import org.kuali.rice.kew.framework.KewFrameworkServiceLocator; 023 import org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService; 024 import org.kuali.rice.kew.rule.RuleValidationAttribute; 025 026 import java.lang.reflect.InvocationHandler; 027 import java.lang.reflect.Method; 028 import java.lang.reflect.Proxy; 029 030 /** 031 * RuleValidationAttributeResolver reference impl. Returns a proxy which delegates to the appropriate 032 * RuleValidationExporterService. 033 * 034 * @see org.kuali.rice.kew.rule.RuleValidationAttribute 035 * @see org.kuali.rice.kew.framework.validation.RuleValidationAttributeExporterService 036 * @author Kuali Rice Team (rice.collab@kuali.org) 037 */ 038 public class RuleValidationAttributeResolverImpl implements RuleValidationAttributeResolver { 039 @Override 040 public RuleValidationAttribute resolveRuleValidationAttribute(final String attributeName, String applicationId) throws Exception { 041 final RuleValidationAttributeExporterService service = findRuleValidationAttributeExporterService(applicationId); 042 return (RuleValidationAttribute) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[] { RuleValidationAttribute.class }, new RuleValidationAttributeInvocationHandler() { 043 @Override 044 protected ValidationResults invokeValidate(RuleValidationContext context) throws Exception { 045 return service.validate(attributeName, context); 046 } 047 }); 048 } 049 050 protected RuleValidationAttributeExporterService findRuleValidationAttributeExporterService(String applicationId) { 051 RuleValidationAttributeExporterService service = KewFrameworkServiceLocator.getRuleValidationAttributeExporterService(applicationId); 052 if (service == null) { 053 throw new WorkflowRuntimeException("Failed to locate RuleValidationAttributeExporterService for applicationId: " + applicationId); 054 } 055 return service; 056 } 057 058 protected static abstract class RuleValidationAttributeInvocationHandler implements InvocationHandler { 059 @Override 060 public ValidationResults invoke(Object o, Method method, Object[] objects) throws Throwable { 061 if (!StringUtils.equals(method.getName(), "validate")) { 062 throw new UnsupportedOperationException("RuleValidationAttribute only supports 'validate'"); 063 } 064 return invokeValidate((RuleValidationContext) objects[0]); 065 } 066 protected abstract ValidationResults invokeValidate(RuleValidationContext context) throws Exception; 067 } 068 }