xref: /trunk/main/framework/qa/complex/loadAllDocuments/InteractionHandler.java (revision ffd38472365e95f6a578737bc9a5eb0fac624a86)
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 package complex.loadAllDocuments;
23 
24 import com.sun.star.beans.PropertyValue;
25 
26 import com.sun.star.uno.Exception;
27 import com.sun.star.uno.RuntimeException;
28 
29 import com.sun.star.task.XInteractionHandler;
30 import com.sun.star.task.XInteractionAbort;
31 import com.sun.star.task.XInteractionRetry;
32 
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.AnyConverter;
35 
36 //import java.lang.*;
37 
38 /**
39  * Implements a simple interaction handler,
40  * which can abort all incoming interactions only ... but make it possible to
41  * log it. So it can be used for debug and test purposes.
42  */
43 public class InteractionHandler implements XInteractionHandler
44 {
45     // ____________________
46 
47     /**
48      * @const   RETRY_COUNT it defines the max count of
49      * retrying of an interaction
50      */
51     private static final int RETRY_COUNT    = 3;
52 
53     // ____________________
54 
55     /**
56      * @member  m_aRequest      the original interaction request
57      * safed for later analyzing
58      * @member  m_bWasUsed      true if the interaction handler was used
59      * @member  m_nTry          count using of RETRY continuations
60      */
61     private Object          m_aRequest  ;
62     private int             m_nTry      ;
63     private boolean         m_bWasUsed  ;
64 
65 
66     /**
67      * ctor
68      * It initializes an object of this class with default values
69      * and set the protocol stack. So the outside code can check
70      * if this handler was used or not.
71      */
72     public InteractionHandler()
73     {
74         m_aRequest  = null     ;
75         //m_aProtocol = aProtocol;
76         m_nTry      = 0        ;
77         m_bWasUsed = false;
78     }
79 
80     /**
81      * Called to start the interaction, because the outside code wish to solve
82      * a detected problem or to inform the user about something.
83      * We save the information here and can handle two well known continuations
84      * only.
85      * [abort and retry].
86      *
87      * @param xRequest
88      *          describe the interaction
89      */
90     public void handle(com.sun.star.task.XInteractionRequest xRequest)
91     {
92         m_bWasUsed = true;
93 
94         // first save the original request
95         // Our user can use this information later for some debug analyzing
96         Object aRequest = xRequest.getRequest();
97         synchronized(this)
98         {
99             m_aRequest = aRequest;
100         }
101 
102         // analyze the possible continuations.
103         // We can abort all incoming interactions only.
104         // But additional we can try to continue it several times too.
105         // Of course after e.g. three loops we have to stop and abort it.
106         com.sun.star.task.XInteractionContinuation[] lContinuations = xRequest.getContinuations();
107 
108         com.sun.star.task.XInteractionAbort xAbort     = null;
109         com.sun.star.task.XInteractionRetry xRetry     = null;
110         com.sun.star.uno.Type               xAbortType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionAbort.class);
111         com.sun.star.uno.Type               xRetryType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionRetry.class);
112 
113         for (int i=0; i<lContinuations.length; ++i)
114         {
115             try
116             {
117                 if (xAbort == null)
118                     xAbort = (com.sun.star.task.XInteractionAbort)AnyConverter.toObject(xAbortType, lContinuations[i]);
119                 if (xRetry == null)
120                     xRetry = (com.sun.star.task.XInteractionRetry)AnyConverter.toObject(xRetryType, lContinuations[i]);
121             }
122             catch(com.sun.star.lang.IllegalArgumentException exArg) {}
123         }
124 
125         // try it again, but only if it wasn't tried to much before.
126         if (xRetry != null)
127         {
128             synchronized(this)
129             {
130                 if (m_nTry < RETRY_COUNT)
131                 {
132                     ++m_nTry;
133                     xRetry.select();
134                     return;
135                 }
136             }
137         }
138 
139         // otherwise we can abort this interaction only
140         if (xAbort != null)
141         {
142             xAbort.select();
143         }
144     }
145 
146     public boolean wasUsed() {
147         return m_bWasUsed;
148     }
149 }
150