Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "cppuhelper/implbase4.hxx"
31 : : #include "cppuhelper/implementationentry.hxx"
32 : : #include "rtl/ustrbuf.hxx"
33 : : #include "rtl/ustring.h"
34 : : #include "rtl/ustring.hxx"
35 : : #include "rtl/bootstrap.hxx"
36 : : #include "sal/types.h"
37 : : #include "sal/config.h"
38 : : #include "boost/scoped_array.hpp"
39 : : #include "com/sun/star/lang/XServiceInfo.hpp"
40 : : #include "com/sun/star/lang/XInitialization.hpp"
41 : : #include "com/sun/star/lang/WrappedTargetException.hpp"
42 : : #include "com/sun/star/task/XJob.hpp"
43 : : #include "com/sun/star/configuration/backend/XLayer.hpp"
44 : : #include "com/sun/star/configuration/backend/XLayerHandler.hpp"
45 : : #include "com/sun/star/configuration/backend/MalformedDataException.hpp"
46 : : #include "com/sun/star/configuration/backend/TemplateIdentifier.hpp"
47 : : #include "jvmfwk/framework.h"
48 : : #include "jvmfwk.hxx"
49 : : #include <stack>
50 : : #include <stdio.h>
51 : :
52 : : #include "osl/thread.hxx"
53 : : using ::rtl::OUString;
54 : :
55 : : #define OUSTR(x) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
56 : :
57 : : #define SERVICE_NAME "com.sun.star.migration.Java"
58 : : #define IMPL_NAME "com.sun.star.comp.desktop.migration.Java"
59 : :
60 : : #define ENABLE_JAVA 1
61 : : #define USER_CLASS_PATH 2
62 : :
63 : : namespace css = com::sun::star;
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* () const;
81 : : };
82 : :
83 : 0 : CJavaInfo::CJavaInfo(): pData(NULL)
84 : : {
85 : 0 : }
86 : :
87 : 0 : CJavaInfo::~CJavaInfo()
88 : : {
89 : 0 : jfw_freeJavaInfo(pData);
90 : 0 : }
91 : :
92 : 0 : CJavaInfo::operator JavaInfo*() const
93 : : {
94 : 0 : 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 : 0 : JavaMigration::~JavaMigration()
226 : : {
227 : : OSL_ASSERT(m_aStack.empty());
228 : 0 : }
229 : :
230 : 0 : OUString jvmfwk_getImplementationName()
231 : : {
232 : 0 : return OUSTR(IMPL_NAME);
233 : : }
234 : :
235 : 0 : css::uno::Sequence< OUString > jvmfwk_getSupportedServiceNames()
236 : : {
237 : 0 : OUString str_name = OUSTR(SERVICE_NAME);
238 : 0 : return css::uno::Sequence< OUString >( &str_name, 1 );
239 : : }
240 : :
241 : : // XServiceInfo
242 : 0 : OUString SAL_CALL JavaMigration::getImplementationName()
243 : : throw (css::uno::RuntimeException)
244 : : {
245 : 0 : return jvmfwk_getImplementationName();
246 : : }
247 : :
248 : 0 : sal_Bool SAL_CALL JavaMigration::supportsService( const OUString & rServiceName )
249 : : throw (css::uno::RuntimeException)
250 : : {
251 : 0 : css::uno::Sequence< OUString > const & rSNL = getSupportedServiceNames();
252 : 0 : OUString const * pArray = rSNL.getConstArray();
253 : 0 : for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
254 : : {
255 : 0 : if (rServiceName.equals( pArray[ nPos ] ))
256 : 0 : return true;
257 : : }
258 : 0 : return false;
259 : :
260 : : }
261 : :
262 : 0 : css::uno::Sequence< OUString > SAL_CALL JavaMigration::getSupportedServiceNames()
263 : : throw (css::uno::RuntimeException)
264 : : {
265 : 0 : return jvmfwk_getSupportedServiceNames();
266 : : }
267 : :
268 : : //XInitialization ----------------------------------------------------------------------
269 : 0 : void SAL_CALL JavaMigration::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
270 : : throw(css::uno::Exception, css::uno::RuntimeException)
271 : : {
272 : 0 : const css::uno::Any* pIter = aArguments.getConstArray();
273 : 0 : const css::uno::Any* pEnd = pIter + aArguments.getLength();
274 : 0 : css::uno::Sequence<css::beans::NamedValue> aOldConfigValues;
275 : 0 : css::beans::NamedValue aValue;
276 : 0 : for(;pIter != pEnd;++pIter)
277 : : {
278 : 0 : *pIter >>= aValue;
279 : 0 : if ( aValue.Name == "OldConfiguration" )
280 : : {
281 : 0 : 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 : 0 : if (bSuccess)
285 : : {
286 : 0 : const css::beans::NamedValue* pIter2 = aOldConfigValues.getConstArray();
287 : 0 : const css::beans::NamedValue* pEnd2 = pIter2 + aOldConfigValues.getLength();
288 : 0 : for(;pIter2 != pEnd2;++pIter2)
289 : : {
290 : 0 : if ( pIter2->Name == "org.openoffice.Office.Java" )
291 : : {
292 : 0 : pIter2->Value >>= m_xLayer;
293 : 0 : break;
294 : : }
295 : : }
296 : : }
297 : : }
298 : 0 : else if ( aValue.Name == "UserData" )
299 : : {
300 : 0 : if ( !(aValue.Value >>= m_sUserDir) )
301 : : {
302 : : OSL_FAIL(
303 : : "[Service implementation " IMPL_NAME
304 : : "] XInitialization::initialize: Argument UserData has wrong type.");
305 : : }
306 : : }
307 : 0 : }
308 : :
309 : 0 : }
310 : :
311 : : //XJob
312 : 0 : css::uno::Any SAL_CALL JavaMigration::execute(
313 : : const css::uno::Sequence<css::beans::NamedValue >& )
314 : : throw (css::lang::IllegalArgumentException, css::uno::Exception,
315 : : css::uno::RuntimeException)
316 : : {
317 : 0 : migrateJavarc();
318 : 0 : if (m_xLayer.is())
319 : 0 : m_xLayer->readData(this);
320 : :
321 : 0 : return css::uno::Any();
322 : : }
323 : :
324 : 0 : void JavaMigration::migrateJavarc()
325 : : {
326 : 0 : if (m_sUserDir.isEmpty())
327 : 0 : return;
328 : :
329 : 0 : OUString sValue;
330 : 0 : rtl::Bootstrap javaini(m_sUserDir + OUSTR( "/user/config/" SAL_CONFIGFILE("java") ));
331 : 0 : sal_Bool bSuccess = javaini.getFrom(OUSTR("Home"), sValue);
332 : : OSL_ENSURE(bSuccess, "[Service implementation " IMPL_NAME
333 : : "] XJob::execute: Could not get Home entry from java.ini/javarc.");
334 : 0 : if (bSuccess == sal_True && !sValue.isEmpty())
335 : : {
336 : : //get the directory
337 : 0 : CJavaInfo aInfo;
338 : 0 : javaFrameworkError err = jfw_getJavaInfoByPath(sValue.pData, &aInfo.pData);
339 : :
340 : 0 : if (err == JFW_E_NONE)
341 : : {
342 : 0 : if (jfw_setSelectedJRE(aInfo) != JFW_E_NONE)
343 : : {
344 : : OSL_FAIL("[Service implementation " IMPL_NAME
345 : : "] XJob::execute: jfw_setSelectedJRE failed.");
346 : 0 : fprintf(stderr, "\nCannot migrate Java. An error occurred.\n");
347 : : }
348 : : }
349 : 0 : else if (err == JFW_E_FAILED_VERSION)
350 : : {
351 : : fprintf(stderr, "\nCannot migrate Java settings because the version of the Java "
352 : 0 : "is not supported anymore.\n");
353 : 0 : }
354 : 0 : }
355 : : }
356 : :
357 : :
358 : : // XLayerHandler
359 : 0 : void SAL_CALL JavaMigration::startLayer()
360 : : throw(css::lang::WrappedTargetException)
361 : : {
362 : 0 : }
363 : : // -----------------------------------------------------------------------------
364 : :
365 : 0 : void SAL_CALL JavaMigration::endLayer()
366 : : throw(
367 : : MalformedDataException,
368 : : WrappedTargetException )
369 : : {
370 : 0 : }
371 : : // -----------------------------------------------------------------------------
372 : :
373 : 0 : void SAL_CALL JavaMigration::overrideNode(
374 : : const ::rtl::OUString&,
375 : : sal_Int16,
376 : : sal_Bool)
377 : : throw(
378 : : MalformedDataException,
379 : : WrappedTargetException )
380 : :
381 : : {
382 : :
383 : 0 : }
384 : : // -----------------------------------------------------------------------------
385 : :
386 : 0 : void SAL_CALL JavaMigration::addOrReplaceNode(
387 : : const ::rtl::OUString&,
388 : : sal_Int16)
389 : : throw(
390 : : MalformedDataException,
391 : : WrappedTargetException )
392 : : {
393 : :
394 : 0 : }
395 : 0 : void SAL_CALL JavaMigration::endNode()
396 : : throw(
397 : : MalformedDataException,
398 : : WrappedTargetException )
399 : : {
400 : 0 : }
401 : : // -----------------------------------------------------------------------------
402 : :
403 : 0 : void SAL_CALL JavaMigration::dropNode(
404 : : const ::rtl::OUString& )
405 : : throw(
406 : : MalformedDataException,
407 : : WrappedTargetException )
408 : : {
409 : 0 : }
410 : : // -----------------------------------------------------------------------------
411 : :
412 : 0 : void SAL_CALL JavaMigration::overrideProperty(
413 : : const ::rtl::OUString& aName,
414 : : sal_Int16,
415 : : const Type&,
416 : : sal_Bool )
417 : : throw(
418 : : MalformedDataException,
419 : : WrappedTargetException )
420 : : {
421 : 0 : if ( aName == "Enable" )
422 : 0 : m_aStack.push(TElementStack::value_type(aName,ENABLE_JAVA));
423 : 0 : else if ( aName == "UserClassPath" )
424 : 0 : m_aStack.push(TElementStack::value_type(aName, USER_CLASS_PATH));
425 : 0 : }
426 : : // -----------------------------------------------------------------------------
427 : :
428 : 0 : void SAL_CALL JavaMigration::setPropertyValue(
429 : : const Any& aValue )
430 : : throw(
431 : : MalformedDataException,
432 : : WrappedTargetException )
433 : : {
434 : 0 : if ( !m_aStack.empty())
435 : : {
436 : 0 : switch (m_aStack.top().second)
437 : : {
438 : : case ENABLE_JAVA:
439 : : {
440 : 0 : sal_Bool val = sal_Bool();
441 : 0 : if ((aValue >>= val) == sal_False)
442 : : throw MalformedDataException(
443 : : OUSTR("[Service implementation " IMPL_NAME
444 : 0 : "] XLayerHandler::setPropertyValue received wrong type for Enable property"), 0, Any());
445 : 0 : if (jfw_setEnabled(val) != JFW_E_NONE)
446 : : throw WrappedTargetException(
447 : : OUSTR("[Service implementation " IMPL_NAME
448 : 0 : "] XLayerHandler::setPropertyValue: jfw_setEnabled failed."), 0, Any());
449 : :
450 : : break;
451 : : }
452 : : case USER_CLASS_PATH:
453 : : {
454 : 0 : OUString cp;
455 : 0 : if ((aValue >>= cp) == sal_False)
456 : : throw MalformedDataException(
457 : : OUSTR("[Service implementation " IMPL_NAME
458 : 0 : "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property"), 0, Any());
459 : :
460 : 0 : if (jfw_setUserClassPath(cp.pData) != JFW_E_NONE)
461 : : throw WrappedTargetException(
462 : : OUSTR("[Service implementation " IMPL_NAME
463 : 0 : "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed."), 0, Any());
464 : 0 : break;
465 : : }
466 : : default:
467 : : OSL_ASSERT(0);
468 : : }
469 : : }
470 : 0 : }
471 : : // -----------------------------------------------------------------------------
472 : :
473 : 0 : void SAL_CALL JavaMigration::setPropertyValueForLocale(
474 : : const Any&,
475 : : const ::rtl::OUString& )
476 : : throw(
477 : : MalformedDataException,
478 : : WrappedTargetException )
479 : : {
480 : 0 : }
481 : : // -----------------------------------------------------------------------------
482 : :
483 : 0 : void SAL_CALL JavaMigration::endProperty()
484 : : throw(
485 : : MalformedDataException,
486 : : WrappedTargetException )
487 : : {
488 : 0 : if (!m_aStack.empty())
489 : 0 : m_aStack.pop();
490 : 0 : }
491 : : // -----------------------------------------------------------------------------
492 : :
493 : 0 : void SAL_CALL JavaMigration::addProperty(
494 : : const rtl::OUString&,
495 : : sal_Int16,
496 : : const Type& )
497 : : throw(
498 : : MalformedDataException,
499 : : WrappedTargetException )
500 : : {
501 : 0 : }
502 : : // -----------------------------------------------------------------------------
503 : :
504 : 0 : void SAL_CALL JavaMigration::addPropertyWithValue(
505 : : const rtl::OUString&,
506 : : sal_Int16,
507 : : const Any& )
508 : : throw(
509 : : MalformedDataException,
510 : : WrappedTargetException )
511 : : {
512 : 0 : }
513 : :
514 : 0 : void SAL_CALL JavaMigration::addOrReplaceNodeFromTemplate(
515 : : const rtl::OUString&,
516 : : const TemplateIdentifier&,
517 : : sal_Int16 )
518 : : throw(
519 : : MalformedDataException,
520 : : WrappedTargetException )
521 : : {
522 : 0 : }
523 : :
524 : : // -----------------------------------------------------------------------------
525 : : //ToDo enable java, user class path
526 : :
527 : : } //end namespace jfw
528 : :
529 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|