📄 wikiengine.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package com.ecyrd.jspwiki;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.security.Principal;import java.util.*;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;import org.apache.commons.lang.time.StopWatch;import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;import com.ecyrd.jspwiki.attachment.Attachment;import com.ecyrd.jspwiki.attachment.AttachmentManager;import com.ecyrd.jspwiki.auth.AuthenticationManager;import com.ecyrd.jspwiki.auth.AuthorizationManager;import com.ecyrd.jspwiki.auth.UserManager;import com.ecyrd.jspwiki.auth.acl.AclManager;import com.ecyrd.jspwiki.auth.acl.DefaultAclManager;import com.ecyrd.jspwiki.auth.authorize.GroupManager;import com.ecyrd.jspwiki.content.PageRenamer;import com.ecyrd.jspwiki.diff.DifferenceManager;import com.ecyrd.jspwiki.event.WikiEngineEvent;import com.ecyrd.jspwiki.event.WikiEventListener;import com.ecyrd.jspwiki.event.WikiEventManager;import com.ecyrd.jspwiki.filters.FilterException;import com.ecyrd.jspwiki.filters.FilterManager;import com.ecyrd.jspwiki.i18n.InternationalizationManager;import com.ecyrd.jspwiki.parser.JSPWikiMarkupParser;import com.ecyrd.jspwiki.parser.MarkupParser;import com.ecyrd.jspwiki.parser.WikiDocument;import com.ecyrd.jspwiki.plugin.PluginManager;import com.ecyrd.jspwiki.providers.ProviderException;import com.ecyrd.jspwiki.providers.WikiPageProvider;import com.ecyrd.jspwiki.render.RenderingManager;import com.ecyrd.jspwiki.rss.RSSGenerator;import com.ecyrd.jspwiki.rss.RSSThread;import com.ecyrd.jspwiki.search.SearchManager;import com.ecyrd.jspwiki.ui.Command;import com.ecyrd.jspwiki.ui.CommandResolver;import com.ecyrd.jspwiki.ui.EditorManager;import com.ecyrd.jspwiki.ui.TemplateManager;import com.ecyrd.jspwiki.ui.admin.AdminBeanManager;import com.ecyrd.jspwiki.ui.progress.ProgressManager;import com.ecyrd.jspwiki.url.URLConstructor;import com.ecyrd.jspwiki.util.ClassUtil;import com.ecyrd.jspwiki.util.WatchDog;import com.ecyrd.jspwiki.workflow.*;/** * Provides Wiki services to the JSP page. * * <P> * This is the main interface through which everything should go. * * <P> * Using this class: Always get yourself an instance from JSP page * by using the WikiEngine.getInstance() method. Never create a new * WikiEngine() from scratch, unless you're writing tests. * <p> * There's basically only a single WikiEngine for each web application, and * you should always get it using the WikiEngine.getInstance() method. */public class WikiEngine{ private static final String ATTR_WIKIENGINE = "com.ecyrd.jspwiki.WikiEngine"; private static final Logger log = Logger.getLogger(WikiEngine.class); /** True, if log4j has been configured. */ // FIXME: If you run multiple applications, the first application // to run defines where the log goes. Not what we want. private static boolean c_configured = false; /** Stores properties. */ private Properties m_properties; /** Property for application name */ public static final String PROP_APPNAME = "jspwiki.applicationName"; /** Property start for any interwiki reference. */ public static final String PROP_INTERWIKIREF = "jspwiki.interWikiRef."; /** If true, then the user name will be stored with the page data.*/ public static final String PROP_STOREUSERNAME= "jspwiki.storeUserName"; /** Define the used encoding. Currently supported are ISO-8859-1 and UTF-8 */ public static final String PROP_ENCODING = "jspwiki.encoding"; /** The name for the base URL to use in all references. */ public static final String PROP_BASEURL = "jspwiki.baseURL"; /** The name for the property which allows you to set the current reference * style. The value is {@value}. */ public static final String PROP_REFSTYLE = "jspwiki.referenceStyle"; /** Property name for the "spaces in titles" -hack. */ public static final String PROP_BEAUTIFYTITLE = "jspwiki.breakTitleWithSpaces"; /** Property name for where the jspwiki work directory should be. If not specified, reverts to ${java.tmpdir}. */ public static final String PROP_WORKDIR = "jspwiki.workDir"; /** The name of the cookie that gets stored to the user browser. */ public static final String PREFS_COOKIE_NAME = "JSPWikiUserProfile"; /** Property name for the "match english plurals" -hack. */ public static final String PROP_MATCHPLURALS = "jspwiki.translatorReader.matchEnglishPlurals"; /** Property name for the template that is used. */ public static final String PROP_TEMPLATEDIR = "jspwiki.templateDir"; /** Property name for the default front page. */ public static final String PROP_FRONTPAGE = "jspwiki.frontPage"; /** Property name for setting the url generator instance */ public static final String PROP_URLCONSTRUCTOR = "jspwiki.urlConstructor"; /** If this property is set to false, all filters are disabled when translating. */ public static final String PROP_RUNFILTERS = "jspwiki.runFilters"; /** Does the work in renaming pages. */ private PageRenamer m_pageRenamer = null; /** The name of the property containing the ACLManager implementing class. * The value is {@value}. */ public static final String PROP_ACL_MANAGER_IMPL = "jspwiki.aclManager"; /** If this property is set to false, we don't allow the creation of empty pages */ public static final String PROP_ALLOW_CREATION_OF_EMPTY_PAGES = "jspwiki.allowCreationOfEmptyPages"; /** Should the user info be saved with the page data as well? */ private boolean m_saveUserInfo = true; /** If true, uses UTF8 encoding for all data */ private boolean m_useUTF8 = true; /** Stores the base URL. */ private String m_baseURL; /** Store the file path to the basic URL. When we're not running as a servlet, it defaults to the user's current directory. */ private String m_rootPath = System.getProperty("user.dir"); /** Stores references between wikipages. */ private ReferenceManager m_referenceManager = null; /** Stores the Plugin manager */ private PluginManager m_pluginManager; /** Stores the Variable manager */ private VariableManager m_variableManager; /** Stores the Attachment manager */ private AttachmentManager m_attachmentManager = null; /** Stores the Page manager */ private PageManager m_pageManager = null; /** Stores the authorization manager */ private AuthorizationManager m_authorizationManager = null; /** Stores the authentication manager.*/ private AuthenticationManager m_authenticationManager = null; /** Stores the ACL manager. */ private AclManager m_aclManager = null; /** Resolves wiki actions, JSPs and special pages. */ private CommandResolver m_commandResolver = null; private TemplateManager m_templateManager = null; /** Does all our diffs for us. */ private DifferenceManager m_differenceManager; /** Handlers page filters. */ private FilterManager m_filterManager; /** Stores the Search manager */ private SearchManager m_searchManager = null; /** Facade for managing users */ private UserManager m_userManager = null; /** Facade for managing users */ private GroupManager m_groupManager = null; private RenderingManager m_renderingManager; private EditorManager m_editorManager; private InternationalizationManager m_internationalizationManager; private ProgressManager m_progressManager; /** Constructs URLs */ private URLConstructor m_urlConstructor; /** Generates RSS feed when requested. */ private RSSGenerator m_rssGenerator; /** The RSS file to generate. */ private String m_rssFile; /** Store the ServletContext that we're in. This may be null if WikiEngine is not running inside a servlet container (i.e. when testing). */ private ServletContext m_servletContext = null; /** If true, all titles will be cleaned. */ private boolean m_beautifyTitle = false; /** Stores the template path. This is relative to "templates". */ private String m_templateDir; /** The default front page name. Defaults to "Main". */ private String m_frontPage; /** The time when this engine was started. */ private Date m_startTime; /** The location where the work directory is. */ private String m_workDir; /** Each engine has their own application id. */ private String m_appid = ""; private boolean m_isConfigured = false; // Flag. /** Each engine has its own workflow manager. */ private WorkflowManager m_workflowMgr = null; private AdminBeanManager m_adminBeanManager; /** Stores wikiengine attributes. */ private Map<String,Object> m_attributes = Collections.synchronizedMap(new HashMap<String,Object>()); /** * Gets a WikiEngine related to this servlet. Since this method * is only called from JSP pages (and JspInit()) to be specific, * we throw a RuntimeException if things don't work. * * @param config The ServletConfig object for this servlet. * * @return A WikiEngine instance. * @throws InternalWikiException in case something fails. This * is a RuntimeException, so be prepared for it. */ // FIXME: It seems that this does not work too well, jspInit() // does not react to RuntimeExceptions, or something... public static synchronized WikiEngine getInstance( ServletConfig config ) throws InternalWikiException { return getInstance( config.getServletContext(), null ); } /** * Gets a WikiEngine related to the servlet. Works like getInstance(ServletConfig), * but does not force the Properties object. This method is just an optional way * of initializing a WikiEngine for embedded JSPWiki applications; normally, you * should use getInstance(ServletConfig). * * @param config The ServletConfig of the webapp servlet/JSP calling this method. * @param props A set of properties, or null, if we are to load JSPWiki's default * jspwiki.properties (this is the usual case). * * @return One well-behaving WikiEngine instance. */ public static synchronized WikiEngine getInstance( ServletConfig config, Properties props ) { return getInstance( config.getServletContext(), props ); } /** * Gets a WikiEngine related to the servlet. Works just like getInstance( ServletConfig ) * * @param context The ServletContext of the webapp servlet/JSP calling this method. * @param props A set of properties, or null, if we are to load JSPWiki's default * jspwiki.properties (this is the usual case). * * @return One fully functional, properly behaving WikiEngine. * @throws InternalWikiException If the WikiEngine instantiation fails. */ // FIXME: Potential make-things-easier thingy here: no need to fetch the wikiengine anymore // Wiki.jsp.jspInit() [really old code]; it's probably even faster to fetch it // using this method every time than go to pageContext.getAttribute(). public static synchronized WikiEngine getInstance( ServletContext context, Properties props ) throws InternalWikiException { WikiEngine engine = (WikiEngine) context.getAttribute( ATTR_WIKIENGINE ); if( engine == null ) { String appid = Integer.toString(context.hashCode()); //FIXME: Kludge, use real type. context.log(" Assigning new engine to "+appid); try { if( props == null ) { props = PropertyReader.loadWebAppProps( context ); } engine = new WikiEngine( context, appid, props ); context.setAttribute( ATTR_WIKIENGINE, engine ); } catch( Exception e ) { context.log( "ERROR: Failed to create a Wiki engine: "+e.getMessage() ); throw new InternalWikiException( "No wiki engine, check logs." ); } } return engine; } /** * Instantiate the WikiEngine using a given set of properties. * Use this constructor for testing purposes only. * * @param properties A set of properties to use to initialize this WikiEngine. * @throws WikiException If the initialization fails. */ public WikiEngine( Properties properties ) throws WikiException { initialize( properties ); } /** * Instantiate using this method when you're running as a servlet and * WikiEngine will figure out where to look for the property * file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -