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/supportsservice.hxx>
23 #include <rtl/ustring.hxx>
24 #include <rtl/bootstrap.hxx>
25 #include <sal/types.h>
26 #include <sal/config.h>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/lang/XInitialization.hpp>
29 #include <com/sun/star/lang/WrappedTargetException.hpp>
30 #include <com/sun/star/task/XJob.hpp>
31 #include <com/sun/star/configuration/backend/XLayer.hpp>
32 #include <com/sun/star/configuration/backend/XLayerHandler.hpp>
33 #include <com/sun/star/configuration/backend/MalformedDataException.hpp>
34 #include <com/sun/star/configuration/backend/TemplateIdentifier.hpp>
35 #include <jvmfwk/framework.hxx>
41 #include <osl/diagnose.h>
43 constexpr OUString SERVICE_NAME
= u
"com.sun.star.migration.Java"_ustr
;
44 #define IMPL_NAME "com.sun.star.comp.desktop.migration.Java"
47 #define USER_CLASS_PATH 2
49 using namespace com::sun::star::uno
;
50 using namespace com::sun::star::beans
;
51 using namespace com::sun::star::lang
;
52 using namespace com::sun::star::configuration::backend
;
59 class JavaMigration
: public ::cppu::WeakImplHelper
<
60 css::lang::XServiceInfo
,
61 css::lang::XInitialization
,
63 css::configuration::backend::XLayerHandler
>
67 virtual OUString SAL_CALL
getImplementationName() override
;
68 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
) override
;
69 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
72 virtual void SAL_CALL
initialize( const css::uno::Sequence
< css::uno::Any
>& aArguments
) override
;
75 virtual css::uno::Any SAL_CALL
execute(
76 const css::uno::Sequence
<css::beans::NamedValue
>& Arguments
) override
;
79 virtual void SAL_CALL
startLayer() override
;
81 virtual void SAL_CALL
endLayer() override
;
83 virtual void SAL_CALL
overrideNode(
84 const OUString
& aName
,
85 sal_Int16 aAttributes
,
86 sal_Bool bClear
) override
;
88 virtual void SAL_CALL
addOrReplaceNode(
89 const OUString
& aName
,
90 sal_Int16 aAttributes
) override
;
92 virtual void SAL_CALL
addOrReplaceNodeFromTemplate(
93 const OUString
& aName
,
94 const css::configuration::backend::TemplateIdentifier
& aTemplate
,
95 sal_Int16 aAttributes
) override
;
97 virtual void SAL_CALL
endNode() override
;
99 virtual void SAL_CALL
dropNode(
100 const OUString
& aName
) override
;
102 virtual void SAL_CALL
overrideProperty(
103 const OUString
& aName
,
104 sal_Int16 aAttributes
,
105 const css::uno::Type
& aType
,
106 sal_Bool bClear
) override
;
108 virtual void SAL_CALL
setPropertyValue(
109 const css::uno::Any
& aValue
) override
;
111 virtual void SAL_CALL
setPropertyValueForLocale(
112 const css::uno::Any
& aValue
,
113 const OUString
& aLocale
) override
;
115 virtual void SAL_CALL
endProperty() override
;
117 virtual void SAL_CALL
addProperty(
118 const OUString
& aName
,
119 sal_Int16 aAttributes
,
120 const css::uno::Type
& aType
) override
;
122 virtual void SAL_CALL
addPropertyWithValue(
123 const OUString
& aName
,
124 sal_Int16 aAttributes
,
125 const css::uno::Any
& aValue
) override
;
128 virtual ~JavaMigration() override
;
132 css::uno::Reference
< css::configuration::backend::XLayer
> m_xLayer
;
134 void migrateJavarc();
135 typedef std::pair
< OUString
, sal_Int16
> TElementType
;
136 typedef std::stack
< TElementType
> TElementStack
;
137 TElementStack m_aStack
;
143 JavaMigration::~JavaMigration()
145 OSL_ASSERT(m_aStack
.empty());
148 OUString
jvmfwk_getImplementationName()
150 return u
"" IMPL_NAME
""_ustr
;
153 css::uno::Sequence
< OUString
> jvmfwk_getSupportedServiceNames()
155 return { SERVICE_NAME
};
159 OUString SAL_CALL
JavaMigration::getImplementationName()
161 return jvmfwk_getImplementationName();
164 sal_Bool
JavaMigration::supportsService(OUString
const & ServiceName
)
166 return cppu::supportsService(this, ServiceName
);
169 css::uno::Sequence
< OUString
> SAL_CALL
JavaMigration::getSupportedServiceNames()
171 return jvmfwk_getSupportedServiceNames();
174 //XInitialization ----------------------------------------------------------------------
175 void SAL_CALL
JavaMigration::initialize( const css::uno::Sequence
< css::uno::Any
>& aArguments
)
177 const css::uno::Any
* pIter
= aArguments
.getConstArray();
178 const css::uno::Any
* pEnd
= pIter
+ aArguments
.getLength();
179 css::uno::Sequence
<css::beans::NamedValue
> aOldConfigValues
;
180 css::beans::NamedValue aValue
;
181 for(;pIter
!= pEnd
;++pIter
)
184 if ( aValue
.Name
== "OldConfiguration" )
186 bool bSuccess
= aValue
.Value
>>= aOldConfigValues
;
187 OSL_ENSURE(bSuccess
, "[Service implementation " IMPL_NAME
188 "] XInitialization::initialize: Argument OldConfiguration has wrong type.");
191 const css::beans::NamedValue
* pIter2
= aOldConfigValues
.getConstArray();
192 const css::beans::NamedValue
* pEnd2
= pIter2
+ aOldConfigValues
.getLength();
193 for(;pIter2
!= pEnd2
;++pIter2
)
195 if ( pIter2
->Name
== "org.openoffice.Office.Java" )
197 pIter2
->Value
>>= m_xLayer
;
203 else if ( aValue
.Name
== "UserData" )
205 if ( !(aValue
.Value
>>= m_sUserDir
) )
208 "[Service implementation " IMPL_NAME
209 "] XInitialization::initialize: Argument UserData has wrong type.");
217 css::uno::Any SAL_CALL
JavaMigration::execute(
218 const css::uno::Sequence
<css::beans::NamedValue
>& )
222 m_xLayer
->readData(this);
224 return css::uno::Any();
227 void JavaMigration::migrateJavarc()
229 if (m_sUserDir
.isEmpty())
233 rtl::Bootstrap
javaini(m_sUserDir
+ "/user/config/" SAL_CONFIGFILE("java"));
234 bool bSuccess
= javaini
.getFrom(u
"Home"_ustr
, sValue
);
235 OSL_ENSURE(bSuccess
, "[Service implementation " IMPL_NAME
236 "] XJob::execute: Could not get Home entry from java.ini/javarc.");
237 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");
262 void SAL_CALL
JavaMigration::startLayer()
267 void SAL_CALL
JavaMigration::endLayer()
272 void SAL_CALL
JavaMigration::overrideNode(
282 void SAL_CALL
JavaMigration::addOrReplaceNode(
288 void SAL_CALL
JavaMigration::endNode()
293 void SAL_CALL
JavaMigration::dropNode(
299 void SAL_CALL
JavaMigration::overrideProperty(
300 const OUString
& aName
,
305 if ( aName
== "Enable" )
306 m_aStack
.push(TElementStack::value_type(aName
,ENABLE_JAVA
));
307 else if ( aName
== "UserClassPath" )
308 m_aStack
.push(TElementStack::value_type(aName
, USER_CLASS_PATH
));
312 void SAL_CALL
JavaMigration::setPropertyValue(
315 if ( m_aStack
.empty())
318 switch (m_aStack
.top().second
)
323 if (!(aValue
>>= val
))
324 throw MalformedDataException(
325 u
"[Service implementation " IMPL_NAME
326 "] XLayerHandler::setPropertyValue received wrong type for Enable property"_ustr
, nullptr, Any());
327 if (jfw_setEnabled(val
) != JFW_E_NONE
)
328 throw WrappedTargetException(
329 u
"[Service implementation " IMPL_NAME
330 "] XLayerHandler::setPropertyValue: jfw_setEnabled failed."_ustr
, nullptr, Any());
334 case USER_CLASS_PATH
:
337 if (!(aValue
>>= cp
))
338 throw MalformedDataException(
339 u
"[Service implementation " IMPL_NAME
340 "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property"_ustr
, nullptr, Any());
342 if (jfw_setUserClassPath(cp
) != JFW_E_NONE
)
343 throw WrappedTargetException(
344 u
"[Service implementation " IMPL_NAME
345 "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed."_ustr
, nullptr, Any());
354 void SAL_CALL
JavaMigration::setPropertyValueForLocale(
361 void SAL_CALL
JavaMigration::endProperty()
363 if (!m_aStack
.empty())
368 void SAL_CALL
JavaMigration::addProperty(
376 void SAL_CALL
JavaMigration::addPropertyWithValue(
383 void SAL_CALL
JavaMigration::addOrReplaceNodeFromTemplate(
385 const TemplateIdentifier
&,
391 //ToDo enable java, user class path
393 } //end namespace jfw
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */