1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <cppuhelper/implbase.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 <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.hxx>
44 #include <osl/thread.hxx>
45 #include <osl/diagnose.h>
47 #define SERVICE_NAME "com.sun.star.migration.Java"
48 #define IMPL_NAME "com.sun.star.comp.desktop.migration.Java"
51 #define USER_CLASS_PATH 2
53 using namespace com::sun::star::uno
;
54 using namespace com::sun::star::beans
;
55 using namespace com::sun::star::lang
;
56 using namespace com::sun::star::configuration::backend
;
61 class JavaMigration
: public ::cppu::WeakImplHelper
<
62 css::lang::XServiceInfo
,
63 css::lang::XInitialization
,
65 css::configuration::backend::XLayerHandler
>
69 virtual OUString SAL_CALL
getImplementationName() override
;
70 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
) override
;
71 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
74 virtual void SAL_CALL
initialize( const css::uno::Sequence
< css::uno::Any
>& aArguments
) override
;
77 virtual css::uno::Any SAL_CALL
execute(
78 const css::uno::Sequence
<css::beans::NamedValue
>& Arguments
) override
;
81 virtual void SAL_CALL
startLayer() override
;
83 virtual void SAL_CALL
endLayer() override
;
85 virtual void SAL_CALL
overrideNode(
86 const OUString
& aName
,
87 sal_Int16 aAttributes
,
88 sal_Bool bClear
) override
;
90 virtual void SAL_CALL
addOrReplaceNode(
91 const OUString
& aName
,
92 sal_Int16 aAttributes
) override
;
94 virtual void SAL_CALL
addOrReplaceNodeFromTemplate(
95 const OUString
& aName
,
96 const css::configuration::backend::TemplateIdentifier
& aTemplate
,
97 sal_Int16 aAttributes
) override
;
99 virtual void SAL_CALL
endNode() override
;
101 virtual void SAL_CALL
dropNode(
102 const OUString
& aName
) override
;
104 virtual void SAL_CALL
overrideProperty(
105 const OUString
& aName
,
106 sal_Int16 aAttributes
,
107 const css::uno::Type
& aType
,
108 sal_Bool bClear
) override
;
110 virtual void SAL_CALL
setPropertyValue(
111 const css::uno::Any
& aValue
) override
;
113 virtual void SAL_CALL
setPropertyValueForLocale(
114 const css::uno::Any
& aValue
,
115 const OUString
& aLocale
) override
;
117 virtual void SAL_CALL
endProperty() override
;
119 virtual void SAL_CALL
addProperty(
120 const OUString
& aName
,
121 sal_Int16 aAttributes
,
122 const css::uno::Type
& aType
) override
;
124 virtual void SAL_CALL
addPropertyWithValue(
125 const OUString
& aName
,
126 sal_Int16 aAttributes
,
127 const css::uno::Any
& aValue
) override
;
130 virtual ~JavaMigration() override
;
134 css::uno::Reference
< css::configuration::backend::XLayer
> m_xLayer
;
136 void migrateJavarc();
137 typedef std::pair
< OUString
, sal_Int16
> TElementType
;
138 typedef std::stack
< TElementType
> TElementStack
;
139 TElementStack m_aStack
;
143 JavaMigration::~JavaMigration()
145 OSL_ASSERT(m_aStack
.empty());
148 OUString
jvmfwk_getImplementationName()
150 return OUString(IMPL_NAME
);
153 css::uno::Sequence
< OUString
> jvmfwk_getSupportedServiceNames()
155 OUString str_name
= SERVICE_NAME
;
156 return css::uno::Sequence
< OUString
>( &str_name
, 1 );
160 OUString SAL_CALL
JavaMigration::getImplementationName()
162 return jvmfwk_getImplementationName();
165 sal_Bool
JavaMigration::supportsService(OUString
const & ServiceName
)
167 return cppu::supportsService(this, ServiceName
);
170 css::uno::Sequence
< OUString
> SAL_CALL
JavaMigration::getSupportedServiceNames()
172 return jvmfwk_getSupportedServiceNames();
175 //XInitialization ----------------------------------------------------------------------
176 void SAL_CALL
JavaMigration::initialize( const css::uno::Sequence
< css::uno::Any
>& aArguments
)
178 const css::uno::Any
* pIter
= aArguments
.getConstArray();
179 const css::uno::Any
* pEnd
= pIter
+ aArguments
.getLength();
180 css::uno::Sequence
<css::beans::NamedValue
> aOldConfigValues
;
181 css::beans::NamedValue aValue
;
182 for(;pIter
!= pEnd
;++pIter
)
185 if ( aValue
.Name
== "OldConfiguration" )
187 bool bSuccess
= aValue
.Value
>>= aOldConfigValues
;
188 OSL_ENSURE(bSuccess
, "[Service implementation " IMPL_NAME
189 "] XInitialization::initialize: Argument OldConfiguration has wrong type.");
192 const css::beans::NamedValue
* pIter2
= aOldConfigValues
.getConstArray();
193 const css::beans::NamedValue
* pEnd2
= pIter2
+ aOldConfigValues
.getLength();
194 for(;pIter2
!= pEnd2
;++pIter2
)
196 if ( pIter2
->Name
== "org.openoffice.Office.Java" )
198 pIter2
->Value
>>= m_xLayer
;
204 else if ( aValue
.Name
== "UserData" )
206 if ( !(aValue
.Value
>>= m_sUserDir
) )
209 "[Service implementation " IMPL_NAME
210 "] XInitialization::initialize: Argument UserData has wrong type.");
218 css::uno::Any SAL_CALL
JavaMigration::execute(
219 const css::uno::Sequence
<css::beans::NamedValue
>& )
223 m_xLayer
->readData(this);
225 return css::uno::Any();
228 void JavaMigration::migrateJavarc()
230 if (m_sUserDir
.isEmpty())
234 rtl::Bootstrap
javaini(m_sUserDir
+ "/user/config/" SAL_CONFIGFILE("java"));
235 bool bSuccess
= javaini
.getFrom("Home", sValue
);
236 OSL_ENSURE(bSuccess
, "[Service implementation " IMPL_NAME
237 "] XJob::execute: Could not get Home entry from java.ini/javarc.");
238 if (bSuccess
&& !sValue
.isEmpty())
241 std::unique_ptr
<JavaInfo
> aInfo
;
242 javaFrameworkError err
= jfw_getJavaInfoByPath(sValue
, &aInfo
);
244 if (err
== JFW_E_NONE
)
246 if (jfw_setSelectedJRE(aInfo
.get()) != JFW_E_NONE
)
248 OSL_FAIL("[Service implementation " IMPL_NAME
249 "] XJob::execute: jfw_setSelectedJRE failed.");
250 fprintf(stderr
, "\nCannot migrate Java. An error occurred.\n");
253 else if (err
== JFW_E_FAILED_VERSION
)
255 fprintf(stderr
, "\nCannot migrate Java settings because the version of the Java "
256 "is not supported anymore.\n");
263 void SAL_CALL
JavaMigration::startLayer()
268 void SAL_CALL
JavaMigration::endLayer()
273 void SAL_CALL
JavaMigration::overrideNode(
283 void SAL_CALL
JavaMigration::addOrReplaceNode(
289 void SAL_CALL
JavaMigration::endNode()
294 void SAL_CALL
JavaMigration::dropNode(
300 void SAL_CALL
JavaMigration::overrideProperty(
301 const OUString
& aName
,
306 if ( aName
== "Enable" )
307 m_aStack
.push(TElementStack::value_type(aName
,ENABLE_JAVA
));
308 else if ( aName
== "UserClassPath" )
309 m_aStack
.push(TElementStack::value_type(aName
, USER_CLASS_PATH
));
313 void SAL_CALL
JavaMigration::setPropertyValue(
316 if ( !m_aStack
.empty())
318 switch (m_aStack
.top().second
)
323 if (!(aValue
>>= val
))
324 throw MalformedDataException(
325 "[Service implementation " IMPL_NAME
326 "] XLayerHandler::setPropertyValue received wrong type for Enable property", nullptr, Any());
327 if (jfw_setEnabled(val
) != JFW_E_NONE
)
328 throw WrappedTargetException(
329 "[Service implementation " IMPL_NAME
330 "] XLayerHandler::setPropertyValue: jfw_setEnabled failed.", nullptr, Any());
334 case USER_CLASS_PATH
:
337 if (!(aValue
>>= cp
))
338 throw MalformedDataException(
339 "[Service implementation " IMPL_NAME
340 "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property", nullptr, Any());
342 if (jfw_setUserClassPath(cp
) != JFW_E_NONE
)
343 throw WrappedTargetException(
344 "[Service implementation " IMPL_NAME
345 "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed.", nullptr, Any());
355 void SAL_CALL
JavaMigration::setPropertyValueForLocale(
362 void SAL_CALL
JavaMigration::endProperty()
364 if (!m_aStack
.empty())
369 void SAL_CALL
JavaMigration::addProperty(
377 void SAL_CALL
JavaMigration::addPropertyWithValue(
384 void SAL_CALL
JavaMigration::addOrReplaceNodeFromTemplate(
386 const TemplateIdentifier
&,
392 //ToDo enable java, user class path
394 } //end namespace jfw
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */