LibreOffice Module desktop (master)  1
jvmfwk.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <cppuhelper/implbase.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>
39 #include "jvmfwk.hxx"
40 #include <memory>
41 #include <stack>
42 #include <stdio.h>
43 
44 #include <osl/thread.hxx>
45 #include <osl/diagnose.h>
46 
47 #define SERVICE_NAME "com.sun.star.migration.Java"
48 #define IMPL_NAME "com.sun.star.comp.desktop.migration.Java"
49 
50 #define ENABLE_JAVA 1
51 #define USER_CLASS_PATH 2
52 
53 using namespace com::sun::star::uno;
54 using namespace com::sun::star::beans;
55 using namespace com::sun::star::lang;
57 
58 namespace migration
59 {
60 
61 class JavaMigration : public ::cppu::WeakImplHelper<
62  css::lang::XServiceInfo,
63  css::lang::XInitialization,
64  css::task::XJob,
65  css::configuration::backend::XLayerHandler>
66 {
67 public:
68  // XServiceInfo
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;
72 
73  //XInitialization
74  virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
75 
76  //XJob
77  virtual css::uno::Any SAL_CALL execute(
78  const css::uno::Sequence<css::beans::NamedValue >& Arguments ) override;
79 
80  // XLayerHandler
81  virtual void SAL_CALL startLayer() override;
82 
83  virtual void SAL_CALL endLayer() override;
84 
85  virtual void SAL_CALL overrideNode(
86  const OUString& aName,
87  sal_Int16 aAttributes,
88  sal_Bool bClear) override;
89 
90  virtual void SAL_CALL addOrReplaceNode(
91  const OUString& aName,
92  sal_Int16 aAttributes) override;
93 
94  virtual void SAL_CALL addOrReplaceNodeFromTemplate(
95  const OUString& aName,
96  const css::configuration::backend::TemplateIdentifier& aTemplate,
97  sal_Int16 aAttributes ) override;
98 
99  virtual void SAL_CALL endNode() override;
100 
101  virtual void SAL_CALL dropNode(
102  const OUString& aName ) override;
103 
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;
109 
110  virtual void SAL_CALL setPropertyValue(
111  const css::uno::Any& aValue ) override;
112 
113  virtual void SAL_CALL setPropertyValueForLocale(
114  const css::uno::Any& aValue,
115  const OUString& aLocale ) override;
116 
117  virtual void SAL_CALL endProperty() override;
118 
119  virtual void SAL_CALL addProperty(
120  const OUString& aName,
121  sal_Int16 aAttributes,
122  const css::uno::Type& aType ) override;
123 
124  virtual void SAL_CALL addPropertyWithValue(
125  const OUString& aName,
126  sal_Int16 aAttributes,
127  const css::uno::Any& aValue ) override;
128 
129 
130  virtual ~JavaMigration() override;
131 
132 private:
133  OUString m_sUserDir;
134  css::uno::Reference< css::configuration::backend::XLayer> m_xLayer;
135 
136  void migrateJavarc();
137  typedef std::pair< OUString, sal_Int16> TElementType;
138  typedef std::stack< TElementType > TElementStack;
139  TElementStack m_aStack;
140 
141 };
142 
143 JavaMigration::~JavaMigration()
144 {
145  OSL_ASSERT(m_aStack.empty());
146 }
147 
149 {
150  return IMPL_NAME;
151 }
152 
153 css::uno::Sequence< OUString > jvmfwk_getSupportedServiceNames()
154 {
155  return { SERVICE_NAME };
156 }
157 
158 // XServiceInfo
160 {
162 }
163 
164 sal_Bool JavaMigration::supportsService(OUString const & ServiceName)
165 {
166  return cppu::supportsService(this, ServiceName);
167 }
168 
169 css::uno::Sequence< OUString > SAL_CALL JavaMigration::getSupportedServiceNames()
170 {
172 }
173 
174 //XInitialization ----------------------------------------------------------------------
175 void SAL_CALL JavaMigration::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
176 {
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)
182  {
183  *pIter >>= aValue;
184  if ( aValue.Name == "OldConfiguration" )
185  {
186  bool bSuccess = aValue.Value >>= aOldConfigValues;
187  OSL_ENSURE(bSuccess, "[Service implementation " IMPL_NAME
188  "] XInitialization::initialize: Argument OldConfiguration has wrong type.");
189  if (bSuccess)
190  {
191  const css::beans::NamedValue* pIter2 = aOldConfigValues.getConstArray();
192  const css::beans::NamedValue* pEnd2 = pIter2 + aOldConfigValues.getLength();
193  for(;pIter2 != pEnd2;++pIter2)
194  {
195  if ( pIter2->Name == "org.openoffice.Office.Java" )
196  {
197  pIter2->Value >>= m_xLayer;
198  break;
199  }
200  }
201  }
202  }
203  else if ( aValue.Name == "UserData" )
204  {
205  if ( !(aValue.Value >>= m_sUserDir) )
206  {
207  OSL_FAIL(
208  "[Service implementation " IMPL_NAME
209  "] XInitialization::initialize: Argument UserData has wrong type.");
210  }
211  }
212  }
213 
214 }
215 
216 //XJob
217 css::uno::Any SAL_CALL JavaMigration::execute(
218  const css::uno::Sequence<css::beans::NamedValue >& )
219 {
220  migrateJavarc();
221  if (m_xLayer.is())
222  m_xLayer->readData(this);
223 
224  return css::uno::Any();
225 }
226 
227 void JavaMigration::migrateJavarc()
228 {
229  if (m_sUserDir.isEmpty())
230  return;
231 
232  OUString sValue;
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())
238  {
239  //get the directory
240  std::unique_ptr<JavaInfo> aInfo;
241  javaFrameworkError err = jfw_getJavaInfoByPath(sValue, &aInfo);
242 
243  if (err == JFW_E_NONE)
244  {
245  if (jfw_setSelectedJRE(aInfo.get()) != JFW_E_NONE)
246  {
247  OSL_FAIL("[Service implementation " IMPL_NAME
248  "] XJob::execute: jfw_setSelectedJRE failed.");
249  fprintf(stderr, "\nCannot migrate Java. An error occurred.\n");
250  }
251  }
252  else if (err == JFW_E_FAILED_VERSION)
253  {
254  fprintf(stderr, "\nCannot migrate Java settings because the version of the Java "
255  "is not supported anymore.\n");
256  }
257  }
258 }
259 
260 
261 // XLayerHandler
262 void SAL_CALL JavaMigration::startLayer()
263 {
264 }
265 
266 
267 void SAL_CALL JavaMigration::endLayer()
268 {
269 }
270 
271 
272 void SAL_CALL JavaMigration::overrideNode(
273  const OUString&,
274  sal_Int16,
275  sal_Bool)
276 
277 {
278 
279 }
280 
281 
282 void SAL_CALL JavaMigration::addOrReplaceNode(
283  const OUString&,
284  sal_Int16)
285 {
286 
287 }
288 void SAL_CALL JavaMigration::endNode()
289 {
290 }
291 
292 
293 void SAL_CALL JavaMigration::dropNode(
294  const OUString& )
295 {
296 }
297 
298 
299 void SAL_CALL JavaMigration::overrideProperty(
300  const OUString& aName,
301  sal_Int16,
302  const Type&,
303  sal_Bool )
304 {
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));
309 }
310 
311 
312 void SAL_CALL JavaMigration::setPropertyValue(
313  const Any& aValue )
314 {
315  if ( !m_aStack.empty())
316  {
317  switch (m_aStack.top().second)
318  {
319  case ENABLE_JAVA:
320  {
321  bool val;
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());
330 
331  break;
332  }
333  case USER_CLASS_PATH:
334  {
335  OUString cp;
336  if (!(aValue >>= cp))
337  throw MalformedDataException(
338  "[Service implementation " IMPL_NAME
339  "] XLayerHandler::setPropertyValue received wrong type for UserClassPath property", nullptr, Any());
340 
341  if (jfw_setUserClassPath(cp) != JFW_E_NONE)
342  throw WrappedTargetException(
343  "[Service implementation " IMPL_NAME
344  "] XLayerHandler::setPropertyValue: jfw_setUserClassPath failed.", nullptr, Any());
345  break;
346  }
347  default:
348  OSL_ASSERT(false);
349  }
350  }
351 }
352 
353 
354 void SAL_CALL JavaMigration::setPropertyValueForLocale(
355  const Any&,
356  const OUString& )
357 {
358 }
359 
360 
361 void SAL_CALL JavaMigration::endProperty()
362 {
363  if (!m_aStack.empty())
364  m_aStack.pop();
365 }
366 
367 
368 void SAL_CALL JavaMigration::addProperty(
369  const OUString&,
370  sal_Int16,
371  const Type& )
372 {
373 }
374 
375 
376 void SAL_CALL JavaMigration::addPropertyWithValue(
377  const OUString&,
378  sal_Int16,
379  const Any& )
380 {
381 }
382 
383 void SAL_CALL JavaMigration::addOrReplaceNodeFromTemplate(
384  const OUString&,
385  const TemplateIdentifier&,
386  sal_Int16 )
387 {
388 }
389 
390 
391 //ToDo enable java, user class path
392 
393 } //end namespace jfw
394 
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
javaFrameworkError jfw_getJavaInfoByPath(OUString const &pPath, std::unique_ptr< JavaInfo > *ppInfo)
#define SERVICE_NAME
Definition: jvmfwk.cxx:47
javaFrameworkError jfw_setEnabled(bool bEnabled)
OUString getImplementationName()
Definition: splash.cxx:640
javaFrameworkError jfw_setUserClassPath(OUString const &pCp)
#define IMPL_NAME
Definition: jvmfwk.cxx:48
css::uno::Sequence< OUString > jvmfwk_getSupportedServiceNames()
Definition: jvmfwk.cxx:153
std::pair< OUString, sal_Int16 > TElementType
Definition: jvmfwk.cxx:137
css::uno::Reference< css::configuration::backend::XLayer > m_xLayer
Definition: jvmfwk.cxx:134
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
std::stack< TElementType > TElementStack
Definition: jvmfwk.cxx:138
JFW_E_FAILED_VERSION
err
#define SAL_CONFIGFILE(name)
OUString jvmfwk_getImplementationName()
Definition: jvmfwk.cxx:148
css::uno::Sequence< OUString > getSupportedServiceNames()
Definition: splash.cxx:644
unsigned char sal_Bool
bool setPropertyValue(uno::Sequence< beans::PropertyValue > &aProp, const OUString &aName, const uno::Any &aValue)
javaFrameworkError jfw_setSelectedJRE(JavaInfo const *pInfo)
TElementStack m_aStack
Definition: jvmfwk.cxx:139
#define USER_CLASS_PATH
Definition: jvmfwk.cxx:51
OString const aName
javaFrameworkError
JFW_E_NONE
#define ENABLE_JAVA
Definition: jvmfwk.cxx:50