1*54c06456SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*54c06456SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*54c06456SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*54c06456SAndrew Rist  * distributed with this work for additional information
6*54c06456SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*54c06456SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*54c06456SAndrew Rist  * "License"); you may not use this file except in compliance
9*54c06456SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*54c06456SAndrew Rist  *
11*54c06456SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*54c06456SAndrew Rist  *
13*54c06456SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*54c06456SAndrew Rist  * software distributed under the License is distributed on an
15*54c06456SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*54c06456SAndrew Rist  * KIND, either express or implied.  See the License for the
17*54c06456SAndrew Rist  * specific language governing permissions and limitations
18*54c06456SAndrew Rist  * under the License.
19*54c06456SAndrew Rist  *
20*54c06456SAndrew Rist  *************************************************************/
21*54c06456SAndrew Rist 
22*54c06456SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cli_ure.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "Cli_environment.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #using <mscorlib.dll>
30cdf0e10cSrcweir #using <cli_ure.dll>
31cdf0e10cSrcweir #using <system.dll>
32cdf0e10cSrcweir #include "osl/diagnose.h"
33cdf0e10cSrcweir #include "cli_proxy.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace System::Runtime::Remoting;
36cdf0e10cSrcweir using namespace System::Runtime::Remoting::Proxies;
37cdf0e10cSrcweir using namespace System::Collections;
38cdf0e10cSrcweir using namespace System::Text;
39cdf0e10cSrcweir using namespace System::Diagnostics;
40cdf0e10cSrcweir using namespace System::Threading;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir namespace cli_uno
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 
createKey(System::String * oid,System::Type * t)45cdf0e10cSrcweir inline System::String* Cli_environment::createKey(System::String* oid, System::Type* t)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     return System::String::Concat(oid, t->get_FullName());
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
Cli_environment()51cdf0e10cSrcweir inline Cli_environment::Cli_environment()
52cdf0e10cSrcweir {
53cdf0e10cSrcweir #if OSL_DEBUG_LEVEL >= 2
54cdf0e10cSrcweir     _numRegisteredObjects = 0;
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
~Cli_environment()58cdf0e10cSrcweir Cli_environment::~Cli_environment()
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     OSL_ENSURE(_numRegisteredObjects == 0,
61cdf0e10cSrcweir                "cli uno bridge: CLI environment contains unrevoked objects");
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 
registerInterface(System::Object * obj,System::String * oid)65cdf0e10cSrcweir System::Object* Cli_environment::registerInterface(
66cdf0e10cSrcweir     System::Object* obj, System::String* oid)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir #if OSL_DEBUG_LEVEL >= 1
69cdf0e10cSrcweir     //obj must be a transparent proxy
70cdf0e10cSrcweir     OSL_ASSERT(RemotingServices::IsTransparentProxy(obj));
71cdf0e10cSrcweir     _numRegisteredObjects ++;
72cdf0e10cSrcweir #endif
73cdf0e10cSrcweir     OSL_ASSERT( ! m_objects->ContainsKey(oid));
74cdf0e10cSrcweir     m_objects->Add(oid, new WeakReference(obj));
75cdf0e10cSrcweir     return obj;
76cdf0e10cSrcweir }
registerInterface(System::Object * obj,System::String * oid,System::Type * type)77cdf0e10cSrcweir System::Object* Cli_environment::registerInterface      (
78cdf0e10cSrcweir     System::Object* obj, System::String* oid, System::Type* type)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir #if OSL_DEBUG_LEVEL >= 1
81cdf0e10cSrcweir     //obj must be a real cli object
82cdf0e10cSrcweir     OSL_ASSERT( ! RemotingServices::IsTransparentProxy(obj));
83cdf0e10cSrcweir     _numRegisteredObjects ++;
84cdf0e10cSrcweir #endif
85cdf0e10cSrcweir     System::String* key = createKey(oid, type);
86cdf0e10cSrcweir     //see synchronization in map_uno2cli in cli_data.cxx
87cdf0e10cSrcweir     OSL_ASSERT( ! m_objects->ContainsKey(key));
88cdf0e10cSrcweir     m_objects->Add(key, new WeakReference(obj));
89cdf0e10cSrcweir     return obj;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
revokeInterface(System::String * oid,System::Type * type)92cdf0e10cSrcweir void Cli_environment::revokeInterface(System::String* oid, System::Type* type)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     System::String* key = type != NULL ? createKey(oid, type) : oid;
95cdf0e10cSrcweir #if OSL_DEBUG_LEVEL >= 1
96cdf0e10cSrcweir     _numRegisteredObjects --;
97cdf0e10cSrcweir #endif
98cdf0e10cSrcweir #if OSL_DEBUG_LEVEL >= 2
99cdf0e10cSrcweir     int i = 1;
100cdf0e10cSrcweir     if (m_objects->ContainsKey(key) == false)
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
103cdf0e10cSrcweir         Trace::WriteLine(oid);
104cdf0e10cSrcweir         i = 0;
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir     Trace::WriteLine(System::String::Format(
107cdf0e10cSrcweir                          new System::String(S"cli uno bridge: {0} remaining registered interfaces"),
108cdf0e10cSrcweir                          __box(m_objects->get_Count() - 1)));
109cdf0e10cSrcweir #endif
110cdf0e10cSrcweir     m_objects->Remove(key);
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
revokeInterface(System::String * oid)113cdf0e10cSrcweir inline void Cli_environment::revokeInterface(System::String* oid)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     return revokeInterface(oid, NULL);
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
getRegisteredInterface(System::String * oid,System::Type * type)118cdf0e10cSrcweir System::Object* Cli_environment::getRegisteredInterface(System::String* oid,
119cdf0e10cSrcweir                                                         System::Type* type)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     //try if it is a UNO interface
122cdf0e10cSrcweir     System::Object* ret = NULL;
123cdf0e10cSrcweir     ret = m_objects->get_Item(oid);
124cdf0e10cSrcweir     if (! ret)
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         //try if if it is a proxy for a cli object
127cdf0e10cSrcweir         oid = createKey(oid, type);
128cdf0e10cSrcweir         ret = m_objects->get_Item( oid );
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir     if (ret != 0)
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         System::WeakReference* weakIface =
133cdf0e10cSrcweir             static_cast< System::WeakReference * >( ret );
134cdf0e10cSrcweir         ret = weakIface->Target;
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir     if (ret == 0)
137cdf0e10cSrcweir         m_objects->Remove( oid );
138cdf0e10cSrcweir     return ret;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
getObjectIdentifier(System::Object * obj)141cdf0e10cSrcweir System::String* Cli_environment::getObjectIdentifier(System::Object* obj)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     System::String* oId= 0;
144cdf0e10cSrcweir     RealProxy* aProxy= RemotingServices::GetRealProxy(obj);
145cdf0e10cSrcweir     if (aProxy)
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         UnoInterfaceProxy* proxyImpl= dynamic_cast<UnoInterfaceProxy*>(aProxy);
148cdf0e10cSrcweir         if (proxyImpl)
149cdf0e10cSrcweir             oId= proxyImpl->getOid();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     if (oId == 0)
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         StringBuilder * buf= new StringBuilder(256);
155cdf0e10cSrcweir 		bool bFirst = false;
156cdf0e10cSrcweir 		System::Threading::Monitor::Enter(__typeof(Cli_environment));
157cdf0e10cSrcweir 		try {
158cdf0e10cSrcweir 			buf->Append(m_IDGen->GetId(obj, & bFirst));
159cdf0e10cSrcweir 		} __finally
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             System::Threading::Monitor::Exit(__typeof(Cli_environment));
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         buf->Append(sOidPart);
165cdf0e10cSrcweir         oId= buf->ToString();
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     return oId;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir } //namespace cli_uno
170