1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cli_ure.hxx"
30 
31 #include "Cli_environment.h"
32 
33 #using <mscorlib.dll>
34 #using <cli_ure.dll>
35 #using <system.dll>
36 #include "osl/diagnose.h"
37 #include "cli_proxy.h"
38 
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;
45 
46 namespace cli_uno
47 {
48 
49 inline System::String* Cli_environment::createKey(System::String* oid, System::Type* t)
50 {
51     return System::String::Concat(oid, t->get_FullName());
52 }
53 
54 
55 inline Cli_environment::Cli_environment()
56 {
57 #if OSL_DEBUG_LEVEL >= 2
58     _numRegisteredObjects = 0;
59 #endif
60 }
61 
62 Cli_environment::~Cli_environment()
63 {
64     OSL_ENSURE(_numRegisteredObjects == 0,
65                "cli uno bridge: CLI environment contains unrevoked objects");
66 }
67 
68 
69 System::Object* Cli_environment::registerInterface(
70     System::Object* obj, System::String* oid)
71 {
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;
80 }
81 System::Object* Cli_environment::registerInterface      (
82     System::Object* obj, System::String* oid, System::Type* type)
83 {
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;
94 }
95 
96 void Cli_environment::revokeInterface(System::String* oid, System::Type* type)
97 {
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)
105     {
106         Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
107         Trace::WriteLine(oid);
108         i = 0;
109     }
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);
115 }
116 
117 inline void Cli_environment::revokeInterface(System::String* oid)
118 {
119     return revokeInterface(oid, NULL);
120 }
121 
122 System::Object* Cli_environment::getRegisteredInterface(System::String* oid,
123                                                         System::Type* type)
124 {
125     //try if it is a UNO interface
126     System::Object* ret = NULL;
127     ret = m_objects->get_Item(oid);
128     if (! ret)
129     {
130         //try if if it is a proxy for a cli object
131         oid = createKey(oid, type);
132         ret = m_objects->get_Item( oid );
133     }
134     if (ret != 0)
135     {
136         System::WeakReference* weakIface =
137             static_cast< System::WeakReference * >( ret );
138         ret = weakIface->Target;
139     }
140     if (ret == 0)
141         m_objects->Remove( oid );
142     return ret;
143 }
144 
145 System::String* Cli_environment::getObjectIdentifier(System::Object* obj)
146 {
147     System::String* oId= 0;
148     RealProxy* aProxy= RemotingServices::GetRealProxy(obj);
149     if (aProxy)
150     {
151         UnoInterfaceProxy* proxyImpl= dynamic_cast<UnoInterfaceProxy*>(aProxy);
152         if (proxyImpl)
153             oId= proxyImpl->getOid();
154     }
155 
156     if (oId == 0)
157     {
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
164         {
165             System::Threading::Monitor::Exit(__typeof(Cli_environment));
166         }
167 
168         buf->Append(sOidPart);
169         oId= buf->ToString();
170     }
171     return oId;
172 }
173 } //namespace cli_uno
174