xref: /trunk/main/framework/qa/complex/loadAllDocuments/InteractionHandler.java (revision 24ebb2589b1437e5dc678c8c93a10999f2ec23d2)
176b6b121SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
376b6b121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
476b6b121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
576b6b121SAndrew Rist  * distributed with this work for additional information
676b6b121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
776b6b121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
876b6b121SAndrew Rist  * "License"); you may not use this file except in compliance
976b6b121SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1176b6b121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1376b6b121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1476b6b121SAndrew Rist  * software distributed under the License is distributed on an
1576b6b121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1676b6b121SAndrew Rist  * KIND, either express or implied.  See the License for the
1776b6b121SAndrew Rist  * specific language governing permissions and limitations
1876b6b121SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2076b6b121SAndrew Rist  *************************************************************/
2176b6b121SAndrew Rist 
22cdf0e10cSrcweir package complex.loadAllDocuments;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.Exception;
27cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.task.XInteractionHandler;
30cdf0e10cSrcweir import com.sun.star.task.XInteractionAbort;
31cdf0e10cSrcweir import com.sun.star.task.XInteractionRetry;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //import java.lang.*;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /**
39*24ebb258Smseidel  * Implements a simple interaction handler,
40cdf0e10cSrcweir  * which can abort all incoming interactions only ... but make it possible to
41cdf0e10cSrcweir  * log it. So it can be used for debug and test purposes.
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir public class InteractionHandler implements XInteractionHandler
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     // ____________________
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /**
48cdf0e10cSrcweir      * @const   RETRY_COUNT it defines the max count of
49cdf0e10cSrcweir      * retrying of an interaction
50cdf0e10cSrcweir      */
51cdf0e10cSrcweir     private static final int RETRY_COUNT    = 3;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     // ____________________
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /**
56*24ebb258Smseidel      * @member  m_aRequest      the original interaction request
57cdf0e10cSrcweir      * safed for later analyzing
58cdf0e10cSrcweir      * @member  m_bWasUsed      true if the interaction handler was used
59cdf0e10cSrcweir      * @member  m_nTry          count using of RETRY continuations
60cdf0e10cSrcweir      */
61cdf0e10cSrcweir     private Object          m_aRequest  ;
62cdf0e10cSrcweir     private int             m_nTry      ;
63cdf0e10cSrcweir     private boolean         m_bWasUsed  ;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     /**
67cdf0e10cSrcweir      * ctor
68*24ebb258Smseidel      * It initializes an object of this class with default values
69cdf0e10cSrcweir      * and set the protocol stack. So the outside code can check
70cdf0e10cSrcweir      * if this handler was used or not.
71cdf0e10cSrcweir      */
InteractionHandler()72cdf0e10cSrcweir     public InteractionHandler()
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         m_aRequest  = null     ;
75cdf0e10cSrcweir         //m_aProtocol = aProtocol;
76cdf0e10cSrcweir         m_nTry      = 0        ;
77cdf0e10cSrcweir         m_bWasUsed = false;
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /**
81*24ebb258Smseidel      * Called to start the interaction, because the outside code wish to solve
82cdf0e10cSrcweir      * a detected problem or to inform the user about something.
83*24ebb258Smseidel      * We save the information here and can handle two well known continuations
84cdf0e10cSrcweir      * only.
85cdf0e10cSrcweir      * [abort and retry].
86cdf0e10cSrcweir      *
87cdf0e10cSrcweir      * @param xRequest
88cdf0e10cSrcweir      *          describe the interaction
89cdf0e10cSrcweir      */
handle(com.sun.star.task.XInteractionRequest xRequest)90cdf0e10cSrcweir     public void handle(com.sun.star.task.XInteractionRequest xRequest)
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         m_bWasUsed = true;
93cdf0e10cSrcweir 
94*24ebb258Smseidel         // first save the original request
95cdf0e10cSrcweir         // Our user can use this information later for some debug analyzing
96cdf0e10cSrcweir         Object aRequest = xRequest.getRequest();
97cdf0e10cSrcweir         synchronized(this)
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             m_aRequest = aRequest;
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         // analyze the possible continuations.
103cdf0e10cSrcweir         // We can abort all incoming interactions only.
104cdf0e10cSrcweir         // But additional we can try to continue it several times too.
105cdf0e10cSrcweir         // Of course after e.g. three loops we have to stop and abort it.
106cdf0e10cSrcweir         com.sun.star.task.XInteractionContinuation[] lContinuations = xRequest.getContinuations();
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         com.sun.star.task.XInteractionAbort xAbort     = null;
109cdf0e10cSrcweir         com.sun.star.task.XInteractionRetry xRetry     = null;
110cdf0e10cSrcweir         com.sun.star.uno.Type               xAbortType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionAbort.class);
111cdf0e10cSrcweir         com.sun.star.uno.Type               xRetryType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionRetry.class);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         for (int i=0; i<lContinuations.length; ++i)
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             try
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 if (xAbort == null)
118cdf0e10cSrcweir                     xAbort = (com.sun.star.task.XInteractionAbort)AnyConverter.toObject(xAbortType, lContinuations[i]);
119cdf0e10cSrcweir                 if (xRetry == null)
120cdf0e10cSrcweir                     xRetry = (com.sun.star.task.XInteractionRetry)AnyConverter.toObject(xRetryType, lContinuations[i]);
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir             catch(com.sun.star.lang.IllegalArgumentException exArg) {}
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         // try it again, but only if it wasn't tried to much before.
126cdf0e10cSrcweir         if (xRetry != null)
127cdf0e10cSrcweir         {
128cdf0e10cSrcweir             synchronized(this)
129cdf0e10cSrcweir             {
130cdf0e10cSrcweir                 if (m_nTry < RETRY_COUNT)
131cdf0e10cSrcweir                 {
132cdf0e10cSrcweir                     ++m_nTry;
133cdf0e10cSrcweir                     xRetry.select();
134cdf0e10cSrcweir                     return;
135cdf0e10cSrcweir                 }
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir 
13915289133Smseidel         // otherwise we can abort this interaction only
140cdf0e10cSrcweir         if (xAbort != null)
141cdf0e10cSrcweir         {
142cdf0e10cSrcweir             xAbort.select();
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
wasUsed()146cdf0e10cSrcweir     public boolean wasUsed() {
147cdf0e10cSrcweir         return m_bWasUsed;
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir }
150