| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
package complex.loadAllDocuments; |
| 19 |
|
| 20 |
import com.sun.star.task.XInteractionHandler; |
| 21 |
import com.sun.star.uno.AnyConverter; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Implements a simple interaction handler, |
| 26 |
* which can abort all incoming interactions only ... but make it possible to |
| 27 |
* log it. So it can be used for debug and test purposes. |
| 28 |
*/ |
| 29 |
public class InteractionHandler implements XInteractionHandler |
| 30 |
{ |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* @const RETRY_COUNT it defines the max count of |
| 35 |
* retrying of an interaction |
| 36 |
*/ |
| 37 |
private static final int RETRY_COUNT = 3; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* count using of RETRY continuations |
| 43 |
*/ |
| 44 |
private int m_nTry ; |
| 45 |
/** true if the interaction handler was used */ |
| 46 |
private boolean m_bWasUsed ; |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* ctor |
| 51 |
* It's initialize an object of this class with default values |
| 52 |
* and set the protocol stack. So the outside code can check |
| 53 |
* if this handler was used or not. |
| 54 |
*/ |
| 55 |
public InteractionHandler() |
| 56 |
{ |
| 57 |
m_nTry = 0 ; |
| 58 |
m_bWasUsed = false; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Called to start the interaction, because the outside code wish to solve |
| 63 |
* a detected problem or to inform the user about something. |
| 64 |
* We save the information here and can handle two well known continuations |
| 65 |
* only. |
| 66 |
* [abort and retry]. |
| 67 |
* |
| 68 |
* @param xRequest |
| 69 |
* describe the interaction |
| 70 |
*/ |
| 71 |
public void handle(com.sun.star.task.XInteractionRequest xRequest) |
| 72 |
{ |
| 73 |
m_bWasUsed = true; |
| 74 |
|
| 75 |
// analyze the possible continuations. |
| 76 |
// We can abort all incoming interactions only. |
| 77 |
// But additional we can try to continue it several times too. |
| 78 |
// Of course after e.g. three loops we have to stop and abort it. |
| 79 |
com.sun.star.task.XInteractionContinuation[] lContinuations = xRequest.getContinuations(); |
| 80 |
|
| 81 |
com.sun.star.task.XInteractionAbort xAbort = null; |
| 82 |
com.sun.star.task.XInteractionRetry xRetry = null; |
| 83 |
com.sun.star.uno.Type xAbortType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionAbort.class); |
| 84 |
com.sun.star.uno.Type xRetryType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionRetry.class); |
| 85 |
|
| 86 |
for (int i=0; i<lContinuations.length; ++i) |
| 87 |
{ |
| 88 |
try |
| 89 |
{ |
| 90 |
if (xAbort == null) |
| 91 |
xAbort = (com.sun.star.task.XInteractionAbort)AnyConverter.toObject(xAbortType, lContinuations[i]); |
| 92 |
if (xRetry == null) |
| 93 |
xRetry = (com.sun.star.task.XInteractionRetry)AnyConverter.toObject(xRetryType, lContinuations[i]); |
| 94 |
} |
| 95 |
catch(com.sun.star.lang.IllegalArgumentException exArg) {} |
| 96 |
} |
| 97 |
|
| 98 |
// try it again, but only if it wasn't tried too much before. |
| 99 |
if (xRetry != null) |
| 100 |
{ |
| 101 |
synchronized(this) |
| 102 |
{ |
| 103 |
if (m_nTry < RETRY_COUNT) |
| 104 |
{ |
| 105 |
++m_nTry; |
| 106 |
xRetry.select(); |
| 107 |
return; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// otherwise we can abort this interaction only |
| 113 |
if (xAbort != null) |
| 114 |
{ |
| 115 |
xAbort.select(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public boolean wasUsed() { |
| 120 |
return m_bWasUsed; |
| 121 |
} |
| 122 |
} |