Line data Source code
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 <cppuhelper/compbase1.hxx>
24 : #include <comphelper/servicedecl.hxx>
25 : #include <com/sun/star/deployment/thePackageManagerFactory.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 {
33 : namespace factory {
34 :
35 : typedef ::cppu::WeakComponentImplHelper1<
36 : deployment::XPackageManagerFactory > t_pmfac_helper;
37 :
38 :
39 : class PackageManagerFactoryImpl : private MutexHolder, public t_pmfac_helper
40 : {
41 : Reference<XComponentContext> m_xComponentContext;
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>,
50 : OUStringHash > t_string2weakref;
51 : t_string2weakref m_managers;
52 :
53 : protected:
54 : inline void check();
55 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
56 :
57 : public:
58 : virtual ~PackageManagerFactoryImpl();
59 : PackageManagerFactoryImpl(
60 : Reference<XComponentContext> const & xComponentContext );
61 :
62 : // XPackageManagerFactory
63 : virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
64 : OUString const & context ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
65 : };
66 :
67 :
68 : namespace sdecl = comphelper::service_decl;
69 111 : sdecl::class_<PackageManagerFactoryImpl> servicePMFI;
70 111 : extern sdecl::ServiceDecl const serviceDecl(
71 : servicePMFI,
72 : // a private one:
73 : "com.sun.star.comp.deployment.PackageManagerFactory",
74 : "com.sun.star.comp.deployment.PackageManagerFactory" );
75 :
76 :
77 111 : PackageManagerFactoryImpl::PackageManagerFactoryImpl(
78 : Reference<XComponentContext> const & xComponentContext )
79 111 : : t_pmfac_helper( getMutex() ),
80 222 : m_xComponentContext( xComponentContext )
81 : {
82 111 : }
83 :
84 :
85 109 : PackageManagerFactoryImpl::~PackageManagerFactoryImpl()
86 : {
87 109 : }
88 :
89 :
90 5283 : inline void PackageManagerFactoryImpl::check()
91 : {
92 5283 : ::osl::MutexGuard guard( getMutex() );
93 5283 : if (rBHelper.bInDispose || rBHelper.bDisposed)
94 : {
95 : throw lang::DisposedException(
96 : "PackageManagerFactory instance has already been disposed!",
97 0 : static_cast<OWeakObject *>(this) );
98 5283 : }
99 5283 : }
100 :
101 :
102 110 : void PackageManagerFactoryImpl::disposing()
103 : {
104 : // dispose all managers:
105 110 : ::osl::MutexGuard guard( getMutex() );
106 110 : t_string2weakref::const_iterator iPos( m_managers.begin() );
107 110 : t_string2weakref::const_iterator const iEnd( m_managers.end() );
108 449 : for ( ; iPos != iEnd; ++iPos )
109 339 : try_dispose( iPos->second );
110 110 : m_managers = t_string2weakref();
111 : // the below are already disposed:
112 110 : m_xUserMgr.clear();
113 110 : m_xSharedMgr.clear();
114 110 : m_xBundledMgr.clear();
115 110 : m_xTmpMgr.clear();
116 110 : m_xBakMgr.clear();
117 110 : }
118 :
119 : // XPackageManagerFactory
120 :
121 : Reference<deployment::XPackageManager>
122 5283 : PackageManagerFactoryImpl::getPackageManager( OUString const & context )
123 : throw (RuntimeException, std::exception)
124 : {
125 5283 : Reference< deployment::XPackageManager > xRet;
126 10566 : ::osl::ResettableMutexGuard guard( getMutex() );
127 5283 : check();
128 5283 : t_string2weakref::const_iterator const iFind( m_managers.find( context ) );
129 5283 : if (iFind != m_managers.end()) {
130 4918 : xRet = iFind->second;
131 4918 : if (xRet.is())
132 4918 : return xRet;
133 : }
134 :
135 365 : guard.clear();
136 365 : xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) );
137 339 : guard.reset();
138 : ::std::pair< t_string2weakref::iterator, bool > insertion(
139 339 : m_managers.insert( t_string2weakref::value_type( context, xRet ) ) );
140 339 : if (insertion.second)
141 : {
142 : OSL_ASSERT( insertion.first->second.get() == xRet );
143 : // hold user, shared mgrs for whole process: live deployment
144 339 : if ( context == "user" )
145 95 : m_xUserMgr = xRet;
146 244 : else if ( context == "shared" )
147 95 : m_xSharedMgr = xRet;
148 149 : else if ( context == "bundled" )
149 95 : m_xBundledMgr = xRet;
150 54 : else if ( context == "tmp" )
151 54 : m_xTmpMgr = xRet;
152 0 : else if ( context == "bak" )
153 0 : m_xBakMgr = xRet;
154 : }
155 : else
156 : {
157 : Reference< deployment::XPackageManager > xAlreadyIn(
158 0 : insertion.first->second );
159 0 : if (xAlreadyIn.is())
160 : {
161 0 : guard.clear();
162 0 : try_dispose( xRet );
163 0 : xRet = xAlreadyIn;
164 : }
165 : else
166 : {
167 0 : insertion.first->second = xRet;
168 0 : }
169 : }
170 339 : return xRet;
171 : }
172 :
173 : } // namespace factory
174 333 : } // namespace dp_manager
175 :
176 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|