📄 pluginmanager.java
字号:
while( resources.hasMoreElements() ) { URL resource = (URL) resources.nextElement(); try { log.debug( "Processing XML: " + resource ); Document doc = builder.build( resource ); List plugins = XPath.selectNodes( doc, "/modules/plugin"); for( Iterator i = plugins.iterator(); i.hasNext(); ) { Element pluginEl = (Element) i.next(); String className = pluginEl.getAttributeValue("class"); WikiPluginInfo pluginInfo = WikiPluginInfo.newInstance( className, pluginEl ); if( pluginInfo != null ) { registerPlugin( pluginInfo ); } } } catch( java.io.IOException e ) { log.error( "Couldn't load " + PLUGIN_RESOURCE_LOCATION + " resources: " + resource, e ); } catch( JDOMException e ) { log.error( "Error parsing XML for plugin: "+PLUGIN_RESOURCE_LOCATION ); } } } catch( java.io.IOException e ) { log.error( "Couldn't load all " + PLUGIN_RESOURCE_LOCATION + " resources", e ); } } /** * Contains information about a bunch of plugins. * * @author Kees Kuip * @author Janne Jalkanen * * @since */ // FIXME: This class needs a better interface to return all sorts of possible // information from the plugin XML. In fact, it probably should have // some sort of a superclass system. public static final class WikiPluginInfo extends WikiModuleInfo { private String m_className; private String m_alias; private Class m_clazz; private boolean m_initialized = false; /** * Creates a new plugin info object which can be used to access a plugin. * * @param className Either a fully qualified class name, or a "short" name which is then * checked against the internal list of plugin packages. * @param el A JDOM Element containing the information about this class. * @return A WikiPluginInfo object. */ protected static WikiPluginInfo newInstance( String className, Element el ) { if( className == null || className.length() == 0 ) return null; WikiPluginInfo info = new WikiPluginInfo( className ); info.initializeFromXML( el ); return info; } /** * Initializes a plugin, if it has not yet been initialized. * * @param engine The WikiEngine */ protected void initializePlugin( WikiEngine engine ) { if( !m_initialized ) { // This makes sure we only try once per class, even if init fails. m_initialized = true; try { WikiPlugin p = newPluginInstance(); if( p instanceof InitializablePlugin ) { ((InitializablePlugin)p).initialize( engine ); } } catch( Exception e ) { log.info( "Cannot initialize plugin "+m_className, e ); } } } /** * {@inheritDoc} */ @Override protected void initializeFromXML( Element el ) { super.initializeFromXML( el ); m_alias = el.getChildText("alias"); } /** * Create a new WikiPluginInfo based on the Class information. * * @param clazz The class to check * @return A WikiPluginInfo instance */ protected static WikiPluginInfo newInstance( Class clazz ) { WikiPluginInfo info = new WikiPluginInfo( clazz.getName() ); return info; } private WikiPluginInfo( String className ) { super(className); setClassName( className ); } private void setClassName( String fullClassName ) { m_name = ClassUtils.getShortClassName( fullClassName ); m_className = fullClassName; } /** * Returns the full class name of this object. * @return The full class name of the object. */ public String getClassName() { return m_className; } /** * Returns the alias name for this object. * @return An alias name for the plugin. */ public String getAlias() { return m_alias; } /** * Creates a new plugin instance. * * @return A new plugin. * @throws ClassNotFoundException If the class declared was not found. * @throws InstantiationException If the class cannot be instantiated- * @throws IllegalAccessException If the class cannot be accessed. */ public WikiPlugin newPluginInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException { if( m_clazz == null ) { m_clazz = Class.forName(m_className); } return (WikiPlugin) m_clazz.newInstance(); } /** * Returns a text for IncludeResources. * * @param type Either "script" or "stylesheet" * @return Text, or an empty string, if there is nothing to be included. */ public String getIncludeText(String type) { try { if( type.equals("script") ) { return getScriptText(); } else if( type.equals("stylesheet") ) { return getStylesheetText(); } } catch( Exception ex ) { // We want to fail gracefully here return ex.getMessage(); } return null; } private String getScriptText() throws IOException { if( m_scriptText != null ) { return m_scriptText; } if( m_scriptLocation == null ) { return ""; } try { m_scriptText = getTextResource(m_scriptLocation); } catch( IOException ex ) { // Only throw this exception once! m_scriptText = ""; throw ex; } return m_scriptText; } private String getStylesheetText() throws IOException { if( m_stylesheetText != null ) { return m_stylesheetText; } if( m_stylesheetLocation == null ) { return ""; } try { m_stylesheetText = getTextResource(m_stylesheetLocation); } catch( IOException ex ) { // Only throw this exception once! m_stylesheetText = ""; throw ex; } return m_stylesheetText; } /** * Returns a string suitable for debugging. Don't assume that the format * would stay the same. * * @return Something human-readable */ public String toString() { return "Plugin :[name=" + m_name + "][className=" + m_className + "]"; } } // WikiPluginClass /** * {@inheritDoc} */ public Collection modules() { TreeSet<WikiModuleInfo> ls = new TreeSet<WikiModuleInfo>(); for( Iterator i = m_pluginClassMap.values().iterator(); i.hasNext(); ) { WikiModuleInfo wmi = (WikiModuleInfo)i.next(); if( !ls.contains(wmi) ) ls.add(wmi); } return ls; } /** * Executes parse stage, unless plugins are disabled. * * @param content The content item. * @param context A WikiContext * @throws PluginException If something goes wrong. */ // FIXME: This method needs to be reintegrated with execute() above, since they // share plenty of code. public void executeParse(PluginContent content, WikiContext context) throws PluginException { if( !m_pluginsEnabled ) return; ResourceBundle rb = context.getBundle(WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE); Object[] args = { content.getPluginName() }; Map params = content.getParameters(); try { WikiPlugin plugin; WikiPluginInfo pluginInfo = m_pluginClassMap.get( content.getPluginName() ); if(pluginInfo == null) { pluginInfo = WikiPluginInfo.newInstance(findPluginClass( content.getPluginName() )); registerPlugin(pluginInfo); } if( !checkCompatibility(pluginInfo) ) { String msg = "Plugin '"+pluginInfo.getName()+"' not compatible with this version of JSPWiki"; log.info(msg); return; } plugin = pluginInfo.newPluginInstance(); if( plugin instanceof ParserStagePlugin ) { ((ParserStagePlugin)plugin).executeParser( content, context, params ); } } catch( InstantiationException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.cannotinstantiate" ), args), e ); } catch( IllegalAccessException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.notallowed" ), args), e ); } catch( ClassNotFoundException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.couldnotfind" ), args) ); } catch( ClassCastException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.notawikiplugin" ), args), e ); } catch( Exception e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.instantationfailed" ), args), e ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -