1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_managerfac.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "dp_manager.h"
35 #include "dp_resource.h"
36 #include "cppuhelper/compbase1.hxx"
37 #include "comphelper/servicedecl.hxx"
38 #include "com/sun/star/deployment/thePackageManagerFactory.hpp"
41 using namespace ::dp_misc
;
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::uno
;
44 using ::rtl::OUString
;
46 namespace dp_manager
{
49 typedef ::cppu::WeakComponentImplHelper1
<
50 deployment::XPackageManagerFactory
> t_pmfac_helper
;
52 //==============================================================================
53 class PackageManagerFactoryImpl
: private MutexHolder
, public t_pmfac_helper
55 Reference
<XComponentContext
> m_xComponentContext
;
57 Reference
<deployment::XPackageManager
> m_xUserMgr
;
58 Reference
<deployment::XPackageManager
> m_xSharedMgr
;
59 typedef ::std::hash_map
<
60 OUString
, WeakReference
<deployment::XPackageManager
>,
61 ::rtl::OUStringHash
> t_string2weakref
;
62 t_string2weakref m_managers
;
66 virtual void SAL_CALL
disposing();
69 virtual ~PackageManagerFactoryImpl();
70 PackageManagerFactoryImpl(
71 Reference
<XComponentContext
> const & xComponentContext
);
73 // XPackageManagerFactory
74 virtual Reference
<deployment::XPackageManager
> SAL_CALL
getPackageManager(
75 OUString
const & context
) throw (RuntimeException
);
78 //==============================================================================
79 namespace sdecl
= comphelper::service_decl
;
80 sdecl::class_
<PackageManagerFactoryImpl
> servicePMFI
;
81 extern sdecl::ServiceDecl
const serviceDecl(
84 "com.sun.star.comp.deployment.PackageManagerFactory",
85 "com.sun.star.comp.deployment.PackageManagerFactory" );
87 //==============================================================================
88 bool singleton_entries(
89 Reference
<registry::XRegistryKey
> const & xRegistryKey
)
92 Reference
<registry::XRegistryKey
> xKey(
93 xRegistryKey
->createKey(
94 serviceDecl
.getImplementationName() +
95 // xxx todo: use future generated function to get singleton name
96 OUSTR("/UNO/SINGLETONS/"
97 "com.sun.star.deployment.thePackageManagerFactory") ) );
98 xKey
->setStringValue( serviceDecl
.getSupportedServiceNames()[0] );
101 catch (registry::InvalidRegistryException
& exc
) {
102 (void) exc
; // avoid warnings
103 OSL_ENSURE( 0, ::rtl::OUStringToOString(
104 exc
.Message
, RTL_TEXTENCODING_UTF8
).getStr() );
109 //______________________________________________________________________________
110 PackageManagerFactoryImpl::PackageManagerFactoryImpl(
111 Reference
<XComponentContext
> const & xComponentContext
)
112 : t_pmfac_helper( getMutex() ),
113 m_xComponentContext( xComponentContext
)
117 //______________________________________________________________________________
118 PackageManagerFactoryImpl::~PackageManagerFactoryImpl()
122 //______________________________________________________________________________
123 inline void PackageManagerFactoryImpl::check()
125 ::osl::MutexGuard
guard( getMutex() );
126 if (rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
128 throw lang::DisposedException(
129 OUSTR("PackageManagerFactory instance has already been disposed!"),
130 static_cast<OWeakObject
*>(this) );
134 //______________________________________________________________________________
135 void PackageManagerFactoryImpl::disposing()
137 // dispose all managers:
138 ::osl::MutexGuard
guard( getMutex() );
139 t_string2weakref::const_iterator
iPos( m_managers
.begin() );
140 t_string2weakref::const_iterator
const iEnd( m_managers
.end() );
141 for ( ; iPos
!= iEnd
; ++iPos
)
142 try_dispose( iPos
->second
);
143 m_managers
= t_string2weakref();
144 // the below are already disposed:
146 m_xSharedMgr
.clear();
149 // XPackageManagerFactory
150 //______________________________________________________________________________
151 Reference
<deployment::XPackageManager
>
152 PackageManagerFactoryImpl::getPackageManager( OUString
const & context
)
153 throw (RuntimeException
)
155 Reference
< deployment::XPackageManager
> xRet
;
156 ::osl::ResettableMutexGuard
guard( getMutex() );
158 t_string2weakref::const_iterator
const iFind( m_managers
.find( context
) );
159 if (iFind
!= m_managers
.end()) {
160 xRet
= iFind
->second
;
166 xRet
.set( PackageManagerImpl::create( m_xComponentContext
, context
) );
168 ::std::pair
< t_string2weakref::iterator
, bool > insertion(
169 m_managers
.insert( t_string2weakref::value_type( context
, xRet
) ) );
170 if (insertion
.second
)
172 OSL_ASSERT( insertion
.first
->second
.get() == xRet
);
173 // hold user, shared mgrs for whole process: live deployment
174 if (context
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("user") ))
176 else if (context
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("shared") ))
181 Reference
< deployment::XPackageManager
> xAlreadyIn(
182 insertion
.first
->second
);
191 insertion
.first
->second
= xRet
;
197 } // namespace factory
198 } // namespace dp_manager