loci.common.services.ServiceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ome-common Show documentation
Show all versions of ome-common Show documentation
Contains common I/O, date parsing, and XML processing classes.
The newest version!
/*
* #%L
* Common package for I/O and related utilities
* %%
* Copyright (C) 2005 - 2016 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package loci.common.services;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Runtime instantiation of services.
*/
public class ServiceFactory {
/** Logger for this class. */
private static final Logger LOGGER =
LoggerFactory.getLogger(ServiceFactory.class);
/** Default service properties file. */
private static final String DEFAULT_PROPERTIES_FILE = "services.properties";
/** Constructor cache. */
private static Map, Constructor extends Service>>
constructorCache =
new HashMap, Constructor extends Service>>();
/**
* Set of available services.
*
* This field is initialized on first usage since if this class has been deserialized,
* the transient flag will have led to it being null again.
*/
private transient Map, Class extends Service>>
services = null;
/**
* Constructor argument passed.
*
* If null, then the default constructor was used. This information is used to
* recreate the services instance.
*/
private String path;
/** Default service factory. */
private static ServiceFactory defaultFactory;
/**
* Constructor loading service configuration from the default location.
* @throws DependencyException If there is an error locating or reading from
* the default configuration location.
*/
public ServiceFactory() throws DependencyException {
this.path = null;
if (defaultFactory == null) {
defaultFactory = new ServiceFactory(DEFAULT_PROPERTIES_FILE);
}
services();
}
/**
* Constructor loading service configuration from a given location.
* @param path Location to load service configuration from.
* @throws DependencyException If there is an error locating or reading from
* path
.
*/
public ServiceFactory(String path) throws DependencyException {
this.path = path;
services();
}
/**
* Common constructor code which dispatches based on the state of
* the path field. This is *not* called during construction, but
* rather on the first call to services() since the same logic is
* needed in the deserialization code path. This way, it's only called
* once.
*/
private static void init(String path,
Map, Class extends Service>> services)
throws DependencyException {
// Matches the default constructor
if (path == null) {
synchronized (defaultFactory) {
services.putAll(defaultFactory.services);
}
return; // EARLY EXIT
}
// Now handle the (String path) constructor.
InputStream stream = ServiceFactory.class.getResourceAsStream(path);
Properties properties = new Properties();
if (stream == null) {
throw new DependencyException(path + " not found on CLASSPATH");
}
try {
properties.load(stream);
LOGGER.debug("Loaded properties from: {}", path);
} catch (Throwable t) {
throw new DependencyException(t);
}
finally {
try {
stream.close();
}
catch (IOException e) {
LOGGER.warn("Error closing properties file stream.", e);
}
}
Set> entries = properties.entrySet();
for (Entry