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_desktop.hxx"
30 
31 #include "cppuhelper/implbase4.hxx"
32 #include "cppuhelper/implementationentry.hxx"
33 #include "rtl/ustrbuf.hxx"
34 #include "rtl/ustring.h"
35 #include "rtl/ustring.hxx"
36 #include "rtl/bootstrap.hxx"
37 #include "sal/types.h"
38 #include "sal/config.h"
39 #include "boost/scoped_array.hpp"
40 #include "com/sun/star/lang/XServiceInfo.hpp"
41 #include "com/sun/star/lang/XInitialization.hpp"
42 #include "com/sun/star/lang/WrappedTargetException.hpp"
43 #include "com/sun/star/task/XJob.hpp"
44 #include "com/sun/star/configuration/backend/XLayer.hpp"
45 #include "com/sun/star/configuration/backend/XLayerHandler.hpp"
46 #include "com/sun/star/configuration/backend/MalformedDataException.hpp"
47 #include "com/sun/star/configuration/backend/TemplateIdentifier.hpp"
48 #include "jvmfwk/framework.h"
49 #include "jvmfwk.hxx"
50 #include <stack>
51 #include <stdio.h>
52 
53 #include "osl/thread.hxx"
54 #define OUSTR(x) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
55 
56 #define SERVICE_NAME "com.sun.star.migration.Java"
57 #define IMPL_NAME "com.sun.star.comp.desktop.migration.Java"
58 
59 #define ENABLE_JAVA     1
60 #define USER_CLASS_PATH 2
61 
62 namespace css = com::sun::star;
63 using namespace rtl;
64 using namespace com::sun::star::uno;
65 using namespace com::sun::star::beans;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::configuration::backend;
68 
69 namespace migration
70 {
71 
72 class CJavaInfo
73 {
74     CJavaInfo(const CJavaInfo&);
75     CJavaInfo& operator = (const CJavaInfo&);
76 public:
77     JavaInfo* pData;
78     CJavaInfo();
79     ~CJavaInfo();
80     operator JavaInfo* ();
81 };
82 
83 CJavaInfo::CJavaInfo(): pData(NULL)
84 {
85 }
86 
87 CJavaInfo::~CJavaInfo()
88 {
89     jfw_freeJavaInfo(pData);
90 }
91 
92 CJavaInfo::operator JavaInfo*()
93 {
94     return pData;
95 }
96 
97 
98 class JavaMigration : public ::cppu::WeakImplHelper4<
99     css::lang::XServiceInfo,
100     css::lang::XInitialization,
101     css::task::XJob,
102     css::configuration::backend::XLayerHandler>
103 {
104 public:
105     // XServiceInfo
106     virtual OUString SAL_CALL getImplementationName()
107         throw (css::uno::RuntimeException);
108     virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName )
109         throw (css::uno::RuntimeException);
110     virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
111         throw (css::uno::RuntimeException);
112 
113 	//XInitialization
114     virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
115         throw(css::uno::Exception, css::uno::RuntimeException);
116 
117     //XJob
118     virtual css::uno::Any SAL_CALL execute(
119         const css::uno::Sequence<css::beans::NamedValue >& Arguments )
120         throw (css::lang::IllegalArgumentException, css::uno::Exception,
121                css::uno::RuntimeException);
122 
123         // XLayerHandler
124     virtual void SAL_CALL startLayer()
125         throw(::com::sun::star::lang::WrappedTargetException);
126 
127     virtual void SAL_CALL endLayer()
128         throw(
129             ::com::sun::star::configuration::backend::MalformedDataException,
130             ::com::sun::star::lang::WrappedTargetException );
131 
132     virtual void SAL_CALL overrideNode(
133             const rtl::OUString& aName,
134             sal_Int16 aAttributes,
135             sal_Bool bClear)
136         throw(
137             ::com::sun::star::configuration::backend::MalformedDataException,
138             ::com::sun::star::lang::WrappedTargetException );
139 
140     virtual void SAL_CALL addOrReplaceNode(
141             const rtl::OUString& aName,
142             sal_Int16 aAttributes)
143         throw(
144             ::com::sun::star::configuration::backend::MalformedDataException,
145             ::com::sun::star::lang::WrappedTargetException );
146 
147     virtual void SAL_CALL  addOrReplaceNodeFromTemplate(
148             const rtl::OUString& aName,
149             const ::com::sun::star::configuration::backend::TemplateIdentifier& aTemplate,
150             sal_Int16 aAttributes )
151         throw(
152             ::com::sun::star::configuration::backend::MalformedDataException,
153             ::com::sun::star::lang::WrappedTargetException );
154 
155     virtual void SAL_CALL  endNode()
156         throw(
157             ::com::sun::star::configuration::backend::MalformedDataException,
158             ::com::sun::star::lang::WrappedTargetException );
159 
160     virtual void SAL_CALL  dropNode(
161             const rtl::OUString& aName )
162         throw(
163             ::com::sun::star::configuration::backend::MalformedDataException,
164             ::com::sun::star::lang::WrappedTargetException );
165 
166     virtual void SAL_CALL  overrideProperty(
167             const rtl::OUString& aName,
168             sal_Int16 aAttributes,
169             const css::uno::Type& aType,
170             sal_Bool bClear )
171         throw(
172             ::com::sun::star::configuration::backend::MalformedDataException,
173             ::com::sun::star::lang::WrappedTargetException );
174 
175     virtual void SAL_CALL  setPropertyValue(
176             const css::uno::Any& aValue )
177         throw(
178             ::com::sun::star::configuration::backend::MalformedDataException,
179             ::com::sun::star::lang::WrappedTargetException );
180 
181     virtual void SAL_CALL setPropertyValueForLocale(
182             const css::uno::Any& aValue,
183             const rtl::OUString& aLocale )
184         throw(
185             ::com::sun::star::configuration::backend::MalformedDataException,
186             ::com::sun::star::lang::WrappedTargetException );
187 
188     virtual void SAL_CALL  endProperty()
189         throw(
190             ::com::sun::star::configuration::backend::MalformedDataException,
191             ::com::sun::star::lang::WrappedTargetException );
192 
193     virtual void SAL_CALL  addProperty(
194             const rtl::OUString& aName,
195             sal_Int16 aAttributes,
196             const css::uno::Type& aType )
197         throw(
198             ::com::sun::star::configuration::backend::MalformedDataException,
199             ::com::sun::star::lang::WrappedTargetException );
200 
201     virtual void SAL_CALL  addPropertyWithValue(
202             const rtl::OUString& aName,
203             sal_Int16 aAttributes,
204             const css::uno::Any& aValue )
205         throw(
206             ::com::sun::star::configuration::backend::MalformedDataException,
207             ::com::sun::star::lang::WrappedTargetException );
208 
209 
210 
211     //----------------
212     ~JavaMigration();
213 
214 private:
215     OUString m_sUserDir;
216 	css::uno::Reference< ::css::configuration::backend::XLayer>	m_xLayer;
217 
218     void migrateJavarc();
219     typedef ::std::pair< ::rtl::OUString,	sal_Int16>	TElementType;
220     typedef ::std::stack< TElementType > TElementStack;
221     TElementStack m_aStack;
222 
223 };
224 
225 JavaMigration::~JavaMigration()
226 {
227     OSL_ASSERT(m_aStack.empty());
228 }
229 
230 OUString jvmfwk_getImplementationName()
231 {
232     return OUSTR(IMPL_NAME);
233 }
234 
235 css::uno::Sequence< OUString > jvmfwk_getSupportedServiceNames()
236 {
237     OUString str_name = OUSTR(SERVICE_NAME);
238     return css::uno::Sequence< OUString >( &str_name, 1 );
239 }
240 
241 // XServiceInfo
242 OUString SAL_CALL JavaMigration::getImplementationName()
243     throw (css::uno::RuntimeException)
244 {
245     return jvmfwk_getImplementationName();
246 }
247 
248 sal_Bool SAL_CALL JavaMigration::supportsService( const OUString & rServiceName )
249         throw (css::uno::RuntimeException)
250 {
251     css::uno::Sequence< OUString > const & rSNL = getSupportedServiceNames();
252     OUString const * pArray = rSNL.getConstArray();
253     for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
254     {
255         if (rServiceName.equals( pArray[ nPos ] ))
256             return true;
257     }
258     return false;
259 
260 }
261 
262 css::uno::Sequence< OUString > SAL_CALL JavaMigration::getSupportedServiceNames()
263         throw (css::uno::RuntimeException)
264 {
265     return jvmfwk_getSupportedServiceNames();
266 }
267 
268 //XInitialization ----------------------------------------------------------------------
269 void SAL_CALL JavaMigration::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
270         throw(css::uno::Exception, css::uno::RuntimeException)
271 {
272     const css::uno::Any* pIter = aArguments.getConstArray();
273 	const css::uno::Any* pEnd = pIter + aArguments.getLength();
274 	css::uno::Sequence<css::beans::NamedValue> aOldConfigValues;
275 	css::beans::NamedValue aValue;
276 	for(;pIter != pEnd;++pIter)
277 	{
278 		*pIter >>= aValue;
279 		if (aValue.Name.equalsAscii("OldConfiguration"))
280 		{
281             sal_Bool bSuccess = aValue.Value >>= aOldConfigValues;
282             OSL_ENSURE(bSuccess == sal_True, "[Service implementation " IMPL_NAME
283                        "] XInitialization::initialize: Argument OldConfiguration has wrong type.");
284             if (bSuccess)
285             {
286                 const css::beans::NamedValue* pIter2 = aOldConfigValues.getConstArray();
287                 const css::beans::NamedValue* pEnd2 = pIter2 + aOldConfigValues.getLength();
288                 for(;pIter2 != pEnd2;++pIter2)
289                 {
290                     if ( pIter2->Name.equalsAscii("org.openoffice.Office.Java") )
291                     {
292                         pIter2->Value >>= m_xLayer;
293                         break;
294                     }
295                 }
296 			}
297 		}
298         else if (aValue.Name.equalsAscii("UserData"))
299         {
300             if ( !(aValue.Value >>= m_sUserDir) )
301             {
302                 OSL_ENSURE(
303                     false,
304                     "[Service implementation " IMPL_NAME
305                     "] XInitialization::initialize: Argument UserData has wrong type.");
306             }
307         }
308 	}
309 
310 }
311 
312 //XJob
313 css::uno::Any SAL_CALL JavaMigration::execute(
314         const css::uno::Sequence<css::beans::NamedValue >& )
315         throw (css::lang::IllegalArgumentException, css::uno::Exception,
316                css::uno::RuntimeException)
317 {
318     migrateJavarc();
319     if (m_xLayer.is())
320         m_xLayer->readData(this);
321 
322     return css::uno::Any();
323 }
324 
325 void JavaMigration::migrateJavarc()
326 {
327     if (m_sUserDir.getLength() == 0)
328         return;
329 
330     OUString sValue;
331     rtl::Bootstrap javaini(m_sUserDir + OUSTR("/user/config/"SAL_CONFIGFILE("java")));
332     sal_Bool bSuccess = javaini.getFrom(OUSTR("Home"), sValue);
333     OSL_ENSURE(bSuccess, "[Service implementation " IMPL_NAME
334                        "] XJob::execute: Could not get Home entry from java.ini/javarc.");
335     if (bSuccess == sal_True && sValue.getLength() > 0)
336     {
337         //get the directory
338         CJavaInfo aInfo;
339         javaFrameworkError err = jfw_getJavaInfoByPath(sValue.pData, &aInfo.pData);
340 
341         if (err == JFW_E_NONE)
342         {
343             if (jfw_setSelectedJRE(aInfo) != JFW_E_NONE)
344             {
345                 OSL_ENSURE(0, "[Service implementation " IMPL_NAME
346                            "] XJob::execute: jfw_setSelectedJRE failed.");
347                 fprintf(stderr, "\nCannot migrate Java. An error occured.\n");
348             }
349         }
350         else if (err == JFW_E_FAILED_VERSION)
351         {
352             fprintf(stderr, "\nCannot migrate Java settings because the version of the Java  "
353                     "is not supported anymore.\n");
354         }
355     }
356 }
357 
358 
359 // XLayerHandler
360 void SAL_CALL JavaMigration::startLayer()
361     throw(css::lang::WrappedTargetException)
362 {
363 }
364 // -----------------------------------------------------------------------------
365 
366 void SAL_CALL JavaMigration::endLayer()
367     throw(
368         MalformedDataException,
369         WrappedTargetException )
370 {
371 }
372 // -----------------------------------------------------------------------------
373 
374 void SAL_CALL JavaMigration::overrideNode(
375         const ::rtl::OUString&,
376         sal_Int16,
377         sal_Bool)
378     throw(
379         MalformedDataException,
380         WrappedTargetException )
381 
382 {
383 
384 }
385 // -----------------------------------------------------------------------------
386 
387 void SAL_CALL JavaMigration::addOrReplaceNode(
388         const ::rtl::OUString&,
389         sal_Int16)
390     throw(
391         MalformedDataException,
392         WrappedTargetException )
393 {
394 
395 }
396 void SAL_CALL  JavaMigration::endNode()
397     throw(
398         MalformedDataException,
399         WrappedTargetException )
400 {
401 }
402 // -----------------------------------------------------------------------------
403 
404 void SAL_CALL  JavaMigration::dropNode(
405         const ::rtl::OUString& )
406     throw(
407         MalformedDataException,
408         WrappedTargetException )
409 {
410 }
411 // -----------------------------------------------------------------------------
412 
413 void SAL_CALL  JavaMigration::overrideProperty(
414         const ::rtl::OUString& aName,
415         sal_Int16,
416         const Type&,
417         sal_Bool )
418     throw(
419         MalformedDataException,
420         WrappedTargetException )
421 {
422     if (aName.equalsAscii("Enable"))
423         m_aStack.push(TElementStack::value_type(aName,ENABLE_JAVA));
424     else if (aName.equalsAscii("UserClassPath"))
425         m_aStack.push(TElementStack::value_type(aName, USER_CLASS_PATH));
426 }
427 // -----------------------------------------------------------------------------
428 
429 void SAL_CALL  JavaMigration::setPropertyValue(
430         const Any& aValue )
431     throw(
432         MalformedDataException,
433         WrappedTargetException )
434 {
435     if ( !m_aStack.empty())
436     {
437         switch (m_aStack.top().second)
438         {
439         case ENABLE_JAVA:
440         {
441             sal_Bool val = sal_Bool();
442             if ((aValue >>= val) == sal_False)
443                 throw MalformedDataException(
444                     OUSTR("[Service implementation " IMPL_NAME
445                        "] XLayerHandler::setPropertyValue received wrong type for Enable property"), 0, Any());
446             if (jfw_setEnabled(val) != JFW_E_NONE)
447                 throw WrappedTargetException(
448                     OUSTR("[Service implementation " IMPL_NAME
449                        "] XLayerHandler::setPropertyValue: jfw_setEnabled failed."), 0, Any());
450 
451             break;
452         }
453         case USER_CLASS_PATH:
454          {
455              OUString cp;
456              if ((aValue >>= cp) == sal_False)
457                  throw MalformedDataException(
458                      OUSTR("[Service implementation " IMPL_NAME
459                            "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property"), 0, Any());
460 
461              if (jfw_setUserClassPath(cp.pData) != JFW_E_NONE)
462                  throw WrappedTargetException(
463                      OUSTR("[Service implementation " IMPL_NAME
464                        "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed."), 0, Any());
465              break;
466          }
467         default:
468             OSL_ASSERT(0);
469         }
470     }
471 }
472 // -----------------------------------------------------------------------------
473 
474 void SAL_CALL JavaMigration::setPropertyValueForLocale(
475         const Any&,
476         const ::rtl::OUString& )
477     throw(
478         MalformedDataException,
479         WrappedTargetException )
480 {
481 }
482 // -----------------------------------------------------------------------------
483 
484 void SAL_CALL  JavaMigration::endProperty()
485     throw(
486         MalformedDataException,
487         WrappedTargetException )
488 {
489         	if (!m_aStack.empty())
490                 m_aStack.pop();
491 }
492 // -----------------------------------------------------------------------------
493 
494 void SAL_CALL  JavaMigration::addProperty(
495         const rtl::OUString&,
496         sal_Int16,
497         const Type& )
498     throw(
499         MalformedDataException,
500         WrappedTargetException )
501 {
502 }
503 // -----------------------------------------------------------------------------
504 
505 void SAL_CALL  JavaMigration::addPropertyWithValue(
506         const rtl::OUString&,
507         sal_Int16,
508         const Any& )
509     throw(
510         MalformedDataException,
511         WrappedTargetException )
512 {
513 }
514 
515 void SAL_CALL JavaMigration::addOrReplaceNodeFromTemplate(
516         const rtl::OUString&,
517         const TemplateIdentifier&,
518         sal_Int16 )
519     throw(
520         MalformedDataException,
521         WrappedTargetException )
522 {
523 }
524 
525 // -----------------------------------------------------------------------------
526 //ToDo enable java, user class path
527 
528 } //end namespace jfw
529 
530