001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019 020package org.apache.wiki.plugin; 021 022import org.apache.commons.lang3.ClassUtils; 023import org.apache.commons.lang3.StringUtils; 024import org.apache.logging.log4j.LogManager; 025import org.apache.logging.log4j.Logger; 026import org.apache.oro.text.regex.MalformedPatternException; 027import org.apache.oro.text.regex.MatchResult; 028import org.apache.oro.text.regex.Pattern; 029import org.apache.oro.text.regex.PatternCompiler; 030import org.apache.oro.text.regex.PatternMatcher; 031import org.apache.oro.text.regex.Perl5Compiler; 032import org.apache.oro.text.regex.Perl5Matcher; 033import org.apache.wiki.InternalWikiException; 034import org.apache.wiki.ajax.WikiAjaxDispatcherServlet; 035import org.apache.wiki.ajax.WikiAjaxServlet; 036import org.apache.wiki.api.core.Context; 037import org.apache.wiki.api.core.Engine; 038import org.apache.wiki.api.exceptions.PluginException; 039import org.apache.wiki.api.plugin.InitializablePlugin; 040import org.apache.wiki.api.plugin.Plugin; 041import org.apache.wiki.modules.BaseModuleManager; 042import org.apache.wiki.modules.WikiModuleInfo; 043import org.apache.wiki.preferences.Preferences; 044import org.apache.wiki.util.ClassUtil; 045import org.apache.wiki.util.FileUtil; 046import org.apache.wiki.util.TextUtil; 047import org.apache.wiki.util.XHTML; 048import org.apache.wiki.util.XhtmlUtil; 049import org.apache.wiki.util.XmlUtil; 050import org.jdom2.Element; 051 052import javax.servlet.http.HttpServlet; 053import java.io.IOException; 054import java.io.PrintWriter; 055import java.io.StreamTokenizer; 056import java.io.StringReader; 057import java.io.StringWriter; 058import java.text.MessageFormat; 059import java.util.ArrayList; 060import java.util.Collection; 061import java.util.HashMap; 062import java.util.List; 063import java.util.Map; 064import java.util.NoSuchElementException; 065import java.util.Properties; 066import java.util.ResourceBundle; 067import java.util.StringTokenizer; 068 069/** 070 * Manages plugin classes. There exists a single instance of PluginManager 071 * per each instance of Engine, that is, each JSPWiki instance. 072 * <P> 073 * A plugin is defined to have three parts: 074 * <OL> 075 * <li>The plugin class 076 * <li>The plugin parameters 077 * <li>The plugin body 078 * </ol> 079 * 080 * For example, in the following line of code: 081 * <pre> 082 * [{INSERT org.apache.wiki.plugin.FunnyPlugin foo='bar' 083 * blob='goo' 084 * 085 * abcdefghijklmnopqrstuvw 086 * 01234567890}] 087 * </pre> 088 * 089 * The plugin class is "org.apache.wiki.plugin.FunnyPlugin", the 090 * parameters are "foo" and "blob" (having values "bar" and "goo", 091 * respectively), and the plugin body is then 092 * "abcdefghijklmnopqrstuvw\n01234567890". The plugin body is 093 * accessible via a special parameter called "_body". 094 * <p> 095 * If the parameter "debug" is set to "true" for the plugin, 096 * JSPWiki will output debugging information directly to the page if there 097 * is an exception. 098 * <P> 099 * The class name can be shortened, and marked without the package. 100 * For example, "FunnyPlugin" would be expanded to 101 * "org.apache.wiki.plugin.FunnyPlugin" automatically. It is also 102 * possible to define other packages, by setting the 103 * "jspwiki.plugin.searchPath" property. See the included 104 * jspwiki.properties file for examples. 105 * <P> 106 * Even though the nominal way of writing the plugin is 107 * <pre> 108 * [{INSERT pluginclass WHERE param1=value1...}], 109 * </pre> 110 * it is possible to shorten this quite a lot, by skipping the 111 * INSERT, and WHERE words, and dropping the package name. For 112 * example: 113 * 114 * <pre> 115 * [{INSERT org.apache.wiki.plugin.Counter WHERE name='foo'}] 116 * </pre> 117 * 118 * is the same as 119 * <pre> 120 * [{Counter name='foo'}] 121 * </pre> 122 * <h3>Plugin property files</h3> 123 * <p> 124 * Since 2.3.25 you can also define a generic plugin XML properties file per 125 * each JAR file. 126 * <pre> 127 * <modules> 128 * <plugin class="org.apache.wiki.foo.TestPlugin"> 129 * <author>Janne Jalkanen</author> 130 * <script>foo.js</script> 131 * <stylesheet>foo.css</stylesheet> 132 * <alias>code</alias> 133 * </plugin> 134 * <plugin class="org.apache.wiki.foo.TestPlugin2"> 135 * <author>Janne Jalkanen</author> 136 * </plugin> 137 * </modules> 138 * </pre> 139 * <h3>Plugin lifecycle</h3> 140 * 141 * <p>Plugin can implement multiple interfaces to let JSPWiki know at which stages they should 142 * be invoked: 143 * <ul> 144 * <li>InitializablePlugin: If your plugin implements this interface, the initialize()-method is 145 * called once for this class 146 * before any actual execute() methods are called. You should use the initialize() for e.g. 147 * precalculating things. But notice that this method is really called only once during the 148 * entire Engine lifetime. The InitializablePlugin is available from 2.5.30 onwards.</li> 149 * <li>ParserStagePlugin: If you implement this interface, the executeParse() method is called 150 * when JSPWiki is forming the DOM tree. You will receive an incomplete DOM tree, as well 151 * as the regular parameters. However, since JSPWiki caches the DOM tree to speed up later 152 * places, which means that whatever this method returns would be irrelevant. You can do some DOM 153 * tree manipulation, though. The ParserStagePlugin is available from 2.5.30 onwards.</li> 154 * <li>Plugin: The regular kind of plugin which is executed at every rendering stage. Each 155 * new page load is guaranteed to invoke the plugin, unlike with the ParserStagePlugins.</li> 156 * </ul> 157 * 158 * @since 1.6.1 159 */ 160public class DefaultPluginManager extends BaseModuleManager implements PluginManager { 161 162 private static final String PLUGIN_INSERT_PATTERN = "\\{?(INSERT)?\\s*([\\w\\._]+)[ \\t]*(WHERE)?[ \\t]*"; 163 private static final Logger LOG = LogManager.getLogger( DefaultPluginManager.class ); 164 private static final String DEFAULT_FORMS_PACKAGE = "org.apache.wiki.forms"; 165 166 private final ArrayList< String > m_searchPath = new ArrayList<>(); 167 private final ArrayList< String > m_externalJars = new ArrayList<>(); 168 private final Pattern m_pluginPattern; 169 private boolean m_pluginsEnabled = true; 170 171 /** Keeps a list of all known plugin classes. */ 172 private final Map< String, WikiPluginInfo > m_pluginClassMap = new HashMap<>(); 173 174 /** 175 * Create a new PluginManager. 176 * 177 * @param engine Engine which owns this manager. 178 * @param props Contents of a "jspwiki.properties" file. 179 */ 180 public DefaultPluginManager( final Engine engine, final Properties props ) { 181 super( engine ); 182 final String packageNames = props.getProperty( Engine.PROP_SEARCHPATH ); 183 if ( packageNames != null ) { 184 final StringTokenizer tok = new StringTokenizer( packageNames, "," ); 185 while( tok.hasMoreTokens() ) { 186 m_searchPath.add( tok.nextToken().trim() ); 187 } 188 } 189 190 final String externalJars = props.getProperty( PROP_EXTERNALJARS ); 191 if( externalJars != null ) { 192 final StringTokenizer tok = new StringTokenizer( externalJars, "," ); 193 while( tok.hasMoreTokens() ) { 194 m_externalJars.add( tok.nextToken().trim() ); 195 } 196 } 197 198 registerPlugins(); 199 200 // The default packages are always added. 201 m_searchPath.add( DEFAULT_PACKAGE ); 202 m_searchPath.add( DEFAULT_FORMS_PACKAGE ); 203 204 final PatternCompiler compiler = new Perl5Compiler(); 205 try { 206 m_pluginPattern = compiler.compile( PLUGIN_INSERT_PATTERN ); 207 } catch( final MalformedPatternException e ) { 208 LOG.fatal( "Internal error: someone messed with pluginmanager patterns.", e ); 209 throw new InternalWikiException( "PluginManager patterns are broken" , e ); 210 } 211 } 212 213 /** {@inheritDoc} */ 214 @Override 215 public void enablePlugins( final boolean enabled ) { 216 m_pluginsEnabled = enabled; 217 } 218 219 /** {@inheritDoc} */ 220 @Override 221 public boolean pluginsEnabled() { 222 return m_pluginsEnabled; 223 } 224 225 /** {@inheritDoc} */ 226 @Override 227 public Pattern getPluginPattern() { 228 return m_pluginPattern; 229 } 230 231 /** 232 * Attempts to locate a plugin class from the class path set in the property file. 233 * 234 * @param classname Either a fully fledged class name, or just the name of the file (that is, "org.apache.wiki.plugin.Counter" or just plain "Counter"). 235 * @return A found class. 236 * @throws ClassNotFoundException if no such class exists. 237 */ 238 private Class< ? > findPluginClass( final String classname ) throws ClassNotFoundException { 239 final Class< ? > clazz = ClassUtil.findClass( m_searchPath, m_externalJars, classname ); 240 if( !Plugin.class.isAssignableFrom( clazz ) || !isInSearchPath( clazz ) ) { 241 // Reject with the same error as an unknown class, so that page markup cannot be used to 242 // load or instantiate arbitrary classpath classes, nor to probe for their presence. 243 throw new ClassNotFoundException( "Class '" + classname + "' not found in search path!" ); 244 } 245 return clazz; 246 } 247 248 /** 249 * Checks whether the package of the given class is one of the packages on the plugin search path. 250 * 251 * @param clazz The class to check. 252 * @return true, if the class' package is on the plugin search path. 253 */ 254 private boolean isInSearchPath( final Class< ? > clazz ) { 255 final Package pkg = clazz.getPackage(); 256 return pkg != null && m_searchPath.contains( pkg.getName() ); 257 } 258 259 /** Outputs an HTML-formatted version of a stack trace. */ 260 private String stackTrace( final Map<String,String> params, final Throwable t ) { 261 final Element div = XhtmlUtil.element( XHTML.div, "Plugin execution failed, stack trace follows:" ); 262 div.setAttribute( XHTML.ATTR_class, "debug" ); 263 264 final StringWriter out = new StringWriter(); 265 t.printStackTrace( new PrintWriter( out ) ); 266 div.addContent( XhtmlUtil.element( XHTML.pre, out.toString() ) ); 267 div.addContent( XhtmlUtil.element( XHTML.b, "Parameters to the plugin" ) ); 268 269 final Element list = XhtmlUtil.element( XHTML.ul ); 270 for( final Map.Entry< String, String > e : params.entrySet() ) { 271 final String key = e.getKey(); 272 list.addContent( XhtmlUtil.element( XHTML.li, key + "'='" + e.getValue() ) ); 273 } 274 div.addContent( list ); 275 return XhtmlUtil.serialize( div ); 276 } 277 278 /** {@inheritDoc} */ 279 @Override 280 public String execute( final Context context, final String classname, final Map< String, String > params ) throws PluginException { 281 if( !m_pluginsEnabled ) { 282 return ""; 283 } 284 285 final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE ); 286 //see JSPWIKI-75 287 final boolean debug = TextUtil.isPositive( params.get( PARAM_DEBUG ) ) && context.hasAdminPermissions(); 288 289 try { 290 // Create... 291 final Plugin plugin = newWikiPlugin( classname, rb ); 292 if( plugin == null ) { 293 return "Plugin '" + classname + "' not compatible with this version of JSPWiki"; 294 } 295 296 // ...and launch. 297 try { 298 return plugin.execute( context, params ); 299 } catch( final PluginException e ) { 300 LOG.warn(e.getMessage(), e); 301 if( debug ) { 302 return stackTrace( params, e ); 303 } 304 305 // Just pass this exception onward. 306 throw ( PluginException )e.fillInStackTrace(); 307 } catch( final Throwable t ) { 308 309 // But all others get captured here. 310 LOG.warn( "Plugin failed while executing:", t ); 311 if( debug ) { 312 return stackTrace( params, t ); 313 } 314 315 throw new PluginException( rb.getString( "plugin.error.failed" ), t ); 316 } 317 318 } catch( final ClassCastException e ) { 319 throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.notawikiplugin" ), classname ), e ); 320 } 321 } 322 323 /** {@inheritDoc} */ 324 @Override 325 public Map< String, String > parseArgs( final String argstring ) throws IOException { 326 final Map< String, String > arglist = new HashMap<>(); 327 // Protection against funny users. 328 if( argstring == null ) { 329 return arglist; 330 } 331 332 arglist.put( PARAM_CMDLINE, argstring ); 333 final StringReader in = new StringReader( argstring ); 334 final StreamTokenizer tok = new StreamTokenizer( in ); 335 tok.eolIsSignificant( true ); 336 337 String param = null; 338 String value; 339 boolean potentialEmptyLine = false; 340 boolean quit = false; 341 while( !quit ) { 342 final String s; 343 final int type = tok.nextToken(); 344 345 switch( type ) { 346 case StreamTokenizer.TT_EOF: 347 quit = true; 348 s = null; 349 break; 350 351 case StreamTokenizer.TT_WORD: 352 s = tok.sval; 353 potentialEmptyLine = false; 354 break; 355 356 case StreamTokenizer.TT_EOL: 357 quit = potentialEmptyLine; 358 potentialEmptyLine = true; 359 s = null; 360 break; 361 362 case StreamTokenizer.TT_NUMBER: 363 s = Integer.toString( ( int )tok.nval ); 364 potentialEmptyLine = false; 365 break; 366 367 case '\'': 368 s = tok.sval; 369 break; 370 371 default: 372 s = null; 373 } 374 375 // Assume that alternate words on the line are parameter and value, respectively. 376 if( s != null ) { 377 if( param == null ) { 378 param = s; 379 } else { 380 value = s; 381 arglist.put( param, value ); 382 param = null; 383 } 384 } 385 } 386 387 // Now, we'll check the body. 388 if( potentialEmptyLine ) { 389 final StringWriter out = new StringWriter(); 390 FileUtil.copyContents( in, out ); 391 final String bodyContent = out.toString(); 392 if( bodyContent != null ) { 393 arglist.put( PARAM_BODY, bodyContent ); 394 } 395 } 396 397 return arglist; 398 } 399 400 /** {@inheritDoc} */ 401 @Override 402 public String execute( final Context context, final String commandline ) throws PluginException { 403 if( !m_pluginsEnabled ) { 404 return ""; 405 } 406 407 final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE ); 408 final PatternMatcher matcher = new Perl5Matcher(); 409 410 try { 411 if( matcher.contains( commandline, m_pluginPattern ) ) { 412 final MatchResult res = matcher.getMatch(); 413 final String plugin = res.group( 2 ); 414 final int endIndex = commandline.length() - ( commandline.charAt( commandline.length() - 1 ) == '}' ? 1 : 0 ); 415 final String args = commandline.substring( res.endOffset( 0 ), endIndex ); 416 final Map< String, String > arglist = parseArgs( args ); 417 return execute( context, plugin, arglist ); 418 } 419 } catch( final NoSuchElementException e ) { 420 final String msg = "Missing parameter in plugin definition: " + commandline; 421 LOG.warn( msg, e ); 422 throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.missingparameter" ), commandline ) ); 423 } catch( final IOException e ) { 424 final String msg = "Zyrf. Problems with parsing arguments: " + commandline; 425 LOG.warn( msg, e ); 426 throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.parsingarguments" ), commandline ) ); 427 } 428 429 // FIXME: We could either return an empty string "", or the original line. If we want unsuccessful requests 430 // to be invisible, then we should return an empty string. 431 return commandline; 432 } 433 434 /** Register a plugin. */ 435 private void registerPlugin( final WikiPluginInfo pluginClass ) { 436 String name; 437 438 // Register the plugin with the className without the package-part 439 name = pluginClass.getName(); 440 if( name != null ) { 441 LOG.debug( "Registering plugin [name]: " + name ); 442 m_pluginClassMap.put( name, pluginClass ); 443 } 444 445 // Register the plugin with a short convenient name. 446 name = pluginClass.getAlias(); 447 if( name != null ) { 448 LOG.debug( "Registering plugin [shortName]: " + name ); 449 m_pluginClassMap.put( name, pluginClass ); 450 } 451 452 // Register the plugin with the className with the package-part 453 name = pluginClass.getClassName(); 454 if( name != null ) { 455 LOG.debug( "Registering plugin [className]: " + name ); 456 m_pluginClassMap.put( name, pluginClass ); 457 } 458 459 pluginClass.initializePlugin( pluginClass, m_engine, m_searchPath, m_externalJars ); 460 } 461 462 private void registerPlugins() { 463 // Register all plugins which have created a resource containing its properties. 464 LOG.info( "Registering plugins" ); 465 final List< Element > plugins = XmlUtil.parse( PLUGIN_RESOURCE_LOCATION, "/modules/plugin" ); 466 467 // Get all resources of all plugins. 468 for( final Element pluginEl : plugins ) { 469 final String className = pluginEl.getAttributeValue( "class" ); 470 final WikiPluginInfo pluginInfo = WikiPluginInfo.newInstance( className, pluginEl ,m_searchPath, m_externalJars ); 471 if( pluginInfo != null ) { 472 registerPlugin( pluginInfo ); 473 } 474 } 475 } 476 477 /** 478 * Contains information about a bunch of plugins. 479 */ 480 // FIXME: This class needs a better interface to return all sorts of possible information from the plugin XML. In fact, it probably 481 // should have some sort of a superclass system. 482 public static final class WikiPluginInfo extends WikiModuleInfo { 483 484 private String m_className; 485 private String m_alias; 486 private String m_ajaxAlias; 487 private Class< Plugin > m_clazz; 488 489 private boolean m_initialized; 490 491 /** 492 * Creates a new plugin info object which can be used to access a plugin. 493 * 494 * @param className Either a fully qualified class name, or a "short" name which is then checked against the internal list of plugin packages. 495 * @param el A JDOM Element containing the information about this class. 496 * @param searchPath A List of Strings, containing different package names. 497 * @param externalJars the list of external jars to search 498 * @return A WikiPluginInfo object. 499 */ 500 static WikiPluginInfo newInstance( final String className, final Element el, final List<String> searchPath, final List<String> externalJars ) { 501 if( className == null || className.isEmpty() ) { 502 return null; 503 } 504 505 final WikiPluginInfo info = new WikiPluginInfo( className ); 506 info.initializeFromXML( el ); 507 return info; 508 } 509 510 /** 511 * Initializes a plugin, if it has not yet been initialized. If the plugin extends {@link HttpServlet} it will automatically 512 * register it as AJAX using {@link WikiAjaxDispatcherServlet#registerServlet(String, WikiAjaxServlet)}. 513 * 514 * @param engine The Engine 515 * @param searchPath A List of Strings, containing different package names. 516 * @param externalJars the list of external jars to search 517 */ 518 void initializePlugin( final WikiPluginInfo info, final Engine engine , final List<String> searchPath, final List<String> externalJars) { 519 if( !m_initialized ) { 520 // This makes sure we only try once per class, even if init fails. 521 m_initialized = true; 522 523 try { 524 final Plugin p = newPluginInstance(searchPath, externalJars); 525 if( p instanceof InitializablePlugin ) { 526 ( ( InitializablePlugin )p ).initialize( engine ); 527 } 528 if( p instanceof WikiAjaxServlet ) { 529 WikiAjaxDispatcherServlet.registerServlet( (WikiAjaxServlet) p ); 530 final String ajaxAlias = info.getAjaxAlias(); 531 if (StringUtils.isNotBlank(ajaxAlias)) { 532 WikiAjaxDispatcherServlet.registerServlet( info.getAjaxAlias(), (WikiAjaxServlet) p ); 533 } 534 } 535 } catch( final Exception e ) { 536 LOG.info( "Cannot initialize plugin " + m_className, e ); 537 } 538 } 539 } 540 541 /** 542 * {@inheritDoc} 543 */ 544 @Override 545 protected void initializeFromXML( final Element el ) { 546 super.initializeFromXML( el ); 547 m_alias = el.getChildText( "alias" ); 548 m_ajaxAlias = el.getChildText( "ajaxAlias" ); 549 } 550 551 /** 552 * Create a new WikiPluginInfo based on the Class information. 553 * 554 * @param clazz The class to check 555 * @return A WikiPluginInfo instance 556 */ 557 static WikiPluginInfo newInstance( final Class< ? > clazz ) { 558 return new WikiPluginInfo( clazz.getName() ); 559 } 560 561 private WikiPluginInfo( final String className ) { 562 super( className ); 563 setClassName( className ); 564 } 565 566 private void setClassName( final String fullClassName ) { 567 m_name = ClassUtils.getShortClassName( fullClassName ); 568 m_className = fullClassName; 569 } 570 571 /** 572 * Returns the full class name of this object. 573 * @return The full class name of the object. 574 */ 575 public String getClassName() { 576 return m_className; 577 } 578 579 /** 580 * Returns the alias name for this object. 581 * @return An alias name for the plugin. 582 */ 583 public String getAlias() { 584 return m_alias; 585 } 586 587 /** 588 * Returns the ajax alias name for this object. 589 * @return An ajax alias name for the plugin. 590 */ 591 public String getAjaxAlias() { 592 return m_ajaxAlias; 593 } 594 595 /** 596 * Creates a new plugin instance. 597 * 598 * @param searchPath A List of Strings, containing different package names. 599 * @param externalJars the list of external jars to search 600 * @return A new plugin. 601 * @throws ClassNotFoundException If the class declared was not found. 602 * @throws InstantiationException If the class cannot be instantiated- 603 * @throws IllegalAccessException If the class cannot be accessed. 604 */ 605 606 public Plugin newPluginInstance( final List< String > searchPath, final List< String > externalJars) throws ReflectiveOperationException { 607 if( m_clazz == null ) { 608 m_clazz = ClassUtil.findClass( searchPath, externalJars ,m_className ); 609 } 610 611 return ClassUtil.buildInstance( m_clazz ); 612 } 613 614 /** 615 * Returns a text for IncludeResources. 616 * 617 * @param type Either "script" or "stylesheet" 618 * @return Text, or an empty string, if there is nothing to be included. 619 */ 620 public String getIncludeText( final String type ) { 621 try { 622 if( "script".equals( type ) ) { 623 return getScriptText(); 624 } else if( "stylesheet".equals( type ) ) { 625 return getStylesheetText(); 626 } 627 } catch( final Exception ex ) { 628 // We want to fail gracefully here 629 return ex.getMessage(); 630 } 631 632 return null; 633 } 634 635 private String getScriptText() throws IOException { 636 if( m_scriptText != null ) { 637 return m_scriptText; 638 } 639 640 if( m_scriptLocation == null ) { 641 return ""; 642 } 643 644 try { 645 m_scriptText = getTextResource(m_scriptLocation); 646 } catch( final IOException ex ) { 647 // Only throw this exception once! 648 m_scriptText = ""; 649 throw ex; 650 } 651 652 return m_scriptText; 653 } 654 655 private String getStylesheetText() throws IOException { 656 if( m_stylesheetText != null ) { 657 return m_stylesheetText; 658 } 659 660 if( m_stylesheetLocation == null ) { 661 return ""; 662 } 663 664 try { 665 m_stylesheetText = getTextResource(m_stylesheetLocation); 666 } catch( final IOException ex ) { 667 // Only throw this exception once! 668 m_stylesheetText = ""; 669 throw ex; 670 } 671 672 return m_stylesheetText; 673 } 674 675 /** 676 * Returns a string suitable for debugging. Don't assume that the format would stay the same. 677 * 678 * @return Something human-readable 679 */ 680 @Override 681 public String toString() { 682 return "Plugin :[name=" + m_name + "][className=" + m_className + "]"; 683 } 684 685 } // WikiPluginClass 686 687 /** 688 * {@inheritDoc} 689 */ 690 @Override 691 public Collection< WikiModuleInfo > modules() { 692 return modules( m_pluginClassMap.values().iterator() ); 693 } 694 695 /** 696 * {@inheritDoc} 697 */ 698 @Override 699 public WikiPluginInfo getModuleInfo( final String moduleName) { 700 return m_pluginClassMap.get(moduleName); 701 } 702 703 /** 704 * Creates a {@link Plugin}. 705 * 706 * @param pluginName plugin's classname 707 * @param rb {@link ResourceBundle} with i18ned text for exceptions. 708 * @return a {@link Plugin}. 709 * @throws PluginException if there is a problem building the {@link Plugin}. 710 */ 711 @Override 712 public Plugin newWikiPlugin( final String pluginName, final ResourceBundle rb ) throws PluginException { 713 Plugin plugin = null; 714 WikiPluginInfo pluginInfo = m_pluginClassMap.get( pluginName ); 715 try { 716 if( pluginInfo == null ) { 717 pluginInfo = WikiPluginInfo.newInstance( findPluginClass( pluginName ) ); 718 registerPlugin( pluginInfo ); 719 } 720 721 if( !checkCompatibility( pluginInfo ) ) { 722 final String msg = "Plugin '" + pluginInfo.getName() + "' not compatible with this version of JSPWiki"; 723 LOG.info( msg ); 724 } else { 725 plugin = pluginInfo.newPluginInstance(m_searchPath, m_externalJars); 726 } 727 } catch( final ClassNotFoundException e ) { 728 throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.couldnotfind" ), pluginName ), e ); 729 } catch( final InstantiationException e ) { 730 throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.cannotinstantiate" ), pluginName ), e ); 731 } catch( final IllegalAccessException e ) { 732 throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.notallowed" ), pluginName ), e ); 733 } catch( final Exception e ) { 734 throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.instantationfailed" ), pluginName ), e ); 735 } 736 return plugin; 737 } 738 739}