1 28 29 package com.caucho.jsp; 30 31 import com.caucho.loader.DynamicClassLoader; 32 import com.caucho.log.Log; 33 import com.caucho.server.connection.CauchoRequest; 34 import com.caucho.server.util.CauchoSystem; 35 import com.caucho.server.webapp.WebApp; 36 import com.caucho.util.Alarm; 37 import com.caucho.util.L10N; 38 import com.caucho.util.LruCache; 39 import com.caucho.vfs.MergePath; 40 import com.caucho.vfs.Path; 41 import com.caucho.xsl.AbstractStylesheetFactory; 42 import com.caucho.xsl.CauchoStylesheet; 43 import com.caucho.xsl.StyleScript; 44 import com.caucho.xsl.StylesheetImpl; 45 import com.caucho.xsl.Xsl; 46 47 import javax.servlet.ServletContext ; 48 import javax.servlet.ServletException ; 49 import javax.xml.transform.Templates ; 50 import java.lang.ref.SoftReference ; 51 import java.util.logging.Logger ; 52 53 class XslManager { 54 private static final Logger log = Log.open(XslManager.class); 55 static final L10N L = new L10N(XslManager.class); 56 57 private WebApp _webApp; 58 private Path _workPath; 59 private LruCache<String ,SoftReference <Templates >> _xslCache = 60 new LruCache<String ,SoftReference <Templates >>(256); 61 private boolean _strictXsl; 62 private long _lastUpdate; 63 64 public XslManager(ServletContext context) 65 { 66 _webApp = (WebApp) context; 67 68 _workPath = CauchoSystem.getWorkPath(); 69 } 70 71 public void setStrictXsl(boolean strictXsl) 72 { 73 _strictXsl = strictXsl; 74 } 75 76 public String getServletInfo() 77 { 78 return "Resin XTP"; 79 } 80 81 Templates get(String href, CauchoRequest req) 82 throws Exception 83 { 84 String servletPath = req.getPageServletPath(); 85 86 WebApp webApp = req.getWebApp(); 87 88 Path appDir = webApp.getAppDir(); 89 Path pwd = appDir.lookupNative(webApp.getRealPath(servletPath)); 90 pwd = pwd.getParent(); 91 92 String fullStyleSheet = pwd.toString() + "/" + href; 93 94 Templates stylesheet = null; 95 96 long now = Alarm.getCurrentTime(); 97 98 SoftReference <Templates > templateRef = _xslCache.get(fullStyleSheet); 99 100 if (templateRef != null) 101 stylesheet = templateRef.get(); 102 103 if (stylesheet instanceof StylesheetImpl && 104 ! ((StylesheetImpl) stylesheet).isModified()) 105 return stylesheet; 106 107 _lastUpdate = now; 108 stylesheet = getStylesheet(req, href); 109 110 if (stylesheet == null) 111 throw new ServletException (L.l("can't find stylesheet `{0}'", href)); 112 113 _xslCache.put(fullStyleSheet, new SoftReference <Templates >(stylesheet)); 114 115 return stylesheet; 116 } 117 118 121 Templates getStylesheet(CauchoRequest req, String href) 122 throws Exception 123 { 124 String servletPath = req.getPageServletPath(); 125 126 WebApp webApp = req.getWebApp(); 127 Path appDir = webApp.getAppDir(); 128 Path pwd = appDir.lookupNative(webApp.getRealPath(servletPath)); 129 pwd = pwd.getParent(); 130 131 DynamicClassLoader loader; 132 loader = (DynamicClassLoader) webApp.getClassLoader(); 133 134 MergePath stylePath = new MergePath(); 135 stylePath.addMergePath(pwd); 136 stylePath.addMergePath(appDir); 137 138 String resourcePath = loader.getResourcePathSpecificFirst(); 139 stylePath.addClassPath(resourcePath); 140 141 Path hrefPath = stylePath.lookup(href); 142 143 if (hrefPath.canRead()) { 144 DynamicClassLoader owningLoader = getStylesheetLoader(href, hrefPath); 145 146 if (owningLoader != null) { 147 loader = owningLoader; 148 149 stylePath = new MergePath(); 150 stylePath.addMergePath(pwd); 151 stylePath.addMergePath(appDir); 152 resourcePath = loader.getResourcePathSpecificFirst(); 153 stylePath.addClassPath(resourcePath); 154 } 155 } 156 157 Thread thread = Thread.currentThread(); 158 ClassLoader oldLoader = thread.getContextClassLoader(); 159 try { 160 thread.setContextClassLoader(loader); 161 162 CauchoStylesheet xsl; 163 164 AbstractStylesheetFactory factory; 165 166 if (_strictXsl) 167 factory = new Xsl(); 168 else 169 factory = new StyleScript(); 170 171 factory.setStylePath(stylePath); 172 factory.setClassLoader(loader); 173 175 String className = ""; 176 177 if (pwd.lookup(href).canRead()) { 178 int p = req.getServletPath().lastIndexOf('/'); 179 if (p >= 0) 180 className += req.getServletPath().substring(0, p); 181 } 182 186 187 className += "/" + href; 188 189 factory.setClassName(className); 190 191 return factory.newTemplates(href); 192 } finally { 193 thread.setContextClassLoader(oldLoader); 194 } 195 } 196 197 private DynamicClassLoader getStylesheetLoader(String href, Path sourcePath) 198 { 199 DynamicClassLoader owningLoader = null; 200 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 201 202 for (; loader != null; loader = loader.getParent()) { 203 if (! (loader instanceof DynamicClassLoader)) 204 continue; 205 206 DynamicClassLoader dynLoader = (DynamicClassLoader) loader; 207 208 MergePath mp = new MergePath(); 209 String resourcePath = dynLoader.getResourcePathSpecificFirst(); 210 mp.addClassPath(resourcePath); 211 212 Path loaderPath = mp.lookup(href); 213 214 if (loaderPath.getNativePath().equals(sourcePath.getNativePath())) 215 owningLoader = dynLoader; 216 } 217 218 return owningLoader; 219 } 220 } 221 222 | Popular Tags |