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 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 
32 
33 #include <svtools/svtools.hrc>
34 #include <tools/resid.hxx>
35 #include <com/sun/star/task/XInteractionContinuation.hpp>
36 #include <com/sun/star/task/XInteractionAbort.hpp>
37 #include <com/sun/star/task/XInteractionRetry.hpp>
38 #include <com/sun/star/java/JavaNotFoundException.hpp>
39 #include <com/sun/star/java/InvalidJavaSettingsException.hpp>
40 #include <com/sun/star/java/JavaDisabledException.hpp>
41 #include <com/sun/star/java/JavaVMCreationFailureException.hpp>
42 #include <com/sun/star/java/RestartRequiredException.hpp>
43 #include <vcl/svapp.hxx>
44 #include <vcl/msgbox.hxx>
45 #include <vos/mutex.hxx>
46 #include <tools/string.hxx>
47 #include <tools/rcid.h>
48 #include <jvmfwk/framework.h>
49 
50 #include <svtools/svtdata.hxx>
51 #include <svtools/javainteractionhandler.hxx>
52 #include <svtools/javacontext.hxx>
53 
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::task;
56 
57 namespace svt
58 {
59 
60 JavaInteractionHandler::JavaInteractionHandler():
61     m_aRefCount(0),
62     m_bShowErrorsOnce(false),
63     m_bJavaDisabled_Handled(false),
64     m_bInvalidSettings_Handled(false),
65     m_bJavaNotFound_Handled(false),
66     m_bVMCreationFailure_Handled(false),
67     m_bRestartRequired_Handled(false),
68     m_nResult_JavaDisabled(RET_NO)
69 {
70 }
71 
72 JavaInteractionHandler::JavaInteractionHandler(bool bReportErrorOnce) :
73     m_aRefCount(0),
74     m_bShowErrorsOnce(bReportErrorOnce),
75     m_bJavaDisabled_Handled(false),
76     m_bInvalidSettings_Handled(false),
77     m_bJavaNotFound_Handled(false),
78     m_bVMCreationFailure_Handled(false),
79     m_bRestartRequired_Handled(false),
80     m_nResult_JavaDisabled(RET_NO)
81 {
82 }
83 
84 JavaInteractionHandler::~JavaInteractionHandler()
85 {
86 }
87 
88 Any SAL_CALL JavaInteractionHandler::queryInterface(const Type& aType )
89     throw (RuntimeException)
90 {
91     if (aType == getCppuType(reinterpret_cast<Reference<XInterface>*>(0)))
92         return Any( static_cast<XInterface*>(this), aType);
93     else if (aType == getCppuType(reinterpret_cast<Reference<XInteractionHandler>*>(0)))
94         return Any( static_cast<XInteractionHandler*>(this), aType);
95     return Any();
96 }
97 
98 void SAL_CALL JavaInteractionHandler::acquire(  ) throw ()
99 {
100     osl_incrementInterlockedCount( &m_aRefCount );
101 }
102 
103 void SAL_CALL JavaInteractionHandler::release(  ) throw ()
104 {
105     if (! osl_decrementInterlockedCount( &m_aRefCount ))
106 		delete this;
107 }
108 
109 
110 void SAL_CALL JavaInteractionHandler::handle( const Reference< XInteractionRequest >& Request ) throw (RuntimeException)
111 {
112     Any anyExc = Request->getRequest();
113     Sequence< Reference< XInteractionContinuation > > aSeqCont = Request->getContinuations();
114 
115     Reference< XInteractionAbort > abort;
116     Reference< XInteractionRetry > retry;
117     sal_Int32 i;
118 
119     for ( i = 0; i < aSeqCont.getLength(); i++ )
120     {
121         abort = Reference< XInteractionAbort>::query( aSeqCont[i]);
122         if ( abort.is() )
123             break;
124     }
125 
126 	for ( i= 0; i < aSeqCont.getLength(); i++)
127     {
128         retry= Reference<XInteractionRetry>::query( aSeqCont[i]);
129         if ( retry.is() )
130             break;
131     }
132 
133     com::sun::star::java::JavaNotFoundException e1;
134     com::sun::star::java::InvalidJavaSettingsException e2;
135  	com::sun::star::java::JavaDisabledException				e3;
136     com::sun::star::java::JavaVMCreationFailureException	e4;
137     com::sun::star::java::RestartRequiredException e5;
138 	// Try to recover the Exception type in the any and
139 	// react accordingly.
140 	sal_uInt16		nResult = RET_CANCEL;
141     ::rtl::OUString    aParameter;
142 
143 	if ( anyExc >>= e1 )
144 	{
145         if( ! (m_bShowErrorsOnce && m_bJavaNotFound_Handled))
146         {
147            // No suitable JRE found
148             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
149             m_bJavaNotFound_Handled = true;
150             WarningBox aWarningBox( NULL, SvtResId( WARNINGBOX_JAVANOTFOUND ) );
151             String aTitle( SvtResId( STR_WARNING_JAVANOTFOUND ) );
152             aWarningBox.SetText( aTitle );
153             nResult = aWarningBox.Execute();
154         }
155         else
156         {
157             nResult = RET_OK;
158         }
159 	}
160 	else if ( anyExc >>= e2 )
161 	{
162         if( !(m_bShowErrorsOnce && m_bInvalidSettings_Handled))
163         {
164            // javavendors.xml was updated and Java has not been configured yet
165             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
166             m_bInvalidSettings_Handled = true;
167             WarningBox aWarningBox( NULL, SvtResId( WARNINGBOX_INVALIDJAVASETTINGS ) );
168             String aTitle( SvtResId(STR_WARNING_INVALIDJAVASETTINGS));
169             aWarningBox.SetText( aTitle );
170             nResult = aWarningBox.Execute();
171         }
172         else
173         {
174             nResult = RET_OK;
175         }
176 	}
177 	else if ( anyExc >>= e3 )
178 	{
179         if( !(m_bShowErrorsOnce && m_bJavaDisabled_Handled))
180         {
181             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
182             m_bJavaDisabled_Handled = true;
183             // Java disabled. Give user a chance to enable Java inside Office.
184             QueryBox aQueryBox( NULL, SvtResId( QBX_JAVADISABLED ) );
185             String aTitle( SvtResId( STR_QUESTION_JAVADISABLED ) );
186             aQueryBox.SetText( aTitle );
187             nResult = aQueryBox.Execute();
188             if ( nResult == RET_YES )
189             {
190                 jfw_setEnabled(sal_True);
191             }
192 
193             m_nResult_JavaDisabled = nResult;
194 
195         }
196         else
197         {
198             nResult = m_nResult_JavaDisabled;
199         }
200 	}
201 	else if ( anyExc >>= e4 )
202 	{
203         if( !(m_bShowErrorsOnce && m_bVMCreationFailure_Handled))
204         {
205             // Java not correctly installed, or damaged
206             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
207             m_bVMCreationFailure_Handled = true;
208             ErrorBox aErrorBox( NULL, SvtResId( ERRORBOX_JVMCREATIONFAILED ) );
209             String aTitle( SvtResId( STR_ERROR_JVMCREATIONFAILED ) );
210             aErrorBox.SetText( aTitle );
211             nResult = aErrorBox.Execute();
212         }
213         else
214         {
215             nResult = RET_OK;
216         }
217 	}
218 	else if ( anyExc >>= e5 )
219 	{
220         if( !(m_bShowErrorsOnce && m_bRestartRequired_Handled))
221         {
222             // a new JRE was selected, but office needs to be restarted
223             //before it can be used.
224             vos::OGuard aSolarGuard( Application::GetSolarMutex() );
225             m_bRestartRequired_Handled = true;
226             ErrorBox aErrorBox(NULL, SvtResId( ERRORBOX_RESTARTREQUIRED ) );
227             String aTitle( SvtResId( STR_ERROR_RESTARTREQUIRED ) );
228             aErrorBox.SetText( aTitle );
229             nResult = aErrorBox.Execute();
230         }
231         else
232         {
233             nResult = RET_OK;
234         }
235 	}
236 
237 	if ( nResult == RET_CANCEL || nResult == RET_NO)
238 	{
239 		// Unknown exception type or user wants to cancel
240 		if ( abort.is() )
241 			abort->select();
242 	}
243 	else // nResult == RET_OK
244 	{
245 		// User selected OK => retry Java usage
246 		if ( retry.is() )
247 			retry->select();
248 	}
249 }
250 
251 }
252