KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > XslManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
48 import javax.servlet.ServletException JavaDoc;
49 import javax.xml.transform.Templates JavaDoc;
50 import java.lang.ref.SoftReference JavaDoc;
51 import java.util.logging.Logger JavaDoc;
52
53 class XslManager {
54   private static final Logger JavaDoc 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 JavaDoc,SoftReference JavaDoc<Templates JavaDoc>> _xslCache =
60     new LruCache<String JavaDoc,SoftReference JavaDoc<Templates JavaDoc>>(256);
61   private boolean _strictXsl;
62   private long _lastUpdate;
63
64   public XslManager(ServletContext JavaDoc 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 JavaDoc getServletInfo()
77   {
78     return "Resin XTP";
79   }
80
81   Templates JavaDoc get(String JavaDoc href, CauchoRequest req)
82     throws Exception JavaDoc
83   {
84     String JavaDoc 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 JavaDoc fullStyleSheet = pwd.toString() + "/" + href;
93
94     Templates JavaDoc stylesheet = null;
95
96     long now = Alarm.getCurrentTime();
97
98     SoftReference JavaDoc<Templates JavaDoc> 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 JavaDoc(L.l("can't find stylesheet `{0}'", href));
112
113     _xslCache.put(fullStyleSheet, new SoftReference JavaDoc<Templates JavaDoc>(stylesheet));
114
115     return stylesheet;
116   }
117
118   /**
119    * Returns the stylesheet given by the references.
120    */

121   Templates JavaDoc getStylesheet(CauchoRequest req, String JavaDoc href)
122     throws Exception JavaDoc
123   {
124     String JavaDoc 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 JavaDoc 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 JavaDoc thread = Thread.currentThread();
158     ClassLoader JavaDoc 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       // factory.setWorkPath(_workPath);
174

175       String JavaDoc 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       /*
183     else if (href.startsWith("/"))
184     href = href.substring(1);
185       */

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 JavaDoc href, Path sourcePath)
198   {
199     DynamicClassLoader owningLoader = null;
200     ClassLoader JavaDoc 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 JavaDoc 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