simplify writeBitmapObject
[LibreOffice.git] / ridljar / com / sun / star / comp / helper / ComponentContext.java
blob34649d563395e7aef98c258b56aed802c8dd4690
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package com.sun.star.comp.helper;
20 import com.sun.star.uno.UnoRuntime;
21 import com.sun.star.uno.Any;
23 import com.sun.star.uno.DeploymentException;
24 import com.sun.star.uno.XComponentContext;
25 import com.sun.star.lang.XMultiComponentFactory;
26 import com.sun.star.lang.XSingleComponentFactory;
27 import com.sun.star.lang.XComponent;
28 import com.sun.star.lang.XEventListener;
29 import com.sun.star.lang.EventObject;
31 import java.util.ArrayList;
32 import java.util.Hashtable;
33 import java.util.Map;
37 class Disposer implements XEventListener
39 private final XComponent m_xComp;
42 Disposer( XComponent xComp )
44 m_xComp = xComp;
47 public void disposing( EventObject Source )
49 m_xComp.dispose();
53 /** Component context implementation.
55 public class ComponentContext implements XComponentContext, XComponent
57 private static final boolean DEBUG = false;
58 private static final String SMGR_NAME = "/singletons/com.sun.star.lang.theServiceManager";
59 private static final String TDMGR_NAME = "/singletons/com.sun.star.reflection.theTypeDescriptionManager";
61 private Map<String,Object> m_table;
62 private XComponentContext m_xDelegate;
64 private XMultiComponentFactory m_xSMgr;
65 private boolean m_bDisposeSMgr;
67 private ArrayList<XEventListener> m_eventListener;
69 public ComponentContext( Hashtable<String,Object> table, XComponentContext xDelegate )
71 this((Map<String,Object>) table, xDelegate);
74 /** Ctor to create a component context passing a hashtable for values and a delegator
75 reference. Entries of the passed hashtable are either direct values or
76 ComponentContextEntry objects.
78 @param table
79 entries
80 @param xDelegate
81 if values are not found, request is delegated to this object
83 public ComponentContext( Map<String,Object> table, XComponentContext xDelegate )
85 m_eventListener = new ArrayList<XEventListener>();
86 m_table = table;
87 m_xDelegate = xDelegate;
88 m_xSMgr = null;
89 m_bDisposeSMgr = false;
91 Object o = table.get( SMGR_NAME );
92 if (o != null)
94 if (o instanceof ComponentContextEntry)
96 o = ((ComponentContextEntry)o).m_value;
98 m_xSMgr = UnoRuntime.queryInterface(
99 XMultiComponentFactory.class, o );
101 if (m_xSMgr != null)
103 m_bDisposeSMgr = true;
105 else if (m_xDelegate != null)
107 m_xSMgr = m_xDelegate.getServiceManager();
110 // listen for delegate
111 XComponent xComp = UnoRuntime.queryInterface(
112 XComponent.class, m_xDelegate );
113 if (xComp != null)
115 xComp.addEventListener( new Disposer( this ) );
119 // XComponentContext impl
121 public Object getValueByName( String rName )
123 Object o = m_table.get( rName );
124 if (o == null)
126 if (m_xDelegate != null)
128 return m_xDelegate.getValueByName( rName );
130 else
132 return Any.VOID;
136 if (!(o instanceof ComponentContextEntry))
138 // direct value in map
139 return o;
142 ComponentContextEntry entry = (ComponentContextEntry)o;
143 if (entry.m_lateInit == null)
145 return entry.m_value;
148 Object xInstance = null;
151 String serviceName = (String)entry.m_lateInit;
152 if (serviceName != null)
154 if (m_xSMgr != null)
156 xInstance = m_xSMgr.createInstanceWithContext( serviceName, this );
158 else
160 if (DEBUG)
161 System.err.println( "### no service manager instance for late init of singleton instance \"" + rName + "\"!" );
164 else
166 XSingleComponentFactory xCompFac = UnoRuntime.queryInterface( XSingleComponentFactory.class, entry.m_lateInit );
167 if (xCompFac != null)
169 xInstance = xCompFac.createInstanceWithContext( this );
171 else
173 if (DEBUG)
174 System.err.println( "### neither service name nor service factory given for late init of singleton instance \"" + rName + "\"!" );
178 catch (com.sun.star.uno.Exception exc)
180 if (DEBUG)
181 System.err.println( "### exception occurred on late init of singleton instance \"" + rName + "\": " + exc.getMessage() );
184 if (xInstance != null)
186 synchronized (entry)
188 if (entry.m_lateInit != null)
190 entry.m_value = xInstance;
191 entry.m_lateInit = null;
193 else // inited in the meantime
195 // dispose fresh service instance
196 XComponent xComp = UnoRuntime.queryInterface(
197 XComponent.class, xInstance );
198 if (xComp != null)
200 xComp.dispose();
205 else
207 if (DEBUG)
208 System.err.println( "### failed late init of singleton instance \"" + rName + "\"!" );
210 return entry.m_value;
213 public XMultiComponentFactory getServiceManager()
215 if (m_xSMgr == null)
217 throw new DeploymentException(
218 "null component context service manager" );
220 return m_xSMgr;
223 // XComponent impl
225 public void dispose()
227 if (DEBUG)
228 System.err.print( "> disposing context " + this );
230 // fire events
231 EventObject evt = new EventObject( this );
232 for (XEventListener listener : m_eventListener)
234 listener.disposing( evt );
236 m_eventListener.clear();
238 XComponent tdmgr = null;
239 // dispose values, then service manager, then typedescription manager
240 for (Map.Entry<String, Object> entry : m_table.entrySet())
242 String name = entry.getKey();
243 if (! name.equals( SMGR_NAME ))
245 Object o = entry.getValue();
246 if (o instanceof ComponentContextEntry)
248 o = ((ComponentContextEntry)o).m_value;
251 XComponent xComp = UnoRuntime.queryInterface( XComponent.class, o );
252 if (xComp != null)
254 if (name.equals( TDMGR_NAME ))
256 tdmgr = xComp;
258 else
260 xComp.dispose();
265 m_table.clear();
267 // smgr
268 if (m_bDisposeSMgr)
270 XComponent xComp = UnoRuntime.queryInterface(
271 XComponent.class, m_xSMgr );
272 if (xComp != null)
274 xComp.dispose();
277 m_xSMgr = null;
279 // tdmgr
280 if (tdmgr != null)
282 tdmgr.dispose();
285 if (DEBUG)
286 System.err.println( "... finished" );
289 public void addEventListener( XEventListener xListener )
291 if (xListener == null)
292 throw new com.sun.star.uno.RuntimeException( "Listener must not be null" );
293 if (m_eventListener.contains( xListener ))
294 throw new com.sun.star.uno.RuntimeException( "Listener already registered." );
296 m_eventListener.add( xListener );
299 public void removeEventListener( XEventListener xListener )
301 if (xListener == null)
302 throw new com.sun.star.uno.RuntimeException( "Listener must not be null" );
303 if (! m_eventListener.contains( xListener ))
304 throw new com.sun.star.uno.RuntimeException( "Listener is not registered." );
306 m_eventListener.remove( xListener );