001 package com.khubla.pragmatach.framework.plugin; 002 003 import java.io.InputStream; 004 import java.net.JarURLConnection; 005 import java.net.URL; 006 import java.net.URLClassLoader; 007 import java.security.AccessController; 008 import java.security.PrivilegedExceptionAction; 009 import java.util.Properties; 010 import java.util.jar.Manifest; 011 012 import javax.servlet.ServletContext; 013 014 import com.khubla.pragmatach.framework.api.Plugin; 015 import com.khubla.pragmatach.framework.api.PragmatachException; 016 import com.khubla.pragmatach.framework.resourceloader.DefaultResourceLoaderImpl; 017 018 /** 019 * @author tome 020 */ 021 public class PluginDescriptor { 022 /** 023 * name 024 */ 025 public static final String ACTIVATOR = "activator"; 026 /** 027 * the jar 028 */ 029 private final URL url; 030 /** 031 * the properties 032 */ 033 private final Properties properties; 034 /** 035 * plugin 036 */ 037 private final Plugin plugin; 038 039 public PluginDescriptor(URL url, InputStream inputStream, 040 ServletContext servletContext) throws PragmatachException { 041 try { 042 this.url = url; 043 properties = new Properties(); 044 properties.load(inputStream); 045 plugin = findPlugin(servletContext); 046 } catch (final Exception e) { 047 throw new PragmatachException(e); 048 } 049 } 050 051 private Plugin findPlugin(ServletContext servletContext) 052 throws PragmatachException { 053 try { 054 final String activatorClassName = properties.getProperty(ACTIVATOR); 055 if (null != activatorClassName) { 056 final Class<?> clazz = Class.forName(activatorClassName); 057 if (null != clazz) { 058 final Plugin ret = (Plugin) clazz.newInstance(); 059 ret.setPluginContext(new PluginContextImpl( 060 new DefaultResourceLoaderImpl(servletContext))); 061 return ret; 062 } 063 } 064 return null; 065 } catch (final Exception e) { 066 throw new PragmatachException(e); 067 } 068 } 069 070 /** 071 * get the plugin jar manifest 072 */ 073 public Manifest getManifest() throws PragmatachException { 074 try { 075 final JarURLConnection jarURLConnection = (JarURLConnection) url 076 .openConnection(); 077 if (null != jarURLConnection) { 078 return jarURLConnection.getManifest(); 079 } 080 return null; 081 } catch (final Exception e) { 082 throw new PragmatachException("Exception in getManifest", e); 083 } 084 } 085 086 public String getName() { 087 return plugin.getName(); 088 } 089 090 public Plugin getPlugin() { 091 return plugin; 092 } 093 094 public Properties getProperties() { 095 return properties; 096 } 097 098 /** 099 * get a resource from this plugin's jar 100 */ 101 public InputStream getResource(String name) throws PragmatachException { 102 try { 103 ClassLoader classLoader = null; 104 classLoader = AccessController 105 .doPrivileged(new PrivilegedExceptionAction<ClassLoader>() { 106 107 @Override 108 public ClassLoader run() throws Exception { 109 return new URLClassLoader(new URL[] { url }); 110 } 111 }); 112 if (null != classLoader) { 113 return classLoader.getResourceAsStream(name); 114 } else { 115 return null; 116 } 117 } catch (final Exception e) { 118 throw new PragmatachException("Exception in getResource", e); 119 } 120 } 121 122 public URL getUrl() { 123 return url; 124 } 125 }