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 package com.sun.star.comp.helper;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30*cdf0e10cSrcweir import com.sun.star.uno.Any;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
33*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
34*cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
35*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
36*cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
37*cdf0e10cSrcweir import com.sun.star.lang.EventObject;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir import java.util.Hashtable;
40*cdf0e10cSrcweir import java.util.Enumeration;
41*cdf0e10cSrcweir import java.util.Vector;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //==================================================================================================
45*cdf0e10cSrcweir class Disposer implements XEventListener
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     private XComponent m_xComp;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     //----------------------------------------------------------------------------------------------
50*cdf0e10cSrcweir     Disposer( XComponent xComp )
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir         m_xComp = xComp;
53*cdf0e10cSrcweir     }
54*cdf0e10cSrcweir     //______________________________________________________________________________________________
55*cdf0e10cSrcweir     public void disposing( EventObject Source )
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir         m_xComp.dispose();
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir /** Component context implementation.
62*cdf0e10cSrcweir */
63*cdf0e10cSrcweir public class ComponentContext implements XComponentContext, XComponent
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir     private static final boolean DEBUG = false;
66*cdf0e10cSrcweir     private static final String SMGR_NAME = "/singletons/com.sun.star.lang.theServiceManager";
67*cdf0e10cSrcweir     private static final String TDMGR_NAME = "/singletons/com.sun.star.reflection.theTypeDescriptionManager";
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     private Hashtable m_table;
70*cdf0e10cSrcweir     private XComponentContext m_xDelegate;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     private XMultiComponentFactory m_xSMgr;
73*cdf0e10cSrcweir     private boolean m_bDisposeSMgr;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     private Vector m_eventListener;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     /** Ctor to create a component context passing a hashtable for values and a delegator
78*cdf0e10cSrcweir         reference. Entries of the passed hashtable are either direct values or
79*cdf0e10cSrcweir         ComponentContextEntry objects.
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir         @param table
82*cdf0e10cSrcweir                entries
83*cdf0e10cSrcweir         @param xDelegate
84*cdf0e10cSrcweir                if values are not found, request is delegated to this object
85*cdf0e10cSrcweir     */
86*cdf0e10cSrcweir     public ComponentContext( Hashtable table, XComponentContext xDelegate )
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         m_eventListener = new Vector();
89*cdf0e10cSrcweir         m_table = table;
90*cdf0e10cSrcweir         m_xDelegate = xDelegate;
91*cdf0e10cSrcweir         m_xSMgr = null;
92*cdf0e10cSrcweir         m_bDisposeSMgr = false;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         Object o = table.get( SMGR_NAME );
95*cdf0e10cSrcweir         if (o != null)
96*cdf0e10cSrcweir         {
97*cdf0e10cSrcweir             if (o instanceof ComponentContextEntry)
98*cdf0e10cSrcweir             {
99*cdf0e10cSrcweir                 o = ((ComponentContextEntry)o).m_value;
100*cdf0e10cSrcweir             }
101*cdf0e10cSrcweir             m_xSMgr = UnoRuntime.queryInterface(
102*cdf0e10cSrcweir                 XMultiComponentFactory.class, o );
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         if (m_xSMgr != null)
105*cdf0e10cSrcweir         {
106*cdf0e10cSrcweir             m_bDisposeSMgr = true;
107*cdf0e10cSrcweir         }
108*cdf0e10cSrcweir         else if (m_xDelegate != null)
109*cdf0e10cSrcweir         {
110*cdf0e10cSrcweir             m_xSMgr = m_xDelegate.getServiceManager();
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         // listen for delegate
114*cdf0e10cSrcweir         XComponent xComp = UnoRuntime.queryInterface(
115*cdf0e10cSrcweir             XComponent.class, m_xDelegate );
116*cdf0e10cSrcweir         if (xComp != null)
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             xComp.addEventListener( new Disposer( this ) );
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     // XComponentContext impl
123*cdf0e10cSrcweir     //______________________________________________________________________________________________
124*cdf0e10cSrcweir     public Object getValueByName( String rName )
125*cdf0e10cSrcweir     {
126*cdf0e10cSrcweir         Object o = m_table.get( rName );
127*cdf0e10cSrcweir         if (o != null)
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir             if (o instanceof ComponentContextEntry)
130*cdf0e10cSrcweir             {
131*cdf0e10cSrcweir                 ComponentContextEntry entry = (ComponentContextEntry)o;
132*cdf0e10cSrcweir                 if (entry.m_lateInit != null)
133*cdf0e10cSrcweir                 {
134*cdf0e10cSrcweir                     Object xInstance = null;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir                     try
137*cdf0e10cSrcweir                     {
138*cdf0e10cSrcweir                         String serviceName = (String)entry.m_lateInit;
139*cdf0e10cSrcweir                         if (serviceName != null)
140*cdf0e10cSrcweir                         {
141*cdf0e10cSrcweir                             if (m_xSMgr != null)
142*cdf0e10cSrcweir                             {
143*cdf0e10cSrcweir                                 xInstance = m_xSMgr.createInstanceWithContext( serviceName, this );
144*cdf0e10cSrcweir                             }
145*cdf0e10cSrcweir                             else
146*cdf0e10cSrcweir                             {
147*cdf0e10cSrcweir                                 if (DEBUG)
148*cdf0e10cSrcweir                                     System.err.println( "### no service manager instance for late init of singleton instance \"" + rName + "\"!" );
149*cdf0e10cSrcweir                             }
150*cdf0e10cSrcweir                         }
151*cdf0e10cSrcweir                         else
152*cdf0e10cSrcweir                         {
153*cdf0e10cSrcweir                             XSingleComponentFactory xCompFac =
154*cdf0e10cSrcweir                                 UnoRuntime.queryInterface(
155*cdf0e10cSrcweir                                     XSingleComponentFactory.class, entry.m_lateInit );
156*cdf0e10cSrcweir                             if (xCompFac != null)
157*cdf0e10cSrcweir                             {
158*cdf0e10cSrcweir                                 xInstance = xCompFac.createInstanceWithContext( this );
159*cdf0e10cSrcweir                             }
160*cdf0e10cSrcweir                             else
161*cdf0e10cSrcweir                             {
162*cdf0e10cSrcweir                                 if (DEBUG)
163*cdf0e10cSrcweir                                     System.err.println( "### neither service name nor service factory given for late init of singleton instance \"" + rName + "\"!" );
164*cdf0e10cSrcweir                             }
165*cdf0e10cSrcweir                         }
166*cdf0e10cSrcweir                     }
167*cdf0e10cSrcweir                     catch (com.sun.star.uno.Exception exc)
168*cdf0e10cSrcweir                     {
169*cdf0e10cSrcweir                         if (DEBUG)
170*cdf0e10cSrcweir                             System.err.println( "### exception occured on late init of singleton instance \"" + rName + "\": " + exc.getMessage() );
171*cdf0e10cSrcweir                     }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir                     if (xInstance != null)
174*cdf0e10cSrcweir                     {
175*cdf0e10cSrcweir                         synchronized (entry)
176*cdf0e10cSrcweir                         {
177*cdf0e10cSrcweir                             if (entry.m_lateInit != null)
178*cdf0e10cSrcweir                             {
179*cdf0e10cSrcweir                                 entry.m_value = xInstance;
180*cdf0e10cSrcweir                                 entry.m_lateInit = null;
181*cdf0e10cSrcweir                             }
182*cdf0e10cSrcweir                             else // inited in the meantime
183*cdf0e10cSrcweir                             {
184*cdf0e10cSrcweir                                 // dispose fresh service instance
185*cdf0e10cSrcweir                                 XComponent xComp = UnoRuntime.queryInterface(
186*cdf0e10cSrcweir                                     XComponent.class, xInstance );
187*cdf0e10cSrcweir                                 if (xComp != null)
188*cdf0e10cSrcweir                                 {
189*cdf0e10cSrcweir                                     xComp.dispose();
190*cdf0e10cSrcweir                                 }
191*cdf0e10cSrcweir                             }
192*cdf0e10cSrcweir                         }
193*cdf0e10cSrcweir                     }
194*cdf0e10cSrcweir                     else
195*cdf0e10cSrcweir                     {
196*cdf0e10cSrcweir                         if (DEBUG)
197*cdf0e10cSrcweir                             System.err.println( "### failed late init of singleton instance \"" + rName + "\"!" );
198*cdf0e10cSrcweir                     }
199*cdf0e10cSrcweir                 }
200*cdf0e10cSrcweir                 return entry.m_value;
201*cdf0e10cSrcweir             }
202*cdf0e10cSrcweir             else // direct value in map
203*cdf0e10cSrcweir             {
204*cdf0e10cSrcweir                 return o;
205*cdf0e10cSrcweir             }
206*cdf0e10cSrcweir         }
207*cdf0e10cSrcweir         else if (m_xDelegate != null)
208*cdf0e10cSrcweir         {
209*cdf0e10cSrcweir             return m_xDelegate.getValueByName( rName );
210*cdf0e10cSrcweir         }
211*cdf0e10cSrcweir         else
212*cdf0e10cSrcweir         {
213*cdf0e10cSrcweir             return Any.VOID;
214*cdf0e10cSrcweir         }
215*cdf0e10cSrcweir     }
216*cdf0e10cSrcweir     //______________________________________________________________________________________________
217*cdf0e10cSrcweir     public XMultiComponentFactory getServiceManager()
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         return m_xSMgr;
220*cdf0e10cSrcweir     }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     // XComponent impl
223*cdf0e10cSrcweir     //______________________________________________________________________________________________
224*cdf0e10cSrcweir     public void dispose()
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         if (DEBUG)
227*cdf0e10cSrcweir             System.err.print( "> disposing context " + this );
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir         // fire events
230*cdf0e10cSrcweir         EventObject evt = new EventObject( this );
231*cdf0e10cSrcweir         Enumeration eventListener = m_eventListener.elements();
232*cdf0e10cSrcweir         while (eventListener.hasMoreElements())
233*cdf0e10cSrcweir         {
234*cdf0e10cSrcweir             XEventListener listener = (XEventListener)eventListener.nextElement();
235*cdf0e10cSrcweir             listener.disposing( evt );
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir         m_eventListener.removeAllElements();
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         XComponent tdmgr = null;
240*cdf0e10cSrcweir         // dispose values, then service manager, then typdescription manager
241*cdf0e10cSrcweir         Enumeration keys = m_table.keys();
242*cdf0e10cSrcweir         while (keys.hasMoreElements())
243*cdf0e10cSrcweir         {
244*cdf0e10cSrcweir             String name = (String)keys.nextElement();
245*cdf0e10cSrcweir             if (! name.equals( SMGR_NAME ))
246*cdf0e10cSrcweir             {
247*cdf0e10cSrcweir                 Object o = m_table.get( name );
248*cdf0e10cSrcweir                 if (o instanceof ComponentContextEntry)
249*cdf0e10cSrcweir                 {
250*cdf0e10cSrcweir                     o = ((ComponentContextEntry)o).m_value;
251*cdf0e10cSrcweir                 }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir                 XComponent xComp = UnoRuntime.queryInterface( XComponent.class, o );
254*cdf0e10cSrcweir                 if (xComp != null)
255*cdf0e10cSrcweir                 {
256*cdf0e10cSrcweir                     if (name.equals( TDMGR_NAME ))
257*cdf0e10cSrcweir                     {
258*cdf0e10cSrcweir                         tdmgr = xComp;
259*cdf0e10cSrcweir                     }
260*cdf0e10cSrcweir                     else
261*cdf0e10cSrcweir                     {
262*cdf0e10cSrcweir                         xComp.dispose();
263*cdf0e10cSrcweir                     }
264*cdf0e10cSrcweir                 }
265*cdf0e10cSrcweir             }
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir         m_table.clear();
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir         // smgr
270*cdf0e10cSrcweir         if (m_bDisposeSMgr)
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir             XComponent xComp = UnoRuntime.queryInterface(
273*cdf0e10cSrcweir                 XComponent.class, m_xSMgr );
274*cdf0e10cSrcweir             if (xComp != null)
275*cdf0e10cSrcweir             {
276*cdf0e10cSrcweir                 xComp.dispose();
277*cdf0e10cSrcweir             }
278*cdf0e10cSrcweir         }
279*cdf0e10cSrcweir         m_xSMgr = null;
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         // tdmgr
282*cdf0e10cSrcweir         if (tdmgr != null)
283*cdf0e10cSrcweir         {
284*cdf0e10cSrcweir             tdmgr.dispose();
285*cdf0e10cSrcweir         }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir         if (DEBUG)
288*cdf0e10cSrcweir             System.err.println( "... finished" );
289*cdf0e10cSrcweir     }
290*cdf0e10cSrcweir     //______________________________________________________________________________________________
291*cdf0e10cSrcweir     public void addEventListener( XEventListener xListener )
292*cdf0e10cSrcweir     {
293*cdf0e10cSrcweir         if (xListener == null)
294*cdf0e10cSrcweir         	throw new com.sun.star.uno.RuntimeException( "Listener must not be null" );
295*cdf0e10cSrcweir   		if (m_eventListener.contains( xListener ))
296*cdf0e10cSrcweir   			throw new com.sun.star.uno.RuntimeException( "Listener already registred." );
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir        	m_eventListener.addElement( xListener );
299*cdf0e10cSrcweir     }
300*cdf0e10cSrcweir     //______________________________________________________________________________________________
301*cdf0e10cSrcweir     public void removeEventListener( XEventListener xListener )
302*cdf0e10cSrcweir     {
303*cdf0e10cSrcweir         if (xListener == null)
304*cdf0e10cSrcweir         	throw new com.sun.star.uno.RuntimeException( "Listener must not be null" );
305*cdf0e10cSrcweir   		if (! m_eventListener.contains( xListener ))
306*cdf0e10cSrcweir   			throw new com.sun.star.uno.RuntimeException( "Listener is not registered." );
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir         m_eventListener.removeElement( xListener );
309*cdf0e10cSrcweir     }
310*cdf0e10cSrcweir }
311