]> source.dussan.org Git - vaadin-framework.git/commitdiff
Move getBaseDirectory() to DeploymentConfiguration (#9402)
authorLeif Åstrand <leif@vaadin.com>
Tue, 4 Sep 2012 13:59:24 +0000 (16:59 +0300)
committerLeif Åstrand <leif@vaadin.com>
Wed, 5 Sep 2012 08:39:35 +0000 (11:39 +0300)
server/src/com/vaadin/server/ApplicationContext.java
server/src/com/vaadin/server/DeploymentConfiguration.java
server/src/com/vaadin/server/PortletApplicationContext2.java
server/src/com/vaadin/server/ServletApplicationContext.java
server/src/com/vaadin/server/VaadinPortlet.java
server/src/com/vaadin/server/VaadinServlet.java
uitest/src/com/vaadin/tests/resources/NonExistingFileResource.java
uitest/src/com/vaadin/tests/tickets/Ticket1975.java
uitest/src/com/vaadin/tests/util/SampleDirectory.java

index 2c09ed38574b9403091ef67786e6a1d6df464575..85a77241d949971effbda72cc4ae17d385b8c77f 100644 (file)
@@ -15,7 +15,6 @@
  */
 package com.vaadin.server;
 
-import java.io.File;
 import java.io.Serializable;
 import java.util.Collection;
 import java.util.Collections;
@@ -148,20 +147,6 @@ public abstract class ApplicationContext implements HttpSessionBindingListener,
         return Logger.getLogger(ApplicationContext.class.getName());
     }
 
-    /**
-     * Returns application context base directory.
-     * 
-     * Typically an application is deployed in a such way that is has an
-     * application directory. For web applications this directory is the root
-     * directory of the web applications. In some cases applications might not
-     * have an application directory (for example web applications running
-     * inside a war).
-     * 
-     * @return The application base directory or null if the application has no
-     *         base directory.
-     */
-    public abstract File getBaseDirectory();
-
     /**
      * Gets the session to which this application context is currently
      * associated.
index 1f202eb92372ad6e8468bd078f7c8edefe87e281..0a70ef09b34edc9d10f195efce7fd20809912ee5 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.vaadin.server;
 
+import java.io.File;
 import java.io.Serializable;
 import java.util.Iterator;
 
@@ -123,4 +124,18 @@ public interface DeploymentConfiguration extends Serializable {
      * @return the system messages object
      */
     public SystemMessages getSystemMessages();
+
+    /**
+     * Returns application context base directory.
+     * 
+     * Typically an application is deployed in a such way that is has an
+     * application directory. For web applications this directory is the root
+     * directory of the web applications. In some cases applications might not
+     * have an application directory (for example web applications running
+     * inside a war).
+     * 
+     * @return The application base directory or null if the application has no
+     *         base directory.
+     */
+    public File getBaseDirectory();
 }
index 6a12eafc471bc835bdbc4976c0c3606fdc2cc9df..f157dc9ae6a71daa85cbd05e310a02469aa7b2ee 100644 (file)
  */
 package com.vaadin.server;
 
-import java.io.File;
 import java.io.Serializable;
-import java.net.URL;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import javax.portlet.ActionRequest;
 import javax.portlet.ActionResponse;
@@ -69,28 +65,6 @@ public class PortletApplicationContext2 extends ApplicationContext {
     private final Map<String, String> sharedParameterActionNameMap = new HashMap<String, String>();
     private final Map<String, String> sharedParameterActionValueMap = new HashMap<String, String>();
 
-    @Override
-    public File getBaseDirectory() {
-        PortletSession session = getPortletSession();
-        String resultPath = session.getPortletContext().getRealPath("/");
-        if (resultPath != null) {
-            return new File(resultPath);
-        } else {
-            try {
-                final URL url = session.getPortletContext().getResource("/");
-                return new File(url.getFile());
-            } catch (final Exception e) {
-                // FIXME: Handle exception
-                getLogger()
-                        .log(Level.INFO,
-                                "Cannot access base directory, possible security issue "
-                                        + "with Application Server or Servlet Container",
-                                e);
-            }
-        }
-        return null;
-    }
-
     protected PortletCommunicationManager getApplicationManager(
             Application application) {
         PortletCommunicationManager mgr = (PortletCommunicationManager) applicationToAjaxAppMgrMap
@@ -403,8 +377,4 @@ public class PortletApplicationContext2 extends ApplicationContext {
                     "Portlet mode can only be changed from a portlet request");
         }
     }
