Coverage Report - org.kuali.rice.kns.util.GlobalVariables
 
Classes in this File Line Coverage Branch Coverage Complexity
GlobalVariables
0%
0/41
0%
0/2
1.105
GlobalVariables$1
0%
0/2
N/A
1.105
GlobalVariables$2
0%
0/2
N/A
1.105
GlobalVariables$3
0%
0/2
N/A
1.105
GlobalVariables$4
0%
0/2
N/A
1.105
 
 1  
 /*
 2  
  * Copyright 2005-2007 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.kns.util;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.kuali.rice.kns.UserSession;
 22  
 import org.kuali.rice.kns.web.struts.form.KualiForm;
 23  
 
 24  
 /**
 25  
  * This class will hold all of our thread local variables and accessors for those
 26  
  *
 27  
  *
 28  
  */
 29  0
 public class GlobalVariables {
 30  
 
 31  0
     private static ThreadLocal<UserSession> userSessions = new ThreadLocal<UserSession>();
 32  0
     private static ThreadLocal<String> hideSessionFromTestsMessage = new ThreadLocal<String>();
 33  0
     private static ThreadLocal<KualiForm> kualiForms = new ThreadLocal<KualiForm>();
 34  
     
 35  0
     private static ThreadLocal<MessageMap> messageMaps = new ThreadLocal<MessageMap>()  {
 36  
                 @Override
 37  
                 protected MessageMap initialValue() {
 38  0
                         return new MessageMap();
 39  
                 }
 40  
         };
 41  
 
 42  0
     private static ThreadLocal<MessageList> messageLists = new ThreadLocal<MessageList>() {
 43  
                 @Override
 44  
                 protected MessageList initialValue() {
 45  0
                         return new MessageList();
 46  
                 }
 47  
         };
 48  
         
 49  0
     private static ThreadLocal<HashMap<String, AuditCluster>> auditErrorMaps = new ThreadLocal<HashMap<String, AuditCluster>>() {
 50  
             @Override
 51  
             protected HashMap<String, AuditCluster> initialValue() {
 52  0
                     return new HashMap<String, AuditCluster>();
 53  
             }
 54  
     };
 55  
     
 56  0
     private static ThreadLocal<Map<String,Object>> requestCaches = new ThreadLocal<Map<String,Object>>() {
 57  
             @Override
 58  
                 protected HashMap<String, Object> initialValue() {
 59  0
                     return new HashMap<String, Object>();
 60  
             }
 61  
     };
 62  
 
 63  
     /**
 64  
      * @return the UserSession that has been assigned to this thread of execution it is important that this not be called by
 65  
      *         anything that lives outside
 66  
      */
 67  
     public static UserSession getUserSession() {
 68  0
         String message = hideSessionFromTestsMessage.get();
 69  0
         if (message != null) {
 70  0
             throw new RuntimeException(message);
 71  
         }
 72  0
         return userSessions.get();
 73  
     }
 74  
 
 75  
     /**
 76  
      * Sets an error message for tests that try to use the session without declaring it.
 77  
      * This method should be use by only KualiTestBase, not by other test code and especially not by production code.
 78  
      *
 79  
      * @param message the detail to throw, or null to allow access to the session
 80  
      */
 81  
     public static void setHideSessionFromTestsMessage(String message) {
 82  0
         hideSessionFromTestsMessage.set(message);
 83  0
     }
 84  
 
 85  
     /**
 86  
      * sets the userSession object into the global variable for this thread
 87  
      *
 88  
      * @param userSession
 89  
      */
 90  
     public static void setUserSession(UserSession userSession) {
 91  0
         userSessions.set(userSession);
 92  0
     }
 93  
     
 94  
     public static MessageMap getMessageMap() {
 95  0
             return messageMaps.get();
 96  
     }
 97  
 
 98  
     /**
 99  
      * Merges a message map into the global variables error map
 100  
      * @param messageMap
 101  
      */
 102  
     public static void mergeErrorMap(MessageMap messageMap) {
 103  0
         getMessageMap().getErrorMessages().putAll(messageMap.getErrorMessages());
 104  0
         getMessageMap().getWarningMessages().putAll(messageMap.getWarningMessages());
 105  0
         getMessageMap().getInfoMessages().putAll(messageMap.getInfoMessages());
 106  0
     }
 107  
     
 108  
     /**
 109  
      * Sets a new (clean) MessageMap
 110  
      *
 111  
      * @param messageMap
 112  
      */
 113  
     public static void setMessageMap(MessageMap messageMap) {
 114  0
             messageMaps.set(messageMap);
 115  0
     }
 116  
 
 117  
     /**
 118  
      * @return ArrayList containing messages.
 119  
      */
 120  
     public static MessageList getMessageList() {
 121  0
         return messageLists.get();
 122  
     }
 123  
 
 124  
     /**
 125  
      * Sets a new message list
 126  
      *
 127  
      * @param messageList
 128  
      */
 129  
     public static void setMessageList(MessageList messageList) {
 130  0
         messageLists.set(messageList);
 131  0
     }
 132  
 
 133  
     /**
 134  
      * @return ArrayList containing audit error messages.
 135  
      */
 136  
     public static Map<String, AuditCluster> getAuditErrorMap() {
 137  0
         return auditErrorMaps.get();
 138  
     }
 139  
 
 140  
     /**
 141  
      * Sets a new (clean) AuditErrorList
 142  
      *
 143  
      * @param errorMap
 144  
      */
 145  
     public static void setAuditErrorMap(HashMap<String, AuditCluster> errorMap) {
 146  0
         auditErrorMaps.set(errorMap);
 147  0
     }
 148  
 
 149  
     /**
 150  
      * @return KualiForm that has been assigned to this thread of execution.
 151  
      */
 152  
     public static KualiForm getKualiForm() {
 153  0
         return kualiForms.get();
 154  
     }
 155  
 
 156  
     /**
 157  
      * sets the kualiForm object into the global variable for this thread
 158  
      *
 159  
      * @param kualiForm
 160  
      */
 161  
     public static void setKualiForm(KualiForm kualiForm) {
 162  0
             kualiForms.set(kualiForm);
 163  0
     }
 164  
 
 165  
     public static Object getRequestCache( String cacheName ) {
 166  0
             return requestCaches.get().get(cacheName);
 167  
     }
 168  
 
 169  
     public static void setRequestCache( String cacheName, Object cacheObject ) {
 170  0
             requestCaches.get().put(cacheName, cacheObject);
 171  0
     }
 172  
 
 173  
 
 174  
     /**
 175  
      * Clears out GlobalVariable objects with the exception of the UserSession
 176  
      */
 177  
     public static void clear() {
 178  0
         messageMaps.set(new MessageMap());
 179  0
         auditErrorMaps.set(new HashMap<String, AuditCluster>());
 180  0
         messageLists.set(new MessageList());
 181  0
         requestCaches.set(new HashMap<String,Object>() );
 182  0
         kualiForms.set(null);
 183  0
     }
 184  
 }