1cdf0e10cSrcweir /*************************************************************************
2cdf0e10cSrcweir  *
3cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4cdf0e10cSrcweir  *
5cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6cdf0e10cSrcweir  *
7cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8cdf0e10cSrcweir  *
9cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10cdf0e10cSrcweir  *
11cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14cdf0e10cSrcweir  *
15cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20cdf0e10cSrcweir  *
21cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  ************************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "dp_manager.h"
32cdf0e10cSrcweir #include "dp_resource.h"
33cdf0e10cSrcweir #include "cppuhelper/compbase1.hxx"
34cdf0e10cSrcweir #include "comphelper/servicedecl.hxx"
35cdf0e10cSrcweir #include "com/sun/star/deployment/thePackageManagerFactory.hpp"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace ::dp_misc;
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41cdf0e10cSrcweir using ::rtl::OUString;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace dp_manager {
44cdf0e10cSrcweir namespace factory {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper1<
47cdf0e10cSrcweir     deployment::XPackageManagerFactory > t_pmfac_helper;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //==============================================================================
50cdf0e10cSrcweir class PackageManagerFactoryImpl : private MutexHolder, public t_pmfac_helper
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     Reference<XComponentContext> m_xComponentContext;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     Reference<deployment::XPackageManager> m_xUserMgr;
55cdf0e10cSrcweir     Reference<deployment::XPackageManager> m_xSharedMgr;
56cdf0e10cSrcweir     Reference<deployment::XPackageManager> m_xBundledMgr;
57*8402cd44SMichael Stahl     Reference<deployment::XPackageManager> m_xTmpMgr;
58*8402cd44SMichael Stahl     Reference<deployment::XPackageManager> m_xBakMgr;
59cdf0e10cSrcweir     typedef ::std::hash_map<
60cdf0e10cSrcweir         OUString, WeakReference<deployment::XPackageManager>,
61cdf0e10cSrcweir         ::rtl::OUStringHash > t_string2weakref;
62cdf0e10cSrcweir     t_string2weakref m_managers;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir protected:
65cdf0e10cSrcweir     inline void check();
66cdf0e10cSrcweir     virtual void SAL_CALL disposing();
67cdf0e10cSrcweir 
68cdf0e10cSrcweir public:
69cdf0e10cSrcweir     virtual ~PackageManagerFactoryImpl();
70cdf0e10cSrcweir     PackageManagerFactoryImpl(
71cdf0e10cSrcweir         Reference<XComponentContext> const & xComponentContext );
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     // XPackageManagerFactory
74cdf0e10cSrcweir     virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
75cdf0e10cSrcweir         OUString const & context ) throw (RuntimeException);
76cdf0e10cSrcweir };
77cdf0e10cSrcweir 
78cdf0e10cSrcweir //==============================================================================
79cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
80cdf0e10cSrcweir sdecl::class_<PackageManagerFactoryImpl> servicePMFI;
81cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
82cdf0e10cSrcweir     servicePMFI,
83cdf0e10cSrcweir     // a private one:
84cdf0e10cSrcweir     "com.sun.star.comp.deployment.PackageManagerFactory",
85cdf0e10cSrcweir     "com.sun.star.comp.deployment.PackageManagerFactory" );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir //==============================================================================
88cdf0e10cSrcweir bool singleton_entries(
89cdf0e10cSrcweir     Reference<registry::XRegistryKey> const & xRegistryKey )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     try {
92cdf0e10cSrcweir         Reference<registry::XRegistryKey> xKey(
93cdf0e10cSrcweir             xRegistryKey->createKey(
94cdf0e10cSrcweir                 serviceDecl.getImplementationName() +
95cdf0e10cSrcweir                 // xxx todo: use future generated function to get singleton name
96cdf0e10cSrcweir                 OUSTR("/UNO/SINGLETONS/"
97cdf0e10cSrcweir                       "com.sun.star.deployment.thePackageManagerFactory") ) );
98cdf0e10cSrcweir         xKey->setStringValue( serviceDecl.getSupportedServiceNames()[0] );
99cdf0e10cSrcweir         return true;
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir     catch (registry::InvalidRegistryException & exc) {
102cdf0e10cSrcweir         (void) exc; // avoid warnings
103cdf0e10cSrcweir         OSL_ENSURE( 0, ::rtl::OUStringToOString(
104cdf0e10cSrcweir                         exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
105cdf0e10cSrcweir         return false;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir //______________________________________________________________________________
110cdf0e10cSrcweir PackageManagerFactoryImpl::PackageManagerFactoryImpl(
111cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext )
112cdf0e10cSrcweir     : t_pmfac_helper( getMutex() ),
113cdf0e10cSrcweir       m_xComponentContext( xComponentContext )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir //______________________________________________________________________________
118cdf0e10cSrcweir PackageManagerFactoryImpl::~PackageManagerFactoryImpl()
119cdf0e10cSrcweir {
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir //______________________________________________________________________________
123cdf0e10cSrcweir inline void PackageManagerFactoryImpl::check()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     ::osl::MutexGuard guard( getMutex() );
126cdf0e10cSrcweir     if (rBHelper.bInDispose || rBHelper.bDisposed)
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         throw lang::DisposedException(
129cdf0e10cSrcweir             OUSTR("PackageManagerFactory instance has already been disposed!"),
130cdf0e10cSrcweir             static_cast<OWeakObject *>(this) );
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //______________________________________________________________________________
135cdf0e10cSrcweir void PackageManagerFactoryImpl::disposing()
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     // dispose all managers:
138cdf0e10cSrcweir     ::osl::MutexGuard guard( getMutex() );
139cdf0e10cSrcweir     t_string2weakref::const_iterator iPos( m_managers.begin() );
140cdf0e10cSrcweir     t_string2weakref::const_iterator const iEnd( m_managers.end() );
141cdf0e10cSrcweir     for ( ; iPos != iEnd; ++iPos )
142cdf0e10cSrcweir         try_dispose( iPos->second );
143cdf0e10cSrcweir     m_managers = t_string2weakref();
144cdf0e10cSrcweir     // the below are already disposed:
145cdf0e10cSrcweir     m_xUserMgr.clear();
146cdf0e10cSrcweir     m_xSharedMgr.clear();
147cdf0e10cSrcweir     m_xBundledMgr.clear();
148*8402cd44SMichael Stahl     m_xTmpMgr.clear();
149*8402cd44SMichael Stahl     m_xBakMgr.clear();
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir // XPackageManagerFactory
153cdf0e10cSrcweir //______________________________________________________________________________
154cdf0e10cSrcweir Reference<deployment::XPackageManager>
155cdf0e10cSrcweir PackageManagerFactoryImpl::getPackageManager( OUString const & context )
156cdf0e10cSrcweir     throw (RuntimeException)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     Reference< deployment::XPackageManager > xRet;
159cdf0e10cSrcweir     ::osl::ResettableMutexGuard guard( getMutex() );
160cdf0e10cSrcweir     check();
161cdf0e10cSrcweir     t_string2weakref::const_iterator const iFind( m_managers.find( context ) );
162cdf0e10cSrcweir     if (iFind != m_managers.end()) {
163cdf0e10cSrcweir         xRet = iFind->second;
164cdf0e10cSrcweir         if (xRet.is())
165cdf0e10cSrcweir             return xRet;
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     guard.clear();
169cdf0e10cSrcweir     xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) );
170cdf0e10cSrcweir     guard.reset();
171cdf0e10cSrcweir     ::std::pair< t_string2weakref::iterator, bool > insertion(
172cdf0e10cSrcweir         m_managers.insert( t_string2weakref::value_type( context, xRet ) ) );
173cdf0e10cSrcweir     if (insertion.second)
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         OSL_ASSERT( insertion.first->second.get() == xRet );
176cdf0e10cSrcweir         // hold user, shared mgrs for whole process: live deployment
177cdf0e10cSrcweir         if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("user") ))
178cdf0e10cSrcweir             m_xUserMgr = xRet;
179cdf0e10cSrcweir         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("shared") ))
180cdf0e10cSrcweir             m_xSharedMgr = xRet;
181cdf0e10cSrcweir         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bundled") ))
182cdf0e10cSrcweir             m_xBundledMgr = xRet;
183*8402cd44SMichael Stahl         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("tmp") ))
184*8402cd44SMichael Stahl             m_xTmpMgr = xRet;
185*8402cd44SMichael Stahl         else if (context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("bak") ))
186*8402cd44SMichael Stahl             m_xBakMgr = xRet;
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir     else
189cdf0e10cSrcweir     {
190cdf0e10cSrcweir         Reference< deployment::XPackageManager > xAlreadyIn(
191cdf0e10cSrcweir             insertion.first->second );
192cdf0e10cSrcweir         if (xAlreadyIn.is())
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir             guard.clear();
195cdf0e10cSrcweir             try_dispose( xRet );
196cdf0e10cSrcweir             xRet = xAlreadyIn;
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir         else
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             insertion.first->second = xRet;
201cdf0e10cSrcweir         }
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir     return xRet;
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir } // namespace factory
207cdf0e10cSrcweir } // namespace dp_manager
208cdf0e10cSrcweir 
209