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 package com.sun.star.lib.uno.helper;
29 import com.sun.star.uno.Type;
30 import com.sun.star.lib.uno.typedesc.TypeDescription;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.lang.XEventListener;
33 import com.sun.star.uno.IQueryInterface;
34 //import com.sun.star.lib.uno.environments.java.Proxy;
35 import com.sun.star.lib.uno.environments.java.java_environment;
36 //import com.sun.star.lib.uno.environments.java.IRequester;
37 
38 
39 public class ProxyProvider
40 {
41     static java_environment env= new java_environment(null);
42 
43     /** Creates a new instance of ProxyProvider */
44     public ProxyProvider()
45     {
46     }
47     /** returns Holder proxy objects for the specified interface. If the method is called
48      * several times with the same arguments then each time a new HolderProxy is returned.
49      * Then all HolderProxy s refer to the same Proxy object.
50      * The proxy can be queried for XEventListener. On the returned proxy disposing can be called
51      *
52      */
53     public static Object createProxy(Object obj, Class iface)
54     {
55 
56         Object retVal= null;
57         if (obj == null || iface == null || iface.isInstance(obj) == false )
58             return retVal;
59 
60         Type type= new Type(TypeDescription.getTypeDescription(iface));
61         Type evtType= new Type(TypeDescription.getTypeDescription(com.sun.star.lang.XEventListener.class));
62         // find the object identifier
63         String sOid= UnoRuntime.generateOid(obj);
64         retVal= env.getRegisteredInterface(sOid, type);
65         // if retVal == null then probably not registered
66         if (retVal == null)
67         {
68             Object aProxy = new Proxy(sOid, type);
69             String[] arOid = new String[]
70             {sOid};
71             retVal= env.registerInterface(aProxy, arOid, type);
72         }
73         return retVal;
74     }
75 }
76 
77 class Proxy implements IQueryInterface, XEventListener
78 {
79     String oid;
80     Type type;
81     Proxy(String oid, Type t) {
82         this.oid = oid;
83         this.type = t;
84     }
85 
86     public String getOid() {
87         return oid;
88     }
89 
90     public boolean isSame(Object object) {
91         if (object instanceof IQueryInterface)
92         {
93             IQueryInterface iquery = (IQueryInterface) object;
94             if (iquery != null)
95             {
96                 if (iquery.getOid().equals(oid))
97                     return true;
98                 else
99                     return false;
100             }
101         }
102 
103         String oidObj = UnoRuntime.generateOid(object);
104         if (oidObj.equals(oid))
105             return true;
106         else
107             return false;
108     }
109 
110     public Object queryInterface(Type type) {
111         return null;
112     }
113 
114     public void disposing(com.sun.star.lang.EventObject eventObject) {
115     }
116 
117 }
118 
119 
120 //class Requester //implements IRequester
121 //{
122 //    int _modus;
123 //    boolean _virtual;
124 //    boolean _forceSynchronous;
125 //    boolean _passed = true;
126 //
127 //    Object _xEventListenerProxy;
128 //    int nDisposingCalled= 0;
129 //
130 //    Requester(boolean virtual, boolean forceSynchronous, Object evtListener)
131 //    {
132 //        _virtual = virtual;
133 //        _forceSynchronous = forceSynchronous;
134 //        _xEventListenerProxy= evtListener;
135 //
136 //    }
137 //
138 //    public Object sendRequest(Object object,
139 //    Type type,
140 //    String operation,
141 //    Object params[],
142 //    Boolean synchron[],
143 //    Boolean mustReply[]) throws Throwable
144 //    {
145 //
146 //        Object result = null;
147 //        if (operation.equals("disposing"))
148 //        {
149 //            System.out.println("Disposing called on XEventListener proxy");
150 //            nDisposingCalled++;
151 //        }
152 //        else if (operation.equals("queryInterface"))
153 //        {
154 //            if (params[0] instanceof Type)
155 //            {
156 //                Type t= (Type) params[0];
157 //                if (t.equals( new Type("com.sun.star.lang.XEventListener")))
158 //                    result= _xEventListenerProxy;
159 //            }
160 //        }
161 //        return result;
162 //    }
163 //}
164 
165 
166