update dev300-m58
[ooovba.git] / cli_ure / source / mono_bridge / cli_environment.cs
blob91a2970a64b94de791f7e10e1abd94921403ed8f
1 /*************************************************************************
3 * $RCSfile: $
5 * $Revision: $
7 * last change: $Author: $ $Date: $
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 * MA 02111-1307 USA
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Sun Microsystems, Inc.
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): _______________________________________
60 ************************************************************************/
62 namespace cli_uno
65 using System;
66 using System.Collections;
67 using System.Runtime.Remoting;
68 using System.Runtime.Remoting.Proxies;
69 using System.Text;
71 using com.sun.star.bridges.mono_uno;
73 public class Cli_environment
75 static string sOidPart = ";cli[0];";
77 static Hashtable m_objects = Hashtable.Synchronized(new Hashtable());
79 static string createKey(string oid, Type t)
81 return oid + t.FullName;
84 // FIXME setup debugging info here
85 // public Cli_environment()
86 // {
87 // }
89 // FIXME assert there are no more registered objects
90 // public ~Cli_environment()
91 // {
92 // }
94 /**
95 Registers an UNO object as being mapped by this bridge. The resulting
96 cli object is represents all interfaces of the UNO object. Therefore the
97 object can be registered only with its OID; a type is not necessary.
99 public object registerInterface(object obj, string oid)
101 // FIXME debugging stuff
102 m_objects.Add(oid, obj); // new WeakReference(obj));
103 return obj;
107 Registers a CLI object as being mapped by this bridge. The resulting
108 object represents exactly one UNO interface.
110 public object registerInterface(object obj, string oid, Type type)
112 // FIXME debugging stuff
113 string key = createKey(oid, type);
114 m_objects.Add(key, obj); // new WeakReference(obj));
115 return obj;
119 By revoking an interface it is declared that the respective interface has
120 not been mapped. The proxy implementations call revoke interface in their
121 destructors.
123 public void revokeInterface(string oid)
125 revokeInterface(oid, null);
128 public void revokeInterface(string oid, Type type)
130 // FIXME debugging stuff
131 string key = type != null ? createKey(oid, type) : oid;
132 m_objects.Remove(key);
136 * Retrieves an interface identified by its object id and type from this
137 * environment.
139 * @param oid object id of interface to be retrieved
140 * @param type the type description of the interface to be retrieved
141 * @see com.sun.star.uno.IEnvironment#getRegisteredInterface
143 public object getRegisteredInterface(string oid, Type type)
145 // try if it is a UNO interface
146 object ret = null;
147 ret = m_objects[oid];
148 if (ret == null)
150 // try if it is a proxy for a cli object
151 oid = createKey(oid, type);
152 ret = m_objects[oid];
154 /* if (ret != null)
156 WeakReference weakIface = (WeakReference)ret;
157 ret = weakIface.Target;
158 } */
160 if (ret == null)
161 m_objects.Remove(oid);
163 return ret;
167 * Generates a worldwide unique object identifier (oid) for the given object. It is
168 * guaranteed, that subsequent calls to the method with the same object
169 * will give the same id.
170 * <p>
171 * @return the generated oid.
172 * @param object the object for which a Oid should be generated.
174 public static string getObjectIdentifier(object obj)
176 string oid = null;
177 RealProxy realProxy = null;
179 if (RemotingServices.IsTransparentProxy(obj))
180 realProxy = RemotingServices.GetRealProxy(obj);
182 if (realProxy != null)
184 UnoInterfaceProxy proxyImpl = realProxy as UnoInterfaceProxy;
185 if (proxyImpl != null)
186 oid = proxyImpl.Oid;
189 if (oid == null)
191 Guid gd = typeof(Cli_environment).GUID; // FIXME apparently not a good idea with mono
192 StringBuilder buf = new StringBuilder(128);
193 buf.Append(obj.GetHashCode());
194 buf.Append(sOidPart);
195 buf.Append(gd);
196 oid = buf.ToString();
199 return oid;