xref: /AOO41X/main/odk/examples/DevelopersGuide/OfficeDev/DesktopEnvironment/OnewayExecutor.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import java.util.Vector;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir // __________ Implementation __________
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir /**
40*cdf0e10cSrcweir  * It's not allowed to call synchronoues back inside an oneway interface call.
41*cdf0e10cSrcweir  * (see IOnewayLink too). So we start a thread (implemented by this class), which
42*cdf0e10cSrcweir  * gets all neccessary parameters from the original called object and
43*cdf0e10cSrcweir  * call it back later inside his run() method. So the execution of such oneway call
44*cdf0e10cSrcweir  * will be asynchronous. It works in a generic way and can be used or any type
45*cdf0e10cSrcweir  * of oneway request. Because the source and the target of this call-link knows,
46*cdf0e10cSrcweir  * which method was used and which parameters must be handled.
47*cdf0e10cSrcweir  *
48*cdf0e10cSrcweir  * @author     Andreas Schlüns
49*cdf0e10cSrcweir  * @created    17.07.2002 08:18
50*cdf0e10cSrcweir  */
51*cdf0e10cSrcweir class OnewayExecutor extends Thread
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     // _______________________________
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     /**
56*cdf0e10cSrcweir      * const
57*cdf0e10cSrcweir      * We define some request for some well known oneway interface
58*cdf0e10cSrcweir      * calls here too. So they mustn't be declared more then ones.
59*cdf0e10cSrcweir      * Of course it's not necessary to use it ... but why not :-)
60*cdf0e10cSrcweir      */
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     public static final int     REQUEST_FRAMEACTION             = 1     ;
63*cdf0e10cSrcweir     public static final int     REQUEST_STATUSCHANGED           = 2     ;
64*cdf0e10cSrcweir     public static final int     REQUEST_ADDSTATUSLISTENER       = 3     ;
65*cdf0e10cSrcweir     public static final int     REQUEST_REMOVESTATUSLISTENER    = 4     ;
66*cdf0e10cSrcweir     public static final int     REQUEST_DISPATCH                = 5     ;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     public static final boolean ENCODE_PARAMS                   = true  ;
69*cdf0e10cSrcweir     public static final boolean DECODE_PARAMS                   = false ;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     // _______________________________
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     /**
74*cdf0e10cSrcweir      * @member  m_rLink     the object, which wish to be called back by this thread
75*cdf0e10cSrcweir      * @member  m_nRequest  describes the type of the original request (means the
76*cdf0e10cSrcweir      *                      called oneyway method)
77*cdf0e10cSrcweir      * @member  m_lParams   list of parameters of the original request
78*cdf0e10cSrcweir      */
79*cdf0e10cSrcweir     private IOnewayLink m_rLink     ;
80*cdf0e10cSrcweir     private int         m_nRequest  ;
81*cdf0e10cSrcweir     private Vector      m_lParams   ;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     // _______________________________
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     /**
86*cdf0e10cSrcweir      * ctor
87*cdf0e10cSrcweir      * It's initialize this thread with all neccessary parameters.
88*cdf0e10cSrcweir      * It gets the object, which wish to be called back and the type
89*cdf0e10cSrcweir      * and parameters of the original request.
90*cdf0e10cSrcweir      *
91*cdf0e10cSrcweir      * @param nRequest
92*cdf0e10cSrcweir      *          The two user of this callback can define an unique number,
93*cdf0e10cSrcweir      *          which identify the type of original interface method.
94*cdf0e10cSrcweir      *          So the called interface object can decide, which action will be
95*cdf0e10cSrcweir      *          neccessary.
96*cdf0e10cSrcweir      *
97*cdf0e10cSrcweir      * @param lParams
98*cdf0e10cSrcweir      *          If the original method used parameters, they will be coded here in
99*cdf0e10cSrcweir      *          a generic way. Only the called interface object know (it depends
100*cdf0e10cSrcweir      *          from the original request - see nRequest too), how this list must
101*cdf0e10cSrcweir      *          be interpreted.
102*cdf0e10cSrcweir      *          Note: Atomic types (e.g. int, long) will be transported as objects
103*cdf0e10cSrcweir      *          too (Integer, Long)!
104*cdf0e10cSrcweir      */
105*cdf0e10cSrcweir     public OnewayExecutor( IOnewayLink rLink    ,
106*cdf0e10cSrcweir                            int         nRequest ,
107*cdf0e10cSrcweir                            Vector      lParams  )
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir         m_rLink    = rLink   ;
110*cdf0e10cSrcweir         m_nRequest = nRequest;
111*cdf0e10cSrcweir         m_lParams  = lParams ;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         if (m_rLink==null)
114*cdf0e10cSrcweir             System.out.println("ctor ... m_rLink == null");
115*cdf0e10cSrcweir         if (m_lParams==null)
116*cdf0e10cSrcweir             System.out.println("ctor ... m_lParams == null");
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     // _______________________________
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     /**
122*cdf0e10cSrcweir      * implements the thread function
123*cdf0e10cSrcweir      * Here we call the internal set link object back and
124*cdf0e10cSrcweir      * give him all neccessary parameters.
125*cdf0e10cSrcweir      * After that we die by ouerself ...
126*cdf0e10cSrcweir      */
127*cdf0e10cSrcweir     public void run()
128*cdf0e10cSrcweir     {
129*cdf0e10cSrcweir         if (m_rLink==null)
130*cdf0e10cSrcweir             System.out.println("run ... m_rLink == null");
131*cdf0e10cSrcweir         if (m_lParams==null)
132*cdf0e10cSrcweir             System.out.println("run ... m_lParams == null");
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         if (m_rLink!=null)
135*cdf0e10cSrcweir             m_rLink.execOneway( m_nRequest, m_lParams );
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     // _______________________________
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     /**
141*cdf0e10cSrcweir      * static helper!
142*cdf0e10cSrcweir      * To make convertion of the generic parameter list to the original
143*cdf0e10cSrcweir      * one easier - you can use this helper methods. They know how suchlist
144*cdf0e10cSrcweir      * must be coded. It's not a must to use it - but you can ...
145*cdf0e10cSrcweir      */
146*cdf0e10cSrcweir     public static void codeFrameAction(
147*cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
148*cdf0e10cSrcweir         com.sun.star.frame.FrameActionEvent[] aAction)
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir         if (bEncode)
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             lParams[0] = new Vector(1);
153*cdf0e10cSrcweir             lParams[0].add( (Object)(aAction[0]) );
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir         else
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             aAction[0] = (com.sun.star.frame.FrameActionEvent)
158*cdf0e10cSrcweir                 (lParams[0].elementAt(0));
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     // _______________________________
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     public static void codeStatusChanged(
165*cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
166*cdf0e10cSrcweir         com.sun.star.frame.FeatureStateEvent[] aStatus)
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir         if (bEncode)
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             lParams[0] = new Vector(1);
171*cdf0e10cSrcweir             lParams[0].add( (Object)aStatus[0] );
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir         else
174*cdf0e10cSrcweir         {
175*cdf0e10cSrcweir             aStatus[0] = (com.sun.star.frame.FeatureStateEvent)
176*cdf0e10cSrcweir                 (lParams[0].elementAt(0));
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     // _______________________________
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     public static void codeAddOrRemoveStatusListener(
183*cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
184*cdf0e10cSrcweir         com.sun.star.frame.XStatusListener[] xListener,
185*cdf0e10cSrcweir         com.sun.star.util.URL[] aURL)
186*cdf0e10cSrcweir     {
187*cdf0e10cSrcweir         if (bEncode)
188*cdf0e10cSrcweir         {
189*cdf0e10cSrcweir             lParams[0] = new Vector(2);
190*cdf0e10cSrcweir             lParams[0].add( (Object)xListener[0] );
191*cdf0e10cSrcweir             lParams[0].add( (Object)aURL[0]      );
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir         else
194*cdf0e10cSrcweir         {
195*cdf0e10cSrcweir             xListener[0] = (com.sun.star.frame.XStatusListener)
196*cdf0e10cSrcweir                 (lParams[0].elementAt(0));
197*cdf0e10cSrcweir             aURL[0] = (com.sun.star.util.URL)(lParams[0].elementAt(1));
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     // _______________________________
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     public static void codeDispatch(
204*cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
205*cdf0e10cSrcweir         com.sun.star.util.URL[] aURL,
206*cdf0e10cSrcweir         com.sun.star.beans.PropertyValue[][] lArgs)
207*cdf0e10cSrcweir     {
208*cdf0e10cSrcweir         if (bEncode)
209*cdf0e10cSrcweir         {
210*cdf0e10cSrcweir             int nLength = lArgs.length+1;
211*cdf0e10cSrcweir             int nPos    = 0;
212*cdf0e10cSrcweir             lParams[0] = new Vector(nLength);
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir             lParams[0].add( (Object)aURL[0] );
215*cdf0e10cSrcweir             --nLength;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir             while (nLength>0)
218*cdf0e10cSrcweir             {
219*cdf0e10cSrcweir                 lParams[0].add( (Object)lArgs[0][nPos] );
220*cdf0e10cSrcweir                 --nLength;
221*cdf0e10cSrcweir                 ++nPos   ;
222*cdf0e10cSrcweir             }
223*cdf0e10cSrcweir         }
224*cdf0e10cSrcweir         else
225*cdf0e10cSrcweir         {
226*cdf0e10cSrcweir             int nLength = lParams[0].size()-1;
227*cdf0e10cSrcweir             int nPos    = 0;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir             lArgs[0] = new com.sun.star.beans.PropertyValue[nLength];
230*cdf0e10cSrcweir             aURL[0]  = (com.sun.star.util.URL)(lParams[0].elementAt(0));
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir             while (nPos<nLength)
233*cdf0e10cSrcweir             {
234*cdf0e10cSrcweir                 lArgs[0][nPos] = (com.sun.star.beans.PropertyValue)
235*cdf0e10cSrcweir                     (lParams[0].elementAt(nPos+1));
236*cdf0e10cSrcweir                 ++nPos;
237*cdf0e10cSrcweir             }
238*cdf0e10cSrcweir         }
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir }
241