merged tag ooo/DEV300_m102
[LibreOffice.git] / cli_ure / source / uno_bridge / cli_environment.cxx
blobcfa0f8e5781739cc58fe8615e5da9aaf7f69bf2c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cli_ure.hxx"
31 #include "Cli_environment.h"
33 #using <mscorlib.dll>
34 #using <cli_ure.dll>
35 #using <system.dll>
36 #include "osl/diagnose.h"
37 #include "cli_proxy.h"
39 using namespace System::Runtime::Remoting;
40 using namespace System::Runtime::Remoting::Proxies;
41 using namespace System::Collections;
42 using namespace System::Text;
43 using namespace System::Diagnostics;
44 using namespace System::Threading;
46 namespace cli_uno
49 inline System::String* Cli_environment::createKey(System::String* oid, System::Type* t)
51 return System::String::Concat(oid, t->get_FullName());
55 inline Cli_environment::Cli_environment()
57 #if OSL_DEBUG_LEVEL >= 2
58 _numRegisteredObjects = 0;
59 #endif
62 Cli_environment::~Cli_environment()
64 OSL_ENSURE(_numRegisteredObjects == 0,
65 "cli uno bridge: CLI environment contains unrevoked objects");
69 System::Object* Cli_environment::registerInterface(
70 System::Object* obj, System::String* oid)
72 #if OSL_DEBUG_LEVEL >= 1
73 //obj must be a transparent proxy
74 OSL_ASSERT(RemotingServices::IsTransparentProxy(obj));
75 _numRegisteredObjects ++;
76 #endif
77 OSL_ASSERT( ! m_objects->ContainsKey(oid));
78 m_objects->Add(oid, new WeakReference(obj));
79 return obj;
81 System::Object* Cli_environment::registerInterface (
82 System::Object* obj, System::String* oid, System::Type* type)
84 #if OSL_DEBUG_LEVEL >= 1
85 //obj must be a real cli object
86 OSL_ASSERT( ! RemotingServices::IsTransparentProxy(obj));
87 _numRegisteredObjects ++;
88 #endif
89 System::String* key = createKey(oid, type);
90 //see synchronization in map_uno2cli in cli_data.cxx
91 OSL_ASSERT( ! m_objects->ContainsKey(key));
92 m_objects->Add(key, new WeakReference(obj));
93 return obj;
96 void Cli_environment::revokeInterface(System::String* oid, System::Type* type)
98 System::String* key = type != NULL ? createKey(oid, type) : oid;
99 #if OSL_DEBUG_LEVEL >= 1
100 _numRegisteredObjects --;
101 #endif
102 #if OSL_DEBUG_LEVEL >= 2
103 int i = 1;
104 if (m_objects->ContainsKey(key) == false)
106 Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
107 Trace::WriteLine(oid);
108 i = 0;
110 Trace::WriteLine(System::String::Format(
111 new System::String(S"cli uno bridge: {0} remaining registered interfaces"),
112 __box(m_objects->get_Count() - 1)));
113 #endif
114 m_objects->Remove(key);
117 inline void Cli_environment::revokeInterface(System::String* oid)
119 return revokeInterface(oid, NULL);
122 System::Object* Cli_environment::getRegisteredInterface(System::String* oid,
123 System::Type* type)
125 //try if it is a UNO interface
126 System::Object* ret = NULL;
127 ret = m_objects->get_Item(oid);
128 if (! ret)
130 //try if if it is a proxy for a cli object
131 oid = createKey(oid, type);
132 ret = m_objects->get_Item( oid );
134 if (ret != 0)
136 System::WeakReference* weakIface =
137 static_cast< System::WeakReference * >( ret );
138 ret = weakIface->Target;
140 if (ret == 0)
141 m_objects->Remove( oid );
142 return ret;
145 System::String* Cli_environment::getObjectIdentifier(System::Object* obj)
147 System::String* oId= 0;
148 RealProxy* aProxy= RemotingServices::GetRealProxy(obj);
149 if (aProxy)
151 UnoInterfaceProxy* proxyImpl= dynamic_cast<UnoInterfaceProxy*>(aProxy);
152 if (proxyImpl)
153 oId= proxyImpl->getOid();
156 if (oId == 0)
158 StringBuilder * buf= new StringBuilder(256);
159 bool bFirst = false;
160 System::Threading::Monitor::Enter(__typeof(Cli_environment));
161 try {
162 buf->Append(m_IDGen->GetId(obj, & bFirst));
163 } __finally
165 System::Threading::Monitor::Exit(__typeof(Cli_environment));
168 buf->Append(sOidPart);
169 oId= buf->ToString();
171 return oId;
173 } //namespace cli_uno