com.hfg.bio.proteinproperty.ProteinPropertyServiceLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.bio.proteinproperty;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
//------------------------------------------------------------------------------
/**
Because we have static class instances extending SimpleProteinProperty we
can't use ServiceLocator directly. This is a variant of ServiceLocator that
just calls Class.forName() on the extending classes so that their values will
be populated for subsequent calls to values().
* @author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg Library
//
// This 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 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 library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
class ProteinPropertyServiceLoader
{
private static Logger sLogger;
static
{
String loggerName = ProteinPropertyServiceLoader.class.getPackage().getName();
LogManager logManager = LogManager.getLogManager();
// This method call finds the logger, but won't create one if it doesn't exist.
sLogger = logManager.getLogger(loggerName);
if (null == sLogger)
{
// Create the new logger. We won't be overwriting settings specified elsewhere.
sLogger = Logger.getLogger(loggerName);
sLogger.setLevel(Level.WARNING);
sLogger.setUseParentHandlers(true);
}
}
//---------------------------------------------------------------------------
public void load()
throws Exception
{
getLogger().log(Level.FINE, "Loading ProteinProperty values...");
String fullName = "META-INF/services/" + SimpleProteinProperty.class.getName();
Enumeration configs = getClass().getClassLoader().getResources(fullName);
while (configs.hasMoreElements())
{
Iterator classnames = parse(configs.nextElement());
while (classnames.hasNext())
{
try
{
String classname = classnames.next();
getLogger().log(Level.FINE, "Initializing " + classname + "...");
Class clazz = Class.forName(classname);
getLogger().fine(clazz.getSimpleName() + " initialized ***************************************");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
//---------------------------------------------------------------------------
public static Logger getLogger()
{
return sLogger;
}
//---------------------------------------------------------------------------
private Iterator parse(URL u)
throws ServiceConfigurationError
{
InputStream in = null;
BufferedReader r = null;
ArrayList names = new ArrayList<>();
try
{
in = u.openStream();
r = new BufferedReader(new InputStreamReader(in, "utf-8"));
int lc = 1;
while ((lc = parseLine(r, lc, names)) >= 0);
}
catch (IOException x)
{
throw new RuntimeException("Error reading configuration file", x);
}
finally
{
try
{
if (r != null) r.close();
if (in != null) in.close();
}
catch (IOException y)
{
throw new RuntimeException("Error closing configuration file", y);
}
}
return names.iterator();
}
//---------------------------------------------------------------------------
private int parseLine(BufferedReader r, int lc, List names)
throws IOException, ServiceConfigurationError
{
String ln = r.readLine();
if (ln == null)
{
return -1;
}
int ci = ln.indexOf('#');
if (ci >= 0) ln = ln.substring(0, ci);
ln = ln.trim();
int n = ln.length();
if (n != 0) {
if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
throw new RuntimeException("Illegal configuration-file syntax");
int cp = ln.codePointAt(0);
if (!Character.isJavaIdentifierStart(cp))
throw new RuntimeException("Illegal provider-class name: " + ln);
for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
cp = ln.codePointAt(i);
if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
throw new RuntimeException("Illegal provider-class name: " + ln);
}
if (!names.contains(ln))
names.add(ln);
}
return lc + 1;
}
}