All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.probes.api.KeyedProbesFactoryBuilder Maven / Gradle / Ivy

/**
 * Copyright (c) 2012-2016 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.probes.api;

import java.util.Iterator;
import java.util.ServiceLoader;

import org.ow2.petals.probes.api.exceptions.MultipleProbesFactoriesFoundException;
import org.ow2.petals.probes.api.exceptions.NoProbesFactoryFoundException;
import org.ow2.petals.probes.api.key.ProbeKey;

/**
 * Builds the keyed probes factory.
 * 
 * @param 
 *            Type of the key identifying each counter, implementing
 *            {@link ProbeKey}.
 * @param 
 *            The type of measured values (internal value type).
 * @param 
 *            The type of value to return to client probe (external value type).
 * 
 * @author Christophe DENEUX (Linagora)
 */
public class KeyedProbesFactoryBuilder, S> {

    /**
     * 

* Get the {@link KeyedProbesFactory} installed. *

*

* If the system property * org.ow2.petals.probes.api.KeyedProbesFactory is defined and * set, its value will be used as probes factory, otherwise, the probes * factory will be loaded from the service * org.ow2.petals.probes.api.KeyedProbesFactory *

* * @throws MultipleProbesFactoriesFoundException * Several probes factories are found. * @throws NoProbesFactoryFoundException * No probes factory is found. */ public KeyedProbesFactory getKeyedProbesFactory() throws MultipleProbesFactoriesFoundException, NoProbesFactoryFoundException { final ServiceLoader probesFactoryLoader = ServiceLoader .load(KeyedProbesFactory.class); final Iterator probesFactoryIterator = probesFactoryLoader.iterator(); if (probesFactoryIterator.hasNext()) { final KeyedProbesFactory probesFactory = probesFactoryIterator.next(); if (probesFactoryIterator.hasNext()) { throw new MultipleProbesFactoriesFoundException(); } return probesFactory; } else { throw new NoProbesFactoryFoundException(); } } /** * Get a specific {@link KeyedProbesFactory} installed. * * @throws NoProbesFactoryFoundException * The probes factory is not found. */ public KeyedProbesFactory getKeyedProbesFactory(final String probesFactoryName) throws NoProbesFactoryFoundException { final ServiceLoader probesFactoryLoader = ServiceLoader .load(KeyedProbesFactory.class); final Iterator probesFactoryIterator = probesFactoryLoader.iterator(); while (probesFactoryIterator.hasNext()) { final KeyedProbesFactory probesFactory = probesFactoryIterator.next(); if (probesFactory.getClass().getName().equals(probesFactoryName)) { return probesFactory; } } // No probes factory matching the provided name was found. throw new NoProbesFactoryFoundException(); } }