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: ComponentContext.java,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 ************************************************************************/
30 package com
.sun
.star
.comp
.helper
;
32 import com
.sun
.star
.uno
.UnoRuntime
;
33 import com
.sun
.star
.uno
.Any
;
35 import com
.sun
.star
.uno
.XComponentContext
;
36 import com
.sun
.star
.lang
.XMultiComponentFactory
;
37 import com
.sun
.star
.lang
.XSingleComponentFactory
;
38 import com
.sun
.star
.lang
.XComponent
;
39 import com
.sun
.star
.lang
.XEventListener
;
40 import com
.sun
.star
.lang
.EventObject
;
42 import java
.util
.Hashtable
;
43 import java
.util
.Enumeration
;
44 import java
.util
.Vector
;
47 //==================================================================================================
48 class Disposer
implements XEventListener
50 private XComponent m_xComp
;
52 //----------------------------------------------------------------------------------------------
53 Disposer( XComponent xComp
)
57 //______________________________________________________________________________________________
58 public void disposing( EventObject Source
)
64 /** Component context implementation.
66 public class ComponentContext
implements XComponentContext
, XComponent
68 private static final boolean DEBUG
= false;
69 private static final String SMGR_NAME
= "/singletons/com.sun.star.lang.theServiceManager";
70 private static final String TDMGR_NAME
= "/singletons/com.sun.star.reflection.theTypeDescriptionManager";
72 private Hashtable m_table
;
73 private XComponentContext m_xDelegate
;
75 private XMultiComponentFactory m_xSMgr
;
76 private boolean m_bDisposeSMgr
;
78 private Vector m_eventListener
;
80 /** Ctor to create a component context passing a hashtable for values and a delegator
81 reference. Entries of the passed hashtable are either direct values or
82 ComponentContextEntry objects.
87 if values are not found, request is delegated to this object
89 public ComponentContext( Hashtable table
, XComponentContext xDelegate
)
91 m_eventListener
= new Vector();
93 m_xDelegate
= xDelegate
;
95 m_bDisposeSMgr
= false;
97 Object o
= table
.get( SMGR_NAME
);
100 if (o
instanceof ComponentContextEntry
)
102 o
= ((ComponentContextEntry
)o
).m_value
;
104 m_xSMgr
= UnoRuntime
.queryInterface(
105 XMultiComponentFactory
.class, o
);
109 m_bDisposeSMgr
= true;
111 else if (m_xDelegate
!= null)
113 m_xSMgr
= m_xDelegate
.getServiceManager();
116 // listen for delegate
117 XComponent xComp
= UnoRuntime
.queryInterface(
118 XComponent
.class, m_xDelegate
);
121 xComp
.addEventListener( new Disposer( this ) );
125 // XComponentContext impl
126 //______________________________________________________________________________________________
127 public Object
getValueByName( String rName
)
129 Object o
= m_table
.get( rName
);
132 if (o
instanceof ComponentContextEntry
)
134 ComponentContextEntry entry
= (ComponentContextEntry
)o
;
135 if (entry
.m_lateInit
!= null)
137 Object xInstance
= null;
141 String serviceName
= (String
)entry
.m_lateInit
;
142 if (serviceName
!= null)
146 xInstance
= m_xSMgr
.createInstanceWithContext( serviceName
, this );
151 System
.err
.println( "### no service manager instance for late init of singleton instance \"" + rName
+ "\"!" );
156 XSingleComponentFactory xCompFac
=
157 UnoRuntime
.queryInterface(
158 XSingleComponentFactory
.class, entry
.m_lateInit
);
159 if (xCompFac
!= null)
161 xInstance
= xCompFac
.createInstanceWithContext( this );
166 System
.err
.println( "### neither service name nor service factory given for late init of singleton instance \"" + rName
+ "\"!" );
170 catch (com
.sun
.star
.uno
.Exception exc
)
173 System
.err
.println( "### exception occured on late init of singleton instance \"" + rName
+ "\": " + exc
.getMessage() );
176 if (xInstance
!= null)
180 if (entry
.m_lateInit
!= null)
182 entry
.m_value
= xInstance
;
183 entry
.m_lateInit
= null;
185 else // inited in the meantime
187 // dispose fresh service instance
188 XComponent xComp
= UnoRuntime
.queryInterface(
189 XComponent
.class, xInstance
);
200 System
.err
.println( "### failed late init of singleton instance \"" + rName
+ "\"!" );
203 return entry
.m_value
;
205 else // direct value in map
210 else if (m_xDelegate
!= null)
212 return m_xDelegate
.getValueByName( rName
);
219 //______________________________________________________________________________________________
220 public XMultiComponentFactory
getServiceManager()
226 //______________________________________________________________________________________________
227 public void dispose()
230 System
.err
.print( "> disposing context " + this );
233 EventObject evt
= new EventObject( this );
234 Enumeration eventListener
= m_eventListener
.elements();
235 while (eventListener
.hasMoreElements())
237 XEventListener listener
= (XEventListener
)eventListener
.nextElement();
238 listener
.disposing( evt
);
240 m_eventListener
.removeAllElements();
242 XComponent tdmgr
= null;
243 // dispose values, then service manager, then typdescription manager
244 Enumeration keys
= m_table
.keys();
245 while (keys
.hasMoreElements())
247 String name
= (String
)keys
.nextElement();
248 if (! name
.equals( SMGR_NAME
))
250 Object o
= m_table
.get( name
);
251 if (o
instanceof ComponentContextEntry
)
253 o
= ((ComponentContextEntry
)o
).m_value
;
256 XComponent xComp
= UnoRuntime
.queryInterface( XComponent
.class, o
);
259 if (name
.equals( TDMGR_NAME
))
275 XComponent xComp
= UnoRuntime
.queryInterface(
276 XComponent
.class, m_xSMgr
);
291 System
.err
.println( "... finished" );
293 //______________________________________________________________________________________________
294 public void addEventListener( XEventListener xListener
)
296 if (xListener
== null)
297 throw new com
.sun
.star
.uno
.RuntimeException( "Listener must not be null" );
298 if (m_eventListener
.contains( xListener
))
299 throw new com
.sun
.star
.uno
.RuntimeException( "Listener already registred." );
301 m_eventListener
.addElement( xListener
);
303 //______________________________________________________________________________________________
304 public void removeEventListener( XEventListener xListener
)
306 if (xListener
== null)
307 throw new com
.sun
.star
.uno
.RuntimeException( "Listener must not be null" );
308 if (! m_eventListener
.contains( xListener
))
309 throw new com
.sun
.star
.uno
.RuntimeException( "Listener is not registered." );
311 m_eventListener
.removeElement( xListener
);