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