1 /*
2 * Copyright 2006-2008 The FLWOR Foundation.
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 #include "stdafx.h"
17
18 #include "api/zorbaimpl.h"
19
20 #include <istream>
21 #include <zorba/diagnostic_list.h>
22 //#include <zorba/function.h>
23 #include <zorba/store_manager.h>
24 #include <zorba/query_location.h>
25
26 #include "api/xqueryimpl.h"
27 #include "api/staticcontextimpl.h"
28 #include "api/itemfactoryimpl.h"
29 #include "api/unmarshaller.h"
30 #include "api/xmldatamanagerimpl.h"
31 #include "api/vectoriterator.h"
32 #include "api/auditimpl.h"
33
34 #include "zorbautils/fatal.h"
35 #include "diagnostics/xquery_diagnostics.h"
36
37 #include "system/globalenv.h"
38 #include "system/properties.h"
39
40 #include "context/static_context.h"
41
42
43 namespace zorba {
44
45
46 class ZorbaImplStatics
47 {
48 public:
~ZorbaImplStatics()49 ~ZorbaImplStatics()
50 {
51 GlobalEnvironment::destroyStatics();
52 }
53 } g_zorbaImplStaticsDestroyer;
54
55
56 #ifdef WIN32
57 bool ZorbaImpl::ctrl_c_signaled = false;
58
CtrlCHandlerRoutine(__in DWORD dwCtrlType)59 BOOL WINAPI CtrlCHandlerRoutine(__in DWORD dwCtrlType)
60 {
61 switch( dwCtrlType )
62 {
63 case CTRL_LOGOFF_EVENT:
64 break;
65 case CTRL_C_EVENT:
66 case CTRL_CLOSE_EVENT:
67 case CTRL_BREAK_EVENT:
68 case CTRL_SHUTDOWN_EVENT:
69 default:
70 ZorbaImpl::ctrl_c_signaled = true;
71 }
72 return FALSE;
73 }
74 #endif
75
ZorbaImpl()76 ZorbaImpl::ZorbaImpl() : theNumUsers(0)
77 {
78 #ifdef WIN32
79 SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlCHandlerRoutine, TRUE);
80 #endif
81 }
82
83
~ZorbaImpl()84 ZorbaImpl::~ZorbaImpl()
85 {
86 shutdownInternal(false);
87 }
88
89
90 /*******************************************************************************
91 Protected function to initialize the Zorba Engine.
92 It's called from the static Zorba::getInstance() method.
93 ********************************************************************************/
init(store::Store * store)94 void ZorbaImpl::init(store::Store* store)
95 {
96 SYNC_CODE(AutoMutex lock(&theUsersMutex);)
97
98 if (theNumUsers == 0)
99 {
100 void* store2 = StoreManager::getStore();
101 (void)store2;
102 assert(store2 == store);
103 GlobalEnvironment::init(store);
104 }
105
106 ++theNumUsers;
107 }
108
109
110 /*******************************************************************************
111 Implements Zorba::shutdown in the API.
112
113 The function is either called explicitly by the user or implicitly at
114 destruction of the singleton instance. The "soft" param differentiates
115 between these 2 cases: if "true", then it's a user-requested shutdown,
116 else it's a shutdown invoked from the destructor.
117 ********************************************************************************/
shutdownInternal(bool soft)118 void ZorbaImpl::shutdownInternal(bool soft)
119 {
120 SYNC_CODE(AutoMutex lock(&theUsersMutex);)
121
122 if (theNumUsers == 0)
123 return;
124
125 --theNumUsers;
126
127 if (theNumUsers == 0 || soft == false)
128 {
129 Loki::DeletableSingleton<ItemFactoryImpl>::GracefulDelete();
130 Loki::DeletableSingleton<XmlDataManagerImpl>::GracefulDelete();
131 void* store = &GENV_STORE;
132 GlobalEnvironment::destroy();
133 StoreManager::shutdownStore(store);
134 }
135 }
136
137
createQuery(DiagnosticHandler * aDiagnosticHandler)138 XQuery_t ZorbaImpl::createQuery(DiagnosticHandler* aDiagnosticHandler)
139 {
140 XQuery_t lXQuery(new XQueryImpl());
141 if (aDiagnosticHandler != 0)
142 lXQuery->registerDiagnosticHandler(aDiagnosticHandler);
143 return lXQuery;
144 }
145
146
compileQuery(const String & aQuery,DiagnosticHandler * aDiagnosticHandler)147 XQuery_t ZorbaImpl::compileQuery(
148 const String& aQuery,
149 DiagnosticHandler* aDiagnosticHandler)
150 {
151 Zorba_CompilerHints_t lHints;
152 return compileQuery(aQuery, lHints, aDiagnosticHandler);
153 }
154
155
compileQuery(const String & aQuery,const StaticContext_t & aStaticContext,DiagnosticHandler * aDiagnosticHandler)156 XQuery_t ZorbaImpl::compileQuery(
157 const String& aQuery,
158 const StaticContext_t& aStaticContext,
159 DiagnosticHandler* aDiagnosticHandler)
160 {
161 Zorba_CompilerHints_t lHints;
162 return compileQuery(aQuery, aStaticContext, lHints, aDiagnosticHandler);
163 }
164
165
compileQuery(const String & aQuery,const Zorba_CompilerHints_t & aHints,DiagnosticHandler * aDiagnosticHandler)166 XQuery_t ZorbaImpl::compileQuery(
167 const String& aQuery,
168 const Zorba_CompilerHints_t& aHints,
169 DiagnosticHandler* aDiagnosticHandler)
170 {
171 XQuery_t lXQuery(new XQueryImpl());
172 if (aDiagnosticHandler != 0)
173 lXQuery->registerDiagnosticHandler(aDiagnosticHandler);
174 lXQuery->compile(aQuery, aHints);
175 return lXQuery;
176 }
177
178
compileQuery(const String & aQuery,const StaticContext_t & aStaticContext,const Zorba_CompilerHints_t & aHints,DiagnosticHandler * aDiagnosticHandler)179 XQuery_t ZorbaImpl::compileQuery(
180 const String& aQuery,
181 const StaticContext_t& aStaticContext,
182 const Zorba_CompilerHints_t& aHints,
183 DiagnosticHandler* aDiagnosticHandler)
184 {
185 XQuery_t lXQuery(new XQueryImpl());
186 if (aDiagnosticHandler != 0)
187 lXQuery->registerDiagnosticHandler(aDiagnosticHandler);
188 lXQuery->compile(aQuery, aStaticContext, aHints);
189 return lXQuery;
190 }
191
192
compileQuery(std::istream & aQuery,DiagnosticHandler * aDiagnosticHandler)193 XQuery_t ZorbaImpl::compileQuery(std::istream& aQuery, DiagnosticHandler* aDiagnosticHandler)
194 {
195 Zorba_CompilerHints_t lHints;
196 return compileQuery(aQuery, lHints, aDiagnosticHandler);
197 }
198
199
compileQuery(std::istream & aQuery,const StaticContext_t & aStaticContext,DiagnosticHandler * aDiagnosticHandler)200 XQuery_t ZorbaImpl::compileQuery(
201 std::istream& aQuery,
202 const StaticContext_t& aStaticContext,
203 DiagnosticHandler* aDiagnosticHandler)
204 {
205 Zorba_CompilerHints_t lHints;
206 return compileQuery(aQuery, aStaticContext, lHints, aDiagnosticHandler);
207 }
208
209
compileQuery(std::istream & aQuery,const Zorba_CompilerHints_t & aHints,DiagnosticHandler * aDiagnosticHandler)210 XQuery_t ZorbaImpl::compileQuery(
211 std::istream& aQuery,
212 const Zorba_CompilerHints_t& aHints,
213 DiagnosticHandler* aDiagnosticHandler)
214 {
215 XQuery_t lXQuery(new XQueryImpl());
216 if (aDiagnosticHandler != 0)
217 lXQuery->registerDiagnosticHandler(aDiagnosticHandler);
218 lXQuery->compile(aQuery, aHints);
219 return lXQuery;
220 }
221
222
compileQuery(std::istream & aQuery,const StaticContext_t & aStaticContext,const Zorba_CompilerHints_t & aHints,DiagnosticHandler * aDiagnosticHandler)223 XQuery_t ZorbaImpl::compileQuery(
224 std::istream& aQuery,
225 const StaticContext_t& aStaticContext,
226 const Zorba_CompilerHints_t& aHints,
227 DiagnosticHandler* aDiagnosticHandler)
228 {
229 XQuery_t lXQuery(new XQueryImpl());
230 if (aDiagnosticHandler != 0)
231 lXQuery->registerDiagnosticHandler(aDiagnosticHandler);
232 lXQuery->compile(aQuery, aStaticContext, aHints);
233 return lXQuery;
234 }
235
236
237 /*******************************************************************************
238
239 ********************************************************************************/
createStaticContext(DiagnosticHandler * aDiagnosticHandler)240 StaticContext_t ZorbaImpl::createStaticContext(DiagnosticHandler* aDiagnosticHandler)
241 {
242 return StaticContext_t(new StaticContextImpl(aDiagnosticHandler));
243 }
244
245
246 /*******************************************************************************
247
248 ********************************************************************************/
getItemFactory()249 ItemFactory* ZorbaImpl::getItemFactory()
250 {
251 return &ItemFactorySingleton::Instance();
252 }
253
254
255 /*******************************************************************************
256
257 ********************************************************************************/
getXmlDataManager()258 XmlDataManager* ZorbaImpl::getXmlDataManager()
259 {
260 return &XmlDataManagerSingleton::Instance();
261 }
262
263
264 /*******************************************************************************
265
266 ********************************************************************************/
getAuditProvider()267 audit::Provider* ZorbaImpl::getAuditProvider()
268 {
269 return &audit::PROVIDER_IMPL;
270 }
271
272
273 /*******************************************************************************
274
275 ********************************************************************************/
getPropertiesGlobal()276 PropertiesGlobal* ZorbaImpl::getPropertiesGlobal()
277 {
278 return Properties::instance();
279 }
280
281
notifyError(DiagnosticHandler * eh,ZorbaException const & ze)282 void ZorbaImpl::notifyError( DiagnosticHandler *eh, ZorbaException const &ze ) {
283 eh->error( ze );
284 }
285
notifyError(DiagnosticHandler * eh,char const * what)286 void ZorbaImpl::notifyError( DiagnosticHandler *eh, char const *what ) {
287 eh->error(
288 ZORBA_EXCEPTION( zerr::ZXQP0003_INTERNAL_ERROR, ERROR_PARAMS( what ) )
289 );
290 }
291
notifyError(DiagnosticHandler * eh)292 void ZorbaImpl::notifyError( DiagnosticHandler *eh ) {
293 eh->error( ZORBA_EXCEPTION( zerr::ZXQP0003_INTERNAL_ERROR ) );
294 }
295
notifyWarning(DiagnosticHandler * dh,XQueryWarning const & xqw)296 void ZorbaImpl::notifyWarning(DiagnosticHandler* dh, XQueryWarning const& xqw)
297 {
298 dh->warning(xqw);
299 }
300
checkItem(const store::Item_t & aItem)301 void ZorbaImpl::checkItem(const store::Item_t& aItem)
302 {
303 if (!aItem)
304 throw ZORBA_EXCEPTION(
305 zerr::ZAPI0014_INVALID_ARGUMENT, ERROR_PARAMS( "null", ZED( BadItem ) )
306 );
307 }
308
309
310 } // namespace zorba
311 /* vim:set et sw=2 ts=2: */
312