1 package org.sapia.resource.include; 2 3 import java.util.Collections ; 4 import java.util.HashMap ; 5 import java.util.Map ; 6 import java.util.Stack ; 7 8 import org.sapia.resource.ResourceCapable; 9 10 89 public class IncludeState { 90 91 private static ThreadLocal _state = new ThreadLocal (); 92 private Stack _stack = new Stack (); 93 94 static{ 95 _state.set(Collections.synchronizedMap(new HashMap ())); 96 } 97 98 IncludeState(){ 99 } 100 101 108 public static synchronized IncludeConfig createConfig( 109 String appKey, 110 IncludeContextFactory fac, 111 ResourceCapable resources){ 112 IncludeConfig conf = new IncludeConfig(appKey, fac, resources); 113 return conf; 114 } 115 116 121 public static synchronized IncludeContext currentContext(String appKey){ 122 IncludeState instance = instance(appKey); 123 if(instance._stack.size() == 0){ 124 return null; 125 } 126 else{ 127 return (IncludeContext)instance._stack.peek(); 128 } 129 } 130 131 140 public static synchronized IncludeContext createContext( 141 String uri, 142 IncludeConfig config){ 143 IncludeState instance = instance(config.getAppKey()); 144 IncludeContext child = config.getFactory().createInstance(); 145 if(instance != null && instance._stack.size() > 0){ 146 child.setParent((IncludeContext)instance._stack.peek()); 147 } 148 instance._stack.push(child); 149 child.setConfig(config); 150 child.setUri(uri); 151 return child; 152 } 153 154 static void popContext(String appKey){ 155 IncludeState instance = instance(appKey); 156 if(instance._stack.size() > 0){ 157 instance._stack.pop(); 158 } 159 } 160 161 static IncludeState instance(String appKey){ 162 IncludeState state = (IncludeState)((Map )_state.get()).get(appKey); 163 if(state == null){ 164 state = new IncludeState(); 165 ((Map )_state.get()).put(appKey, state); 166 } 167 return state; 168 } 169 170 } 171 | Popular Tags |