xref: /trunk/main/desktop/source/migration/services/jvmfwk.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_desktop.hxx"
26 
27 #include "cppuhelper/implbase4.hxx"
28 #include "cppuhelper/implementationentry.hxx"
29 #include "rtl/ustrbuf.hxx"
30 #include "rtl/ustring.h"
31 #include "rtl/ustring.hxx"
32 #include "rtl/bootstrap.hxx"
33 #include "sal/types.h"
34 #include "sal/config.h"
35 #include "boost/scoped_array.hpp"
36 #include "com/sun/star/lang/XServiceInfo.hpp"
37 #include "com/sun/star/lang/XInitialization.hpp"
38 #include "com/sun/star/lang/WrappedTargetException.hpp"
39 #include "com/sun/star/task/XJob.hpp"
40 #include "com/sun/star/configuration/backend/XLayer.hpp"
41 #include "com/sun/star/configuration/backend/XLayerHandler.hpp"
42 #include "com/sun/star/configuration/backend/MalformedDataException.hpp"
43 #include "com/sun/star/configuration/backend/TemplateIdentifier.hpp"
44 #include "jvmfwk/framework.h"
45 #include "jvmfwk.hxx"
46 #include <stack>
47 #include <stdio.h>
48 
49 #include "osl/thread.hxx"
50 #define OUSTR(x) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
51 
52 #define SERVICE_NAME "com.sun.star.migration.Java"
53 #define IMPL_NAME "com.sun.star.comp.desktop.migration.Java"
54 
55 #define ENABLE_JAVA     1
56 #define USER_CLASS_PATH 2
57 
58 namespace css = com::sun::star;
59 using namespace rtl;
60 using namespace com::sun::star::uno;
61 using namespace com::sun::star::beans;
62 using namespace com::sun::star::lang;
63 using namespace com::sun::star::configuration::backend;
64 
65 namespace migration
66 {
67 
68 class CJavaInfo
69 {
70     CJavaInfo(const CJavaInfo&);
71     CJavaInfo& operator = (const CJavaInfo&);
72 public:
73     JavaInfo* pData;
74     CJavaInfo();
75     ~CJavaInfo();
76     operator JavaInfo* ();
77 };
78 
CJavaInfo()79 CJavaInfo::CJavaInfo(): pData(NULL)
80 {
81 }
82 
~CJavaInfo()83 CJavaInfo::~CJavaInfo()
84 {
85     jfw_freeJavaInfo(pData);
86 }
87 
operator JavaInfo*()88 CJavaInfo::operator JavaInfo*()
89 {
90     return pData;
91 }
92 
93 
94 class JavaMigration : public ::cppu::WeakImplHelper4<
95     css::lang::XServiceInfo,
96     css::lang::XInitialization,
97     css::task::XJob,
98     css::configuration::backend::XLayerHandler>
99 {
100 public:
101     // XServiceInfo
102     virtual OUString SAL_CALL getImplementationName();
103     virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName );
104     virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames();
105 
106     //XInitialization
107     virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments );
108 
109     //XJob
110     virtual css::uno::Any SAL_CALL execute(
111         const css::uno::Sequence<css::beans::NamedValue >& Arguments );
112 
113         // XLayerHandler
114     virtual void SAL_CALL startLayer();
115 
116     virtual void SAL_CALL endLayer();
117 
118     virtual void SAL_CALL overrideNode(
119             const rtl::OUString& aName,
120             sal_Int16 aAttributes,
121             sal_Bool bClear);
122 
123     virtual void SAL_CALL addOrReplaceNode(
124             const rtl::OUString& aName,
125             sal_Int16 aAttributes);
126 
127     virtual void SAL_CALL  addOrReplaceNodeFromTemplate(
128             const rtl::OUString& aName,
129             const ::com::sun::star::configuration::backend::TemplateIdentifier& aTemplate,
130             sal_Int16 aAttributes );
131 
132     virtual void SAL_CALL  endNode();
133 
134     virtual void SAL_CALL  dropNode(
135             const rtl::OUString& aName );
136 
137     virtual void SAL_CALL  overrideProperty(
138             const rtl::OUString& aName,
139             sal_Int16 aAttributes,
140             const css::uno::Type& aType,
141             sal_Bool bClear );
142 
143     virtual void SAL_CALL  setPropertyValue(
144             const css::uno::Any& aValue );
145 
146     virtual void SAL_CALL setPropertyValueForLocale(
147             const css::uno::Any& aValue,
148             const rtl::OUString& aLocale );
149 
150     virtual void SAL_CALL  endProperty();
151 
152     virtual void SAL_CALL  addProperty(
153             const rtl::OUString& aName,
154             sal_Int16 aAttributes,
155             const css::uno::Type& aType );
156 
157     virtual void SAL_CALL  addPropertyWithValue(
158             const rtl::OUString& aName,
159             sal_Int16 aAttributes,
160             const css::uno::Any& aValue );
161 
162 
163 
164     //----------------
165     ~JavaMigration();
166 
167 private:
168     OUString m_sUserDir;
169     css::uno::Reference< ::css::configuration::backend::XLayer> m_xLayer;
170 
171     void migrateJavarc();
172     typedef ::std::pair< ::rtl::OUString,   sal_Int16>  TElementType;
173     typedef ::std::stack< TElementType > TElementStack;
174     TElementStack m_aStack;
175 
176 };
177 
~JavaMigration()178 JavaMigration::~JavaMigration()
179 {
180     OSL_ASSERT(m_aStack.empty());
181 }
182 
jvmfwk_getImplementationName()183 OUString jvmfwk_getImplementationName()
184 {
185     return OUSTR(IMPL_NAME);
186 }
187 
jvmfwk_getSupportedServiceNames()188 css::uno::Sequence< OUString > jvmfwk_getSupportedServiceNames()
189 {
190     OUString str_name = OUSTR(SERVICE_NAME);
191     return css::uno::Sequence< OUString >( &str_name, 1 );
192 }
193 
194 // XServiceInfo
getImplementationName()195 OUString SAL_CALL JavaMigration::getImplementationName()
196 {
197     return jvmfwk_getImplementationName();
198 }
199 
supportsService(const OUString & rServiceName)200 sal_Bool SAL_CALL JavaMigration::supportsService( const OUString & rServiceName )
201 {
202     css::uno::Sequence< OUString > const & rSNL = getSupportedServiceNames();
203     OUString const * pArray = rSNL.getConstArray();
204     for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
205     {
206         if (rServiceName.equals( pArray[ nPos ] ))
207             return true;
208     }
209     return false;
210 
211 }
212 
getSupportedServiceNames()213 css::uno::Sequence< OUString > SAL_CALL JavaMigration::getSupportedServiceNames()
214 {
215     return jvmfwk_getSupportedServiceNames();
216 }
217 
218 //XInitialization ----------------------------------------------------------------------
initialize(const css::uno::Sequence<css::uno::Any> & aArguments)219 void SAL_CALL JavaMigration::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
220 {
221     const css::uno::Any* pIter = aArguments.getConstArray();
222     const css::uno::Any* pEnd = pIter + aArguments.getLength();
223     css::uno::Sequence<css::beans::NamedValue> aOldConfigValues;
224     css::beans::NamedValue aValue;
225     for(;pIter != pEnd;++pIter)
226     {
227         *pIter >>= aValue;
228         if (aValue.Name.equalsAscii("OldConfiguration"))
229         {
230             sal_Bool bSuccess = aValue.Value >>= aOldConfigValues;
231             OSL_ENSURE(bSuccess == sal_True, "[Service implementation " IMPL_NAME
232                        "] XInitialization::initialize: Argument OldConfiguration has wrong type.");
233             if (bSuccess)
234             {
235                 const css::beans::NamedValue* pIter2 = aOldConfigValues.getConstArray();
236                 const css::beans::NamedValue* pEnd2 = pIter2 + aOldConfigValues.getLength();
237                 for(;pIter2 != pEnd2;++pIter2)
238                 {
239                     if ( pIter2->Name.equalsAscii("org.openoffice.Office.Java") )
240                     {
241                         pIter2->Value >>= m_xLayer;
242                         break;
243                     }
244                 }
245             }
246         }
247         else if (aValue.Name.equalsAscii("UserData"))
248         {
249             if ( !(aValue.Value >>= m_sUserDir) )
250             {
251                 OSL_ENSURE(
252                     false,
253                     "[Service implementation " IMPL_NAME
254                     "] XInitialization::initialize: Argument UserData has wrong type.");
255             }
256         }
257     }
258 
259 }
260 
261 //XJob
execute(const css::uno::Sequence<css::beans::NamedValue> &)262 css::uno::Any SAL_CALL JavaMigration::execute(
263         const css::uno::Sequence<css::beans::NamedValue >& )
264 {
265     migrateJavarc();
266     if (m_xLayer.is())
267         m_xLayer->readData(this);
268 
269     return css::uno::Any();
270 }
271 
migrateJavarc()272 void JavaMigration::migrateJavarc()
273 {
274     if (m_sUserDir.getLength() == 0)
275         return;
276 
277     OUString sValue;
278     rtl::Bootstrap javaini(m_sUserDir + OUSTR( "/user/config/" SAL_CONFIGFILE("java")));
279     sal_Bool bSuccess = javaini.getFrom(OUSTR("Home"), sValue);
280     OSL_ENSURE(bSuccess, "[Service implementation " IMPL_NAME
281                        "] XJob::execute: Could not get Home entry from java.ini/javarc.");
282     if (bSuccess == sal_True && sValue.getLength() > 0)
283     {
284         //get the directory
285         CJavaInfo aInfo;
286         javaFrameworkError err = jfw_getJavaInfoByPath(sValue.pData, &aInfo.pData);
287 
288         if (err == JFW_E_NONE)
289         {
290             if (jfw_setSelectedJRE(aInfo) != JFW_E_NONE)
291             {
292                 OSL_ENSURE(0, "[Service implementation " IMPL_NAME
293                            "] XJob::execute: jfw_setSelectedJRE failed.");
294                 fprintf(stderr, "\nCannot migrate Java. An error occurred.\n");
295             }
296         }
297         else if (err == JFW_E_FAILED_VERSION)
298         {
299             fprintf(stderr, "\nCannot migrate Java settings because the version of the Java  "
300                     "is not supported anymore.\n");
301         }
302     }
303 }
304 
305 
306 // XLayerHandler
startLayer()307 void SAL_CALL JavaMigration::startLayer()
308 {
309 }
310 // -----------------------------------------------------------------------------
311 
endLayer()312 void SAL_CALL JavaMigration::endLayer()
313 {
314 }
315 // -----------------------------------------------------------------------------
316 
overrideNode(const::rtl::OUString &,sal_Int16,sal_Bool)317 void SAL_CALL JavaMigration::overrideNode(
318         const ::rtl::OUString&,
319         sal_Int16,
320         sal_Bool)
321 
322 {
323 
324 }
325 // -----------------------------------------------------------------------------
326 
addOrReplaceNode(const::rtl::OUString &,sal_Int16)327 void SAL_CALL JavaMigration::addOrReplaceNode(
328         const ::rtl::OUString&,
329         sal_Int16)
330 {
331 
332 }
endNode()333 void SAL_CALL  JavaMigration::endNode()
334 {
335 }
336 // -----------------------------------------------------------------------------
337 
dropNode(const::rtl::OUString &)338 void SAL_CALL  JavaMigration::dropNode(
339         const ::rtl::OUString& )
340 {
341 }
342 // -----------------------------------------------------------------------------
343 
overrideProperty(const::rtl::OUString & aName,sal_Int16,const Type &,sal_Bool)344 void SAL_CALL  JavaMigration::overrideProperty(
345         const ::rtl::OUString& aName,
346         sal_Int16,
347         const Type&,
348         sal_Bool )
349 {
350     if (aName.equalsAscii("Enable"))
351         m_aStack.push(TElementStack::value_type(aName,ENABLE_JAVA));
352     else if (aName.equalsAscii("UserClassPath"))
353         m_aStack.push(TElementStack::value_type(aName, USER_CLASS_PATH));
354 }
355 // -----------------------------------------------------------------------------
356 
setPropertyValue(const Any & aValue)357 void SAL_CALL  JavaMigration::setPropertyValue(
358         const Any& aValue )
359 {
360     if ( !m_aStack.empty())
361     {
362         switch (m_aStack.top().second)
363         {
364         case ENABLE_JAVA:
365         {
366             sal_Bool val = sal_Bool();
367             if ((aValue >>= val) == sal_False)
368                 throw MalformedDataException(
369                     OUSTR("[Service implementation " IMPL_NAME
370                        "] XLayerHandler::setPropertyValue received wrong type for Enable property"), 0, Any());
371             if (jfw_setEnabled(val) != JFW_E_NONE)
372                 throw WrappedTargetException(
373                     OUSTR("[Service implementation " IMPL_NAME
374                        "] XLayerHandler::setPropertyValue: jfw_setEnabled failed."), 0, Any());
375 
376             break;
377         }
378         case USER_CLASS_PATH:
379          {
380              OUString cp;
381              if ((aValue >>= cp) == sal_False)
382                  throw MalformedDataException(
383                      OUSTR("[Service implementation " IMPL_NAME
384                            "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property"), 0, Any());
385 
386              if (jfw_setUserClassPath(cp.pData) != JFW_E_NONE)
387                  throw WrappedTargetException(
388                      OUSTR("[Service implementation " IMPL_NAME
389                        "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed."), 0, Any());
390              break;
391          }
392         default:
393             OSL_ASSERT(0);
394         }
395     }
396 }
397 // -----------------------------------------------------------------------------
398 
setPropertyValueForLocale(const Any &,const::rtl::OUString &)399 void SAL_CALL JavaMigration::setPropertyValueForLocale(
400         const Any&,
401         const ::rtl::OUString& )
402 {
403 }
404 // -----------------------------------------------------------------------------
405 
endProperty()406 void SAL_CALL  JavaMigration::endProperty()
407 {
408             if (!m_aStack.empty())
409                 m_aStack.pop();
410 }
411 // -----------------------------------------------------------------------------
412 
addProperty(const rtl::OUString &,sal_Int16,const Type &)413 void SAL_CALL  JavaMigration::addProperty(
414         const rtl::OUString&,
415         sal_Int16,
416         const Type& )
417 {
418 }
419 // -----------------------------------------------------------------------------
420 
addPropertyWithValue(const rtl::OUString &,sal_Int16,const Any &)421 void SAL_CALL  JavaMigration::addPropertyWithValue(
422         const rtl::OUString&,
423         sal_Int16,
424         const Any& )
425 {
426 }
427 
addOrReplaceNodeFromTemplate(const rtl::OUString &,const TemplateIdentifier &,sal_Int16)428 void SAL_CALL JavaMigration::addOrReplaceNodeFromTemplate(
429         const rtl::OUString&,
430         const TemplateIdentifier&,
431         sal_Int16 )
432 {
433 }
434 
435 // -----------------------------------------------------------------------------
436 //ToDo enable java, user class path
437 
438 } //end namespace jfw
439