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