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