You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SampleDirectory.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.vaadin.tests.util;
  2. import java.io.File;
  3. import com.vaadin.server.SystemError;
  4. import com.vaadin.server.VaadinService;
  5. import com.vaadin.server.VaadinSession;
  6. import com.vaadin.shared.ui.ContentMode;
  7. import com.vaadin.ui.Label;
  8. import com.vaadin.ui.LegacyWindow;
  9. import com.vaadin.ui.Panel;
  10. import com.vaadin.ui.VerticalLayout;
  11. /**
  12. * Provides sample directory based on application directory. If this fails then
  13. * sampleDirectory property is read. If no sample directory is resolved, then a
  14. * panel displaying error message is added to main window.
  15. *
  16. * @author Vaadin Ltd.
  17. *
  18. */
  19. public class SampleDirectory {
  20. /**
  21. * Get sample directory.
  22. *
  23. * @param application
  24. * @return file pointing to sample directory
  25. */
  26. public static File getDirectory(VaadinSession application,
  27. LegacyWindow uI) {
  28. String errorMessage = "Access to application "
  29. + "context base directory failed, "
  30. + "possible security constraint with Application "
  31. + "Server or Servlet Container.<br />";
  32. File file = VaadinService.getCurrent().getBaseDirectory();
  33. if ((file == null) || (!file.canRead())
  34. || (file.getAbsolutePath() == null)) {
  35. // cannot access example directory, possible security issue with
  36. // Application Server or Servlet Container
  37. // Try to read sample directory from web.xml parameter
  38. String sampleDirProperty = application.getConfiguration()
  39. .getInitParameters().getProperty("sampleDirectory");
  40. if (sampleDirProperty != null) {
  41. file = new File(sampleDirProperty);
  42. if ((file != null) && (file.canRead())
  43. && (file.getAbsolutePath() != null)) {
  44. // Success using property
  45. return file;
  46. }
  47. // Failure using property
  48. errorMessage += "Failed also to access sample directory <b>["
  49. + sampleDirProperty
  50. + "]</b> defined in <b>sampleDirectory property</b>.";
  51. } else {
  52. // Failure using application context base dir, no property set
  53. errorMessage += "<b>Note: </b>You can set this manually in "
  54. + "web.xml by defining " + "sampleDirectory property.";
  55. }
  56. } else {
  57. // Success using application context base dir
  58. return file;
  59. }
  60. // Add failure notification as an Panel to main window
  61. VerticalLayout errorLayout = new VerticalLayout();
  62. errorLayout.setMargin(true);
  63. final Panel errorPanel = new Panel("Demo application error",
  64. errorLayout);
  65. errorPanel.setStyleName("strong");
  66. errorPanel.setComponentError(
  67. new SystemError("Cannot provide sample directory"));
  68. errorLayout.addComponent(new Label(errorMessage, ContentMode.HTML));
  69. // Remove all components from applications main window
  70. uI.removeAllComponents();
  71. // Add error panel
  72. uI.addComponent(errorPanel);
  73. return null;
  74. }
  75. }