This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit 201c48ca41e686e57d10cd276264d650b0375cc0
Author: juanpablo <[email protected]>
AuthorDate: Thu Mar 26 20:59:57 2020 +0100

    JSPWIKI-303: SPI to obtain Engine instances
---
 .../java/org/apache/wiki/api/spi/EngineSPI.java    | 41 ++++++++++++
 .../main/java/org/apache/wiki/api/spi/Wiki.java    | 73 ++++++++++++++++++++++
 .../main/java/org/apache/wiki/api/spi/package.html | 37 +++++++++++
 .../src/main/java/org/apache/wiki/WikiServlet.java |  3 +-
 .../wiki/ajax/WikiAjaxDispatcherServlet.java       |  6 +-
 .../org/apache/wiki/rpc/atom/AtomAPIServlet.java   |  6 +-
 .../org/apache/wiki/spi/EngineSPIDefaultImpl.java  | 44 +++++++++++++
 .../java/org/apache/wiki/ui/WikiServletFilter.java |  4 +-
 .../java/org/apache/wiki/xmlrpc/RPCServlet.java    | 32 ++++++----
 .../services/org.apache.wiki.api.spi.EngineSPI     |  1 +
 10 files changed, 226 insertions(+), 21 deletions(-)

diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/spi/EngineSPI.java 
b/jspwiki-api/src/main/java/org/apache/wiki/api/spi/EngineSPI.java
new file mode 100644
index 0000000..cabb811
--- /dev/null
+++ b/jspwiki-api/src/main/java/org/apache/wiki/api/spi/EngineSPI.java
@@ -0,0 +1,41 @@
+/*
+    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 org.apache.wiki.api.spi;
+
+import org.apache.wiki.api.core.Engine;
+
+import javax.servlet.ServletContext;
+import java.util.Properties;
+
+
+/**
+ * SPI used to locate and provide {@link Engine} instances.
+ */
+public interface EngineSPI {
+
+    /**
+     * Locate, or build if necessary, a configured {@link Engine} instance.
+     *
+     * @param context servlet context holding the {@link Engine} instance.
+     * @param props Engine configuration properties.
+     * @return a configured {@link Engine} instance.
+     */
+    Engine getInstance( ServletContext context, Properties props );
+
+}
diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/spi/Wiki.java 
b/jspwiki-api/src/main/java/org/apache/wiki/api/spi/Wiki.java
new file mode 100644
index 0000000..f986dc7
--- /dev/null
+++ b/jspwiki-api/src/main/java/org/apache/wiki/api/spi/Wiki.java
@@ -0,0 +1,73 @@
+/*
+    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 org.apache.wiki.api.spi;
+
+import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.util.PropertyReader;
+import org.apache.wiki.util.TextUtil;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import java.nio.file.ProviderNotFoundException;
+import java.util.Properties;
+import java.util.ServiceLoader;
+
+
+public class Wiki {
+
+    private static final String ATTR_ENGINE_SPI = 
"org.apache.wiki.api.core.Engine";
+    private static final String PROP_ENGINE_PROVIDER_IMPL = 
"jspwiki.provider.impl.engine";
+    private static final String DEFAULT_ENGINE_PROVIDER_IMPL = 
"org.apache.wiki.spi.EngineSPIDefaultImpl";
+
+    public static Engine engine( final ServletConfig config ) {
+        return engine( config.getServletContext(), null );
+    }
+
+    public static Engine engine( final ServletConfig config, final Properties 
props ) {
+        return engine( config.getServletContext(), props );
+    }
+
+    public static Engine engine( final ServletContext context, final 
Properties props ) {
+        final Engine engine = ( Engine )context.getAttribute( ATTR_ENGINE_SPI 
);
+        if( engine == null ) {
+            final Properties properties = loadPropertiesFrom( context, props );
+            return getSPI( EngineSPI.class, properties, 
PROP_ENGINE_PROVIDER_IMPL, DEFAULT_ENGINE_PROVIDER_IMPL ).getInstance( context, 
props );
+        }
+        return engine;
+    }
+
+    static Properties loadPropertiesFrom( final ServletContext context, final 
Properties props ) {
+        if( props == null ) {
+            return PropertyReader.loadWebAppProps( context );
+        }
+        return props;
+    }
+
+    static < SPI > SPI getSPI( final Class< SPI > spi, final Properties props, 
final String prop, final String defValue ) {
+        final String providerImpl = TextUtil.getStringProperty( props, prop, 
defValue );
+        final ServiceLoader< SPI > loader = ServiceLoader.load( spi );
+        for( final SPI provider : loader ) {
+            if( providerImpl.equals( provider.getClass().getName() ) ) {
+                return provider;
+            }
+        }
+        throw new ProviderNotFoundException( spi.getName() + " provider not 
found" );
+    }
+
+}
diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/spi/package.html 
b/jspwiki-api/src/main/java/org/apache/wiki/api/spi/package.html
new file mode 100644
index 0000000..48143e8
--- /dev/null
+++ b/jspwiki-api/src/main/java/org/apache/wiki/api/spi/package.html
@@ -0,0 +1,37 @@
+<!--
+    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.
+-->
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd";>
+<html lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>JSPWiki's API's SPI</title>
+</head>
+<body>
+JSPWiki's SPI package.
+
+This package holds all the service provider interfaces provided by JSPWiki to 
obtain instances of objects from the
+<code>org.apache.wiki.api.core</code> package.
+
+<h3>Package Specification</h3>
+
+<h3>Related Documentation</h3>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java 
b/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java
index d401dd4..98e5ac6 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiServlet.java
@@ -21,6 +21,7 @@ package org.apache.wiki;
 import net.sf.ehcache.CacheManager;
 import org.apache.log4j.Logger;
 import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.api.spi.Wiki;
 import org.apache.wiki.url.URLConstructor;
 
 import javax.servlet.RequestDispatcher;
@@ -50,7 +51,7 @@ public class WikiServlet extends HttpServlet {
     @Override
     public void init( final ServletConfig config ) throws ServletException {
         super.init( config );
-        m_engine = WikiEngine.getInstance( config );
+        m_engine = Wiki.engine( config );
         log.info( "WikiServlet initialized." );
     }
 
diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/ajax/WikiAjaxDispatcherServlet.java
 
b/jspwiki-main/src/main/java/org/apache/wiki/ajax/WikiAjaxDispatcherServlet.java
index 70b872e..97645ec 100644
--- 
a/jspwiki-main/src/main/java/org/apache/wiki/ajax/WikiAjaxDispatcherServlet.java
+++ 
b/jspwiki-main/src/main/java/org/apache/wiki/ajax/WikiAjaxDispatcherServlet.java
@@ -21,9 +21,9 @@ package org.apache.wiki.ajax;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.log4j.Logger;
-import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiSession;
 import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.api.spi.Wiki;
 import org.apache.wiki.auth.AuthorizationManager;
 import org.apache.wiki.auth.permissions.PagePermission;
 import org.apache.wiki.util.TextUtil;
@@ -65,7 +65,7 @@ public class WikiAjaxDispatcherServlet extends HttpServlet {
     @Override
     public void init( final ServletConfig config ) throws ServletException {
         super.init( config );
-        m_engine = WikiEngine.getInstance( config );
+        m_engine = Wiki.engine( config );
         PATH_AJAX = "/" + TextUtil.getStringProperty( 
m_engine.getWikiProperties(), "jspwiki.ajax.url.prefix", "ajax" ) + "/";
         log.info( "WikiAjaxDispatcherServlet initialized." );
     }
@@ -161,7 +161,7 @@ public class WikiAjaxDispatcherServlet extends HttpServlet {
      * @return true if permission is valid
      */
     private boolean validatePermission( final HttpServletRequest req, final 
AjaxServletContainer container ) {
-        final WikiEngine e = 
WikiEngine.getInstance(req.getSession().getServletContext(), null);
+        final Engine e = Wiki.engine( req.getSession().getServletContext(), 
null );
         boolean valid = false;
         if( container != null ) {
             valid = e.getManager( AuthorizationManager.class 
).checkPermission( WikiSession.getWikiSession( e, req ), container.permission );
diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java 
b/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java
index a4e8223..392ca51 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/rpc/atom/AtomAPIServlet.java
@@ -20,12 +20,12 @@ package org.apache.wiki.rpc.atom;
 
 import org.apache.log4j.Logger;
 import org.apache.wiki.WikiContext;
-import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiPage;
 import org.apache.wiki.api.core.Engine;
 import org.apache.wiki.api.core.Page;
 import org.apache.wiki.api.exceptions.ProviderException;
 import org.apache.wiki.api.exceptions.WikiException;
+import org.apache.wiki.api.spi.Wiki;
 import org.apache.wiki.pages.PageManager;
 import org.apache.wiki.plugin.WeblogEntryPlugin;
 import org.apache.wiki.plugin.WeblogPlugin;
@@ -69,7 +69,7 @@ public class AtomAPIServlet extends HttpServlet {
      */
     @Override
     public void init( final ServletConfig config ) throws ServletException {
-        m_engine = WikiEngine.getInstance( config );
+        m_engine = Wiki.engine( config );
     }
 
     /**
@@ -123,7 +123,7 @@ public class AtomAPIServlet extends HttpServlet {
             final WeblogEntryPlugin plugin = new WeblogEntryPlugin();
             final String pageName = plugin.getNewEntryPage( m_engine, blogid );
             final String username = author.getName();
-            final WikiPage entryPage = new WikiPage( m_engine, pageName );
+            final Page entryPage = new WikiPage( m_engine, pageName );
             entryPage.setAuthor( username );
 
             final WikiContext context = new WikiContext( m_engine, request, 
entryPage );
diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/spi/EngineSPIDefaultImpl.java 
b/jspwiki-main/src/main/java/org/apache/wiki/spi/EngineSPIDefaultImpl.java
new file mode 100644
index 0000000..669ceb4
--- /dev/null
+++ b/jspwiki-main/src/main/java/org/apache/wiki/spi/EngineSPIDefaultImpl.java
@@ -0,0 +1,44 @@
+/*
+    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 org.apache.wiki.spi;
+
+import org.apache.wiki.WikiEngine;
+import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.api.spi.EngineSPI;
+
+import javax.servlet.ServletContext;
+import java.util.Properties;
+
+
+/**
+ * Default implementation for {@link EngineSPI}
+ *
+ * @see EngineSPI
+ */
+public class EngineSPIDefaultImpl implements EngineSPI {
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Engine getInstance( final ServletContext context, final Properties 
props ) {
+        return WikiEngine.getInstance( context, props );
+    }
+
+}
diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiServletFilter.java 
b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiServletFilter.java
index 6583ba6..284ee33 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiServletFilter.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiServletFilter.java
@@ -21,10 +21,10 @@ package org.apache.wiki.ui;
 import org.apache.log4j.Logger;
 import org.apache.log4j.NDC;
 import org.apache.wiki.WikiContext;
-import org.apache.wiki.WikiEngine;
 import org.apache.wiki.api.core.Context;
 import org.apache.wiki.api.core.Engine;
 import org.apache.wiki.api.core.Session;
+import org.apache.wiki.api.spi.Wiki;
 import org.apache.wiki.auth.AuthenticationManager;
 import org.apache.wiki.auth.SessionMonitor;
 import org.apache.wiki.auth.WikiSecurityException;
@@ -78,7 +78,7 @@ public class WikiServletFilter implements Filter {
             context.log( "== JSPWIKI WARNING ==  : This container is running 
with a security manager. JSPWiki does not yet really support that right now. 
See issue JSPWIKI-129 for details and information on how to proceed." );
         }
 
-        m_engine = WikiEngine.getInstance( context, null );
+        m_engine = Wiki.engine( context, null );
     }
 
     /**
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCServlet.java 
b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCServlet.java
index 4259acc..99e7982 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCServlet.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCServlet.java
@@ -20,8 +20,9 @@ package org.apache.wiki.xmlrpc;
 
 import org.apache.log4j.Logger;
 import org.apache.wiki.WikiContext;
-import org.apache.wiki.WikiEngine;
+import org.apache.wiki.api.core.Context;
 import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.api.spi.Wiki;
 import org.apache.xmlrpc.ContextXmlRpcHandler;
 import org.apache.xmlrpc.Invoker;
 import org.apache.xmlrpc.XmlRpcContext;
@@ -76,8 +77,9 @@ public class RPCServlet extends HttpServlet {
     /**
      *  Initializes the servlet.
      */
-    @Override public void init( final ServletConfig config ) throws 
ServletException {
-        m_engine = WikiEngine.getInstance( config );
+    @Override
+    public void init( final ServletConfig config ) throws ServletException {
+        m_engine = Wiki.engine( config );
 
         String handlerName = config.getInitParameter( "handler" );
         String prefix      = config.getInitParameter( "prefix" );
@@ -103,7 +105,8 @@ public class RPCServlet extends HttpServlet {
     /**
      *  Handle HTTP POST.  This is an XML-RPC call, and we'll just forward the 
query to an XmlRpcServer.
      */
-    @Override public void doPost( final HttpServletRequest request, final 
HttpServletResponse response ) throws ServletException {
+    @Override
+    public void doPost( final HttpServletRequest request, final 
HttpServletResponse response ) throws ServletException {
         log.debug("Received POST to RPCServlet");
 
         try {
@@ -132,7 +135,8 @@ public class RPCServlet extends HttpServlet {
     /**
      *  Handles HTTP GET.  However, we do not respond to GET requests, other 
than to show an explanatory text.
      */
-    @Override public void doGet( final HttpServletRequest request, final 
HttpServletResponse response ) throws ServletException {
+    @Override
+    public void doGet( final HttpServletRequest request, final 
HttpServletResponse response ) throws ServletException {
         log.debug("Received HTTP GET to RPCServlet");
 
         try {
@@ -157,7 +161,8 @@ public class RPCServlet extends HttpServlet {
             m_clazz = clazz;
         }
 
-        @Override public Object execute( final String method, final Vector 
params, final XmlRpcContext context ) throws Exception {
+        @Override
+        public Object execute( final String method, final Vector params, final 
XmlRpcContext context ) throws Exception {
             final WikiRPCHandler rpchandler = (WikiRPCHandler) 
m_clazz.newInstance();
             rpchandler.initialize( 
((WikiXmlRpcContext)context).getWikiContext() );
 
@@ -169,27 +174,30 @@ public class RPCServlet extends HttpServlet {
     private static class WikiXmlRpcContext implements XmlRpcContext {
 
         private XmlRpcHandlerMapping m_mapping;
-        private WikiContext m_context;
+        private Context m_context;
 
-        public WikiXmlRpcContext( final XmlRpcHandlerMapping map, final 
WikiContext ctx ) {
+        public WikiXmlRpcContext( final XmlRpcHandlerMapping map, final 
Context ctx ) {
             m_mapping = map;
             m_context = ctx;
         }
 
-        @Override public XmlRpcHandlerMapping getHandlerMapping()
+        @Override
+        public XmlRpcHandlerMapping getHandlerMapping()
         {
             return m_mapping;
         }
 
-        @Override public String getPassword() {
+        @Override
+        public String getPassword() {
             return null;
         }
 
-        @Override public String getUserName() {
+        @Override
+        public String getUserName() {
             return null;
         }
 
-        public WikiContext getWikiContext()
+        public Context getWikiContext()
         {
             return m_context;
         }
diff --git 
a/jspwiki-main/src/main/resources/META-INF/services/org.apache.wiki.api.spi.EngineSPI
 
b/jspwiki-main/src/main/resources/META-INF/services/org.apache.wiki.api.spi.EngineSPI
new file mode 100644
index 0000000..4c6c216
--- /dev/null
+++ 
b/jspwiki-main/src/main/resources/META-INF/services/org.apache.wiki.api.spi.EngineSPI
@@ -0,0 +1 @@
+org.apache.wiki.spi.EngineSPIDefaultImpl
\ No newline at end of file

Reply via email to