-
-    private Logger getLogger() {
-        return Logger.getLogger(PortletApplicationContext2.class.getName());
-    }
 }
index 4184b68de488f4ae4d4b03acb61cb6509ed83823..910051a26b2943f301a107884064d6b1eb4bf837 100644 (file)
@@ -16,7 +16,6 @@
 
 package com.vaadin.server;
 
-import java.io.File;
 import java.util.Enumeration;
 import java.util.HashMap;
 
@@ -98,21 +97,6 @@ public class ServletApplicationContext extends ApplicationContext {
         setSession(new WrappedHttpSession(newSession));
     }
 
-    /**
-     * Gets the application context base directory.
-     * 
-     * @see com.vaadin.server.ApplicationContext#getBaseDirectory()
-     */
-    @Override
-    public File getBaseDirectory() {
-        final String realPath = VaadinServlet.getResourcePath(getHttpSession()
-                .getServletContext(), "/");
-        if (realPath == null) {
-            return null;
-        }
-        return new File(realPath);
-    }
-
     /**
      * Gets the http-session application is running in.
      * 
index 923fcd3b71ebf02fe13bf1f9df3a95ad0fc00a70..5fb2340bc8133d71e2834c29a93a8b01a610e7bb 100644 (file)
@@ -16,6 +16,7 @@
 package com.vaadin.server;
 
 import java.io.BufferedWriter;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -24,11 +25,13 @@ import java.io.PrintWriter;
 import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
+import java.net.URL;
 import java.security.GeneralSecurityException;
 import java.util.Enumeration;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.portlet.ActionRequest;
@@ -154,6 +157,29 @@ public class VaadinPortlet extends GenericPortlet implements Constants {
         public SystemMessages getSystemMessages() {
             return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES;
         }
+
+        @Override
+        public File getBaseDirectory() {
+            PortletContext context = getPortlet().getPortletContext();
+            String resultPath = context.getRealPath("/");
+            if (resultPath != null) {
+                return new File(resultPath);
+            } else {
+                try {
+                    final URL url = context.getResource("/");
+                    return new File(url.getFile());
+                } catch (final Exception e) {
+                    // FIXME: Handle exception
+                    getLogger()
+                            .log(Level.INFO,
+                                    "Cannot access base directory, possible security issue "
+                                            + "with Application Server or Servlet Container",
+                                    e);
+                }
+            }
+            return null;
+        }
+
     }
 
     public static class WrappedHttpAndPortletRequest extends
index 3cf2c645a04800c79ffe30aa0573c956ff783761..62fb8ba71e92b0ab3942fb8fb28f18a355973d66 100644 (file)
@@ -16,6 +16,7 @@
 package com.vaadin.server;
 
 import java.io.BufferedWriter;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -140,6 +141,16 @@ public class VaadinServlet extends HttpServlet implements Constants {
         public SystemMessages getSystemMessages() {
             return ServletPortletHelper.DEFAULT_SYSTEM_MESSAGES;
         }
+
+        @Override
+        public File getBaseDirectory() {
+            final String realPath = VaadinServlet.getResourcePath(
+                    servlet.getServletContext(), "/");
+            if (realPath == null) {
+                return null;
+            }
+            return new File(realPath);
+        }
     }
 
     private static class AbstractApplicationServletWrapper implements Callback {
index eb8559f746b9dada3febe0ba1e0355a57897bfea..45552df39cbdcb7b8035d4b5311d070fc765e53c 100644 (file)
@@ -3,9 +3,11 @@ package com.vaadin.tests.resources;
 import java.io.File;
 
 import com.vaadin.server.FileResource;
+import com.vaadin.server.WrappedRequest;
 import com.vaadin.tests.components.TestBase;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.util.CurrentInstance;
 
 public class NonExistingFileResource extends TestBase {
 
@@ -24,8 +26,10 @@ public class NonExistingFileResource extends TestBase {
 
             @Override
             public void buttonClick(ClickEvent event) {
-                FileResource res = new FileResource(new File(getContext()
-                        .getBaseDirectory() + "/" + filename));
+                FileResource res = new FileResource(new File(CurrentInstance
+                        .get(WrappedRequest.class).getDeploymentConfiguration()
+                        .getBaseDirectory()
+                        + "/" + filename));
                 getMainWindow().open(res);
 
             }
index e85fe294f2cf5008a87de171bfb098d5283e4db4..4fa3470f0b6624b173d53950a1d5fe0df09dd195 100644 (file)
@@ -5,13 +5,14 @@ import java.io.File;
 import java.io.FileInputStream;
 
 import com.vaadin.Application;
-import com.vaadin.server.ServletApplicationContext;
+import com.vaadin.server.WrappedRequest;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Button.ClickEvent;
 import com.vaadin.ui.Button.ClickListener;
 import com.vaadin.ui.CustomLayout;
 import com.vaadin.ui.GridLayout;
 import com.vaadin.ui.UI.LegacyWindow;
+import com.vaadin.util.CurrentInstance;
 
 public class Ticket1975 extends Application.LegacyApplication {
 
@@ -33,7 +34,6 @@ public class Ticket1975 extends Application.LegacyApplication {
         try {
             cl1 = new CustomLayout(new ByteArrayInputStream(s.getBytes()));
             layout.addComponent(cl1);
-            ServletApplicationContext wc = ((ServletApplicationContext) getContext());
 
             layout.addComponent(new Button("Disable/Enable",
                     new ClickListener() {
@@ -47,8 +47,10 @@ public class Ticket1975 extends Application.LegacyApplication {
                         }
 
                     }));
-            File f = new File(wc.getBaseDirectory().getAbsoluteFile()
-                    + "/VAADIN/themes/" + getTheme()
+            File baseDir = CurrentInstance.get(WrappedRequest.class)
+                    .getDeploymentConfiguration().getBaseDirectory()
+                    .getAbsoluteFile();
+            File f = new File(baseDir + "/VAADIN/themes/" + getTheme()
                     + "/layouts/Ticket1975.html");
 
             cl2 = new CustomLayout(new FileInputStream(f));
index e15f0ccfe316a7130d178d01dda0ca517eb714ac..7304f1cab88ea0593cc01dd5201ad64cca98cf62 100644 (file)
@@ -20,10 +20,12 @@ import java.io.File;
 
 import com.vaadin.Application;
 import com.vaadin.server.SystemError;
+import com.vaadin.server.WrappedRequest;
 import com.vaadin.shared.ui.label.ContentMode;
 import com.vaadin.ui.Label;
 import com.vaadin.ui.Panel;
 import com.vaadin.ui.UI;
+import com.vaadin.util.CurrentInstance;
 
 /**
  * Provides sample directory based on application directory. If this fails then
@@ -46,7 +48,8 @@ public class SampleDirectory {
                 + "context base directory failed, "
                 + "possible security constraint with Application "
                 + "Server or Servlet Container.<br />";
-        File file = application.getContext().getBaseDirectory();
+        File file = CurrentInstance.get(WrappedRequest.class)
+                .getDeploymentConfiguration().getBaseDirectory();
         if ((file == null) || (!file.canRead())
                 || (file.getAbsolutePath() == null)) {
             // cannot access example directory, possible security issue with