660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos * Copyright (c) 2011-2013 ForgeRock AS. All Rights Reserved
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * The contents of this file are subject to the terms
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * of the Common Development and Distribution License
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * (the License). You may not use this file except in
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * compliance with the License.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * You can obtain a copy of the License at
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * See the License for the specific language governing
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * permission and limitations under the License.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * When distributing Covered Code, include this CDDL
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * Header Notice in each file and include the License file
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * at http://forgerock.org/license/CDDLv1.0.html
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * If applicable, add the following below the CDDL Header,
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * with the fields enclosed by brackets [] replaced by
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * your own identifying information:
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * "Portions Copyrighted [year] [name of copyright owner]"
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * A ServicePropertyAccessor tracks services with
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * {@code ServiceTracker<Map<String, Object>, Map<String, Object>>} and gets the
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * Properties.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordospublic class ServicePropertyAccessor implements PropertyAccessor {
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos private final ServiceTracker<Map<String, Object>, Map<String, Object>> serviceTracker;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos public ServicePropertyAccessor(final BundleContext context,
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos final ServiceTracker<Map<String, Object>, Map<String, Object>> serviceTracker,
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * Returns the value of the specified property. If the key is not found in
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * the tracked services, the {@code delegate} is then requested. The method
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * returns {@code null} if the property is not found.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * The name of the requested property.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * @param defaultValue
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * The value to return if the property is not found.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * @param expected
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * The type of the expected value.
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos * The type of the expected property.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * @return The value of the requested property, or {@code null} if the
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * property is undefined.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * @throws SecurityException
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * If the caller does not have the appropriate
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * {@code PropertyPermission} to read the property, and the Java
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * Runtime Environment supports permissions.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos public <T> T getProperty(String key, T defaultValue, Class<T> expected) {
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos ServiceReference<Map<String, Object>>[] references = serviceTracker.getServiceReferences();
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos Object candidateValue = context.getService(references[i]).get(key);
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos if (null != expected && null != candidateValue
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos && expected.isAssignableFrom(candidateValue.getClass())) {
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos // We found the value
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos return null != value ? value : (null != delegate) ? delegate.getProperty(key, defaultValue,