View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.krms.impl.provider.repository;
17  
18  import java.util.Collections;
19  import java.util.List;
20  
21  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
22  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
23  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
24  import org.kuali.rice.krms.framework.engine.Proposition;
25  import org.kuali.rice.krms.framework.engine.PropositionResult;
26  import org.kuali.rice.krms.framework.type.PropositionTypeService;
27  import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
28  
29  /**
30   * TODO... 
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  final class LazyProposition implements Proposition {
36  
37  	private final PropositionDefinition propositionDefinition;
38  	private final KrmsTypeResolver resolver;
39  	
40  	private final Object mutex = new Object();
41  	
42  	// volatile for double-checked locking idiom
43  	private volatile Proposition proposition;
44  	
45  	LazyProposition(PropositionDefinition propositionDefinition, KrmsTypeResolver resolver) {
46  		this.propositionDefinition = propositionDefinition;
47  		this.resolver = resolver;
48  		this.proposition = null;
49  	}
50  	
51  	@Override
52  	public PropositionResult evaluate(ExecutionEnvironment environment) {
53  		return getProposition().evaluate(environment);
54  	}
55  	
56  	/**
57  	 * Gets the proposition using a lazy double-checked locking mechanism as documented in Effective Java Item 71.
58  	 */
59  	private Proposition getProposition() {
60  		Proposition localProposition = proposition;
61  		if (localProposition == null) {
62  			synchronized (mutex) {
63  				localProposition = proposition;
64  				if (localProposition == null) {
65  					proposition = localProposition = constructProposition();
66  				}
67  			}
68  		}
69  		return localProposition;
70  	}
71  	
72  	private Proposition constructProposition() {
73  		PropositionTypeService propositionTypeService = resolver.getPropositionTypeService(propositionDefinition);
74  		Proposition proposition = propositionTypeService.loadProposition(propositionDefinition);
75  		if (proposition == null) {
76  			proposition = new Proposition() {
77  				@Override
78  				public PropositionResult evaluate(ExecutionEnvironment environment) {
79  					return new PropositionResult(true);
80  				}
81  
82  			    @Override
83  			    public List<Proposition> getChildren() {
84  			        return Collections.emptyList();
85  			    }
86  			    
87  			    @Override
88  			    public boolean isCompound() {
89  			        return false;
90  			    }
91  			};
92  		}
93  		return proposition;
94  	}
95  
96  	@Override
97  	public List<Proposition> getChildren() {
98  	    return getProposition().getChildren();
99  	}
100 	
101 	@Override
102 	public boolean isCompound() {
103 	    return getProposition().isCompound();
104 	}
105 }