134dd1e25SAndrew Rist /**************************************************************
234dd1e25SAndrew Rist  *
334dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
434dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
534dd1e25SAndrew Rist  * distributed with this work for additional information
634dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
734dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
834dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
934dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
1034dd1e25SAndrew Rist  *
1134dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1234dd1e25SAndrew Rist  *
1334dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1434dd1e25SAndrew Rist  * software distributed under the License is distributed on an
1534dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1634dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
1734dd1e25SAndrew Rist  * specific language governing permissions and limitations
1834dd1e25SAndrew Rist  * under the License.
1934dd1e25SAndrew Rist  *
2034dd1e25SAndrew Rist  *************************************************************/
2134dd1e25SAndrew Rist 
2234dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.util.Vector;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // __________ Implementation __________
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /**
29*b12a77c9Smseidel  * It's not allowed to call synchronous back inside an oneway interface call.
30cdf0e10cSrcweir  * (see IOnewayLink too). So we start a thread (implemented by this class), which
31a893be29SPedro Giffuni  * gets all necessary parameters from the original called object and
32cdf0e10cSrcweir  * call it back later inside his run() method. So the execution of such oneway call
33cdf0e10cSrcweir  * will be asynchronous. It works in a generic way and can be used or any type
34cdf0e10cSrcweir  * of oneway request. Because the source and the target of this call-link knows,
35cdf0e10cSrcweir  * which method was used and which parameters must be handled.
36cdf0e10cSrcweir  *
37cdf0e10cSrcweir  * @author     Andreas Schlüns
38cdf0e10cSrcweir  * @created    17.07.2002 08:18
39cdf0e10cSrcweir  */
40cdf0e10cSrcweir class OnewayExecutor extends Thread
41cdf0e10cSrcweir {
42cdf0e10cSrcweir     // _______________________________
43cdf0e10cSrcweir 
44cdf0e10cSrcweir     /**
45cdf0e10cSrcweir      * const
46cdf0e10cSrcweir      * We define some request for some well known oneway interface
47cdf0e10cSrcweir      * calls here too. So they mustn't be declared more then ones.
48cdf0e10cSrcweir      * Of course it's not necessary to use it ... but why not :-)
49cdf0e10cSrcweir      */
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     public static final int     REQUEST_FRAMEACTION             = 1     ;
52cdf0e10cSrcweir     public static final int     REQUEST_STATUSCHANGED           = 2     ;
53cdf0e10cSrcweir     public static final int     REQUEST_ADDSTATUSLISTENER       = 3     ;
54cdf0e10cSrcweir     public static final int     REQUEST_REMOVESTATUSLISTENER    = 4     ;
55cdf0e10cSrcweir     public static final int     REQUEST_DISPATCH                = 5     ;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     public static final boolean ENCODE_PARAMS                   = true  ;
58cdf0e10cSrcweir     public static final boolean DECODE_PARAMS                   = false ;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     // _______________________________
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     /**
63cdf0e10cSrcweir      * @member  m_rLink     the object, which wish to be called back by this thread
64cdf0e10cSrcweir      * @member  m_nRequest  describes the type of the original request (means the
65cdf0e10cSrcweir      *                      called oneyway method)
66cdf0e10cSrcweir      * @member  m_lParams   list of parameters of the original request
67cdf0e10cSrcweir      */
68cdf0e10cSrcweir     private IOnewayLink m_rLink     ;
69cdf0e10cSrcweir     private int         m_nRequest  ;
70cdf0e10cSrcweir     private Vector      m_lParams   ;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     // _______________________________
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     /**
75cdf0e10cSrcweir      * ctor
76a893be29SPedro Giffuni      * It's initialize this thread with all necessary parameters.
77cdf0e10cSrcweir      * It gets the object, which wish to be called back and the type
78cdf0e10cSrcweir      * and parameters of the original request.
79cdf0e10cSrcweir      *
80cdf0e10cSrcweir      * @param nRequest
81cdf0e10cSrcweir      *          The two user of this callback can define an unique number,
82cdf0e10cSrcweir      *          which identify the type of original interface method.
83cdf0e10cSrcweir      *          So the called interface object can decide, which action will be
84a893be29SPedro Giffuni      *          necessary.
85cdf0e10cSrcweir      *
86cdf0e10cSrcweir      * @param lParams
87cdf0e10cSrcweir      *          If the original method used parameters, they will be coded here in
88cdf0e10cSrcweir      *          a generic way. Only the called interface object know (it depends
89cdf0e10cSrcweir      *          from the original request - see nRequest too), how this list must
90cdf0e10cSrcweir      *          be interpreted.
91cdf0e10cSrcweir      *          Note: Atomic types (e.g. int, long) will be transported as objects
92cdf0e10cSrcweir      *          too (Integer, Long)!
93cdf0e10cSrcweir      */
OnewayExecutor( IOnewayLink rLink , int nRequest , Vector lParams )94cdf0e10cSrcweir     public OnewayExecutor( IOnewayLink rLink    ,
95cdf0e10cSrcweir                            int         nRequest ,
96cdf0e10cSrcweir                            Vector      lParams  )
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         m_rLink    = rLink   ;
99cdf0e10cSrcweir         m_nRequest = nRequest;
100cdf0e10cSrcweir         m_lParams  = lParams ;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         if (m_rLink==null)
103cdf0e10cSrcweir             System.out.println("ctor ... m_rLink == null");
104cdf0e10cSrcweir         if (m_lParams==null)
105cdf0e10cSrcweir             System.out.println("ctor ... m_lParams == null");
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     // _______________________________
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     /**
111cdf0e10cSrcweir      * implements the thread function
112cdf0e10cSrcweir      * Here we call the internal set link object back and
113a893be29SPedro Giffuni      * give it all necessary parameters.
114*b12a77c9Smseidel      * After that we die by ourself ...
115cdf0e10cSrcweir      */
run()116cdf0e10cSrcweir     public void run()
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         if (m_rLink==null)
119cdf0e10cSrcweir             System.out.println("run ... m_rLink == null");
120cdf0e10cSrcweir         if (m_lParams==null)
121cdf0e10cSrcweir             System.out.println("run ... m_lParams == null");
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         if (m_rLink!=null)
124cdf0e10cSrcweir             m_rLink.execOneway( m_nRequest, m_lParams );
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     // _______________________________
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     /**
130cdf0e10cSrcweir      * static helper!
131a893be29SPedro Giffuni      * To make conversion of the generic parameter list to the original
132cdf0e10cSrcweir      * one easier - you can use this helper methods. They know how suchlist
133cdf0e10cSrcweir      * must be coded. It's not a must to use it - but you can ...
134cdf0e10cSrcweir      */
codeFrameAction( boolean bEncode, Vector[] lParams, com.sun.star.frame.FrameActionEvent[] aAction)135cdf0e10cSrcweir     public static void codeFrameAction(
136cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
137cdf0e10cSrcweir         com.sun.star.frame.FrameActionEvent[] aAction)
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         if (bEncode)
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             lParams[0] = new Vector(1);
142cdf0e10cSrcweir             lParams[0].add( (Object)(aAction[0]) );
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir         else
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             aAction[0] = (com.sun.star.frame.FrameActionEvent)
147cdf0e10cSrcweir                 (lParams[0].elementAt(0));
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     // _______________________________
152cdf0e10cSrcweir 
codeStatusChanged( boolean bEncode, Vector[] lParams, com.sun.star.frame.FeatureStateEvent[] aStatus)153cdf0e10cSrcweir     public static void codeStatusChanged(
154cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
155cdf0e10cSrcweir         com.sun.star.frame.FeatureStateEvent[] aStatus)
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         if (bEncode)
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             lParams[0] = new Vector(1);
160cdf0e10cSrcweir             lParams[0].add( (Object)aStatus[0] );
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         else
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             aStatus[0] = (com.sun.star.frame.FeatureStateEvent)
165cdf0e10cSrcweir                 (lParams[0].elementAt(0));
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     // _______________________________
170cdf0e10cSrcweir 
codeAddOrRemoveStatusListener( boolean bEncode, Vector[] lParams, com.sun.star.frame.XStatusListener[] xListener, com.sun.star.util.URL[] aURL)171cdf0e10cSrcweir     public static void codeAddOrRemoveStatusListener(
172cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
173cdf0e10cSrcweir         com.sun.star.frame.XStatusListener[] xListener,
174cdf0e10cSrcweir         com.sun.star.util.URL[] aURL)
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         if (bEncode)
177cdf0e10cSrcweir         {
178cdf0e10cSrcweir             lParams[0] = new Vector(2);
179cdf0e10cSrcweir             lParams[0].add( (Object)xListener[0] );
180cdf0e10cSrcweir             lParams[0].add( (Object)aURL[0]      );
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir         else
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             xListener[0] = (com.sun.star.frame.XStatusListener)
185cdf0e10cSrcweir                 (lParams[0].elementAt(0));
186cdf0e10cSrcweir             aURL[0] = (com.sun.star.util.URL)(lParams[0].elementAt(1));
187cdf0e10cSrcweir         }
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     // _______________________________
191cdf0e10cSrcweir 
codeDispatch( boolean bEncode, Vector[] lParams, com.sun.star.util.URL[] aURL, com.sun.star.beans.PropertyValue[][] lArgs)192cdf0e10cSrcweir     public static void codeDispatch(
193cdf0e10cSrcweir         boolean bEncode, Vector[] lParams,
194cdf0e10cSrcweir         com.sun.star.util.URL[] aURL,
195cdf0e10cSrcweir         com.sun.star.beans.PropertyValue[][] lArgs)
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         if (bEncode)
198cdf0e10cSrcweir         {
199cdf0e10cSrcweir             int nLength = lArgs.length+1;
200cdf0e10cSrcweir             int nPos    = 0;
201cdf0e10cSrcweir             lParams[0] = new Vector(nLength);
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             lParams[0].add( (Object)aURL[0] );
204cdf0e10cSrcweir             --nLength;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir             while (nLength>0)
207cdf0e10cSrcweir             {
208cdf0e10cSrcweir                 lParams[0].add( (Object)lArgs[0][nPos] );
209cdf0e10cSrcweir                 --nLength;
210cdf0e10cSrcweir                 ++nPos   ;
211cdf0e10cSrcweir             }
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir         else
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir             int nLength = lParams[0].size()-1;
216cdf0e10cSrcweir             int nPos    = 0;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir             lArgs[0] = new com.sun.star.beans.PropertyValue[nLength];
219cdf0e10cSrcweir             aURL[0]  = (com.sun.star.util.URL)(lParams[0].elementAt(0));
220cdf0e10cSrcweir 
221cdf0e10cSrcweir             while (nPos<nLength)
222cdf0e10cSrcweir             {
223cdf0e10cSrcweir                 lArgs[0][nPos] = (com.sun.star.beans.PropertyValue)
224cdf0e10cSrcweir                     (lParams[0].elementAt(nPos+1));
225cdf0e10cSrcweir                 ++nPos;
226cdf0e10cSrcweir             }
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir }
230