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

com.kryshchuk.imcollector.dao.FactoryBuilder Maven / Gradle / Ivy

The newest version!
/*
 * imcollector DAO
 * Copyright (C) 2013  Yuriy Kryshchuk
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package com.kryshchuk.imcollector.dao;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yura
 * @since 1.0
 */
public class FactoryBuilder {

  private static final String FACTORY_CLASS_NAME = "factoryClassName";

  private static final Logger LOGGER = LoggerFactory.getLogger(FactoryBuilder.class);

  private static final Map FACTORIES = new HashMap<>();

  private static final ReentrantReadWriteLock FACTORIES_LOCK = new ReentrantReadWriteLock();

  /**
   * Hidden utility constructor.
   */
  private FactoryBuilder() {
  }

  public static  F getFactory(final String name, final Class factoryClass) {
    final Factory f = getCachedFactory(name);
    if (factoryClass.isInstance(f)) {
      return factoryClass.cast(f); // TODO double invocation of isInstance
    } else {
      LOGGER.warn("Cached factory {} ({}) does not match expected {}", name, f.getClass(), factoryClass);
      FACTORIES_LOCK.writeLock().lock();
      try {
        FACTORIES.remove(name);
      } finally {
        FACTORIES_LOCK.writeLock().unlock();
      }
      return factoryClass.cast(getCachedFactory(name));
    }
  }

  private static Factory getCachedFactory(final String name) {
    FACTORIES_LOCK.readLock().lock();
    final Factory f1 = FACTORIES.get(name);
    FACTORIES_LOCK.readLock().unlock();
    if (f1 == null) {
      FACTORIES_LOCK.writeLock().lock();
      try {
        final Factory f2 = FACTORIES.get(name);
        if (f2 == null) {
          final String resourceName = String.format("META-INF/%1$s.dao.properties", name);
          try {
            final Enumeration urls = Thread.currentThread().getContextClassLoader().getResources(resourceName);
            if (urls.hasMoreElements()) {
              final URL daoPropertiesResource = urls.nextElement();
              try (final InputStream s = daoPropertiesResource.openStream()) {
                final Properties providerProperties = new Properties();
                providerProperties.load(s);
                LOGGER.info("Found Factory Propertis {} for {}", providerProperties, name);
                final String className = providerProperties.getProperty(FACTORY_CLASS_NAME);
                final Factory f = instantiateFactory(className);
                if (f != null) {
                  FACTORIES.put(name, f);
                  return f;
                } else {
                  LOGGER.error("Unable to instantiate Factory for {} with {}", name, providerProperties);
                }
              } catch (final IOException e) {
                LOGGER.error("Failed to load Factory Properties {}", daoPropertiesResource, e);
              }
              while (urls.hasMoreElements()) {
                final URL anotherPropertiesResource = urls.nextElement();
                LOGGER.warn("Found another Factory ({}) Properties: {}", name, anotherPropertiesResource);
              }
            } else {
              LOGGER.error("Could not find any resource by {}", resourceName);
            }
          } catch (final IOException e) {
            LOGGER.error("Cannot enumerate resources by {}", resourceName, e);
          }
          throw new IllegalStateException("Cannot instantiate Factory for " + name);
        } else {
          return f2;
        }
      } finally {
        FACTORIES_LOCK.writeLock().unlock();
      }
    } else {
      return f1;
    }
  }

  private static Factory instantiateFactory(final String className) {
    try {
      final Class aClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
      final Class factoryClass = aClass.asSubclass(Factory.class);
      return factoryClass.newInstance();
    } catch (final ClassNotFoundException e) {
      LOGGER.error("Cannot find Factory class {}", className, e);
    } catch (final IllegalAccessException e) {
      LOGGER.error("Factory class {} is not accessible", className, e);
    } catch (final InstantiationException e) {
      LOGGER.error("Failed to instantiate Factory class {}", className, e);
    }
    return null;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy