ServicePropertyAccessor.java revision 660a40ad15749d74efa0dd4ef12cb8781c570e22
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos/*
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos *
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * Copyright (c) 2012 ForgeRock AS. All Rights Reserved
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos *
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 *
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * You can obtain a copy of the License at
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * http://forgerock.org/license/CDDLv1.0.html
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * See the License for the specific language governing
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * permission and limitations under the License.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos *
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 */
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordospackage org.forgerock.openidm.core;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordosimport java.util.Map;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordosimport org.osgi.framework.BundleContext;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordosimport org.osgi.framework.ServiceReference;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordosimport org.osgi.util.tracker.ServiceTracker;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos/**
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * A ServicePropertyAccessor tracks services with
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * {@code ServiceTracker<Map<String, Object>, Map<String, Object>>} and gets the
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * Properties.
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos *
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * @author Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos */
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordospublic class ServicePropertyAccessor implements PropertyAccessor {
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos private final PropertyAccessor delegate;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos private final BundleContext context;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos private final ServiceTracker<Map<String, Object>, Map<String, Object>> serviceTracker;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos public ServicePropertyAccessor(final BundleContext context,
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos final ServiceTracker<Map<String, Object>, Map<String, Object>> serviceTracker,
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos final PropertyAccessor delegate) {
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos assert null != context;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos this.delegate = delegate;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos this.context = context;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos this.serviceTracker = serviceTracker;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos }
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos /**
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 *
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos * @param key
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.
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 */
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos public <T> T getProperty(String key, T defaultValue, Class<T> expected) {
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos T value = null;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos ServiceReference<Map<String, Object>>[] references = serviceTracker.getServiceReferences();
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos if (null != references) {
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos int i = 0;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos while (i < references.length) {
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 value = (T) candidateValue;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos break;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos }
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos i++;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos }
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos }
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos return null != value ? value : (null != delegate) ? delegate.getProperty(key, defaultValue,
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos expected) : null;
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos }
660a40ad15749d74efa0dd4ef12cb8781c570e22Laszlo Hordos}