xref: /AOO41X/main/javaunohelper/test/com/sun/star/lib/uno/helper/ProxyProvider.java (revision a5b190bfa3e1bed4623e2958a8877664a3b5506c)
1*a5b190bfSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a5b190bfSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a5b190bfSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a5b190bfSAndrew Rist  * distributed with this work for additional information
6*a5b190bfSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a5b190bfSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a5b190bfSAndrew Rist  * "License"); you may not use this file except in compliance
9*a5b190bfSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*a5b190bfSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*a5b190bfSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a5b190bfSAndrew Rist  * software distributed under the License is distributed on an
15*a5b190bfSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a5b190bfSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a5b190bfSAndrew Rist  * specific language governing permissions and limitations
18*a5b190bfSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*a5b190bfSAndrew Rist  *************************************************************/
21*a5b190bfSAndrew Rist 
22*a5b190bfSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.lib.uno.helper;
25cdf0e10cSrcweir import com.sun.star.uno.Type;
26cdf0e10cSrcweir import com.sun.star.lib.uno.typedesc.TypeDescription;
27cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
28cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
29cdf0e10cSrcweir import com.sun.star.uno.IQueryInterface;
30cdf0e10cSrcweir //import com.sun.star.lib.uno.environments.java.Proxy;
31cdf0e10cSrcweir import com.sun.star.lib.uno.environments.java.java_environment;
32cdf0e10cSrcweir //import com.sun.star.lib.uno.environments.java.IRequester;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir public class ProxyProvider
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     static java_environment env= new java_environment(null);
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     /** Creates a new instance of ProxyProvider */
ProxyProvider()40cdf0e10cSrcweir     public ProxyProvider()
41cdf0e10cSrcweir     {
42cdf0e10cSrcweir     }
43cdf0e10cSrcweir     /** returns Holder proxy objects for the specified interface. If the method is called
44cdf0e10cSrcweir      * several times with the same arguments then each time a new HolderProxy is returned.
45cdf0e10cSrcweir      * Then all HolderProxy s refer to the same Proxy object.
46cdf0e10cSrcweir      * The proxy can be queried for XEventListener. On the returned proxy disposing can be called
47cdf0e10cSrcweir      *
48cdf0e10cSrcweir      */
createProxy(Object obj, Class iface)49cdf0e10cSrcweir     public static Object createProxy(Object obj, Class iface)
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir 
52cdf0e10cSrcweir         Object retVal= null;
53cdf0e10cSrcweir         if (obj == null || iface == null || iface.isInstance(obj) == false )
54cdf0e10cSrcweir             return retVal;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir         Type type= new Type(TypeDescription.getTypeDescription(iface));
57cdf0e10cSrcweir         Type evtType= new Type(TypeDescription.getTypeDescription(com.sun.star.lang.XEventListener.class));
58cdf0e10cSrcweir         // find the object identifier
59cdf0e10cSrcweir         String sOid= UnoRuntime.generateOid(obj);
60cdf0e10cSrcweir         retVal= env.getRegisteredInterface(sOid, type);
61cdf0e10cSrcweir         // if retVal == null then probably not registered
62cdf0e10cSrcweir         if (retVal == null)
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             Object aProxy = new Proxy(sOid, type);
65cdf0e10cSrcweir             String[] arOid = new String[]
66cdf0e10cSrcweir             {sOid};
67cdf0e10cSrcweir             retVal= env.registerInterface(aProxy, arOid, type);
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir         return retVal;
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir class Proxy implements IQueryInterface, XEventListener
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     String oid;
76cdf0e10cSrcweir     Type type;
Proxy(String oid, Type t)77cdf0e10cSrcweir     Proxy(String oid, Type t) {
78cdf0e10cSrcweir         this.oid = oid;
79cdf0e10cSrcweir         this.type = t;
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
getOid()82cdf0e10cSrcweir     public String getOid() {
83cdf0e10cSrcweir         return oid;
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
isSame(Object object)86cdf0e10cSrcweir     public boolean isSame(Object object) {
87cdf0e10cSrcweir         if (object instanceof IQueryInterface)
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             IQueryInterface iquery = (IQueryInterface) object;
90cdf0e10cSrcweir             if (iquery != null)
91cdf0e10cSrcweir             {
92cdf0e10cSrcweir                 if (iquery.getOid().equals(oid))
93cdf0e10cSrcweir                     return true;
94cdf0e10cSrcweir                 else
95cdf0e10cSrcweir                     return false;
96cdf0e10cSrcweir             }
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         String oidObj = UnoRuntime.generateOid(object);
100cdf0e10cSrcweir         if (oidObj.equals(oid))
101cdf0e10cSrcweir             return true;
102cdf0e10cSrcweir         else
103cdf0e10cSrcweir             return false;
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
queryInterface(Type type)106cdf0e10cSrcweir     public Object queryInterface(Type type) {
107cdf0e10cSrcweir         return null;
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
disposing(com.sun.star.lang.EventObject eventObject)110cdf0e10cSrcweir     public void disposing(com.sun.star.lang.EventObject eventObject) {
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 
116cdf0e10cSrcweir //class Requester //implements IRequester
117cdf0e10cSrcweir //{
118cdf0e10cSrcweir //    int _modus;
119cdf0e10cSrcweir //    boolean _virtual;
120cdf0e10cSrcweir //    boolean _forceSynchronous;
121cdf0e10cSrcweir //    boolean _passed = true;
122cdf0e10cSrcweir //
123cdf0e10cSrcweir //    Object _xEventListenerProxy;
124cdf0e10cSrcweir //    int nDisposingCalled= 0;
125cdf0e10cSrcweir //
126cdf0e10cSrcweir //    Requester(boolean virtual, boolean forceSynchronous, Object evtListener)
127cdf0e10cSrcweir //    {
128cdf0e10cSrcweir //        _virtual = virtual;
129cdf0e10cSrcweir //        _forceSynchronous = forceSynchronous;
130cdf0e10cSrcweir //        _xEventListenerProxy= evtListener;
131cdf0e10cSrcweir //
132cdf0e10cSrcweir //    }
133cdf0e10cSrcweir //
134cdf0e10cSrcweir //    public Object sendRequest(Object object,
135cdf0e10cSrcweir //    Type type,
136cdf0e10cSrcweir //    String operation,
137cdf0e10cSrcweir //    Object params[],
138cdf0e10cSrcweir //    Boolean synchron[],
139cdf0e10cSrcweir //    Boolean mustReply[]) throws Throwable
140cdf0e10cSrcweir //    {
141cdf0e10cSrcweir //
142cdf0e10cSrcweir //        Object result = null;
143cdf0e10cSrcweir //        if (operation.equals("disposing"))
144cdf0e10cSrcweir //        {
145cdf0e10cSrcweir //            System.out.println("Disposing called on XEventListener proxy");
146cdf0e10cSrcweir //            nDisposingCalled++;
147cdf0e10cSrcweir //        }
148cdf0e10cSrcweir //        else if (operation.equals("queryInterface"))
149cdf0e10cSrcweir //        {
150cdf0e10cSrcweir //            if (params[0] instanceof Type)
151cdf0e10cSrcweir //            {
152cdf0e10cSrcweir //                Type t= (Type) params[0];
153cdf0e10cSrcweir //                if (t.equals( new Type("com.sun.star.lang.XEventListener")))
154cdf0e10cSrcweir //                    result= _xEventListenerProxy;
155cdf0e10cSrcweir //            }
156cdf0e10cSrcweir //        }
157cdf0e10cSrcweir //        return result;
158cdf0e10cSrcweir //    }
159cdf0e10cSrcweir //}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162