summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJani Laakso <jani.laakso@itmill.com>2007-04-05 12:44:01 +0000
committerJani Laakso <jani.laakso@itmill.com>2007-04-05 12:44:01 +0000
commit215211a582e7321d9a8c64d6e21b83e1e0574a4c (patch)
treef071f489ae9d9b6a95b0762f1329b72fc8945c40 /src
parentf87235993e154b776a7b7ca641930d4434208fbc (diff)
downloadvaadin-framework-215211a582e7321d9a8c64d6e21b83e1e0574a4c.tar.gz
vaadin-framework-215211a582e7321d9a8c64d6e21b83e1e0574a4c.zip
Sample directory helper class.
svn changeset:1170/svn branch:trunk
Diffstat (limited to 'src')
-rw-r--r--src/com/itmill/toolkit/demo/util/SampleDirectory.java69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/com/itmill/toolkit/demo/util/SampleDirectory.java b/src/com/itmill/toolkit/demo/util/SampleDirectory.java
new file mode 100644
index 0000000000..95faf970c8
--- /dev/null
+++ b/src/com/itmill/toolkit/demo/util/SampleDirectory.java
@@ -0,0 +1,69 @@
+package com.itmill.toolkit.demo.util;
+
+import java.io.File;
+
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.terminal.SystemError;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.Panel;
+
+/**
+ * Provides sample directory based on application directory. If this fails then
+ * sampleDirectory property is read. If no sample directory is resolved, then a
+ * panel displaying error message is added to main window.
+ *
+ * @author IT Mill Ltd.
+ *
+ */
+public class SampleDirectory {
+
+ /**
+ * Get sample directory.
+ *
+ * @param application
+ * @return file pointing to sample directory
+ */
+ public static File getDirectory(Application application) {
+ String errorMessage = "Access to application "
+ + "context base directory failed, "
+ + "possible security constraint with Application "
+ + "Server or Servlet Container.<br />";
+ File file = application.getContext().getBaseDirectory();
+ if ((file == null) || (!file.canRead())
+ || (file.getAbsolutePath() == null)) {
+ // cannot access example directory, possible security issue with
+ // Application Server or Servlet Container
+ // Try to read sample directory from web.xml parameter
+ if (application.getProperty("sampleDirectory") != null) {
+ file = new File(application.getProperty("sampleDirectory"));
+ if ((file != null) && (file.canRead())
+ && (file.getAbsolutePath() != null)) {
+ // Success using property
+ return file;
+ }
+ // Failure using property
+ errorMessage += "Failed also to access sample directory <b>["
+ + application.getProperty("sampleDirectory")
+ + "]</b> defined in <b>sampleDirectory property</b>.";
+ } else {
+ // Failure using application context base dir, no property set
+ errorMessage += "<b>Note: </b>You can set this manually in "
+ + "web.xml by defining " + "sampleDirectory property.";
+ }
+ } else {
+ // Success using application context base dir
+ return file;
+ }
+ // Add failure notification as an Panel to main window
+ Panel errorPanel = new Panel("Demo application error");
+ errorPanel.setStyle("strong");
+ errorPanel.setComponentError(new SystemError(
+ "Cannot provide sample directory"));
+ errorPanel.addComponent(new Label(errorMessage, Label.CONTENT_XHTML));
+ // Remove all components from applications main window
+ application.getMainWindow().getLayout().removeAllComponents();
+ // Add error panel
+ application.getMainWindow().getLayout().addComponent(errorPanel);
+ return null;
+ }
+}