Coverage Report - org.kuali.rice.krms.impl.provider.repository.LazyAction
 
Classes in this File Line Coverage Branch Coverage Complexity
LazyAction
0%
0/23
0%
0/6
1.429
LazyAction$1
0%
0/3
N/A
1.429
 
 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 org.kuali.rice.krms.api.engine.ExecutionEnvironment;
 19  
 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
 20  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
 21  
 import org.kuali.rice.krms.framework.engine.Action;
 22  
 import org.kuali.rice.krms.framework.type.ActionTypeService;
 23  
 import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
 24  
 
 25  
 /**
 26  
  * TODO... 
 27  
  * 
 28  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 29  
  *
 30  
  */
 31  
 final class LazyAction implements Action {
 32  
 
 33  
         private final ActionDefinition actionDefinition;
 34  
         private final KrmsTypeResolver typeResolver;
 35  
         
 36  0
         private final Object mutex = new Object();
 37  
         
 38  
         // volatile for double-checked locking idiom
 39  
         private volatile Action action;
 40  
         
 41  0
         LazyAction(ActionDefinition actionDefinition, KrmsTypeResolver typeResolver) {
 42  0
                 this.actionDefinition = actionDefinition;
 43  0
                 this.typeResolver = typeResolver;
 44  0
                 this.action = null;
 45  0
         }
 46  
         
 47  
         @Override
 48  
         public void execute(ExecutionEnvironment environment) {
 49  0
                 getAction().execute(environment);
 50  0
         }
 51  
 
 52  
         @Override
 53  
         public void executeSimulation(ExecutionEnvironment environment) {
 54  0
                 getAction().executeSimulation(environment);
 55  0
         }
 56  
         
 57  
         /**
 58  
          * Gets the action using a lazy double-checked locking mechanism as documented in Effective Java Item 71.
 59  
          */
 60  
         private Action getAction() {
 61  0
                 Action localAction = action;
 62  0
                 if (localAction == null) {
 63  0
                         synchronized (mutex) {
 64  0
                                 localAction = action;
 65  0
                                 if (localAction == null) {
 66  0
                                         action = localAction = constructAction();
 67  
                                 }
 68  0
                         }
 69  
                 }
 70  0
                 return localAction;
 71  
         }
 72  
         
 73  
         private Action constructAction() {
 74  0
                 ActionTypeService actionTypeService = typeResolver.getActionTypeService(actionDefinition);
 75  0
                 Action action = actionTypeService.loadAction(actionDefinition);
 76  0
                 if (action == null) {
 77  0
                         action = new Action() {
 78  
                                 @Override
 79  
                                 public void execute(ExecutionEnvironment environment) {
 80  0
                                 }
 81  
                                 public void executeSimulation(ExecutionEnvironment environment) {
 82  0
                                 }
 83  
                         };
 84  
                 }
 85  0
                 return action;
 86  
         }
 87  
 
 88  
         
 89  
 }