1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.loadAllDocuments;
28 
29 import com.sun.star.beans.PropertyValue;
30 
31 import com.sun.star.uno.Exception;
32 import com.sun.star.uno.RuntimeException;
33 
34 import com.sun.star.task.XInteractionHandler;
35 import com.sun.star.task.XInteractionAbort;
36 import com.sun.star.task.XInteractionRetry;
37 
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.AnyConverter;
40 
41 //import java.lang.*;
42 
43 
44 /**
45  * Implemets a simple interaction handler,
46  * which can abort all incoming interactions only ... but make it possible to
47  * log it. So it can be used for debug and test purposes.
48  */
49 public class InteractionHandler implements XInteractionHandler
50 {
51     // ____________________
52 
53     /**
54      * @const   RETRY_COUNT it defines the max count of
55      * retrying of an interaction
56      */
57     private static final int RETRY_COUNT    = 3;
58 
59     // ____________________
60 
61     /**
62      * @member  m_aRequest      the origianl interaction request
63      * safed for later analyzing
64      * @member  m_bWasUsed      true if the interaction handler was used
65      * @member  m_nTry          count using of RETRY continuations
66      */
67     private Object          m_aRequest  ;
68     private int             m_nTry      ;
69     private boolean         m_bWasUsed  ;
70 
71 
72     /**
73      * ctor
74      * It's initialize an object of this class with default values
75      * and set the protocol stack. So the outside code can check
76      * if this handler was used or not.
77      */
78     public InteractionHandler()
79     {
80         m_aRequest  = null     ;
81         //m_aProtocol = aProtocol;
82         m_nTry      = 0        ;
83         m_bWasUsed = false;
84     }
85 
86     /**
87      * Called to start the interaction, because the outside code whish to solve
88      * a detected problem or to inform the user about something.
89      * We safe the informations here and can handle two well known continuations
90      * only.
91      * [abort and retry].
92      *
93      * @param xRequest
94      *          describe the interaction
95      */
96     public void handle(com.sun.star.task.XInteractionRequest xRequest)
97     {
98         m_bWasUsed = true;
99 
100         // first sav thje original request
101         // Our user can use this information later for some debug analyzing
102         Object aRequest = xRequest.getRequest();
103         synchronized(this)
104         {
105             m_aRequest = aRequest;
106         }
107 
108         // analyze the possible continuations.
109         // We can abort all incoming interactions only.
110         // But additional we can try to continue it several times too.
111         // Of course after e.g. three loops we have to stop and abort it.
112         com.sun.star.task.XInteractionContinuation[] lContinuations = xRequest.getContinuations();
113 
114         com.sun.star.task.XInteractionAbort xAbort     = null;
115         com.sun.star.task.XInteractionRetry xRetry     = null;
116         com.sun.star.uno.Type               xAbortType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionAbort.class);
117         com.sun.star.uno.Type               xRetryType = new com.sun.star.uno.Type(com.sun.star.task.XInteractionRetry.class);
118 
119         for (int i=0; i<lContinuations.length; ++i)
120         {
121             try
122             {
123                 if (xAbort == null)
124                     xAbort = (com.sun.star.task.XInteractionAbort)AnyConverter.toObject(xAbortType, lContinuations[i]);
125                 if (xRetry == null)
126                     xRetry = (com.sun.star.task.XInteractionRetry)AnyConverter.toObject(xRetryType, lContinuations[i]);
127             }
128             catch(com.sun.star.lang.IllegalArgumentException exArg) {}
129         }
130 
131         // try it again, but only if it wasn't tried to much before.
132         if (xRetry != null)
133         {
134             synchronized(this)
135             {
136                 if (m_nTry < RETRY_COUNT)
137                 {
138                     ++m_nTry;
139                     xRetry.select();
140                     return;
141                 }
142             }
143         }
144 
145         // otherwhise we can abort this interaction only
146         if (xAbort != null)
147         {
148             xAbort.select();
149         }
150     }
151 
152     public boolean wasUsed() {
153         return m_bWasUsed;
154     }
155 }
156