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