Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pierre De Rop | f74a116 | 2009-12-04 22:40:38 +0000 | [diff] [blame^] | 19 | package org.apache.felix.dm.impl; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 20 | |
| 21 | import java.util.Dictionary; |
| 22 | |
| 23 | import org.osgi.framework.ServiceReference; |
| 24 | import org.osgi.framework.ServiceRegistration; |
| 25 | |
| 26 | /** |
| 27 | * A wrapper around a service registration that blocks until the |
| 28 | * service registration is available. |
| 29 | * |
| 30 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 31 | */ |
| 32 | public final class ServiceRegistrationImpl implements ServiceRegistration { |
| 33 | public static final ServiceRegistrationImpl ILLEGAL_STATE = new ServiceRegistrationImpl(); |
| 34 | private volatile ServiceRegistration m_registration; |
| 35 | |
| 36 | public ServiceRegistrationImpl() { |
| 37 | m_registration = null; |
| 38 | } |
| 39 | |
| 40 | public ServiceReference getReference() { |
| 41 | return ensureRegistration().getReference(); |
| 42 | } |
| 43 | |
| 44 | public void setProperties(Dictionary dictionary) { |
| 45 | ensureRegistration().setProperties(dictionary); |
| 46 | } |
| 47 | |
| 48 | public void unregister() { |
| 49 | ensureRegistration().unregister(); |
| 50 | } |
| 51 | |
| 52 | public boolean equals(Object obj) { |
| 53 | return ensureRegistration().equals(obj); |
| 54 | } |
| 55 | |
| 56 | public int hashCode() { |
| 57 | return ensureRegistration().hashCode(); |
| 58 | } |
| 59 | |
| 60 | public String toString() { |
| 61 | return ensureRegistration().toString(); |
| 62 | } |
| 63 | |
| 64 | private synchronized ServiceRegistration ensureRegistration() { |
| 65 | while (m_registration == null) { |
| 66 | try { |
| 67 | wait(); |
| 68 | } |
| 69 | catch (InterruptedException ie) { |
| 70 | // we were interrupted so hopefully we will now have a |
| 71 | // service registration ready; if not we wait again |
| 72 | } |
| 73 | } |
| 74 | // check if we're in an illegal state and throw an exception |
| 75 | if (ILLEGAL_STATE == m_registration) { |
| 76 | throw new IllegalStateException("Service is not registered."); |
| 77 | } |
| 78 | return m_registration; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the service registration and notifies all waiting parties. |
| 83 | */ |
| 84 | void setServiceRegistration(ServiceRegistration registration) { |
| 85 | synchronized (this) { |
| 86 | m_registration = registration; |
| 87 | notifyAll(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sets this wrapper to an illegal state, which will cause all threads |
| 93 | * that are waiting for this service registration to fail. |
| 94 | */ |
| 95 | void setIllegalState() { |
| 96 | setServiceRegistration(ServiceRegistrationImpl.ILLEGAL_STATE); |
| 97 | } |
| 98 | } |