Package br.com.caelum.tubaina.resources

Source Code of br.com.caelum.tubaina.resources.ResourceLocator

package br.com.caelum.tubaina.resources;

import java.io.File;

import org.apache.log4j.Logger;

import br.com.caelum.tubaina.TubainaException;

public class ResourceLocator {

  private static final Logger LOG = Logger.getLogger(ResourceLocator.class);
  private static ResourceLocator locator;
  private final File rootDir;
 
  private ResourceLocator(File rootDir) {
    this.rootDir = rootDir;
  }
 
  public static void initialize(String rootPath) {
    initialize(new File(rootPath));
  }
 
  public static void initialize(File rootDir) {
    if (locator != null) {
      LOG.warn("Locator has already been initialized");
    }
    if(!rootDir.exists()) {
      throw new RuntimeException("Resource directory does not exist: " + rootDir.getAbsolutePath());
    }
    LOG.info("Initializing ResourceLocator at dir: " + rootDir.getAbsolutePath());
    locator = new ResourceLocator(rootDir);
  }

  public static ResourceLocator getInstance() {
    if (locator == null) {
      throw new TubainaException("ResourceLocator has not been initialized");
    }
    return locator;
  }
 
  public File getFile(String path) {
    File file = new File(rootDir, path);
    if (!file.exists()) {
      LOG.warn("File requested: '" + path + "' doesn't exist");
    }
    return file;
  }
}
TOP

Related Classes of br.com.caelum.tubaina.resources.ResourceLocator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.