source: mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py @ 8425

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py@8425
Revision 8425, 4.6 KB checked in by mnagni, 7 years ago (diff)

Complete - # 22488: CEDA Observation Collection - Geographical Extent
 http://team.ceda.ac.uk/trac/ceda/ticket/22488
Complete - # 22513: Missing ValidTime? tag in the CedaSearch?
 http://team.ceda.ac.uk/trac/ceda/ticket/22513

Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 9 Jan 2012
30
31@author: Maurizio Nagni
32'''
33from MolesManager.moles3epb import Moles3EPBFactory
34from libs.migration.client import MigrationThread
35from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\
36    MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, MOLES3_DB_CONNECTION,\
37    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION
38from libs.commons_db import DbManager
39from libs.migration.InfodbEPB import InfodbEPBFactory
40from libs.migration.MigrationEPB import MigrationEPBFactory
41from sqlalchemy import exc
42from django.core.context_processors import csrf
43from django.shortcuts import render_to_response
44
45
46
47class MolesSessionMiddleware(object):
48   
49    #This attribute should be not here.
50    #unfortunately I cannot find any start/stop signals from Django
51    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
52    _epbInitialized = False
53    _infodbEPBFactory = None   
54    _moles3EPBFactory = None
55    _migrationEPBFactory = None
56   
57    def _doInitialization(self):
58
59        infoDB = DbManager(INFO_DB_CONNECTION)
60        MolesSessionMiddleware._infodbEPBFactory = InfodbEPBFactory(infoDB)
61        #InfodbEPB.overrrideDBManager(infoDB)
62           
63        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
64        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)               
65
66        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
67        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
68       
69        MolesSessionMiddleware._migrationThread = MigrationThread( \
70            MolesSessionMiddleware._moles3EPBFactory.createEPB(), \
71            MolesSessionMiddleware._migrationEPBFactory.createEPB(), \
72            MolesSessionMiddleware._infodbEPBFactory.createEPB(), \
73            interval = MIGRATION_INTERVAL )
74        MolesSessionMiddleware._epbInitialized = True     
75           
76        _migration()     
77   
78    def process_request(self, request): 
79        try:
80            if not MolesSessionMiddleware._epbInitialized:
81                self._doInitialization()
82                 
83            request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createEPB()                   
84        except exc.DBAPIError, e:
85            # an exception is raised, Connection is invalidated.
86            #if e.connection_invalidated:
87            #    print "Connection was invalidated!"
88            return self.process_exception(request, e)
89
90    def process_response(self, request, response):
91        if hasattr(request, "moles3EPB"):           
92            request.moles3EPB.close()
93           
94        return response
95
96    def process_exception(self, request, exception):
97        c = {}
98        c['exception'] = exception
99        c.update(csrf(request))
100        return render_to_response('errorTemplate.html', c)
101   
102def _migration(runMigration = RUN_MIGRATION):
103    if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
104        #t.setDaemon(False)
105        MolesSessionMiddleware._migrationThread.start()       
Note: See TracBrowser for help on using the repository browser.