001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.base.terminal;
039:
040: import org.millstone.base.Application;
041: import org.millstone.base.service.FileTypeResolver;
042:
043: /** Class resource is a named resource accessed with the class loader.
044: *
045: * This can be used to access resources such as icons, files, etc.
046: * @see java.lang.Class#getResource(java.lang.String)
047: *
048: * @author IT Mill Ltd.
049: * @version 3.1.1
050: * @since 3.0
051: */
052: public class ClassResource implements ApplicationResource {
053:
054: /** Default buffer size for this stream resource */
055: private int bufferSize = 0;
056:
057: /** Default cache time for this stream resource */
058: private long cacheTime = DEFAULT_CACHETIME;
059:
060: /** Associated class used for indetifying the source of the resource */
061: private Class associatedClass;
062:
063: /** Name of the resource is relative to the associated class */
064: private String resourceName;
065:
066: /** Application used for serving the class */
067: private Application application;
068:
069: /** Create new application resource instance.
070: * The resource id is relative to the location of the application class.
071: *
072: * @param resourceName Unique identifier of the resource within the application.
073: * @param application The application this resource will be added to.
074: * */
075: public ClassResource(String resourceName, Application application) {
076: this .associatedClass = application.getClass();
077: this .resourceName = resourceName;
078: this .application = application;
079: if (resourceName == null)
080: throw new NullPointerException();
081: application.addResource(this );
082: }
083:
084: /** Create new application resource instance.
085: *
086: * @param associatedClass The class of the which the resource is associated.
087: * @param resourceName Unique identifier of the resource within the application.
088: * @param application The application this resource will be added to.
089: * */
090: public ClassResource(Class associatedClass, String resourceName,
091: Application application) {
092: this .associatedClass = associatedClass;
093: this .resourceName = resourceName;
094: this .application = application;
095: if (resourceName == null || associatedClass == null)
096: throw new NullPointerException();
097: application.addResource(this );
098: }
099:
100: public String getMIMEType() {
101: return FileTypeResolver.getMIMEType(this .resourceName);
102: }
103:
104: public Application getApplication() {
105: return application;
106: }
107:
108: public String getFilename() {
109: int index = 0;
110: int next = 0;
111: while ((next = resourceName.indexOf('/', index)) > 0
112: && next + 1 < resourceName.length())
113: index = next + 1;
114: return resourceName.substring(index);
115: }
116:
117: public DownloadStream getStream() {
118: DownloadStream ds = new DownloadStream(associatedClass
119: .getResourceAsStream(resourceName), getMIMEType(),
120: getFilename());
121: ds.setBufferSize(getBufferSize());
122: ds.setCacheTime(cacheTime);
123: return ds;
124: }
125:
126: /* documented in superclass */
127: public int getBufferSize() {
128: return bufferSize;
129: }
130:
131: /** Set the size of the download buffer used for this resource.
132: * @param bufferSize The size of the buffer in bytes.
133: */
134: public void setBufferSize(int bufferSize) {
135: this .bufferSize = bufferSize;
136: }
137:
138: /* documented in superclass */
139: public long getCacheTime() {
140: return cacheTime;
141: }
142:
143: /** Set lenght of cache expiracy time.
144: *
145: * <p>This gives the adapter the possibility cache streams sent to the
146: * client. The caching may be made in adapter or at the client if the
147: * client supports caching. Zero or negavive value disbales the
148: * caching of this stream.</p>
149: *
150: * @param cacheTime The cache time in milliseconds.
151: *
152: */
153: public void setCacheTime(long cacheTime) {
154: this.cacheTime = cacheTime;
155: }
156: }
|