Annotation of parser3/src/main/pa_xslt_stylesheet_manager.C, revision 1.2
1.1 parser 1: /** @file
2: Parser: sql driver manager implementation.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7: */
1.2 ! parser 8: static const char *RCSId="$Id: pa_xslt_stylesheet_manager.C,v 1.1 2001/09/14 15:41:59 parser Exp $";
1.1 parser 9:
10: #include "pa_xslt_stylesheet_manager.h"
11: #include "pa_stylesheet_connection.h"
12: #include "ltdl.h"
13: #include "pa_exception.h"
14: #include "pa_common.h"
15: #include "pa_threads.h"
16:
1.2 ! parser 17: #if _MSC_VER
! 18: # pragma warning(disable:4291) // disable warning
! 19: // "no matching operator delete found; memory will not be freed if initialization throws an exception
! 20: #endif
1.1 parser 21:
22: // globals
23:
24: XSLT_Stylesheet_manager *XSLT_stylesheet_manager;
25:
26: // consts
27:
1.2 ! parser 28: const int EXPIRE_UNUSED_CONNECTION_SECONDS=5*60;
1.1 parser 29: const int CHECK_EXPIRED_CONNECTION_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
30:
31:
32: // XSLT_Stylesheet_manager
33:
34: Stylesheet_connection& XSLT_Stylesheet_manager::get_connection(const String& request_file_spec) {
35: Pool& pool=request_file_spec.pool(); // request pool
36:
37: // first trying to get cached stylesheet
38: Stylesheet_connection *result=get_connection_from_cache(request_file_spec);
39: if(!result) {
40: // then just construct it
41:
42: // make global_file_spec C-string on global pool
43: const char *request_file_spec_cstr=request_file_spec.cstr(String::UL_FILE_SPEC);
44: char *global_file_spec_cstr=(char *)malloc(strlen(request_file_spec_cstr)+1);
45: strcpy(global_file_spec_cstr, request_file_spec_cstr);
46: // make global_file_spec string on global pool
47: String& global_file_spec=*new(this->pool()) String(this->pool(), global_file_spec_cstr);
48:
49: result=new(this->pool()) Stylesheet_connection(this->pool(), global_file_spec);
50: }
51: // associate with services[request] (deassociates at close)
52: result->set_services(&pool);
53: // return it
54: return *result;
55: }
56:
57: void XSLT_Stylesheet_manager::close_connection(const String& file_spec,
58: Stylesheet_connection& connection) {
59: // deassociate from services[request]
60: connection.set_services(0);
61: put_connection_to_cache(file_spec, connection);
62: }
63:
64:
65: // stylesheet cache
66: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
67: Stylesheet_connection *XSLT_Stylesheet_manager::get_connection_from_cache(const String& file_spec) {
68: SYNCHRONIZED;
69:
70: maybe_expire_connection_cache();
71:
72: if(Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec)))
73: while(connections->top_index()>=0) { // there are cached stylesheets to that 'file_spec'
74: Stylesheet_connection *result=static_cast<Stylesheet_connection *>(connections->pop());
75: if(result->connected()) // not expired?
76: return result;
77: }
78:
79: return 0;
80: }
81:
82: void XSLT_Stylesheet_manager::put_connection_to_cache(const String& file_spec,
83: Stylesheet_connection& connection) {
84: SYNCHRONIZED;
85:
86: Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec));
87: if(!connections) { // there are no cached stylesheets to that 'file_spec' yet?
88: connections=NEW Stack(pool()); // NOTE: never freed up!
89: connection_cache.put(file_spec, connections);
90: }
91: connections->push(&connection);
92: }
93:
94: static void expire_connection(Array::Item *value, void *info) {
95: Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value);
96: time_t older_dies=reinterpret_cast<time_t>(info);
97:
98: if(connection.connected() && connection.expired(older_dies))
99: connection.disconnect();
100: }
101: static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
102: Stack& stack=*static_cast<Stack *>(value);
103: for(int i=0; i<=stack.top_index(); i++)
104: expire_connection(stack.get(i), info);
105: }
106: void XSLT_Stylesheet_manager::maybe_expire_connection_cache() {
107: time_t now=time(0);
108:
109: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTION_SECONDS) {
110: connection_cache.for_each(expire_connections,
111: reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
112:
113: prev_expiration_pass_time=now;
114: }
115: }
E-mail: