update dev300-m58
[ooovba.git] / cli_ure / source / uno_bridge / cli_environment.cxx
blob1d40328e1b4267805d018bf1246540e55f458ed8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cli_environment.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cli_ure.hxx"
34 #include "Cli_environment.h"
36 #using <mscorlib.dll>
37 #using <cli_ure.dll>
38 #using <system.dll>
39 #include "osl/diagnose.h"
40 #include "cli_proxy.h"
42 using namespace System::Runtime::Remoting;
43 using namespace System::Runtime::Remoting::Proxies;
44 using namespace System::Collections;
45 using namespace System::Text;
46 using namespace System::Diagnostics;
47 using namespace System::Threading;
49 namespace cli_uno
52 inline System::String* Cli_environment::createKey(System::String* oid, System::Type* t)
54 return System::String::Concat(oid, t->get_FullName());
58 inline Cli_environment::Cli_environment()
60 #if OSL_DEBUG_LEVEL >= 2
61 _numRegisteredObjects = 0;
62 #endif
65 Cli_environment::~Cli_environment()
67 OSL_ENSURE(_numRegisteredObjects == 0,
68 "cli uno bridge: CLI environment contains unrevoked objects");
72 System::Object* Cli_environment::registerInterface(
73 System::Object* obj, System::String* oid)
75 #if OSL_DEBUG_LEVEL >= 1
76 //obj must be a transparent proxy
77 OSL_ASSERT(RemotingServices::IsTransparentProxy(obj));
78 _numRegisteredObjects ++;
79 #endif
80 OSL_ASSERT( ! m_objects->ContainsKey(oid));
81 m_objects->Add(oid, new WeakReference(obj));
82 return obj;
84 System::Object* Cli_environment::registerInterface (
85 System::Object* obj, System::String* oid, System::Type* type)
87 #if OSL_DEBUG_LEVEL >= 1
88 //obj must be a real cli object
89 OSL_ASSERT( ! RemotingServices::IsTransparentProxy(obj));
90 _numRegisteredObjects ++;
91 #endif
92 System::String* key = createKey(oid, type);
93 //see synchronization in map_uno2cli in cli_data.cxx
94 OSL_ASSERT( ! m_objects->ContainsKey(key));
95 m_objects->Add(key, new WeakReference(obj));
96 return obj;
99 void Cli_environment::revokeInterface(System::String* oid, System::Type* type)
101 System::String* key = type != NULL ? createKey(oid, type) : oid;
102 #if OSL_DEBUG_LEVEL >= 1
103 _numRegisteredObjects --;
104 #endif
105 #if OSL_DEBUG_LEVEL >= 2
106 int i = 1;
107 if (m_objects->ContainsKey(key) == false)
109 Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
110 Trace::WriteLine(oid);
111 i = 0;
113 Trace::WriteLine(System::String::Format(
114 new System::String(S"cli uno bridge: {0} remaining registered interfaces"),
115 __box(m_objects->get_Count() - 1)));
116 #endif
117 m_objects->Remove(key);
120 inline void Cli_environment::revokeInterface(System::String* oid)
122 return revokeInterface(oid, NULL);
125 System::Object* Cli_environment::getRegisteredInterface(System::String* oid,
126 System::Type* type)
128 //try if it is a UNO interface
129 System::Object* ret = NULL;
130 ret = m_objects->get_Item(oid);
131 if (! ret)
133 //try if if it is a proxy for a cli object
134 oid = createKey(oid, type);
135 ret = m_objects->get_Item( oid );
137 if (ret != 0)
139 System::WeakReference* weakIface =
140 static_cast< System::WeakReference * >( ret );
141 ret = weakIface->Target;
143 if (ret == 0)
144 m_objects->Remove( oid );
145 return ret;
148 System::String* Cli_environment::getObjectIdentifier(System::Object* obj)
150 System::String* oId= 0;
151 RealProxy* aProxy= RemotingServices::GetRealProxy(obj);
152 if (aProxy)
154 UnoInterfaceProxy* proxyImpl= dynamic_cast<UnoInterfaceProxy*>(aProxy);
155 if (proxyImpl)
156 oId= proxyImpl->getOid();
159 if (oId == 0)
161 StringBuilder * buf= new StringBuilder(256);
162 bool bFirst = false;
163 System::Threading::Monitor::Enter(__typeof(Cli_environment));
164 try {
165 buf->Append(m_IDGen->GetId(obj, & bFirst));
166 } __finally
168 System::Threading::Monitor::Exit(__typeof(Cli_environment));
171 buf->Append(sOidPart);
172 oId= buf->ToString();
174 return oId;
176 } //namespace cli_uno