Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / cli_ure / source / uno_bridge / cli_environment.cxx
blob87f7286635cd3dc4d449e3733ef5787fed7a7c00
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "Cli_environment.h"
31 #using <mscorlib.dll>
32 #using <cli_ure.dll>
33 #using <system.dll>
34 #include "osl/diagnose.h"
35 #include "cli_proxy.h"
37 using namespace System::Runtime::Remoting;
38 using namespace System::Runtime::Remoting::Proxies;
39 using namespace System::Collections;
40 using namespace System::Text;
41 using namespace System::Diagnostics;
42 using namespace System::Threading;
44 namespace cli_uno
47 inline System::String* Cli_environment::createKey(System::String* oid, System::Type* t)
49 return System::String::Concat(oid, t->get_FullName());
53 inline Cli_environment::Cli_environment()
55 #if OSL_DEBUG_LEVEL >= 2
56 _numRegisteredObjects = 0;
57 #endif
60 Cli_environment::~Cli_environment()
62 OSL_ENSURE(_numRegisteredObjects == 0,
63 "cli uno bridge: CLI environment contains unrevoked objects");
67 System::Object* Cli_environment::registerInterface(
68 System::Object* obj, System::String* oid)
70 #if OSL_DEBUG_LEVEL >= 1
71 //obj must be a transparent proxy
72 OSL_ASSERT(RemotingServices::IsTransparentProxy(obj));
73 _numRegisteredObjects ++;
74 #endif
75 OSL_ASSERT( ! m_objects->ContainsKey(oid));
76 m_objects->Add(oid, new WeakReference(obj));
77 return obj;
79 System::Object* Cli_environment::registerInterface (
80 System::Object* obj, System::String* oid, System::Type* type)
82 #if OSL_DEBUG_LEVEL >= 1
83 //obj must be a real cli object
84 OSL_ASSERT( ! RemotingServices::IsTransparentProxy(obj));
85 _numRegisteredObjects ++;
86 #endif
87 System::String* key = createKey(oid, type);
88 //see synchronization in map_uno2cli in cli_data.cxx
89 OSL_ASSERT( ! m_objects->ContainsKey(key));
90 m_objects->Add(key, new WeakReference(obj));
91 return obj;
94 void Cli_environment::revokeInterface(System::String* oid, System::Type* type)
96 System::String* key = type != NULL ? createKey(oid, type) : oid;
97 #if OSL_DEBUG_LEVEL >= 1
98 _numRegisteredObjects --;
99 #endif
100 #if OSL_DEBUG_LEVEL >= 2
101 int i = 1;
102 if (m_objects->ContainsKey(key) == false)
104 Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
105 Trace::WriteLine(oid);
106 i = 0;
108 Trace::WriteLine(System::String::Format(
109 new System::String(S"cli uno bridge: {0} remaining registered interfaces"),
110 __box(m_objects->get_Count() - 1)));
111 #endif
112 m_objects->Remove(key);
115 inline void Cli_environment::revokeInterface(System::String* oid)
117 return revokeInterface(oid, NULL);
120 System::Object* Cli_environment::getRegisteredInterface(System::String* oid,
121 System::Type* type)
123 //try if it is a UNO interface
124 System::Object* ret = NULL;
125 ret = m_objects->get_Item(oid);
126 if (! ret)
128 //try if if it is a proxy for a cli object
129 oid = createKey(oid, type);
130 ret = m_objects->get_Item( oid );
132 if (ret != 0)
134 System::WeakReference* weakIface =
135 static_cast< System::WeakReference * >( ret );
136 ret = weakIface->Target;
138 if (ret == 0)
139 m_objects->Remove( oid );
140 return ret;
143 System::String* Cli_environment::getObjectIdentifier(System::Object* obj)
145 System::String* oId= 0;
146 RealProxy* aProxy= RemotingServices::GetRealProxy(obj);
147 if (aProxy)
149 UnoInterfaceProxy* proxyImpl= dynamic_cast<UnoInterfaceProxy*>(aProxy);
150 if (proxyImpl)
151 oId= proxyImpl->getOid();
154 if (oId == 0)
156 StringBuilder * buf= new StringBuilder(256);
157 bool bFirst = false;
158 System::Threading::Monitor::Enter(__typeof(Cli_environment));
159 try {
160 buf->Append(m_IDGen->GetId(obj, & bFirst));
161 } __finally
163 System::Threading::Monitor::Exit(__typeof(Cli_environment));
166 buf->Append(sOidPart);
167 oId= buf->ToString();
169 return oId;
171 } //namespace cli_uno
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */