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.

ClassResource.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import java.io.Serializable;
  6. import com.vaadin.Application;
  7. import com.vaadin.service.FileTypeResolver;
  8. /**
  9. * <code>ClassResource</code> is a named resource accessed with the class
  10. * loader.
  11. *
  12. * This can be used to access resources such as icons, files, etc.
  13. *
  14. * @see java.lang.Class#getResource(java.lang.String)
  15. *
  16. * @author IT Mill Ltd.
  17. * @version
  18. * @VERSION@
  19. * @since 3.0
  20. */
  21. @SuppressWarnings("serial")
  22. public class ClassResource implements ApplicationResource, Serializable {
  23. /**
  24. * Default buffer size for this stream resource.
  25. */
  26. private int bufferSize = 0;
  27. /**
  28. * Default cache time for this stream resource.
  29. */
  30. private long cacheTime = DEFAULT_CACHETIME;
  31. /**
  32. * Associated class used for indetifying the source of the resource.
  33. */
  34. private final Class associatedClass;
  35. /**
  36. * Name of the resource is relative to the associated class.
  37. */
  38. private final String resourceName;
  39. /**
  40. * Application used for serving the class.
  41. */
  42. private final Application application;
  43. /**
  44. * Creates a new application resource instance. The resource id is relative
  45. * to the location of the application class.
  46. *
  47. * @param resourceName
  48. * the Unique identifier of the resource within the application.
  49. * @param application
  50. * the application this resource will be added to.
  51. */
  52. public ClassResource(String resourceName, Application application) {
  53. associatedClass = application.getClass();
  54. this.resourceName = resourceName;
  55. this.application = application;
  56. if (resourceName == null) {
  57. throw new NullPointerException();
  58. }
  59. application.addResource(this);
  60. }
  61. /**
  62. * Creates a new application resource instance.
  63. *
  64. * @param associatedClass
  65. * the class of the which the resource is associated.
  66. * @param resourceName
  67. * the Unique identifier of the resource within the 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.vaadin.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.vaadin.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.vaadin.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.vaadin.terminal.ApplicationResource#getStream()
  116. */
  117. public DownloadStream getStream() {
  118. final DownloadStream ds = new DownloadStream(
  119. associatedClass.getResourceAsStream(resourceName),
  120. getMIMEType(), 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. }