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