1 29 30 package com.caucho.config; 31 32 import com.caucho.el.AbstractVariableResolver; 33 import com.caucho.el.EL; 34 import com.caucho.loader.Environment; 35 import com.caucho.loader.EnvironmentClassLoader; 36 37 import javax.el.ELContext; 38 import java.util.HashMap ; 39 40 43 public class ConfigVariableResolver extends AbstractVariableResolver { 44 private HashMap <String ,Object > _varMap = new HashMap <String ,Object >(); 45 46 private ClassLoader _originalLoader; 47 private ClassLoader _configureClassLoader; 48 51 public ConfigVariableResolver() 52 { 53 this(Thread.currentThread().getContextClassLoader()); 54 } 55 56 59 public ConfigVariableResolver(ClassLoader loader) 60 { 61 super(EL.getEnvironment(loader).getELResolver()); 62 63 _originalLoader = loader; 64 } 65 66 69 ClassLoader getConfigLoader() 70 { 71 return _configureClassLoader; 72 } 73 74 77 void setConfigLoader(ClassLoader loader) 78 { 79 if (_configureClassLoader == null) 81 _configureClassLoader = loader; 82 } 83 84 87 @Override 88 public Object getValue(ELContext context, 89 Object base, 90 Object property) 91 { 92 if (base != null) 93 return null; 94 else if (! (property instanceof String )) 95 return null; 96 97 context.setPropertyResolved(true); 98 99 String var = (String ) property; 100 101 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 102 103 for (; 104 loader != _originalLoader && loader != null; 105 loader = loader.getParent()) { 106 if (loader instanceof EnvironmentClassLoader) { 107 Object value = EL.getLevelVar(var, loader); 108 109 if (value == EL.NULL) 110 return null; 111 else if (value != null) 112 return value; 113 } 114 } 115 116 if (loader == _configureClassLoader) { 117 Object value = EL.getLevelVar(var, loader); 118 119 if (value == EL.NULL) 120 return null; 121 else if (value != null) 122 return value; 123 } 124 125 Object value = _varMap.get(var); 126 127 if (value == EL.NULL) 128 return null; 129 else if (value != null) 130 return value; 131 132 value = Environment.getAttribute(var, _originalLoader); 133 134 if (value == EL.NULL) 135 return null; 136 else if (value != null) 137 return value; 138 else 139 return super.getValue(context, base, property); 140 } 141 142 145 @Override 146 public void setValue(ELContext context, 147 Object base, 148 Object property, 149 Object value) 150 { 151 if (base != null || ! (property instanceof String )) 152 return; 153 154 context.setPropertyResolved(true); 155 156 String name = (String ) property; 157 158 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 159 160 for (; 161 loader != _originalLoader && loader != null; 162 loader = loader.getParent()) { 163 if (loader instanceof EnvironmentClassLoader) { 164 EL.putVar(name, value, loader); 165 166 return; 167 } 168 } 169 170 if (loader == _configureClassLoader) { 171 EL.putVar(name, value, loader); 172 173 return; 174 } 175 176 _varMap.put(name, value); 177 } 178 179 public String toString() 180 { 181 return "ConfigVariableResolver[]"; 182 } 183 } 184 | Popular Tags |