Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / javaunohelper / com / sun / star / comp / helper / ComponentContext.java
blob25cce2236c9a260f50c5f2f05c5892f486c3dbf5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package com.sun.star.comp.helper;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.Any;
32 import com.sun.star.uno.XComponentContext;
33 import com.sun.star.lang.XMultiComponentFactory;
34 import com.sun.star.lang.XSingleComponentFactory;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XEventListener;
37 import com.sun.star.lang.EventObject;
39 import java.util.Hashtable;
40 import java.util.Enumeration;
41 import java.util.Vector;
44 //==================================================================================================
45 class Disposer implements XEventListener
47 private XComponent m_xComp;
49 //----------------------------------------------------------------------------------------------
50 Disposer( XComponent xComp )
52 m_xComp = xComp;
54 //______________________________________________________________________________________________
55 public void disposing( EventObject Source )
57 m_xComp.dispose();
61 /** Component context implementation.
63 public class ComponentContext implements XComponentContext, XComponent
65 private static final boolean DEBUG = false;
66 private static final String SMGR_NAME = "/singletons/com.sun.star.lang.theServiceManager";
67 private static final String TDMGR_NAME = "/singletons/com.sun.star.reflection.theTypeDescriptionManager";
69 private Hashtable m_table;
70 private XComponentContext m_xDelegate;
72 private XMultiComponentFactory m_xSMgr;
73 private boolean m_bDisposeSMgr;
75 private Vector m_eventListener;
77 /** Ctor to create a component context passing a hashtable for values and a delegator
78 reference. Entries of the passed hashtable are either direct values or
79 ComponentContextEntry objects.
81 @param table
82 entries
83 @param xDelegate
84 if values are not found, request is delegated to this object
86 public ComponentContext( Hashtable table, XComponentContext xDelegate )
88 m_eventListener = new Vector();
89 m_table = table;
90 m_xDelegate = xDelegate;
91 m_xSMgr = null;
92 m_bDisposeSMgr = false;
94 Object o = table.get( SMGR_NAME );
95 if (o != null)
97 if (o instanceof ComponentContextEntry)
99 o = ((ComponentContextEntry)o).m_value;
101 m_xSMgr = UnoRuntime.queryInterface(
102 XMultiComponentFactory.class, o );
104 if (m_xSMgr != null)
106 m_bDisposeSMgr = true;
108 else if (m_xDelegate != null)
110 m_xSMgr = m_xDelegate.getServiceManager();
113 // listen for delegate
114 XComponent xComp = UnoRuntime.queryInterface(
115 XComponent.class, m_xDelegate );
116 if (xComp != null)
118 xComp.addEventListener( new Disposer( this ) );
122 // XComponentContext impl
123 //______________________________________________________________________________________________
124 public Object getValueByName( String rName )
126 Object o = m_table.get( rName );
127 if (o != null)
129 if (o instanceof ComponentContextEntry)
131 ComponentContextEntry entry = (ComponentContextEntry)o;
132 if (entry.m_lateInit != null)
134 Object xInstance = null;
138 String serviceName = (String)entry.m_lateInit;
139 if (serviceName != null)
141 if (m_xSMgr != null)
143 xInstance = m_xSMgr.createInstanceWithContext( serviceName, this );
145 else
147 if (DEBUG)
148 System.err.println( "### no service manager instance for late init of singleton instance \"" + rName + "\"!" );
151 else
153 XSingleComponentFactory xCompFac =
154 UnoRuntime.queryInterface(
155 XSingleComponentFactory.class, entry.m_lateInit );
156 if (xCompFac != null)
158 xInstance = xCompFac.createInstanceWithContext( this );
160 else
162 if (DEBUG)
163 System.err.println( "### neither service name nor service factory given for late init of singleton instance \"" + rName + "\"!" );
167 catch (com.sun.star.uno.Exception exc)
169 if (DEBUG)
170 System.err.println( "### exception occurred on late init of singleton instance \"" + rName + "\": " + exc.getMessage() );
173 if (xInstance != null)
175 synchronized (entry)
177 if (entry.m_lateInit != null)
179 entry.m_value = xInstance;
180 entry.m_lateInit = null;
182 else // inited in the meantime
184 // dispose fresh service instance
185 XComponent xComp = UnoRuntime.queryInterface(
186 XComponent.class, xInstance );
187 if (xComp != null)
189 xComp.dispose();
194 else
196 if (DEBUG)
197 System.err.println( "### failed late init of singleton instance \"" + rName + "\"!" );
200 return entry.m_value;
202 else // direct value in map
204 return o;
207 else if (m_xDelegate != null)
209 return m_xDelegate.getValueByName( rName );
211 else
213 return Any.VOID;
216 //______________________________________________________________________________________________
217 public XMultiComponentFactory getServiceManager()
219 return m_xSMgr;
222 // XComponent impl
223 //______________________________________________________________________________________________
224 public void dispose()
226 if (DEBUG)
227 System.err.print( "> disposing context " + this );
229 // fire events
230 EventObject evt = new EventObject( this );
231 Enumeration eventListener = m_eventListener.elements();
232 while (eventListener.hasMoreElements())
234 XEventListener listener = (XEventListener)eventListener.nextElement();
235 listener.disposing( evt );
237 m_eventListener.removeAllElements();
239 XComponent tdmgr = null;
240 // dispose values, then service manager, then typdescription manager
241 Enumeration keys = m_table.keys();
242 while (keys.hasMoreElements())
244 String name = (String)keys.nextElement();
245 if (! name.equals( SMGR_NAME ))
247 Object o = m_table.get( name );
248 if (o instanceof ComponentContextEntry)
250 o = ((ComponentContextEntry)o).m_value;
253 XComponent xComp = UnoRuntime.queryInterface( XComponent.class, o );
254 if (xComp != null)
256 if (name.equals( TDMGR_NAME ))
258 tdmgr = xComp;
260 else
262 xComp.dispose();
267 m_table.clear();
269 // smgr
270 if (m_bDisposeSMgr)
272 XComponent xComp = UnoRuntime.queryInterface(
273 XComponent.class, m_xSMgr );
274 if (xComp != null)
276 xComp.dispose();
279 m_xSMgr = null;
281 // tdmgr
282 if (tdmgr != null)
284 tdmgr.dispose();
287 if (DEBUG)
288 System.err.println( "... finished" );
290 //______________________________________________________________________________________________
291 public void addEventListener( XEventListener xListener )
293 if (xListener == null)
294 throw new com.sun.star.uno.RuntimeException( "Listener must not be null" );
295 if (m_eventListener.contains( xListener ))
296 throw new com.sun.star.uno.RuntimeException( "Listener already registred." );
298 m_eventListener.addElement( xListener );
300 //______________________________________________________________________________________________
301 public void removeEventListener( XEventListener xListener )
303 if (xListener == null)
304 throw new com.sun.star.uno.RuntimeException( "Listener must not be null" );
305 if (! m_eventListener.contains( xListener ))
306 throw new com.sun.star.uno.RuntimeException( "Listener is not registered." );
308 m_eventListener.removeElement( xListener );