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_resource.h>
23 #include <dp_services.hxx>
24 #include <cppuhelper/compbase.hxx>
26 #include <com/sun/star/deployment/thePackageManagerFactory.hpp>
27 #include <unordered_map>
28 
29 using namespace ::dp_misc;
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 
33 namespace dp_manager {
34 namespace factory {
35 
36 typedef ::cppu::WeakComponentImplHelper<
37  deployment::XPackageManagerFactory > t_pmfac_helper;
38 
39 
40 class PackageManagerFactoryImpl : private MutexHolder, public t_pmfac_helper
41 {
43 
44  Reference<deployment::XPackageManager> m_xUserMgr;
45  Reference<deployment::XPackageManager> m_xSharedMgr;
46  Reference<deployment::XPackageManager> m_xBundledMgr;
47  Reference<deployment::XPackageManager> m_xTmpMgr;
48  Reference<deployment::XPackageManager> m_xBakMgr;
49  typedef std::unordered_map<
50  OUString, WeakReference<deployment::XPackageManager> > t_string2weakref;
51  t_string2weakref m_managers;
52 
53 protected:
54  inline void check();
55  virtual void SAL_CALL disposing() override;
56 
57 public:
59  Reference<XComponentContext> const & xComponentContext );
60 
61  // XPackageManagerFactory
62  virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
63  OUString const & context ) override;
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 
77  Reference<XComponentContext> const & xComponentContext )
78  : t_pmfac_helper( getMutex() ),
79  m_xComponentContext( xComponentContext )
80 {
81 }
82 
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 
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>
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 factory
164 } // namespace dp_manager
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< deployment::XPackageManager > m_xSharedMgr
Reference< XComponentContext > m_xComponentContext
virtual void SAL_CALL disposing() override
Reference< deployment::XPackageManager > m_xBakMgr
Reference< deployment::XPackageManager > m_xBundledMgr
virtual Reference< deployment::XPackageManager > SAL_CALL getPackageManager(OUString const &context) override
::cppu::WeakComponentImplHelper< deployment::XPackageManagerFactory > t_pmfac_helper
Reference< deployment::XPackageManager > m_xUserMgr
sdecl::class_< PackageManagerFactoryImpl > const servicePMFI
void try_dispose(css::uno::Reference< css::uno::XInterface > const &x)
Definition: dp_misc.h:49
static css::uno::Reference< css::deployment::XPackageManager > create(css::uno::Reference< css::uno::XComponentContext > const &xComponentContext, OUString const &context)
Definition: dp_manager.cxx:350
Reference< deployment::XPackageManager > m_xTmpMgr
std::unordered_map< OUString, WeakReference< deployment::XPackageManager > > t_string2weakref
comphelper::service_decl::ServiceDecl const serviceDecl
Definition: dp_services.hxx:22
PackageManagerFactoryImpl(Reference< XComponentContext > const &xComponentContext)
::osl::Mutex & getMutex()