org.ow2.petals.probes.api.ProbesFactoryBuilder 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;
/**
* Builds the probes factory.
*
* @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 ProbesFactoryBuilder, S> {
/**
*
* Get the {@link ProbesFactory} installed.
*
*
* If the system property
* org.ow2.petals.probes.api.ProbesFactory
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.ProbesFactory
*
*
* @throws MultipleProbesFactoriesFoundException
* Several probes factories are found.
* @throws NoProbesFactoryFoundException
* No probes factory is found.
*/
public ProbesFactory getProbesFactory() throws MultipleProbesFactoriesFoundException,
NoProbesFactoryFoundException {
final ServiceLoader probesFactoryLoader = ServiceLoader
.load(ProbesFactory.class);
final Iterator probesFactoryIterator = probesFactoryLoader.iterator();
if (probesFactoryIterator.hasNext()) {
final ProbesFactory probesFactory = probesFactoryIterator.next();
if (probesFactoryIterator.hasNext()) {
throw new MultipleProbesFactoriesFoundException();
}
return probesFactory;
} else {
throw new NoProbesFactoryFoundException();
}
}
/**
* Get a specific {@link ProbesFactory} installed.
*
* @throws NoProbesFactoryFoundException
* The probes factory is not found.
*/
public ProbesFactory getProbesFactory(final String probesFactoryName)
throws NoProbesFactoryFoundException {
final ServiceLoader probesFactoryLoader = ServiceLoader
.load(ProbesFactory.class);
final Iterator probesFactoryIterator = probesFactoryLoader.iterator();
while (probesFactoryIterator.hasNext()) {
final ProbesFactory probesFactory = probesFactoryIterator.next();
if (probesFactory.getClass().getName().equals(probesFactoryName)) {
return probesFactory;
}
}
// No probes factory matching the provided name was found.
throw new NoProbesFactoryFoundException();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy