1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.lib.uno.helper;
29 import com.sun.star.lang.XComponent;
30 import com.sun.star.lang.XEventListener;
31 import com.sun.star.lang.EventObject;
32 import com.sun.star.uno.Type;
33 
34 /** This class can be used as the base class for UNO components. In addition to the functionality ,which
35  *  is inherited from WeakBase, it implements com.sun.star.lang.XComponent.
36  */
37 public class ComponentBase extends WeakBase implements XComponent
38 {
39     private final boolean DEBUG= false;
40     protected MultiTypeInterfaceContainer listenerContainer;
41     protected boolean bInDispose= false;
42     protected boolean bDisposed= false;
43     static final Type  EVT_LISTENER_TYPE= new Type(XEventListener.class);
44 
45 
46     /** Creates a new instance of CompBase */
47     public ComponentBase()
48     {
49         super();
50         listenerContainer= new MultiTypeInterfaceContainer();
51     }
52 
53     /** Override to perform extra clean-up work. Provided for subclasses. It is
54         called during dispose()
55      */
56     protected void preDisposing()
57     {
58     }
59     /** Override to become notified right before the disposing action is performed.
60      */
61     protected void postDisposing()
62     {
63     }
64 
65 
66     /** Method of XComponent. It is called by the owning client when the component is not needed
67      *  anymore. The registered listeners are notified that this method has been called.
68      */
69     public void dispose()
70     {
71         // Determine in a thread-safe way if this is the first call to this method.
72         // Only then we proceed with the notification of event listeners.
73         // It is an error to call this method more then once.
74         boolean bDoDispose= false;
75         synchronized (this)
76         {
77             if ( ! bInDispose && ! bDisposed)
78             {
79                 bDoDispose= true;
80                 bInDispose= true;
81             }
82         }
83         // The notification occures in an unsynchronized block in order to avoid
84         // deadlocks if one of the listeners calls back in a different thread on
85         // a synchronized method which uses the same object.
86         if (bDoDispose)
87         {
88             try
89             {
90                 preDisposing();
91                 listenerContainer.disposeAndClear(new EventObject(this));
92                 //notify subclasses that disposing is in progress
93                 postDisposing();
94             }
95             finally
96             {
97                 // finally makes sure that the  flags are set even if a RuntimeException is thrown.
98                 // That ensures that this function is only called once.
99                 bDisposed= true;
100                 bInDispose= false;
101             }
102         }
103         else
104         {
105             // in a multithreaded environment, it can't be avoided, that dispose is called twice.
106             // However this condition is traced, because it MAY indicate an error.
107             if (DEBUG)
108                 System.out.println("OComponentHelper::dispose() - dispose called twice" );
109         }
110     }
111 
112     /** Method of XComponent.
113      */
114     public void removeEventListener(XEventListener xEventListener)
115     {
116         listenerContainer.removeInterface( EVT_LISTENER_TYPE, xEventListener);
117     }
118 
119     public void addEventListener(XEventListener listener)
120     {
121         boolean bDoDispose= false;
122         synchronized (this)
123         {
124             if (bDisposed || bInDispose)
125                 bDoDispose= true;
126             else
127                listenerContainer.addInterface(EVT_LISTENER_TYPE, listener);
128         }
129         if (bDoDispose )
130         {
131             listener.disposing( new EventObject(this));
132         }
133     }
134 
135     protected void finalize() throws Throwable
136     {
137         if ( ! bInDispose && ! bDisposed)
138             dispose();
139         super.finalize();
140     }
141 }
142