Package muntjac :: Package terminal :: Package gwt :: Package server :: Module application_runner_servlet
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.gwt.server.application_runner_servlet

  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  import re 
 17  import logging 
 18   
 19  from muntjac.terminal.gwt.server.exceptions import ServletException 
 20   
 21  from muntjac.terminal.gwt.server.abstract_application_servlet import \ 
 22      AbstractApplicationServlet 
 23   
 24  from muntjac.util import loadClass 
 25   
 26   
 27  logger = logging.getLogger(__name__) 
28 29 30 -class ApplicationRunnerServlet(AbstractApplicationServlet):
31
32 - def awake(self, transaction):
33 super(ApplicationRunnerServlet, self).awake(transaction) 34 35 # The name of the application class currently used. Only valid 36 # within one request. 37 self._defaultPackages = None 38 39 self._request = None # ThreadLocal() 40 41 initParameter = self.getApplicationOrSystemProperty('defaultPackages', 42 None) 43 if initParameter is not None: 44 self._defaultPackages = re.split(',', initParameter)
45 46
47 - def respond(self, transaction):
48 self._request = transaction.request() 49 50 super(ApplicationRunnerServlet, self).respond(transaction) 51 52 self._request = None
53 54
55 - def getApplicationUrl(self, request):
56 path = super(ApplicationRunnerServlet, self).getApplicationUrl(request) 57 58 path += self.getApplicationRunnerApplicationClassName(request) 59 path += '/' 60 61 return path
62 63
64 - def getNewApplication(self, request):
65 # Creates a new application instance 66 try: 67 application = self.getApplicationClass()() 68 return application 69 except TypeError: 70 raise ServletException('Failed to load application class: ' 71 + self.getApplicationRunnerApplicationClassName(request))
72 73
75 return self.getApplicationRunnerURIs(request).applicationClassname
76 77 78 @classmethod
79 - def getApplicationRunnerURIs(cls, request):
80 """Parses application runner URIs. 81 82 If request URL is e.g. 83 http://localhost:8080/muntjac/run/muntjac.demo.calc.Calc then 84 85 - context=muntjac 86 - Runner servlet=run 87 - Muntjac application=muntjac.demo.calc.Calc 88 89 @return: string array containing widgetset URI, application URI and 90 context, runner, application classname 91 """ 92 urlParts = re.split('\\/', request.uri()) 93 context = None 94 # String runner = null; 95 uris = URIS() 96 applicationClassname = None 97 contextPath = cls.getContextPath(request) 98 if urlParts[1] == re.sub('\\/', '', contextPath): 99 # class name comes after web context and runner application 100 context = urlParts[1] 101 # runner = urlParts[2] 102 if len(urlParts) == 3: 103 raise ValueError, 'No application specified' 104 105 applicationClassname = urlParts[3] 106 107 uris.staticFilesPath = '/' + context 108 # uris.applicationURI = "/" + context + "/" + runner + "/" 109 # + applicationClassname 110 # uris.context = context 111 # uris.runner = runner 112 uris.applicationClassname = applicationClassname 113 else: 114 # no context 115 context = '' 116 # runner = urlParts[1]; 117 if len(urlParts) == 2: 118 raise ValueError, 'No application specified' 119 120 applicationClassname = urlParts[2] 121 122 uris.staticFilesPath = '/' 123 # uris.applicationURI = "/" + runner + "/" + applicationClassname 124 # uris.context = context 125 # uris.runner = runner 126 uris.applicationClassname = applicationClassname 127 128 return uris
129 130
131 - def getApplicationClass(self):
132 appClass = None 133 134 baseName = self.getApplicationRunnerApplicationClassName(self._request) 135 136 try: 137 appClass = loadClass(baseName) 138 return appClass 139 except Exception: 140 if self._defaultPackages is not None: 141 for i in range(len(self._defaultPackages)): 142 try: 143 clsName = self._defaultPackages[i] + '.' + baseName 144 appClass = loadClass(clsName) 145 except TypeError: 146 pass # Ignore as this is expected for many packages 147 except Exception: 148 # TODO: handle exception 149 logger.info('Failed to find application ' 150 'class in the default package.') 151 152 if appClass is not None: 153 return appClass 154 155 raise TypeError, 'class not found exception'
156 157
158 - def getRequestPathInfo(self, request):
159 path = self.getPathInfo(request) 160 if path is None: 161 return None 162 clsName = self.getApplicationRunnerApplicationClassName(request) 163 path = path[1 + len(clsName):] 164 return path
165 166
167 - def getStaticFilesLocation(self, request):
168 uris = self._getApplicationRunnerURIs(request) 169 staticFilesPath = uris.staticFilesPath 170 if staticFilesPath == '/': 171 staticFilesPath = '' 172 return staticFilesPath
173
174 175 -class URIS(object):
176
177 - def __init__(self):
178 self.staticFilesPath = None 179 # self.applicationURI; 180 # self.context; 181 # self.runner; 182 self.applicationClassname = None
183