1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2722ceddSAndrew Rist  * distributed with this work for additional information
6*2722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2722ceddSAndrew Rist  *
11*2722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2722ceddSAndrew Rist  *
13*2722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist  * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2722ceddSAndrew Rist  * specific language governing permissions and limitations
18*2722ceddSAndrew Rist  * under the License.
19*2722ceddSAndrew Rist  *
20*2722ceddSAndrew Rist  *************************************************************/
21*2722ceddSAndrew Rist 
22*2722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "dp_manager.h"
28cdf0e10cSrcweir #include "dp_resource.h"
29cdf0e10cSrcweir #include "cppuhelper/compbase1.hxx"
30cdf0e10cSrcweir #include "comphelper/servicedecl.hxx"
31cdf0e10cSrcweir #include "com/sun/star/deployment/thePackageManagerFactory.hpp"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::dp_misc;
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir using namespace ::com::sun::star::uno;
37cdf0e10cSrcweir using ::rtl::OUString;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace dp_manager {
40cdf0e10cSrcweir namespace factory {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper1<
43cdf0e10cSrcweir     deployment::XPackageManagerFactory > t_pmfac_helper;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //==============================================================================
46cdf0e10cSrcweir class PackageManagerFactoryImpl : private MutexHolder, public t_pmfac_helper
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     Reference<XComponentContext> m_xComponentContext;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     Reference<deployment::XPackageManager> m_xUserMgr;
51cdf0e10cSrcweir     Reference<deployment::XPackageManager> m_xSharedMgr;
52cdf0e10cSrcweir     Reference<deployment::XPackageManager> m_xBundledMgr;
538402cd44SMichael Stahl     Reference<deployment::XPackageManager> m_xTmpMgr;
548402cd44SMichael Stahl     Reference<deployment::XPackageManager> m_xBakMgr;
55cdf0e10cSrcweir     typedef ::std::hash_map<
56cdf0e10cSrcweir         OUString, WeakReference<deployment::XPackageManager>,
57cdf0e10cSrcweir         ::rtl::OUStringHash > t_string2weakref;
58cdf0e10cSrcweir     t_string2weakref m_managers;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir protected:
61cdf0e10cSrcweir     inline void check();
62cdf0e10cSrcweir     virtual void SAL_CALL disposing();
63cdf0e10cSrcweir 
64cdf0e10cSrcweir public:
65cdf0e10cSrcweir     virtual ~PackageManagerFactoryImpl();
66cdf0e10cSrcweir     PackageManagerFactoryImpl(
67cdf0e10cSrcweir         Reference<XComponentContext> const & xComponentContext );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     // XPackageManagerFactory
70cdf0e10cSrcweir     virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
71cdf0e10cSrcweir         OUString const & context ) throw (RuntimeException);
72cdf0e10cSrcweir };
73cdf0e10cSrcweir 
74cdf0e10cSrcweir //==============================================================================
75cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
76cdf0e10cSrcweir sdecl::class_<PackageManagerFactoryImpl> servicePMFI;
77cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
78cdf0e10cSrcweir     servicePMFI,
79cdf0e10cSrcweir     // a private one:
80cdf0e10cSrcweir     "com.sun.star.comp.deployment.PackageManagerFactory",
81cdf0e10cSrcweir     "com.sun.star.comp.deployment.PackageManagerFactory" );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir //==============================================================================
singleton_entries(Reference<registry::XRegistryKey> const & xRegistryKey)84cdf0e10cSrcweir bool singleton_entries(
85cdf0e10cSrcweir     Reference<registry::XRegistryKey> const & xRegistryKey )
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     try {
88cdf0e10cSrcweir         Reference<registry::XRegistryKey> xKey(
89cdf0e10cSrcweir             xRegistryKey->createKey(
90cdf0e10cSrcweir                 serviceDecl.getImplementationName() +
91cdf0e10cSrcweir                 // xxx todo: use future generated function to get singleton name
92cdf0e10cSrcweir                 OUSTR("/UNO/SINGLETONS/"
93cdf0e10cSrcweir                       "com.sun.star.deployment.thePackageManagerFactory") ) );
94cdf0e10cSrcweir         xKey->setStringValue( serviceDecl.getSupportedServiceNames()[0] );
95cdf0e10cSrcweir         return true;
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir     catch (registry::InvalidRegistryException & exc) {
98cdf0e10cSrcweir         (void) exc; // avoid warnings
99cdf0e10cSrcweir         OSL_ENSURE( 0, ::rtl::OUStringToOString(
100cdf0e10cSrcweir                         exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
101cdf0e10cSrcweir         return false;
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir //______________________________________________________________________________
PackageManagerFactoryImpl(Reference<XComponentContext> const & xComponentContext)106cdf0e10cSrcweir PackageManagerFactoryImpl::PackageManagerFactoryImpl(
107cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext )
108cdf0e10cSrcweir     : t_pmfac_helper( getMutex() ),
109cdf0e10cSrcweir       m_xComponentContext( xComponentContext )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir //______________________________________________________________________________
~PackageManagerFactoryImpl()114cdf0e10cSrcweir PackageManagerFactoryImpl::~PackageManagerFactoryImpl()
115cdf0e10cSrcweir {
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //______________________________________________________________________________
check()119cdf0e10cSrcweir inline void PackageManagerFactoryImpl::check()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     ::osl::MutexGuard guard( getMutex() );
122cdf0e10cSrcweir     if (rBHelper.bInDispose || rBHelper.bDisposed)
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir         throw lang::DisposedException(
125cdf0e10cSrcweir             OUSTR("PackageManagerFactory instance has already been disposed!"),
126cdf0e10cSrcweir             static_cast<OWeakObject *>(this) );
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir //______________________________________________________________________________
disposing()131cdf0e10cSrcweir void PackageManagerFactoryImpl::disposing()
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     // dispose all managers:
134cdf0e10cSrcweir     ::osl::MutexGuard guard( getMutex() );
135cdf0e10cSrcweir     t_string2weakref::const_iterator iPos( m_managers.begin() );
136cdf0e10cSrcweir     t_string2weakref::const_iterator const iEnd( m_managers.end() );
137cdf0e10cSrcweir     for ( ; iPos != iEnd; ++iPos )
138cdf0e10cSrcweir         try_dispose( iPos->second );
139cdf0e10cSrcweir     m_managers = t_string2weakref();
140cdf0e10cSrcweir     // the below are already disposed:
141cdf0e10cSrcweir     m_xUserMgr.clear();
142cdf0e10cSrcweir     m_xSharedMgr.clear();
143cdf0e10cSrcweir     m_xBundledMgr.clear();
1448402cd44SMichael Stahl     m_xTmpMgr.clear();
1458402cd44SMichael Stahl     m_xBakMgr.clear();
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir // XPackageManagerFactory
149cdf0e10cSrcweir //______________________________________________________________________________
150cdf0e10cSrcweir Reference<deployment::XPackageManager>
getPackageManager(OUString const & context)151cdf0e10cSrcweir PackageManagerFactoryImpl::getPackageManager( OUString const & context )
152cdf0e10cSrcweir     throw (RuntimeException)
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     Reference< deployment::XPackageManager > xRet;
155cdf0e10cSrcweir     ::osl::ResettableMutexGuard guard( getMutex() );
156cdf0e10cSrcweir     check();
157cdf0e10cSrcweir     t_string2weakref::const_iterator const iFind( m_managers.find( context ) );
158cdf0e10cSrcweir     if (iFind != m_managers.end()) {
159cdf0e10cSrcweir         xRet = iFind->second;
160cdf0e10cSrcweir         if (xRet.is())
161cdf0e10cSrcweir             return xRet;
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     guard.clear();
165cdf0e10cSrcweir     xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) );
166cdf0e10cSrcweir     guard.reset();
167cdf0e10cSrcweir     ::std::pair< t_string2weakref::iterator, bool > insertion(
168cdf0e10cSrcweir         m_managers.insert( t_string2weakref::value_type( context, xRet ) ) );
169cdf0e10cSrcweir     if (insertion.second)
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         OSL_ASSERT( insertion.first->second.get() == xRet );
172cdf0e10cSrcweir         // hold user, shared mgrs for whole process: live deployment
173cdf0e10cSrcweir         if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("user") ))
174cdf0e10cSrcweir             m_xUserMgr = xRet;
175cdf0e10cSrcweir         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("shared") ))
176cdf0e10cSrcweir             m_xSharedMgr = xRet;
177cdf0e10cSrcweir         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bundled") ))
178cdf0e10cSrcweir             m_xBundledMgr = xRet;
1798402cd44SMichael Stahl         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("tmp") ))
1808402cd44SMichael Stahl             m_xTmpMgr = xRet;
1818402cd44SMichael Stahl         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bak") ))
1828402cd44SMichael Stahl             m_xBakMgr = xRet;
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir     else
185cdf0e10cSrcweir     {
186cdf0e10cSrcweir         Reference< deployment::XPackageManager > xAlreadyIn(
187cdf0e10cSrcweir             insertion.first->second );
188cdf0e10cSrcweir         if (xAlreadyIn.is())
189cdf0e10cSrcweir         {
190cdf0e10cSrcweir             guard.clear();
191cdf0e10cSrcweir             try_dispose( xRet );
192cdf0e10cSrcweir             xRet = xAlreadyIn;
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir         else
195cdf0e10cSrcweir         {
196cdf0e10cSrcweir             insertion.first->second = xRet;
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir     return xRet;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir } // namespace factory
203cdf0e10cSrcweir } // namespace dp_manager
204cdf0e10cSrcweir 
205