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 2.9KB

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