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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal;
  5. import com.itmill.toolkit.Application;
  6. import com.itmill.toolkit.service.FileTypeResolver;
  7. /**
  8. * <code>ClassResource</code> is a named resource accessed with the class
  9. * loader.
  10. *
  11. * This can be used to access resources such as icons, files, etc.
  12. *
  13. * @see java.lang.Class#getResource(java.lang.String)
  14. *
  15. * @author IT Mill Ltd.
  16. * @version
  17. * @VERSION@
  18. * @since 3.0
  19. */
  20. public class ClassResource implements ApplicationResource {
  21. /**
  22. * Default buffer size for this stream resource.
  23. */
  24. private int bufferSize = 0;
  25. /**
  26. * Default cache time for this stream resource.
  27. */
  28. private long cacheTime = DEFAULT_CACHETIME;
  29. /**
  30. * Associated class used for indetifying the source of the resource.
  31. */
  32. private final Class associatedClass;
  33. /**
  34. * Name of the resource is relative to the associated class.
  35. */
  36. private final String resourceName;
  37. /**
  38. * Application used for serving the class.
  39. */
  40. private final Application application;
  41. /**
  42. * Creates a new application resource instance. The resource id is relative
  43. * to the location of the application class.
  44. *
  45. * @param resourceName
  46. * the Unique identifier of the resource within the
  47. * application.
  48. * @param application
  49. * the application this resource will be added to.
  50. */
  51. public ClassResource(String resourceName, Application application) {
  52. associatedClass = application.getClass();
  53. this.resourceName = resourceName;
  54. this.application = application;
  55. if (resourceName == null) {
  56. throw new NullPointerException();
  57. }
  58. application.addResource(this);
  59. }
  60. /**
  61. * Creates a new application resource instance.
  62. *
  63. * @param associatedClass
  64. * the class of the which the resource is associated.
  65. * @param resourceName
  66. * the Unique identifier of the resource within the
  67. * application.
  68. * @param application
  69. * the application this resource will be added to.
  70. */
  71. public ClassResource(Class associatedClass, String resourceName,
  72. Application application) {
  73. this.associatedClass = associatedClass;
  74. this.resourceName = resourceName;
  75. this.application = application;
  76. if (resourceName == null || associatedClass == null) {
  77. throw new NullPointerException();
  78. }
  79. application.addResource(this);
  80. }
  81. /**
  82. * Gets the MIME type of this resource.
  83. *
  84. * @see com.itmill.toolkit.terminal.Resource#getMIMEType()
  85. */
  86. public String getMIMEType() {
  87. return FileTypeResolver.getMIMEType(resourceName);
  88. }
  89. /**
  90. * Gets the application of this resource.
  91. *
  92. * @see com.itmill.toolkit.terminal.ApplicationResource#getApplication()
  93. */
  94. public Application getApplication() {
  95. return application;
  96. }
  97. /**
  98. * Gets the virtual filename for this resource.
  99. *
  100. * @return the file name associated to this resource.
  101. * @see com.itmill.toolkit.terminal.ApplicationResource#getFilename()
  102. */
  103. public String getFilename() {
  104. int index = 0;
  105. int next = 0;
  106. while ((next = resourceName.indexOf('/', index)) > 0
  107. && next + 1 < resourceName.length()) {
  108. index = next + 1;
  109. }
  110. return resourceName.substring(index);
  111. }
  112. /**
  113. * Gets resource as stream.
  114. *
  115. * @see com.itmill.toolkit.terminal.ApplicationResource#getStream()
  116. */
  117. public DownloadStream getStream() {
  118. final DownloadStream ds = new DownloadStream(associatedClass
  119. .getResourceAsStream(resourceName), getMIMEType(),
  120. getFilename());
  121. ds.setBufferSize(getBufferSize());
  122. ds.setCacheTime(cacheTime);
  123. return ds;
  124. }
  125. /* documented in superclass */
  126. public int getBufferSize() {
  127. return bufferSize;
  128. }
  129. /**
  130. * Sets the size of the download buffer used for this resource.
  131. *
  132. * @param bufferSize
  133. * the size of the buffer in bytes.
  134. */
  135. public void setBufferSize(int bufferSize) {
  136. this.bufferSize = bufferSize;
  137. }
  138. /* documented in superclass */
  139. public long getCacheTime() {
  140. return cacheTime;
  141. }
  142. /**
  143. * Sets the length of cache expiration time.
  144. *
  145. * <p>
  146. * This gives the adapter the possibility cache streams sent to the client.
  147. * The caching may be made in adapter or at the client if the client
  148. * supports caching. Zero or negavive value disbales the caching of this
  149. * stream.
  150. * </p>
  151. *
  152. * @param cacheTime
  153. * the cache time in milliseconds.
  154. *
  155. */
  156. public void setCacheTime(long cacheTime) {
  157. this.cacheTime = cacheTime;
  158. }
  159. }