1 29 30 package com.caucho.jsp; 31 32 import com.caucho.config.types.FileSetType; 33 import com.caucho.jsp.cfg.TldFunction; 34 import com.caucho.jsp.cfg.TldTaglib; 35 import com.caucho.loader.DynamicClassLoader; 36 import com.caucho.server.util.CauchoSystem; 37 import com.caucho.server.webapp.WebApp; 38 import com.caucho.util.L10N; 39 import com.caucho.vfs.Path; 40 import com.caucho.vfs.Vfs; 41 42 import java.io.IOException ; 43 import java.lang.reflect.Method ; 44 import java.util.ArrayList ; 45 import java.util.HashMap ; 46 import java.util.logging.Logger ; 47 48 51 public class TaglibManager { 52 static final L10N L = new L10N(TaglibManager.class); 53 private static final Logger log 54 = Logger.getLogger(TaglibManager.class.getName()); 55 56 private JspResourceManager _resourceManager; 57 private WebApp _webApp; 58 59 private TldManager _tldManager; 60 61 private String _tldDir; 62 private FileSetType _tldFileSet; 63 64 private TagFileManager _tagFileManager; 65 66 private HashMap <String ,String > _uriLocationMap 67 = new HashMap <String ,String >(); 68 private HashMap <String ,TldTaglib> _tldMap = new HashMap <String ,TldTaglib>(); 69 private HashMap <String ,Taglib> _taglibMap = new HashMap <String ,Taglib>(); 70 private HashMap <String ,Taglib> _taglibDirMap = new HashMap <String ,Taglib>(); 71 72 private TagAnalyzer _tagAnalyzer = new TagAnalyzer(); 73 74 private volatile boolean _isInit; 75 76 public TaglibManager(JspResourceManager resourceManager, 77 WebApp webApp, 78 TagFileManager tagFileManager) 79 throws JspParseException, IOException 80 { 81 _resourceManager = resourceManager; 82 _webApp = webApp; 83 84 _tldManager = TldManager.create(resourceManager, webApp); 85 _tagFileManager = tagFileManager; 86 87 if (webApp != null) 88 webApp.getJspApplicationContext().setTaglibManager(this); 89 } 90 91 94 void setWebApp(WebApp webApp) 95 { 96 _webApp = webApp; 97 } 98 99 public void setTldDir(String tldDir) 100 { 101 _tldDir = tldDir; 102 } 103 104 105 public void setTldFileSet(FileSetType fileSet) 106 { 107 _tldManager.setTldFileSet(fileSet); 108 } 109 110 113 public void addLocationMap(String uri, String location) 114 { 115 _uriLocationMap.put(uri, location); 116 } 117 118 122 public synchronized void init() 123 throws JspParseException, IOException 124 { 125 if (_isInit) 126 return; 127 _isInit = true; 128 } 129 130 133 AnalyzedTag analyzeTag(Class cl) 134 { 135 AnalyzedTag tag = _tagAnalyzer.analyze(cl); 136 137 return tag; 138 } 139 140 143 public synchronized Taglib getTaglib(String prefix, 144 String uri, 145 String location) 146 throws JspParseException 147 { 148 if (JspParser.JSP_NS.equals(uri)) 149 return null; 150 else if (prefix != null && prefix.startsWith("jsp")) { 151 throw new JspParseException(L.l("tag prefix '{0}' may not start with 'jsp'.", 152 prefix)); 153 } 154 155 try { 156 init(); 157 } catch (IOException e) { 158 throw new JspParseException(e); 159 } 160 161 Taglib taglib = _taglibMap.get(uri); 162 163 if (taglib != null) 164 return taglib; 165 166 String mapLocation = _uriLocationMap.get(uri); 168 169 if (mapLocation != null) 170 location = mapLocation; 171 172 taglib = readTaglib(prefix, uri, location); 173 174 if (taglib != null) 175 _taglibMap.put(uri, taglib); 176 177 return taglib; 178 } 179 180 183 public synchronized Taglib getTaglibDir(String prefix, String dir) 184 throws JspParseException 185 { 186 try { 187 init(); 188 } catch (IOException e) { 189 throw new JspParseException(e); 190 } 191 192 Taglib taglib = _taglibDirMap.get(dir); 193 194 if (taglib != null) 195 return taglib; 196 197 TldTaglib tldTaglib = new TldTaglib(); 198 199 taglib = new Taglib(prefix, "urn:jsptagdir:" + dir, 200 tldTaglib, 201 _tagFileManager); 202 203 if (taglib != null) 204 _taglibDirMap.put(dir, taglib); 205 206 return taglib; 207 } 208 209 212 public void addTaglibFunctions(HashMap <String ,Method > functionMap, 213 String prefix, 214 String uri) 215 throws JspParseException 216 { 217 Taglib taglib = _taglibMap.get(uri); 218 219 if (taglib == null) 220 return; 221 222 ArrayList <TldFunction> functions = taglib.getFunctionList(); 223 224 for (int i = 0; i < functions.size(); i++) { 225 TldFunction function = functions.get(i); 226 227 String name = prefix + ":" + function.getName(); 228 229 functionMap.put(name, function.getMethod()); 230 } 231 } 232 233 236 private Taglib readTaglib(String prefix, String uri, String location) 237 throws JspParseException 238 { 239 try { 240 TldTaglib tldTaglib = _tldMap.get(uri); 241 242 if (tldTaglib != null) { 243 } 244 else { 245 String mapLocation = _uriLocationMap.get(uri); 246 247 if ((location == null || location.equals("")) && 248 (mapLocation == null || mapLocation.equals(""))) 249 return null; 250 251 tldTaglib = _tldManager.parseTld(uri, mapLocation, location); 252 253 _tldMap.put(uri, tldTaglib); 254 } 255 256 if (tldTaglib != null) { 257 if (tldTaglib.getConfigException() != null) 258 throw JspParseException.create(tldTaglib.getConfigException()); 259 260 return new Taglib(prefix, uri, tldTaglib, _tagFileManager); 261 } 262 else 263 return null; 264 } catch (JspParseException e) { 265 throw e; 266 } catch (Exception e) { 267 throw new JspParseException(e); 268 } 269 } 270 271 279 private Path findJar(String location) 280 { 281 Path path; 282 283 if (location.startsWith("file:")) 284 path = Vfs.lookup(location); 285 else if (location.startsWith("/")) 286 path = _resourceManager.resolvePath("." + location); 287 else 288 path = _resourceManager.resolvePath(location); 289 290 if (path.exists()) 291 return path; 292 293 DynamicClassLoader loader; 294 loader = (DynamicClassLoader) Thread.currentThread().getContextClassLoader(); 295 String classPath = loader.getClassPath(); 296 char sep = CauchoSystem.getPathSeparatorChar(); 297 298 int head = 0; 299 int tail = 0; 300 301 while ((tail = classPath.indexOf(sep, head)) >= 0) { 302 String sub = classPath.substring(head, tail); 303 304 path = Vfs.lookup(sub); 305 306 if (sub.endsWith(location) && path.exists()) 307 return path; 308 309 head = tail + 1; 310 } 311 312 if (classPath.length() <= head) 313 return null; 314 315 String sub = classPath.substring(head); 316 317 path = Vfs.lookup(sub); 318 319 if (sub.endsWith(location) && path.exists()) 320 return path; 321 else 322 return null; 323 } 324 } 325 | Popular Tags |