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