LibreOffice Module desktop (master)  1
dp_managerfac.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 "dp_manager.h"
22 #include <dp_services.hxx>
23 #include <cppuhelper/compbase.hxx>
25 #include <com/sun/star/deployment/XPackageManagerFactory.hpp>
26 #include <unordered_map>
27 
28 using namespace ::dp_misc;
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
31 
32 namespace dp_manager::factory {
33 
34 typedef ::cppu::WeakComponentImplHelper<
35  deployment::XPackageManagerFactory > t_pmfac_helper;
36 
37 namespace {
38 
39 class PackageManagerFactoryImpl : private MutexHolder, public t_pmfac_helper
40 {
42 
43  Reference<deployment::XPackageManager> m_xUserMgr;
44  Reference<deployment::XPackageManager> m_xSharedMgr;
45  Reference<deployment::XPackageManager> m_xBundledMgr;
46  Reference<deployment::XPackageManager> m_xTmpMgr;
47  Reference<deployment::XPackageManager> m_xBakMgr;
48  typedef std::unordered_map<
49  OUString, WeakReference<deployment::XPackageManager> > t_string2weakref;
50  t_string2weakref m_managers;
51 
52 protected:
53  inline void check();
54  virtual void SAL_CALL disposing() override;
55 
56 public:
57  explicit PackageManagerFactoryImpl(
58  Reference<XComponentContext> const & xComponentContext );
59 
60  // XPackageManagerFactory
61  virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
62  OUString const & context ) override;
63 };
64 
65 }
66 
70  servicePMFI,
71  // a private one:
72  "com.sun.star.comp.deployment.PackageManagerFactory",
73  "com.sun.star.comp.deployment.PackageManagerFactory" );
74 
75 
76 PackageManagerFactoryImpl::PackageManagerFactoryImpl(
77  Reference<XComponentContext> const & xComponentContext )
78  : t_pmfac_helper( getMutex() ),
79  m_xComponentContext( xComponentContext )
80 {
81 }
82 
83 inline void PackageManagerFactoryImpl::check()
84 {
85  ::osl::MutexGuard guard( getMutex() );
86  if (rBHelper.bInDispose || rBHelper.bDisposed)
87  {
88  throw lang::DisposedException(
89  "PackageManagerFactory instance has already been disposed!",
90  static_cast<OWeakObject *>(this) );
91  }
92 }
93 
94 
95 void PackageManagerFactoryImpl::disposing()
96 {
97  // dispose all managers:
98  ::osl::MutexGuard guard( getMutex() );
99  for (auto const& elem : m_managers)
100  try_dispose( elem.second );
101  m_managers = t_string2weakref();
102  // the below are already disposed:
103  m_xUserMgr.clear();
104  m_xSharedMgr.clear();
105  m_xBundledMgr.clear();
106  m_xTmpMgr.clear();
107  m_xBakMgr.clear();
108 }
109 
110 // XPackageManagerFactory
111 
112 Reference<deployment::XPackageManager>
113 PackageManagerFactoryImpl::getPackageManager( OUString const & context )
114 {
115  Reference< deployment::XPackageManager > xRet;
116  ::osl::ResettableMutexGuard guard( getMutex() );
117  check();
118  t_string2weakref::const_iterator const iFind( m_managers.find( context ) );
119  if (iFind != m_managers.end()) {
120  xRet = iFind->second;
121  if (xRet.is())
122  return xRet;
123  }
124 
125  guard.clear();
126  xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) );
127  guard.reset();
128  std::pair< t_string2weakref::iterator, bool > insertion(
129  m_managers.emplace( context, xRet ) );
130  if (insertion.second)
131  {
132  OSL_ASSERT( insertion.first->second.get() == xRet );
133  // hold user, shared mgrs for whole process: live deployment
134  if ( context == "user" )
135  m_xUserMgr = xRet;
136  else if ( context == "shared" )
137  m_xSharedMgr = xRet;
138  else if ( context == "bundled" )
139  m_xBundledMgr = xRet;
140  else if ( context == "tmp" )
141  m_xTmpMgr = xRet;
142  else if ( context == "bak" )
143  m_xBakMgr = xRet;
144  }
145  else
146  {
147  Reference< deployment::XPackageManager > xAlreadyIn(
148  insertion.first->second );
149  if (xAlreadyIn.is())
150  {
151  guard.clear();
152  try_dispose( xRet );
153  xRet = xAlreadyIn;
154  }
155  else
156  {
157  insertion.first->second = xRet;
158  }
159  }
160  return xRet;
161 }
162 
163 } // namespace dp_manager::factory
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
def check(model)
Reference< deployment::XPackageManager > m_xUserMgr
Reference< deployment::XPackageManager > m_xTmpMgr
::cppu::WeakComponentImplHelper< deployment::XPackageManagerFactory > t_pmfac_helper
t_string2weakref m_managers
Reference< deployment::XPackageManager > m_xSharedMgr
sdecl::class_< PackageManagerFactoryImpl > const servicePMFI
Reference< deployment::XPackageManager > m_xBundledMgr
void try_dispose(css::uno::Reference< css::uno::XInterface > const &x)
Definition: dp_misc.h:44
static css::uno::Reference< css::deployment::XPackageManager > create(css::uno::Reference< css::uno::XComponentContext > const &xComponentContext, OUString const &context)
Definition: dp_manager.cxx:343
comphelper::service_decl::ServiceDecl const serviceDecl
Definition: dp_services.hxx:22
Reference< XComponentContext > m_xComponentContext
Reference< deployment::XPackageManager > m_xBakMgr
::osl::Mutex & getMutex()