View Javadoc

1   /* ==========================================================================
2    * Copyright 2003-2004 Mevenide Team
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   * =========================================================================
16   */
17  package org.mevenide.grammar.impl;
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.util.Enumeration;
21  import java.util.Map;
22  import java.util.Properties;
23  import java.util.Set;
24  import java.util.TreeMap;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.mevenide.environment.ConfigUtils;
28  import org.mevenide.grammar.TagLib;
29  import org.mevenide.grammar.TagLibProvider;
30  
31  /***
32   * a TagLibProvider that will retrieve tagLibs from the
33   * MAVEN_LOCAL_REPO/plugins/dynatag.cache Will probably not be used on it's own
34   * but as part of a aggregating TagLibProvider.
35   * 
36   * @author Milos Kleint (ca206216@tiscali.cz)
37   */
38  public class MavenTagLibProvider implements TagLibProvider {
39  	
40  	private static Log logger = LogFactory.getLog(MavenTagLibProvider.class);
41  	
42  	private File cacheFile;
43  	private File pluginDir;
44  	
45  	private Map taglibs;
46  	
47  	private boolean cacheRead;
48  	
49  	private ClassLoader mavenClassLoader;
50  	
51  	/*** Creates a new instance of MavenTagLibProvider */
52  	public MavenTagLibProvider(File pluginCache) {
53  		taglibs = new TreeMap();
54  		cacheRead = false;
55  		cacheFile = pluginCache;
56  		pluginDir = cacheFile.getParentFile();
57  	}
58  	
59  	public MavenTagLibProvider() {
60  		taglibs = new TreeMap();
61  		cacheRead = false;
62  		cacheFile = new File(ConfigUtils.getDefaultLocationFinder().getMavenPluginsDir(), ".dynatag.cache");
63  		pluginDir = cacheFile.getParentFile();
64  	}
65  	
66  	public String[] getAvailableTags() {
67  		checkCache();
68  		Set keys = taglibs.keySet();
69  		String[] tags = new String[keys.size()];
70  		tags = (String[]) keys.toArray(tags);
71  		return tags;
72  	}
73  	
74  	public TagLib retrieveTagLib(String name) {
75  		checkCache();
76  		String pluginName = (String) taglibs.get(name);
77  		if (pluginName != null) {
78  			File pluginLoc = new File(pluginDir, pluginName);
79  			File jellyFileLoc = new File(pluginLoc, "plugin.jelly");
80  			if (jellyFileLoc.exists()) {
81  				try {
82  					return new JellyDefineTagLibImpl(jellyFileLoc, mavenClassLoader);
83  				} 
84  				catch (Exception exc) {
85  					// just ignore, something went wrong, no CC.
86  					logger.error("Cannot cached plugin.jelly.", exc);
87  				}
88  			} 
89  			else {
90  				logger.warn("cannot read plugin jelly file=" + jellyFileLoc.getAbsolutePath());
91  			}
92  		} 
93  		else {
94  			logger.warn("no content in taglibs=" + taglibs.size() + "name="
95  					+ name + "=");
96  		}
97  		return null;
98  	}
99  	
100 	private void checkCache() {
101 		if (!cacheRead) {
102 			try {
103 				readCache();
104 				cacheRead = true;
105 			} 
106 			catch (Exception exc) {
107 				logger.error("Cannot read dynatag cache.", exc);
108 			}
109 		}
110 	}
111 	
112 	private void readCache() throws Exception {
113 		taglibs.clear();
114 		Properties pros = new Properties();
115 		pros.load(new FileInputStream(cacheFile));
116 		Enumeration keys = pros.keys();
117 		while (keys.hasMoreElements()) {
118 			String key = (String) keys.nextElement();
119 			if (key.startsWith("http")) {
120 				//skip the taglibs defined as URL, no idea what to do with
121 				// them..
122 				continue;
123 			}
124 			taglibs.put(key, pros.getProperty(key));
125 			logger.debug("readCache:key=" + key + "=value=" + pros.getProperty(key));
126 		}
127 	}
128 
129 }