KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
43 import java.lang.reflect.Method JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Stores the entire information for a tag library.
50  */

51 public class TaglibManager {
52   static final L10N L = new L10N(TaglibManager.class);
53   private static final Logger JavaDoc 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 JavaDoc _tldDir;
62   private FileSetType _tldFileSet;
63
64   private TagFileManager _tagFileManager;
65
66   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _uriLocationMap
67     = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
68   private HashMap JavaDoc<String JavaDoc,TldTaglib> _tldMap = new HashMap JavaDoc<String JavaDoc,TldTaglib>();
69   private HashMap JavaDoc<String JavaDoc,Taglib> _taglibMap = new HashMap JavaDoc<String JavaDoc,Taglib>();
70   private HashMap JavaDoc<String JavaDoc,Taglib> _taglibDirMap = new HashMap JavaDoc<String JavaDoc,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 JavaDoc
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   /**
92    * Sets the webApp.
93    */

94   void setWebApp(WebApp webApp)
95   {
96     _webApp = webApp;
97   }
98
99   public void setTldDir(String JavaDoc tldDir)
100   {
101     _tldDir = tldDir;
102   }
103     
104
105   public void setTldFileSet(FileSetType fileSet)
106   {
107     _tldManager.setTldFileSet(fileSet);
108   }
109
110   /**
111    * Adds a URI to location map.
112    */

113   public void addLocationMap(String JavaDoc uri, String JavaDoc location)
114   {
115     _uriLocationMap.put(uri, location);
116   }
117
118   /**
119    * Loads all the .tld files in the WEB-INF and the META-INF for
120    * the entire classpath.
121    */

122   public synchronized void init()
123     throws JspParseException, IOException JavaDoc
124   {
125     if (_isInit)
126       return;
127     _isInit = true;
128   }
129
130   /**
131    * Analyze tag.
132    */

133   AnalyzedTag analyzeTag(Class JavaDoc cl)
134   {
135     AnalyzedTag tag = _tagAnalyzer.analyze(cl);
136
137     return tag;
138   }
139
140   /**
141    * Returns the taglib with the given prefix and uri.
142    */

143   public synchronized Taglib getTaglib(String JavaDoc prefix,
144                                        String JavaDoc uri,
145                                        String JavaDoc 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 JavaDoc e) {
158       throw new JspParseException(e);
159     }
160     
161     Taglib taglib = _taglibMap.get(uri);
162
163     if (taglib != null)
164       return taglib;
165
166     // jsp/188u
167
String JavaDoc 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   /**
181    * Returns the taglib with the given prefix and uri.
182    */

183   public synchronized Taglib getTaglibDir(String JavaDoc prefix, String JavaDoc dir)
184     throws JspParseException
185   {
186     try {
187       init();
188     } catch (IOException JavaDoc 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   /**
210    * Returns the taglib with the given prefix and uri.
211    */

212   public void addTaglibFunctions(HashMap JavaDoc<String JavaDoc,Method JavaDoc> functionMap,
213                  String JavaDoc prefix,
214                  String JavaDoc uri)
215     throws JspParseException
216   {
217     Taglib taglib = _taglibMap.get(uri);
218
219     if (taglib == null)
220       return;
221              
222     ArrayList JavaDoc<TldFunction> functions = taglib.getFunctionList();
223
224     for (int i = 0; i < functions.size(); i++) {
225       TldFunction function = functions.get(i);
226
227       String JavaDoc name = prefix + ":" + function.getName();
228
229       functionMap.put(name, function.getMethod());
230     }
231   }
232
233   /**
234    * Returns the taglib with the given prefix and uri.
235    */

236   private Taglib readTaglib(String JavaDoc prefix, String JavaDoc uri, String JavaDoc location)
237     throws JspParseException
238   {
239     try {
240       TldTaglib tldTaglib = _tldMap.get(uri);
241
242       if (tldTaglib != null) {
243       }
244       else {
245     String JavaDoc 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 JavaDoc e) {
267       throw new JspParseException(e);
268     }
269   }
270
271   /**
272    * Finds the path to the jar specified by the location.
273    *
274    * @param appDir the webApp directory
275    * @param location the tag-location specified in the web.xml
276    *
277    * @return the found jar or null
278    */

279   private Path findJar(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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