diff options
Diffstat (limited to 'bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxFactory.java')
-rw-r--r-- | bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxFactory.java | 95 |
1 files changed, 78 insertions, 17 deletions
diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxFactory.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxFactory.java index fdf94297..e9736f6d 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxFactory.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxFactory.java @@ -17,6 +17,7 @@ package at.gv.egiz.bku.slcommands.impl; import java.util.HashMap; +import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -38,24 +39,68 @@ public class InfoboxFactory { private static Log log = LogFactory.getLog(InfoboxFactory.class); /** - * The mapping of Infobox name to concrete Infobox factory. + * The singleton instance of this InfoboxFactory. */ - private HashMap<String, AbstractInfoboxFactory> infoboxFactories = new HashMap<String, AbstractInfoboxFactory>(); - + private static InfoboxFactory instance; + /** - * @param infoboxFactories the infoboxFactories to set + * @return an instance of this InfoboxFactory */ - public void setInfoboxFactories( - HashMap<String, AbstractInfoboxFactory> factories) { - if (log.isDebugEnabled()) { - StringBuilder sb = new StringBuilder(); - sb.append("Registered infobox factories for"); - for (String name : factories.keySet()) { - sb.append("\n " + name + " : " + factories.get(name).getClass()); - } - log.debug(sb); + public synchronized static InfoboxFactory getInstance() { + if (instance == null) { + instance = new InfoboxFactory(); + } + return instance; + } + + /** + * The mapping of infobox identifier to implementation class. + */ + private HashMap<String, Class<? extends Infobox>> implementations; + + /** + * Private constructor. + */ + private InfoboxFactory() { + } + + /** + * Sets the mapping of infobox identifier to implementation class name. + * + * @param infoboxImplMap + * a mapping of infobox identifiers to implementation class names + * + * @throws ClassNotFoundException + * if implementation class is not an instance of {@link Infobox} + */ + @SuppressWarnings("unchecked") + public void setInfoboxImpl(Map<String, String> infoboxImplMap) throws ClassNotFoundException { + HashMap<String, Class<? extends Infobox>> implMap = new HashMap<String, Class<? extends Infobox>>(); + ClassLoader cl = getClass().getClassLoader(); + for (String key : infoboxImplMap.keySet()) { + Class<? extends Infobox> impl = (Class<? extends Infobox>) cl.loadClass(infoboxImplMap.get(key)); + log.debug("Registering infobox '" + key + "' implementation '" + impl.getCanonicalName() + "'."); + implMap.put(key, impl); + } + implementations = implMap; + } + + /** + * Returns the configured implementation class for the given + * <code>infoboxIdentifier</code>. + * + * @param infoboxIdentifier + * the infobox identifier + * + * @return the implementation class for the given infobox identifier or + * <code>null</code> if there is no implementation class configured + */ + public Class<? extends Infobox> getImplClass(String infoboxIdentifier) { + if (implementations != null) { + return implementations.get(infoboxIdentifier); + } else { + return null; } - this.infoboxFactories = factories; } /** @@ -74,15 +119,31 @@ public class InfoboxFactory { */ public Infobox createInfobox(String infoboxIdentifier) throws SLCommandException, SLRuntimeException { - AbstractInfoboxFactory factory = infoboxFactories.get(infoboxIdentifier); - if (factory == null) { + Class<? extends Infobox> implClass = getImplClass(infoboxIdentifier); + if (implClass == null) { + // infobox not supported log.info("Unsupported infobox '" + infoboxIdentifier + "."); throw new SLCommandException(4002, SLExceptionMessages.EC4002_INFOBOX_UNKNOWN, new Object[] { infoboxIdentifier }); } - return factory.createInfobox(); + // try to instantiate + Infobox infobox; + try { + infobox = implClass.newInstance(); + log.debug("Infobox '" + infobox.getIdentifier() + "' created."); + } catch (InstantiationException e) { + // unexpected error + log.error("Failed to instantiate infobox implementation.", e); + throw new SLRuntimeException(e); + } catch (IllegalAccessException e) { + // unexpected error + log.error("Failed to instantiate infobox implementation.", e); + throw new SLRuntimeException(e); + } + + return infobox; } |