Package muntjac :: Package terminal :: Module class_resource
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.class_resource

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  from muntjac.service.file_type_resolver import FileTypeResolver 
 17  from muntjac.terminal.application_resource import IApplicationResource 
 18  from muntjac.terminal.download_stream import DownloadStream 
 19   
 20   
21 -class ClassResource(IApplicationResource):
22 """C{ClassResource} is a named resource accessed with the 23 class loader. 24 25 This can be used to access resources such as icons, files, etc. 26 27 @author: Vaadin Ltd. 28 @author: Richard Lincoln 29 @version: 1.1.2 30 """ 31
32 - def __init__(self, *args):
33 """Creates a new application resource instance. The resource id is 34 relative to the location of the application class. 35 36 @param args: tuple of the form 37 - (resourceName, application) 38 1. the Unique identifier of the resource within the application 39 2. the application this resource will be added to 40 - (associatedClass, resourceName, application) 41 1. the class of the which the resource is associated. 42 2. the Unique identifier of the resource within the application 43 3. the application this resource will be added to 44 """ 45 # Default buffer size for this stream resource. 46 self._bufferSize = 0 47 48 # Default cache time for this stream resource. 49 self._cacheTime = self.DEFAULT_CACHETIME 50 51 # Associated class used for indetifying the source of the resource. 52 self._associatedClass = None 53 54 # Name of the resource is relative to the associated class. 55 self._resourceName = None 56 57 # Application used for serving the class. 58 self._application = None 59 60 nargs = len(args) 61 if nargs == 2: 62 resourceName, application = args 63 self._associatedClass = application.__class__ 64 self._resourceName = resourceName 65 self._application = application 66 if resourceName is None: 67 raise ValueError 68 application.addResource(self) 69 elif nargs == 3: 70 associatedClass, resourceName, application = args 71 self._associatedClass = associatedClass 72 self._resourceName = resourceName 73 self._application = application 74 if (resourceName is None) or (associatedClass is None): 75 raise ValueError 76 application.addResource(self) 77 else: 78 raise ValueError, 'invalid number of arguments'
79 80
81 - def getMIMEType(self):
82 """Gets the MIME type of this resource. 83 84 @see: L{muntjac.terminal.resource.IResource.getMIMEType} 85 """ 86 return FileTypeResolver.getMIMEType(self._resourceName)
87 88
89 - def getApplication(self):
90 """Gets the application of this resource. 91 92 @see: L{IApplicationResource.getApplication} 93 """ 94 return self._application
95 96
97 - def getFilename(self):
98 """Gets the virtual filename for this resource. 99 100 @return: the file name associated to this resource. 101 @see: L{IApplicationResource.getFilename} 102 """ 103 index = 0 104 idx = self._resourceName.find('/', index) 105 while idx > 0 and idx + 1 < len(self._resourceName): 106 index = idx + 1 107 idx = self._resourceName.find('/', index) 108 return self._resourceName[index:]
109 110
111 - def getStream(self):
112 """Gets resource as stream. 113 114 @see: L{IApplicationResource.getStream} 115 """ 116 ds = DownloadStream( 117 self._associatedClass.getResourceAsStream(self._resourceName), 118 self.getMIMEType(), self.getFilename()) 119 ds.setBufferSize(self.getBufferSize()) 120 ds.setCacheTime(self._cacheTime) 121 return ds
122 123
124 - def getBufferSize(self):
125 return self._bufferSize
126 127
128 - def setBufferSize(self, bufferSize):
129 """Sets the size of the download buffer used for this resource. 130 131 @param bufferSize: 132 the size of the buffer in bytes. 133 """ 134 self._bufferSize = bufferSize
135 136
137 - def getCacheTime(self):
138 return self._cacheTime
139 140
141 - def setCacheTime(self, cacheTime):
142 """Sets the length of cache expiration time. 143 144 This gives the adapter the possibility cache streams sent to the 145 client. The caching may be made in adapter or at the client if the 146 client supports caching. Zero or negative value disables the caching 147 of this stream. 148 149 @param cacheTime: 150 the cache time in milliseconds. 151 """ 152 self._cacheTime = cacheTime
153