You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ch...@apache.org on 2017/03/08 07:20:15 UTC

svn commit: r1785919 - /jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RemotingInitializer.java

Author: chetanm
Date: Wed Mar  8 07:20:15 2017
New Revision: 1785919

URL: http://svn.apache.org/viewvc?rev=1785919&view=rev
Log:
OAK-5500 - Oak Standalone throws ClassNotFoundException: remoting/protectedHandlersConfig.xml

Modified:
    jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RemotingInitializer.java

Modified: jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RemotingInitializer.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RemotingInitializer.java?rev=1785919&r1=1785918&r2=1785919&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RemotingInitializer.java (original)
+++ jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RemotingInitializer.java Wed Mar  8 07:20:15 2017
@@ -19,9 +19,13 @@
 
 package org.apache.jackrabbit.oak.standalone;
 
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
 import javax.jcr.Repository;
+import javax.servlet.ServletContext;
 
-import org.apache.jackrabbit.server.remoting.davex.DavexServletService;
 import org.apache.jackrabbit.server.remoting.davex.JcrRemotingServlet;
 import org.apache.jackrabbit.webdav.simple.SimpleWebdavServlet;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -29,6 +33,8 @@ import org.springframework.beans.factory
 import org.springframework.boot.context.embedded.ServletRegistrationBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
 
 /**
  * Configures the Webdav and Davex servlet to enabled remote
@@ -43,6 +49,8 @@ public class RemotingInitializer {
     @Autowired
     private Repository repository;
 
+    @Autowired
+    private ServletContext servletContext;
 
     @Bean
     public ServletRegistrationBean webDavServlet() {
@@ -51,10 +59,15 @@ public class RemotingInitializer {
             public Repository getRepository() {
                 return repository;
             }
+
+            @Override
+            public ServletContext getServletContext() {
+                return RemotingInitializer.this.getServletContext();
+            }
         }, "/repository/*");
 
         bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, "/repository");
-        bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_CONFIG, "remoting/webdav-config.xml");
+        bean.addInitParameter(SimpleWebdavServlet.INIT_PARAM_RESOURCE_CONFIG, "/remoting/webdav-config.xml");
         return bean;
     }
 
@@ -66,17 +79,43 @@ public class RemotingInitializer {
             public Repository getRepository() {
                 return repository;
             }
+
+            @Override
+            public ServletContext getServletContext() {
+                return RemotingInitializer.this.getServletContext();
+            }
         }, "/server/*");
 
         bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, "/server");
-        bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_BATCHREAD_CONFIG, "remoting/batchread.properties");
+        bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_BATCHREAD_CONFIG, "/remoting/batchread.properties");
 
-        //TODO By docs this is meant to point to a file which gets loaded
-        //but servlet always reads it as File not via input stream. Hence using
-        //actual class
         bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_PROTECTED_HANDLERS_CONFIG,
-                "remoting/protectedHandlersConfig.xml");
+                "/remoting/protectedHandlersConfig.xml");
         bean.addInitParameter(JcrRemotingServlet.INIT_PARAM_HOME, davHome);
         return bean;
     }
+
+    /**
+     * Creates a proxy ServletContext which delegates the resource loading to Spring Resource support
+     * Without this default embedded server ServletContext based resource loading was failing
+     */
+    private ServletContext getServletContext(){
+        return (ServletContext) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ServletContext.class},
+                new InvocationHandler(){
+                    @Override
+                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+                        if ("getResourceAsStream".equals(method.getName())){
+                            return getResource((String) args[0]).getInputStream();
+                        }
+                        if ("getResource".equals(method.getName())){
+                            return getResource((String) args[0]).getURL();
+                        }
+                        return method.invoke(servletContext, args);
+                    }
+                });
+    }
+
+    private Resource getResource(String path) {
+        return new ClassPathResource(path);
+    }
 }