1 14 package org.wings; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.externalizer.ExternalizeManager; 19 import org.wings.io.Device; 20 import org.wings.session.PropertyService; 21 import org.wings.session.SessionManager; 22 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 32 public abstract class StaticResource extends Resource { 33 private final transient static Log log = LogFactory.getLog(StaticResource.class); 34 37 protected int externalizerFlags = ExternalizeManager.FINAL; 38 39 42 protected transient LimitedBuffer buffer; 43 44 47 protected int maxBufferSize = -1; 48 49 53 protected int size = -1; 54 55 59 protected final static class LimitedBuffer extends ByteArrayOutputStream { 60 public static final int MAX_SIZE_TO_BUFFER = 8 * 1024; 62 private boolean withinLimit; 63 64 private int maxSizeToBuffer = MAX_SIZE_TO_BUFFER; 65 66 69 LimitedBuffer() { 70 74 super(64); 75 withinLimit = true; 76 77 initMaxSizeToBuffer(); 78 } 79 80 84 LimitedBuffer(int maxSizeToBuffer) { 85 this(); 86 this.maxSizeToBuffer = maxSizeToBuffer; 87 } 88 89 private void initMaxSizeToBuffer() { 90 if (SessionManager.getSession() == null) 91 return; 92 Object prop = 93 SessionManager.getSession().getProperty("Resource.MaxSizeToBuffer"); 94 95 if (prop != null && 96 prop instanceof Number ) { 97 98 maxSizeToBuffer = ((Number ) prop).intValue(); 99 } 100 } 101 102 106 public void write(byte[] b, int off, int len) { 107 if (!withinLimit) return; 108 withinLimit = (count + len < maxSizeToBuffer); 109 if (withinLimit) 110 super.write(b, off, len); 111 else 112 reset(); } 114 115 117 121 public boolean isValid() { 122 return withinLimit; 123 } 124 125 128 public void setValid(boolean valid) { 129 withinLimit = valid; 130 } 131 132 136 public byte[] getBytes() { 137 return buf; 138 } 139 140 143 public void writeTo(Device out) throws IOException { 144 out.write(buf, 0, size()); 145 } 146 } 147 148 151 protected StaticResource(String extension, String mimeType) { 152 super(extension, mimeType); 153 } 154 155 161 public String getId() { 162 if (id == null && SessionManager.getSession() != null) { 163 ExternalizeManager ext = SessionManager.getSession().getExternalizeManager(); 164 id = ext.getId(ext.externalize(this, externalizerFlags)); 165 log.debug("new " + getClass().getName() + " with id " + id); 166 } 167 return id; 168 } 169 170 public void setMimeType(String mimeType) { 171 this.mimeType = mimeType; 172 } 173 174 186 protected LimitedBuffer bufferResource() throws IOException { 187 if (buffer == null) { 188 if (maxBufferSize != -1) { 189 buffer = new LimitedBuffer(maxBufferSize); 190 } else { 191 buffer = new LimitedBuffer(); 192 } 193 InputStream resource = getResourceStream(); 194 if (resource != null) { 195 byte[] copyBuffer = new byte[1024]; 196 int read; 197 while (buffer.isValid() 198 && (read = resource.read(copyBuffer)) > 0) { 199 buffer.write(copyBuffer, 0, read); 200 } 201 resource.close(); 202 if (buffer.isValid()) { 203 size = buffer.size(); 204 } 205 } else { 206 log.fatal("Resource returned empty stream: " + this); 207 buffer.setValid(false); 208 } 209 } 210 return buffer; 211 } 212 213 222 public final void write(Device out) throws IOException { 223 226 if (buffer == null) { 227 bufferResource(); 228 if (buffer == null) return; 230 } 231 232 if (buffer.isValid()) { buffer.writeTo(out); 234 } else { InputStream resource = getResourceStream(); 236 if (resource != null) { 237 int deliverSize = 0; 238 byte[] copyBuffer = new byte[1024]; 239 int read; 240 while ((read = resource.read(copyBuffer)) > 0) { 241 out.write(copyBuffer, 0, read); 242 deliverSize += read; 243 } 244 resource.close(); 245 size = deliverSize; 246 } 247 } 248 249 out.flush(); 250 } 251 252 255 public final int getLength() { 256 return size; 257 } 258 259 public SimpleURL getURL() { 260 String name = getId(); 261 262 if ((externalizerFlags & ExternalizeManager.GLOBAL) > 0) { 264 return new SimpleURL(name); 265 } else { 266 RequestURL requestURL = (RequestURL) getPropertyService().getProperty("request.url"); 267 requestURL = (RequestURL) requestURL.clone(); 268 requestURL.setResource(name); 269 return requestURL; 270 } 271 } 272 273 private PropertyService propertyService; 274 275 protected PropertyService getPropertyService() { 276 if (propertyService == null) 277 propertyService = (PropertyService) SessionManager.getSession(); 278 return propertyService; 279 } 280 281 282 public String toString() { 283 return getId(); 284 } 285 286 290 public void setExternalizerFlags(int flags) { 291 externalizerFlags = flags; 292 } 293 294 public int getExternalizerFlags() { 295 return externalizerFlags; 296 } 297 298 protected static String resolveName(Class baseClass, String fileName) { 299 if (fileName == null) { 300 return fileName; 301 } 302 if (!fileName.startsWith("/")) { 303 while (baseClass.isArray()) { 304 baseClass = baseClass.getComponentType(); 305 } 306 String baseName = baseClass.getName(); 307 int index = baseName.lastIndexOf('.'); 308 if (index != -1) { 309 fileName = baseName.substring(0, index).replace('.', '/') 310 + "/" + fileName; 311 } 312 } else { 313 fileName = fileName.substring(1); 314 } 315 return fileName; 316 } 317 318 protected abstract InputStream getResourceStream() throws IOException ; 319 320 public int getMaxBufferSize() { 321 return maxBufferSize; 322 } 323 324 public void setMaxBufferSize(int maxBufferSize) { 325 this.maxBufferSize = maxBufferSize; 326 } 327 328 } 329 330 331 | Popular Tags |