import os, time from Queue import Queue from AdminSecurity import AdminSecurity from WebUtils.Funcs import htmlEncode class ServletCache(AdminSecurity): """Display servlet cache. This servlet displays, in a readable form, the internal data structure of the cache of all servlet factories. This can be useful for debugging WebKit problems and the information is interesting in general. """ def title(self): return 'Servlet Cache' def writeContent(self): from WebKit.URLParser import ServletFactoryManager factories = filter(lambda f: f._classCache, ServletFactoryManager._factories) req = self.request() wr = self.writeln if len(factories) > 1: factories.sort() wr('

Servlet Factories:

') wr('') for factory in factories: wr('' % ((factory.name(),)*2)) wr('
%s
') wr('
') for factory in factories: name = factory.name() wr('

%s

' % ((name,)*2)) if req.hasField('flush_' + name): factory.flushCache() wr('

' 'The servlet cache has been flushed.   ' '

') continue wr(htCache(factory)) wr('
') def sortSplitFilenames(a, b): """Custom comparison function for file names. This is a utility function for list.sort() that handles list elements that come from os.path.split. We sort first by base filename and then by directory, case insensitive. """ result = cmp(a['base'].lower(), b['base'].lower()) if result == 0: result = cmp(a['dir'].lower(), b['dir'].lower()) return result def htCache(factory): """Output the cache of a servlet factory.""" html = [] wr = html.append cache = factory._classCache keys = cache.keys() keys.sort() wr('

Uniqueness: %s

' % factory.uniqueness()) wr('

Extensions: %s

' % ', '.join(map(repr, factory.extensions()))) wr('

Unique paths in the servlet cache: %d' '  

' % (len(keys), factory.name())) wr('

Click any link to jump to the details for that path.

') wr('
Filenames:
') wr('') wr('' '') paths = [] for key in keys: dir, base = os.path.split(key) path = {'dir': dir, 'base': base, 'full': key} paths.append(path) paths.sort(sortSplitFilenames) # At this point, paths is a list where each element is a tuple # of (basename, dirname, fullPathname) sorted first by basename # and second by dirname for path in paths: wr('' '' % (id(path['full']), path['base'], path['dir'])) wr('
FileDirectory
' '%s%s
') wr('
Full paths:
') wr('') for key in keys: wr('' % (id(key), key)) wr('
' '%s
') wr('
Details:
') wr('') for path in paths: wr('' % (id(path['full']), path['base'], path['dir'])) record = cache[path['full']].copy() record['path'] = path['full'] if factory._threadsafeServletCache.has_key(path['full']): record['instances'] = 'one servlet instance (threadsafe)' else: record['instances'] = ('free reusable servlets: %d' % len(factory._servletPool)) wr(htRecord(record)) wr('
' '

%s - %s

') return '\n'.join(html) def htRecord(record): html = [] wr = html.append keys = record.keys() keys.sort() for key in keys: htKey = htmlEncode(key) # determine the HTML for the value value = record[key] htValue = None # check for special cases where we want a custom display if hasattr(value, '__name__'): htValue = value.__name__ if key == 'mtime': htValue = '%s (%s)' % (time.asctime(time.localtime(value)), str(value)) # the general case: if not htValue: htValue = htmlEncode(str(value)) wr('%s' '%s' % (htKey, htValue)) return '\n'.join(html)