PlatformUtils.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:12k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Log: PlatformUtils.cpp,v $
  58.  * Revision 1.16  2001/10/25 21:52:28  peiyongz
  59.  * XMLDeleterFor related functions and data are removed.
  60.  *
  61.  * Revision 1.15  2001/10/24 19:59:49  peiyongz
  62.  * [Bug 3975] gInitFlag problem
  63.  *
  64.  * Revision 1.14  2001/10/23 23:09:32  peiyongz
  65.  * [Bug#880] patch to PlatformUtils:init()/term() and related. from Mark Weaver
  66.  *
  67.  * Revision 1.13  2001/05/11 13:26:27  tng
  68.  * Copyright update.
  69.  *
  70.  * Revision 1.12  2001/05/03 18:43:01  knoaman
  71.  * Added new option to the parsers so that the NEL (0x85) char can be treated as a newline character.
  72.  *
  73.  * Revision 1.11  2001/02/07 17:46:34  billsch
  74.  * Rearranged statements in Initialize() so that platformInit() is called
  75.  * before an XMLMutex is created.
  76.  *
  77.  * Revision 1.10  2000/07/25 20:55:23  jpolast
  78.  * use gInitFlag as a reference to the number of times
  79.  * Initialized was called.  this way, the terminate routines are
  80.  * not invoked until all processes call Terminate() on the parser.
  81.  *
  82.  * Revision 1.9  2000/06/26 20:30:04  jpolast
  83.  * check if initialized in Terminate() to stop access violations
  84.  * submitted by John_Roper@iOra.com
  85.  *
  86.  * Revision 1.8  2000/05/09 00:22:40  andyh
  87.  * Memory Cleanup.  XMLPlatformUtils::Terminate() deletes all lazily
  88.  * allocated memory; memory leak checking tools will no longer report
  89.  * that leaks exist.  (DOM GetElementsByTagID temporarily removed
  90.  * as part of this.)
  91.  *
  92.  * Revision 1.7  2000/03/24 19:50:29  roddey
  93.  * Clear the 'initialized' flag when the termination call is made. Probably
  94.  * not required technically, but...
  95.  *
  96.  * Revision 1.6  2000/03/02 19:54:44  roddey
  97.  * This checkin includes many changes done while waiting for the
  98.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  99.  * available elsewhere.
  100.  *
  101.  * Revision 1.5  2000/02/06 07:48:03  rahulj
  102.  * Year 2K copyright swat.
  103.  *
  104.  * Revision 1.4  2000/01/19 00:56:59  roddey
  105.  * Changes to get rid of dependence on old utils standard streams and to
  106.  * get rid of the fgLibLocation stuff.
  107.  *
  108.  * Revision 1.3  1999/12/18 00:18:10  roddey
  109.  * More changes to support the new, completely orthagonal support for
  110.  * intrinsic encodings.
  111.  *
  112.  * Revision 1.2  1999/12/15 19:41:28  roddey
  113.  * Support for the new transcoder system, where even intrinsic encodings are
  114.  * done via the same transcoder abstraction as external ones.
  115.  *
  116.  * Revision 1.1.1.1  1999/11/09 01:04:53  twl
  117.  * Initial checkin
  118.  *
  119.  * Revision 1.2  1999/11/08 20:45:11  rahul
  120.  * Swat for adding in Product name and CVS comment log variable.
  121.  *
  122.  */
  123. // ---------------------------------------------------------------------------
  124. //  Includes
  125. // ---------------------------------------------------------------------------
  126. #include <util/XMLMsgLoader.hpp>
  127. #include <util/Mutexes.hpp>
  128. #include <util/PlatformUtils.hpp>
  129. #include <util/RefVectorOf.hpp>
  130. #include <util/TransService.hpp>
  131. #include <util/XMLString.hpp>
  132. #include <util/XMLNetAccessor.hpp>
  133. #include <util/XMLUni.hpp>
  134. #include <internal/XMLReader.hpp>
  135. #include <util/RuntimeException.hpp>
  136. #include <util/XMLRegisterCleanup.hpp>
  137. #include <limits.h>
  138. // ---------------------------------------------------------------------------
  139. //  Local data members
  140. //
  141. //  gSyncMutex
  142. //      This is a mutex that will be used to synchronize access to some of
  143. //      the static data of the platform utilities class and here locally.
  144. // ---------------------------------------------------------------------------
  145. static XMLMutex*                gSyncMutex = 0;
  146. static long                     gInitFlag = 0;
  147. // ---------------------------------------------------------------------------
  148. //  Global data
  149. //
  150. // gXMLCleanupList
  151. // This is a list of cleanup functions to be called on 
  152. // XMLPlatformUtils::Terminate.  Their function is to reset static
  153. // data in classes that use it.
  154. //
  155. // gXMLCleanupListMutex
  156. // This is a mutex that will be used to synchronise access to the global
  157. // static data cleanup list
  158. // ---------------------------------------------------------------------------
  159. XMLRegisterCleanup* gXMLCleanupList = 0;
  160. XMLMutex* gXMLCleanupListMutex = 0;
  161. // ---------------------------------------------------------------------------
  162. //  XMLPlatformUtils: Static Data Members
  163. // ---------------------------------------------------------------------------
  164. XMLNetAccessor*     XMLPlatformUtils::fgNetAccessor = 0;
  165. XMLTransService*    XMLPlatformUtils::fgTransService = 0;
  166. // ---------------------------------------------------------------------------
  167. //  XMLPlatformUtils: Init/term methods
  168. // ---------------------------------------------------------------------------
  169. void XMLPlatformUtils::Initialize()
  170. {
  171.     //
  172.     //  Effects of overflow:
  173.     //  . resouce re-allocations
  174.     //  . consequently resource leaks
  175.     //  . potentially terminate() may never get executed
  176.     //
  177.     //  We got to prevent overflow from happening.
  178.     //  no error or exception
  179.     //  
  180.     if (gInitFlag == LONG_MAX)
  181.         return;
  182.     //
  183.     //  Make sure we haven't already been initialized. Note that this is not
  184.     //  thread safe and is not intended for that. Its more for those COM
  185.     //  like processes that cannot keep up with whether they have initialized
  186.     //  us yet or not.
  187.     //
  188.     gInitFlag++;
  189.     if (gInitFlag > 1)
  190.       return;
  191.     //
  192.     //  Call the platform init method, which is implemented in each of the
  193.     //  per-platform implementation cpp files. This one does the very low
  194.     //  level per-platform setup. It cannot use any XML util services at all,
  195.     //  i.e. only native services.
  196.     //
  197.     platformInit();
  198.     // Create the local sync mutex
  199.     gSyncMutex = new XMLMutex;
  200. // Create the mutex for the static data cleanup list
  201.     gXMLCleanupListMutex = new XMLMutex;
  202.     //
  203.     //  Ask the per-platform code to make the desired transcoding service for
  204.     //  us to use. This call cannot throw any exceptions or do anything that
  205.     //  cause any transcoding to happen. It should create the service and
  206.     //  return it or zero if it cannot.
  207.     //
  208.     //  This one also cannot use any utility services. It can only create a
  209.     //  transcoding service object and return it.
  210.     //
  211.     //  If we cannot make one, then we call panic to end the process.
  212.     //
  213.     fgTransService = makeTransService();
  214.     if (!fgTransService)
  215.         panic(Panic_NoTransService);
  216.     // Initialize the transcoder service
  217.     fgTransService->initTransService();
  218.     //
  219.     //  Try to create a default local code page transcoder. This is the one
  220.     //  that will be used internally by the XMLString class. If we cannot
  221.     //  create one, then call the panic method.
  222.     //
  223.     XMLLCPTranscoder* defXCode = XMLPlatformUtils::fgTransService->makeNewLCPTranscoder();
  224.     if (!defXCode)
  225.         panic(Panic_NoDefTranscoder);
  226.     XMLString::initString(defXCode);
  227.     //
  228.     //  Now lets ask the per-platform code to give us an instance of the type
  229.     //  of network access implementation he wants to use. This can return
  230.     //  a zero pointer if this platform doesn't want to support this.
  231.     //
  232.     fgNetAccessor = makeNetAccessor();
  233. }
  234. void XMLPlatformUtils::Terminate()
  235. {
  236.     //
  237.     // To prevent it from running underflow.
  238.     // otherwise we come to delete non-existing resources.
  239.     //
  240.     //  no error or exception
  241.     //  
  242.     if (gInitFlag == 0)
  243.         return;
  244. gInitFlag--;
  245. if (gInitFlag > 0)
  246. return;
  247.     // Delete any net accessor that got installed
  248.     delete fgNetAccessor;
  249.     fgNetAccessor = 0;
  250.     //
  251.     //  Call some other internal modules to give them a chance to clean up.
  252.     //  Do the string class last in case something tries to use it during
  253.     //  cleanup.
  254.     //
  255.     XMLString::termString();
  256.     // Clean up the the transcoding service
  257.     delete fgTransService;
  258.     fgTransService = 0;
  259.     // Clean up the sync mutex
  260.     delete gSyncMutex;
  261.     gSyncMutex = 0;
  262. // Clean up statically allocated, lazily cleaned data in each class
  263. // that has registered for it.
  264. // Note that calling doCleanup() also unregisters the cleanup
  265. // function, so that we are chewing the list down to nothing here
  266. while (gXMLCleanupList)
  267. gXMLCleanupList->doCleanup();
  268. // Clean up the mutex for accessing gXMLCleanupList
  269. delete gXMLCleanupListMutex;
  270. gXMLCleanupListMutex = 0;
  271.     //
  272.     //  And do platform termination. This cannot do use any XML services
  273.     //  at all, it can only clean up local stuff. It it reports an error,
  274.     //  it cannot use any XML exception or error reporting services.
  275.     //
  276.     platformTerm();
  277.     // And say we are no longer initialized
  278.     gInitFlag = 0;
  279. }
  280. // ---------------------------------------------------------------------------
  281. //  XMLPlatformUtils: Msg support methods
  282. // ---------------------------------------------------------------------------
  283. XMLMsgLoader* XMLPlatformUtils::loadMsgSet(const XMLCh* const msgDomain)
  284. {
  285.     //
  286.     //  Ask the platform support to load up the correct type of message
  287.     //  loader for the indicated message set. We don't check here whether it
  288.     //  works or not. That's their decision.
  289.     //
  290.     return loadAMsgSet(msgDomain);
  291. }
  292. // ---------------------------------------------------------------------------
  293. //  XMLPlatformUtils: NEL Character Handling
  294. // ---------------------------------------------------------------------------
  295. void XMLPlatformUtils::recognizeNEL(bool state) {
  296.     //Make sure initialize has been called
  297.     if (gInitFlag == 0) {
  298.         return;
  299.     }
  300.     if (state) {
  301.         if (!XMLReader::isNELRecognized()) {
  302.             XMLReader::enableNELWS();
  303.         }
  304.     }
  305.     else {
  306.         if (XMLReader::isNELRecognized()) {
  307.             ThrowXML(RuntimeException, XMLExcepts::NEL_RepeatedCalls);
  308.         }
  309.     }
  310. }
  311. bool XMLPlatformUtils::isNELRecognized() {
  312.     return XMLReader::isNELRecognized();
  313. }