GME  13
CurlNetAccessor.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: CurlNetAccessor.cpp 471747 2006-11-06 14:31:56Z amassari $
00020  */
00021 
00022 #include <xercesc/util/XMLUniDefs.hpp>
00023 #include <xercesc/util/XMLUni.hpp>
00024 #include <xercesc/util/XMLString.hpp>
00025 #include <xercesc/util/XMLExceptMsgs.hpp>
00026 #include <xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp>
00027 #include <xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp>
00028 
00029 XERCES_CPP_NAMESPACE_BEGIN
00030 
00031 const XMLCh CurlNetAccessor::fgMyName[] =
00032 {
00033     chLatin_C, chLatin_u, chLatin_r, chLatin_l, chLatin_N, chLatin_e,
00034     chLatin_t, chLatin_A, chLatin_c, chLatin_c, chLatin_e, chLatin_s,
00035     chLatin_s, chLatin_o, chLatin_r, chNull
00036 };
00037 
00038 
00039 CurlNetAccessor::CurlNetAccessor()
00040 {
00041         initCurl();
00042 }
00043 
00044 
00045 CurlNetAccessor::~CurlNetAccessor()
00046 {
00047         cleanupCurl();
00048 }
00049 
00050 
00051 //
00052 // Global once-only init and cleanup of curl
00053 //
00054 // The init count used here is not thread protected; we assume
00055 // that creation of the CurlNetAccessor will be serialized by
00056 // the application. If the application is also using curl, then
00057 // care must be taken that curl is initialized only once, by some
00058 // other means, or by overloading these methods.
00059 //
00060 int CurlNetAccessor::fgCurlInitCount = 0;
00061 
00062 void
00063 CurlNetAccessor::initCurl()
00064 {
00065         if (fgCurlInitCount++ == 0)
00066                 curl_global_init(       0
00067                                                   | CURL_GLOBAL_ALL                     // Initialize all curl modules
00068                                         //        | CURL_GLOBAL_WIN32           // Initialize Windows sockets first
00069                                         //        | CURL_GLOBAL_SSL                     // Initialize SSL first
00070                                                   );
00071 }
00072 
00073 
00074 void
00075 CurlNetAccessor::cleanupCurl()
00076 {
00077         if (fgCurlInitCount > 0 && --fgCurlInitCount == 0)
00078                 curl_global_cleanup();
00079 }
00080 
00081 
00082 BinInputStream*
00083 CurlNetAccessor::makeNew(const XMLURL&  urlSource, const XMLNetHTTPInfo* httpInfo/*=0*/)
00084 {
00085         // Just create a CurlURLInputStream
00086         // We defer any checking of the url type for curl in CurlURLInputStream
00087         CurlURLInputStream* retStrm =
00088                 new (urlSource.getMemoryManager()) CurlURLInputStream(urlSource, httpInfo);
00089         return retStrm;            
00090 }
00091 
00092 XERCES_CPP_NAMESPACE_END
00093