| 1 19 package org.java.plugin.boot; 20 21 import java.io.File ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.StringTokenizer ; 29 30 import javax.xml.parsers.SAXParserFactory ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.java.plugin.PluginManager.PluginLocation; 35 import org.java.plugin.standard.StandardPluginLocation; 36 import org.java.plugin.util.ExtendedProperties; 37 import org.xml.sax.Attributes ; 38 import org.xml.sax.helpers.DefaultHandler ; 39 40 81 public class DefaultPluginsCollector implements PluginsCollector { 82 protected static final String PARAM_PLUGINS_REPOSITORIES = 83 "org.java.plugin.boot.pluginsRepositories"; protected static final String PARAM_PLUGINS_LOCATIONS_DESCRIPTORS = 85 "org.java.plugin.boot.pluginsLocationsDescriptors"; 87 protected Log log = LogFactory.getLog(this.getClass()); 88 private List repositories; 89 private List descriptors; 90 91 95 public void configure(ExtendedProperties config) throws Exception { 96 repositories = new LinkedList (); 97 for (StringTokenizer st = new StringTokenizer ( 98 config.getProperty(PARAM_PLUGINS_REPOSITORIES, 99 '.' + File.separator + "plugins"), ",", false); st.hasMoreTokens();) { String token = st.nextToken().trim(); 102 if (token.length() == 0) { 103 continue; 104 } 105 repositories.add(new File (token).getCanonicalFile()); 106 } 107 log.debug("found " + repositories.size() + " local plug-ins repositories"); descriptors = new LinkedList (); 110 for (StringTokenizer st = new StringTokenizer ( 111 config.getProperty(PARAM_PLUGINS_LOCATIONS_DESCRIPTORS, ""), ",", false); st.hasMoreTokens();) { String token = st.nextToken().trim(); 114 if (token.length() == 0) { 115 continue; 116 } 117 descriptors.add(new URL (token)); 118 } 119 log.debug("found " + descriptors.size() + " plug-ins locations descriptors"); } 122 123 126 public Collection collectPluginLocations() { 127 List result = new LinkedList (); 128 for (Iterator it = repositories.iterator(); it.hasNext();) { 129 File file = (File ) it.next(); 130 if (file.isDirectory()) { 131 processFolder(file, result); 132 } else if (file.isFile()) { 133 processFile(file, result); 134 } else { 135 log.warn("unknown repository location " + file); } 137 } 138 for (Iterator it = descriptors.iterator(); it.hasNext();) { 139 processDescriptor((URL ) it.next(), result); 140 } 141 return result; 142 } 143 144 protected void processFolder(final File folder, final List result) { 145 log.debug("processing folder - " + folder); try { 147 PluginLocation pluginLocation = 148 StandardPluginLocation.create(folder); 149 if (pluginLocation != null) { 150 result.add(pluginLocation); 151 return; 152 } 153 } catch (MalformedURLException mue) { 154 log.warn("failed collecting plug-in folder " + folder + ", ignoring it", mue); return; 157 } 158 File [] files = folder.listFiles(); 159 for (int i = 0; i < files.length; i++) { 160 File file = files[i]; 161 if (file.isDirectory()) { 162 processFolder(file, result); 163 } else if (file.isFile()) { 164 processFile(file, result); 165 } 166 } 167 } 168 169 protected void processFile(final File file, final List result) { 170 log.debug("processing file - " + file); try { 172 PluginLocation pluginLocation = StandardPluginLocation.create(file); 173 if (pluginLocation != null) { 174 result.add(pluginLocation); 175 } 176 } catch (MalformedURLException mue) { 177 log.warn("failed collecting plug-in file " + file + ", ignoring it", mue); } 180 } 181 182 private void processDescriptor(final URL url, final List result) { 183 log.debug("processing plug-ins locations descriptor, URL=" + url); try { 185 SAXParserFactory.newInstance().newSAXParser().parse( 186 url.toExternalForm(), 187 new LocationsDescriptorHandler(result)); 188 } catch (Exception e) { 189 log.warn("failed processing plug-ins locations descriptor, URL=" + url, e); 191 } 192 } 193 194 private final class LocationsDescriptorHandler extends DefaultHandler { 195 private final List resultData; 196 197 LocationsDescriptorHandler(final List result) { 198 resultData = result; 199 } 200 201 206 public void startElement(final String uri, final String localName, 207 final String qName, final Attributes attributes) { 208 if (!"plugin".equals(qName)) { return; 210 } 211 String manifest = attributes.getValue("manifest"); if (manifest == null) { 213 log.warn("manifest attribute missing"); return; 215 } 216 URL manifestUrl; 217 try { 218 manifestUrl = new URL (manifest); 219 } catch (MalformedURLException mue) { 220 log.warn("invalid manifest URL - " + manifest, mue); return; 222 } 223 String context = attributes.getValue("context"); if (context == null) { 225 log.warn("context attribute missing"); return; 227 } 228 URL contextUrl; 229 try { 230 contextUrl = new URL (context); 231 } catch (MalformedURLException mue) { 232 log.warn("invalid context URL - " + context, mue); return; 234 } 235 resultData.add(new StandardPluginLocation(contextUrl, manifestUrl)); 236 log.debug("got plug-in location from descriptor, manifestUrl=" + manifestUrl + ", contextURL=" + contextUrl); } 239 } 240 } 241 | Popular Tags |