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 */
019package org.apache.wiki.api.engine;
020
021import java.io.IOException;
022import java.util.Collection;
023import java.util.Map;
024import java.util.ResourceBundle;
025
026import org.apache.oro.text.regex.Pattern;
027import org.apache.wiki.WikiContext;
028import org.apache.wiki.api.exceptions.PluginException;
029import org.apache.wiki.api.plugin.WikiPlugin;
030import org.apache.wiki.modules.WikiModuleInfo;
031import org.apache.wiki.plugin.DefaultPluginManager.WikiPluginInfo;
032
033
034public interface PluginManager {
035
036    /** The property name defining which packages will be searched for plugin classes. */
037    String PROP_SEARCHPATH = "jspwiki.plugin.searchPath";
038
039    /** The property name defining which external jars will be added to the classpath when searching for plugin classes. */
040    String PROP_EXTERNALJARS = "jspwiki.plugin.externalJars";
041
042    /** This is the default package to try in case the instantiation fails. */
043    String DEFAULT_PACKAGE = "org.apache.wiki.plugin";
044
045    /**
046     *  The name of the body content.  Current value is "_body".
047     */
048    String PARAM_BODY      = "_body";
049
050    /** The name of the command line content parameter. The value is "_cmdline". */
051    String PARAM_CMDLINE   = "_cmdline";
052
053    /**
054     *  The name of the parameter containing the start and end positions in the
055     *  read stream of the plugin text (stored as a two-element int[], start
056     *  and end resp.).
057     */
058    String PARAM_BOUNDS    = "_bounds";
059
060    /** A special name to be used in case you want to see debug output */
061    String PARAM_DEBUG     = "debug";
062
063    /**
064     * Enables or disables plugin execution.
065     * 
066     * @param enabled True, if plugins should be globally enabled; false, if disabled.
067     */
068    void enablePlugins( boolean enabled );
069
070    /**
071     * Returns plugin execution status. If false, plugins are not
072     * executed when they are encountered on a WikiPage, and an
073     * empty string is returned in their place.
074     * 
075     * @return True, if plugins are enabled; false otherwise.
076     */
077    boolean pluginsEnabled();
078    
079    /**
080     * Returns plugin insert pattern.
081     * 
082     * @return plugin insert pattern.
083     */
084    Pattern getPluginPattern();
085    
086    /**
087     * Returns plugins' search path.
088     * 
089     * @return plugins' search path.
090     */
091    String getPluginSearchPath();
092
093    /**
094     *  Executes a plugin class in the given context.
095     *  <P>Used to be private, but is public since 1.9.21.
096     *
097     *  @param context The current WikiContext.
098     *  @param classname The name of the class.  Can also be a
099     *  shortened version without the package name, since the class name is searched from the
100     *  package search path.
101     *
102     *  @param params A parsed map of key-value pairs.
103     *
104     *  @return Whatever the plugin returns.
105     *
106     *  @throws PluginException If the plugin execution failed for
107     *  some reason.
108     *
109     *  @since 2.0
110     */
111    String execute( WikiContext context, String classname, Map< String, String > params ) throws PluginException;
112
113    /**
114     *  Parses plugin arguments.  Handles quotes and all other kewl stuff.
115     *
116     *  <h3>Special parameters</h3>
117     *  The plugin body is put into a special parameter defined by {@link #PARAM_BODY};
118     *  the plugin's command line into a parameter defined by {@link #PARAM_CMDLINE};
119     *  and the bounds of the plugin within the wiki page text by a parameter defined
120     *  by {@link #PARAM_BOUNDS}, whose value is stored as a two-element int[] array,
121     *  i.e., <tt>[start,end]</tt>.
122     *
123     * @param argstring The argument string to the plugin.  This is
124     *  typically a list of key-value pairs, using "'" to escape
125     *  spaces in strings, followed by an empty line and then the
126     *  plugin body.  In case the parameter is null, will return an
127     *  empty parameter list.
128     *
129     * @return A parsed list of parameters.
130     *
131     * @throws IOException If the parsing fails.
132     */
133    Map< String, String > parseArgs( String argstring ) throws IOException;
134
135    /**
136     *  Parses a plugin.  Plugin commands are of the form:
137     *  [{INSERT myplugin WHERE param1=value1, param2=value2}]
138     *  myplugin may either be a class name or a plugin alias.
139     *  <P>
140     *  This is the main entry point that is used.
141     *
142     *  @param context The current WikiContext.
143     *  @param commandline The full command line, including plugin
144     *  name, parameters and body.
145     *
146     *  @return HTML as returned by the plugin, or possibly an error
147     *  message.
148     *  
149     *  @throws PluginException From the plugin itself, it propagates, waah!
150     */
151    String execute( WikiContext context, String commandline ) throws PluginException;
152
153    /**
154     * Returns a collection of modules currently managed by this ModuleManager.  Each
155     * entry is an instance of the {@link WikiModuleInfo} class.  This method should return something
156     * which is safe to iterate over, even if the underlying collection changes.
157     * 
158     * @return A Collection of {@link WikiModuleInfo} instances.
159     */
160    Collection< WikiModuleInfo > modules();
161
162    /**
163     * Returns the {@link WikiPluginInfo} information about the provided pluginName.
164     * @param pluginName
165     * @return The wikiPluginInfo
166     */
167    WikiPluginInfo getModuleInfo(String pluginName);
168    
169    /**
170     * Creates a {@link WikiPlugin}.
171     * 
172     * @param pluginName plugin's classname
173     * @param rb {@link ResourceBundle} with i18ned text for exceptions.
174     * @return a {@link WikiPlugin}.
175     * @throws PluginException if there is a problem building the {@link WikiPlugin}.
176     */
177    WikiPlugin newWikiPlugin( String pluginName, ResourceBundle rb ) throws PluginException;
178    
179}