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()
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("Home", 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())
240 std::unique_ptr
<JavaInfo
> aInfo
;
241 javaFrameworkError err
= jfw_getJavaInfoByPath(sValue
, &aInfo
);
243 if (err
== JFW_E_NONE
)
245 if (jfw_setSelectedJRE(aInfo
.get()) != JFW_E_NONE
)
247 OSL_FAIL("[Service implementation " IMPL_NAME
248 "] XJob::execute: jfw_setSelectedJRE failed.");
249 fprintf(stderr
, "\nCannot migrate Java. An error occurred.\n");
252 else if (err
== JFW_E_FAILED_VERSION
)
254 fprintf(stderr
, "\nCannot migrate Java settings because the version of the Java "
255 "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())
317 switch (m_aStack
.top().second
)
322 if (!(aValue
>>= val
))
323 throw MalformedDataException(
324 "[Service implementation " IMPL_NAME
325 "] XLayerHandler::setPropertyValue received wrong type for Enable property", nullptr, Any());
326 if (jfw_setEnabled(val
) != JFW_E_NONE
)
327 throw WrappedTargetException(
328 "[Service implementation " IMPL_NAME
329 "] XLayerHandler::setPropertyValue: jfw_setEnabled failed.", nullptr, Any());
333 case USER_CLASS_PATH
:
336 if (!(aValue
>>= cp
))
337 throw MalformedDataException(
338 "[Service implementation " IMPL_NAME
339 "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property", nullptr, Any());
341 if (jfw_setUserClassPath(cp
) != JFW_E_NONE
)
342 throw WrappedTargetException(
343 "[Service implementation " IMPL_NAME
344 "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed.", 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: */