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