| 1 | /* |
| 2 | * $Id: ListenerCookieTable.java,v 1.2 2005/12/01 06:10:00 hastings Exp $ |
| 3 | * |
| 4 | * Copyright (c) 2004, Moebius Solutions, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * Redistributions in binary form must reproduce the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer in the documentation and/or other materials provided |
| 17 | * with the distribution. |
| 18 | * |
| 19 | * Neither the name of Moebius Solutions, Inc. nor the names of |
| 20 | * its contributors may be used to endorse or promote products |
| 21 | * derived from this software without specific prior written |
| 22 | * permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 33 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 35 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | package com.moesol.bindings.platform_sdk.component_services; |
| 38 | |
| 39 | import java.util.Hashtable; |
| 40 | |
| 41 | import com.moesol.bindings.platform_sdk.component_services.IUnknown.Jni; |
| 42 | |
| 43 | /** |
| 44 | * Manage the connection point cookies for an object. These tables cannot be |
| 45 | * fields in the java interface objects because two java objects may refer to |
| 46 | * the same COM object. |
| 47 | * |
| 48 | * @author Hastings |
| 49 | */ |
| 50 | public class ListenerCookieTable { |
| 51 | |
| 52 | /** |
| 53 | * Exposed for unit tests. |
| 54 | * |
| 55 | * @return number of cookies in the cookie table. |
| 56 | */ |
| 57 | public int _getListenerListSize() { |
| 58 | return m_listener_cookie_table.size(); |
| 59 | } |
| 60 | |
| 61 | void addCookiePairToTable(Object o, int cookie) { |
| 62 | if (o == null) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | m_listener_cookie_table.put(o, new Integer(cookie)); |
| 67 | } |
| 68 | |
| 69 | int removeListenerAndCookieFromTable(Jni unk, Object o) { |
| 70 | if (o == null) { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | synchronized (m_listener_cookie_table) { |
| 75 | Integer cookie = (Integer)m_listener_cookie_table.remove(o); |
| 76 | cleanUpMap(unk); |
| 77 | if (cookie == null) { |
| 78 | throw new IllegalStateException("Cookie cannot be null"); |
| 79 | } |
| 80 | return cookie.intValue(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private void cleanUpMap(Jni unk) { |
| 85 | if (m_listener_cookie_table.size() == 0) { |
| 86 | EventSourceToListenerCookieTable.singleton().removeListenerCookieTable(unk); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private Hashtable m_listener_cookie_table = new Hashtable(); |
| 91 | } |