1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jvmfwk.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "cppuhelper/implbase4.hxx"
35 #include "cppuhelper/implementationentry.hxx"
36 #include "rtl/ustrbuf.hxx"
37 #include "rtl/ustring.h"
38 #include "rtl/ustring.hxx"
39 #include "rtl/bootstrap.hxx"
40 #include "sal/types.h"
41 #include "sal/config.h"
42 #include "boost/scoped_array.hpp"
43 #include "com/sun/star/lang/XServiceInfo.hpp"
44 #include "com/sun/star/lang/XInitialization.hpp"
45 #include "com/sun/star/lang/WrappedTargetException.hpp"
46 #include "com/sun/star/task/XJob.hpp"
47 #include "com/sun/star/configuration/backend/XLayer.hpp"
48 #include "com/sun/star/configuration/backend/XLayerHandler.hpp"
49 #include "com/sun/star/configuration/backend/MalformedDataException.hpp"
50 #include "com/sun/star/configuration/backend/TemplateIdentifier.hpp"
51 #include "jvmfwk/framework.h"
56 #include "osl/thread.hxx"
57 #define OUSTR(x) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
59 #define SERVICE_NAME "com.sun.star.migration.Java"
60 #define IMPL_NAME "com.sun.star.comp.desktop.migration.Java"
63 #define USER_CLASS_PATH 2
65 namespace css
= com::sun::star
;
67 using namespace com::sun::star::uno
;
68 using namespace com::sun::star::beans
;
69 using namespace com::sun::star::lang
;
70 using namespace com::sun::star::configuration::backend
;
77 CJavaInfo(const CJavaInfo
&);
78 CJavaInfo
& operator = (const CJavaInfo
&);
83 operator JavaInfo
* ();
86 CJavaInfo::CJavaInfo(): pData(NULL
)
90 CJavaInfo::~CJavaInfo()
92 jfw_freeJavaInfo(pData
);
95 CJavaInfo::operator JavaInfo
*()
101 class JavaMigration
: public ::cppu::WeakImplHelper4
<
102 css::lang::XServiceInfo
,
103 css::lang::XInitialization
,
105 css::configuration::backend::XLayerHandler
>
109 virtual OUString SAL_CALL
getImplementationName()
110 throw (css::uno::RuntimeException
);
111 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
)
112 throw (css::uno::RuntimeException
);
113 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
114 throw (css::uno::RuntimeException
);
117 virtual void SAL_CALL
initialize( const css::uno::Sequence
< css::uno::Any
>& aArguments
)
118 throw(css::uno::Exception
, css::uno::RuntimeException
);
121 virtual css::uno::Any SAL_CALL
execute(
122 const css::uno::Sequence
<css::beans::NamedValue
>& Arguments
)
123 throw (css::lang::IllegalArgumentException
, css::uno::Exception
,
124 css::uno::RuntimeException
);
127 virtual void SAL_CALL
startLayer()
128 throw(::com::sun::star::lang::WrappedTargetException
);
130 virtual void SAL_CALL
endLayer()
132 ::com::sun::star::configuration::backend::MalformedDataException
,
133 ::com::sun::star::lang::WrappedTargetException
);
135 virtual void SAL_CALL
overrideNode(
136 const rtl::OUString
& aName
,
137 sal_Int16 aAttributes
,
140 ::com::sun::star::configuration::backend::MalformedDataException
,
141 ::com::sun::star::lang::WrappedTargetException
);
143 virtual void SAL_CALL
addOrReplaceNode(
144 const rtl::OUString
& aName
,
145 sal_Int16 aAttributes
)
147 ::com::sun::star::configuration::backend::MalformedDataException
,
148 ::com::sun::star::lang::WrappedTargetException
);
150 virtual void SAL_CALL
addOrReplaceNodeFromTemplate(
151 const rtl::OUString
& aName
,
152 const ::com::sun::star::configuration::backend::TemplateIdentifier
& aTemplate
,
153 sal_Int16 aAttributes
)
155 ::com::sun::star::configuration::backend::MalformedDataException
,
156 ::com::sun::star::lang::WrappedTargetException
);
158 virtual void SAL_CALL
endNode()
160 ::com::sun::star::configuration::backend::MalformedDataException
,
161 ::com::sun::star::lang::WrappedTargetException
);
163 virtual void SAL_CALL
dropNode(
164 const rtl::OUString
& aName
)
166 ::com::sun::star::configuration::backend::MalformedDataException
,
167 ::com::sun::star::lang::WrappedTargetException
);
169 virtual void SAL_CALL
overrideProperty(
170 const rtl::OUString
& aName
,
171 sal_Int16 aAttributes
,
172 const css::uno::Type
& aType
,
175 ::com::sun::star::configuration::backend::MalformedDataException
,
176 ::com::sun::star::lang::WrappedTargetException
);
178 virtual void SAL_CALL
setPropertyValue(
179 const css::uno::Any
& aValue
)
181 ::com::sun::star::configuration::backend::MalformedDataException
,
182 ::com::sun::star::lang::WrappedTargetException
);
184 virtual void SAL_CALL
setPropertyValueForLocale(
185 const css::uno::Any
& aValue
,
186 const rtl::OUString
& aLocale
)
188 ::com::sun::star::configuration::backend::MalformedDataException
,
189 ::com::sun::star::lang::WrappedTargetException
);
191 virtual void SAL_CALL
endProperty()
193 ::com::sun::star::configuration::backend::MalformedDataException
,
194 ::com::sun::star::lang::WrappedTargetException
);
196 virtual void SAL_CALL
addProperty(
197 const rtl::OUString
& aName
,
198 sal_Int16 aAttributes
,
199 const css::uno::Type
& aType
)
201 ::com::sun::star::configuration::backend::MalformedDataException
,
202 ::com::sun::star::lang::WrappedTargetException
);
204 virtual void SAL_CALL
addPropertyWithValue(
205 const rtl::OUString
& aName
,
206 sal_Int16 aAttributes
,
207 const css::uno::Any
& aValue
)
209 ::com::sun::star::configuration::backend::MalformedDataException
,
210 ::com::sun::star::lang::WrappedTargetException
);
219 css::uno::Reference
< ::css::configuration::backend::XLayer
> m_xLayer
;
221 void migrateJavarc();
222 typedef ::std::pair
< ::rtl::OUString
, sal_Int16
> TElementType
;
223 typedef ::std::stack
< TElementType
> TElementStack
;
224 TElementStack m_aStack
;
228 JavaMigration::~JavaMigration()
230 OSL_ASSERT(m_aStack
.empty());
233 OUString
jvmfwk_getImplementationName()
235 return OUSTR(IMPL_NAME
);
238 css::uno::Sequence
< OUString
> jvmfwk_getSupportedServiceNames()
240 OUString str_name
= OUSTR(SERVICE_NAME
);
241 return css::uno::Sequence
< OUString
>( &str_name
, 1 );
244 css::uno::Reference
< css::uno::XInterface
> SAL_CALL
jvmfwk_create(
245 css::uno::Reference
< css::uno::XComponentContext
> const & )
246 throw (css::uno::Exception
)
248 return static_cast< ::cppu::OWeakObject
* >(new JavaMigration
);
254 OUString SAL_CALL
JavaMigration::getImplementationName()
255 throw (css::uno::RuntimeException
)
257 return jvmfwk_getImplementationName();
260 sal_Bool SAL_CALL
JavaMigration::supportsService( const OUString
& rServiceName
)
261 throw (css::uno::RuntimeException
)
263 css::uno::Sequence
< OUString
> const & rSNL
= getSupportedServiceNames();
264 OUString
const * pArray
= rSNL
.getConstArray();
265 for ( sal_Int32 nPos
= rSNL
.getLength(); nPos
--; )
267 if (rServiceName
.equals( pArray
[ nPos
] ))
274 css::uno::Sequence
< OUString
> SAL_CALL
JavaMigration::getSupportedServiceNames()
275 throw (css::uno::RuntimeException
)
277 return jvmfwk_getSupportedServiceNames();
280 //XInitialization ----------------------------------------------------------------------
281 void SAL_CALL
JavaMigration::initialize( const css::uno::Sequence
< css::uno::Any
>& aArguments
)
282 throw(css::uno::Exception
, css::uno::RuntimeException
)
284 const css::uno::Any
* pIter
= aArguments
.getConstArray();
285 const css::uno::Any
* pEnd
= pIter
+ aArguments
.getLength();
286 css::uno::Sequence
<css::beans::NamedValue
> aOldConfigValues
;
287 css::beans::NamedValue aValue
;
288 for(;pIter
!= pEnd
;++pIter
)
291 if (aValue
.Name
.equalsAscii("OldConfiguration"))
293 sal_Bool bSuccess
= aValue
.Value
>>= aOldConfigValues
;
294 OSL_ENSURE(bSuccess
== sal_True
, "[Service implementation " IMPL_NAME
295 "] XInitialization::initialize: Argument OldConfiguration has wrong type.");
298 const css::beans::NamedValue
* pIter2
= aOldConfigValues
.getConstArray();
299 const css::beans::NamedValue
* pEnd2
= pIter2
+ aOldConfigValues
.getLength();
300 for(;pIter2
!= pEnd2
;++pIter2
)
302 if ( pIter2
->Name
.equalsAscii("org.openoffice.Office.Java") )
304 pIter2
->Value
>>= m_xLayer
;
310 else if (aValue
.Name
.equalsAscii("UserData"))
312 if ( !(aValue
.Value
>>= m_sUserDir
) )
316 "[Service implementation " IMPL_NAME
317 "] XInitialization::initialize: Argument UserData has wrong type.");
325 css::uno::Any SAL_CALL
JavaMigration::execute(
326 const css::uno::Sequence
<css::beans::NamedValue
>& )
327 throw (css::lang::IllegalArgumentException
, css::uno::Exception
,
328 css::uno::RuntimeException
)
332 m_xLayer
->readData(this);
334 return css::uno::Any();
337 void JavaMigration::migrateJavarc()
339 if (m_sUserDir
.getLength() == 0)
343 rtl::Bootstrap
javaini(m_sUserDir
+ OUSTR("/user/config/"SAL_CONFIGFILE("java")));
344 sal_Bool bSuccess
= javaini
.getFrom(OUSTR("Home"), sValue
);
345 OSL_ENSURE(bSuccess
, "[Service implementation " IMPL_NAME
346 "] XJob::execute: Could not get Home entry from java.ini/javarc.");
347 if (bSuccess
== sal_True
&& sValue
.getLength() > 0)
351 javaFrameworkError err
= jfw_getJavaInfoByPath(sValue
.pData
, &aInfo
.pData
);
353 if (err
== JFW_E_NONE
)
355 if (jfw_setSelectedJRE(aInfo
) != JFW_E_NONE
)
357 OSL_ENSURE(0, "[Service implementation " IMPL_NAME
358 "] XJob::execute: jfw_setSelectedJRE failed.");
359 fprintf(stderr
, "\nCannot migrate Java. An error occured.\n");
362 else if (err
== JFW_E_FAILED_VERSION
)
364 fprintf(stderr
, "\nCannot migrate Java settings because the version of the Java "
365 "is not supported anymore.\n");
372 void SAL_CALL
JavaMigration::startLayer()
373 throw(css::lang::WrappedTargetException
)
376 // -----------------------------------------------------------------------------
378 void SAL_CALL
JavaMigration::endLayer()
380 MalformedDataException
,
381 WrappedTargetException
)
384 // -----------------------------------------------------------------------------
386 void SAL_CALL
JavaMigration::overrideNode(
387 const ::rtl::OUString
&,
391 MalformedDataException
,
392 WrappedTargetException
)
397 // -----------------------------------------------------------------------------
399 void SAL_CALL
JavaMigration::addOrReplaceNode(
400 const ::rtl::OUString
&,
403 MalformedDataException
,
404 WrappedTargetException
)
408 void SAL_CALL
JavaMigration::endNode()
410 MalformedDataException
,
411 WrappedTargetException
)
414 // -----------------------------------------------------------------------------
416 void SAL_CALL
JavaMigration::dropNode(
417 const ::rtl::OUString
& )
419 MalformedDataException
,
420 WrappedTargetException
)
423 // -----------------------------------------------------------------------------
425 void SAL_CALL
JavaMigration::overrideProperty(
426 const ::rtl::OUString
& aName
,
431 MalformedDataException
,
432 WrappedTargetException
)
434 if (aName
.equalsAscii("Enable"))
435 m_aStack
.push(TElementStack::value_type(aName
,ENABLE_JAVA
));
436 else if (aName
.equalsAscii("UserClassPath"))
437 m_aStack
.push(TElementStack::value_type(aName
, USER_CLASS_PATH
));
439 // -----------------------------------------------------------------------------
441 void SAL_CALL
JavaMigration::setPropertyValue(
444 MalformedDataException
,
445 WrappedTargetException
)
447 if ( !m_aStack
.empty())
449 switch (m_aStack
.top().second
)
453 sal_Bool val
= sal_Bool();
454 if ((aValue
>>= val
) == sal_False
)
455 throw MalformedDataException(
456 OUSTR("[Service implementation " IMPL_NAME
457 "] XLayerHandler::setPropertyValue received wrong type for Enable property"), 0, Any());
458 if (jfw_setEnabled(val
) != JFW_E_NONE
)
459 throw WrappedTargetException(
460 OUSTR("[Service implementation " IMPL_NAME
461 "] XLayerHandler::setPropertyValue: jfw_setEnabled failed."), 0, Any());
465 case USER_CLASS_PATH
:
468 if ((aValue
>>= cp
) == sal_False
)
469 throw MalformedDataException(
470 OUSTR("[Service implementation " IMPL_NAME
471 "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property"), 0, Any());
473 if (jfw_setUserClassPath(cp
.pData
) != JFW_E_NONE
)
474 throw WrappedTargetException(
475 OUSTR("[Service implementation " IMPL_NAME
476 "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed."), 0, Any());
484 // -----------------------------------------------------------------------------
486 void SAL_CALL
JavaMigration::setPropertyValueForLocale(
488 const ::rtl::OUString
& )
490 MalformedDataException
,
491 WrappedTargetException
)
494 // -----------------------------------------------------------------------------
496 void SAL_CALL
JavaMigration::endProperty()
498 MalformedDataException
,
499 WrappedTargetException
)
501 if (!m_aStack
.empty())
504 // -----------------------------------------------------------------------------
506 void SAL_CALL
JavaMigration::addProperty(
507 const rtl::OUString
&,
511 MalformedDataException
,
512 WrappedTargetException
)
515 // -----------------------------------------------------------------------------
517 void SAL_CALL
JavaMigration::addPropertyWithValue(
518 const rtl::OUString
&,
522 MalformedDataException
,
523 WrappedTargetException
)
527 void SAL_CALL
JavaMigration::addOrReplaceNodeFromTemplate(
528 const rtl::OUString
&,
529 const TemplateIdentifier
&,
532 MalformedDataException
,
533 WrappedTargetException
)
537 // -----------------------------------------------------------------------------
538 //ToDo enable java, user class path
540 } //end namespace jfw