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