首页

分享定义ResourceBuilder通用资源构件器可以访问常用的资源(文件、http、https及class等资源Resource)的设计源码

标签:资源构件器,ResourceBuilder,dbcon5.0.1,系统设计,源码     发布时间:2018-01-21   

一、前言

下面基于dbcon-5.0.1.jar开源包中通用资源构件器uk.ac.sanger.cgp.dbcon.util.resources.ResourceBuilder实现常见资源文件资源FileResource、http资源UrlResource、https资源及类资源ClasspathResource的统一访问,并通用通过返回java.io.InputStream流、字符串的方式返回结果。

二、源码说明

1.ResourceBuilder统一资源构造器

package uk.ac.sanger.cgp.dbcon.util.resources;@b@@b@import uk.ac.sanger.cgp.dbcon.exceptions.DbConException;@b@@b@public class ResourceBuilder@b@{@b@  public static Resource getResource(String location)@b@  {@b@    Resource resource = null;@b@@b@    if (location.startsWith("file")) {@b@      resource = new FileResource(location);@b@    }@b@    else if (location.startsWith("http")) {@b@      resource = new UrlResource(location);@b@    }@b@    else if (location.startsWith("https")) {@b@      resource = new UrlResource(location);@b@    }@b@    else if (location.startsWith("/")) {@b@      resource = new ClasspathResource(location);@b@    }@b@    else@b@      throw new DbConException("Unknown resource protocol for " + location);@b@@b@    return resource;@b@  }@b@}

2.Resource接口、AbstractResource公共资源抽象类

package uk.ac.sanger.cgp.dbcon.util.resources;@b@@b@import java.io.InputStream;@b@@b@public abstract interface Resource@b@{@b@  public abstract InputStream getInputStream();@b@@b@  public abstract String getLocation();@b@}
package uk.ac.sanger.cgp.dbcon.util.resources;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import org.apache.commons.io.IOUtils;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import uk.ac.sanger.cgp.dbcon.exceptions.DbConException;@b@import uk.ac.sanger.cgp.dbcon.util.InputOutputUtils;@b@@b@public abstract class AbstractResource@b@  implements Resource@b@{@b@  private Log log = LogFactory.getLog(super.getClass());@b@  private final String location;@b@  private byte[] output;@b@@b@  public AbstractResource(String location)@b@  {@b@    this.location = location;@b@  }@b@@b@  public String getLocation() {@b@    return this.location;@b@  }@b@@b@  public InputStream getInputStream()@b@  {@b@    return new ByteArrayInputStream(getByteArray());@b@  }@b@@b@  protected Log getLog() {@b@    return this.log;@b@  }@b@@b@  protected abstract InputStream getActualInputStream();@b@@b@  private byte[] getByteArray()@b@  {@b@    if (this.output == null) {@b@      InputStream is = getActualInputStream();@b@      try {@b@        this.output = IOUtils.toByteArray(is);@b@      }@b@      catch (IOException e)@b@      {@b@      }@b@      finally@b@      {@b@        InputOutputUtils.closeQuietly(is);@b@      }@b@      if (this.output == null)@b@        throw new DbConException("The streamed byte[] from resource " + getLocation() + " was null");@b@    }@b@@b@    return this.output;@b@  }@b@}

3.文件资源FileResource、http资源UrlResource、https资源及类资源ClasspathResource

package uk.ac.sanger.cgp.dbcon.util.resources;@b@@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.InputStream;@b@import java.net.URI;@b@import org.apache.commons.logging.Log;@b@@b@public class FileResource extends AbstractResource@b@{@b@  public FileResource(String location)@b@  {@b@    super(location);@b@  }@b@@b@  protected InputStream getActualInputStream() {@b@    InputStream is = null;@b@    try {@b@      URI uri = URI.create(getLocation());@b@      File file = new File(uri);@b@      is = new FileInputStream(file);@b@    }@b@    catch (FileNotFoundException e) {@b@      if (getLog().isWarnEnabled())@b@        getLog().warn("Could not find the given file " + getLocation(), e);@b@    }@b@@b@    return is;@b@  }@b@}
package uk.ac.sanger.cgp.dbcon.util.resources;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.net.HttpURLConnection;@b@import java.net.MalformedURLException;@b@import java.net.URI;@b@import java.net.URL;@b@import java.net.URLConnection;@b@import org.apache.commons.io.IOUtils;@b@import org.apache.commons.logging.Log;@b@import uk.ac.sanger.cgp.dbcon.exceptions.DbConException;@b@import uk.ac.sanger.cgp.dbcon.util.InputOutputUtils;@b@@b@public class UrlResource extends AbstractResource@b@{@b@  public UrlResource(String location)@b@  {@b@    super(location);@b@  }@b@@b@  protected InputStream getActualInputStream() {@b@    InputStream is = null;@b@    URLConnection connection = null;@b@    try@b@    {@b@      URL url = URI.create(getLocation()).toURL();@b@      connection = url.openConnection();@b@      connection.connect();@b@      InputStream bytesInput = connection.getInputStream();@b@      byte[] bytes = IOUtils.toByteArray(bytesInput);@b@      is = new ByteArrayInputStream(bytes);@b@    }@b@    catch (MalformedURLException e) {@b@      cleanUpHttpURLConnection(connection);@b@      throw new DbConException("Detected a malformed URL; aborting", e);@b@    }@b@    catch (FileNotFoundException e) {@b@      cleanUpHttpURLConnection(connection);@b@      throw new DbConException("Could not find the specified file at the given URL", e);@b@    }@b@    catch (IOException e) {@b@      cleanUpHttpURLConnection(connection);@b@      throw new DbConException("Detected IO problems whilst reading URL's content", e);@b@    }@b@@b@    return is;@b@  }@b@@b@  private void cleanUpHttpURLConnection(URLConnection connection)@b@    throws DbConException@b@  {@b@    if (connection == null) {@b@      getLog().fatal("Input HttpURLConnection was null ... will not perform cleanup");@b@    }@b@    else if (HttpURLConnection.class.isAssignableFrom(connection.getClass())) {@b@      InputStream errorStream = null;@b@      try {@b@        HttpURLConnection httpUrlConnection = (HttpURLConnection)connection;@b@        errorStream = httpUrlConnection.getErrorStream();@b@        if (errorStream != null) {@b@          byte[] errorOutput = IOUtils.toByteArray(errorStream);@b@          if (getLog().isDebugEnabled()) {@b@            String lengthOfError = (errorOutput == null) ? "NULL STREAM" : Integer.toString(errorOutput.length);@b@@b@            getLog().debug("Length of error stream (in bytes): " + lengthOfError);@b@          }@b@        }@b@@b@        int responseCode = httpUrlConnection.getResponseCode();@b@        if (getLog().isDebugEnabled()) {@b@          getLog().debug("Detected response code: " + responseCode);@b@        }@b@@b@      }@b@      catch (IOException e)@b@      {@b@      }@b@      finally@b@      {@b@        InputOutputUtils.closeQuietly(errorStream);@b@      }@b@    }@b@    else {@b@      getLog().debug("Input URLConnection was not a HttpURLConnection; will not perform cleanup");@b@    }@b@  }@b@}
package uk.ac.sanger.cgp.dbcon.util.resources;@b@@b@import java.io.InputStream;@b@import uk.ac.sanger.cgp.dbcon.exceptions.DbConException;@b@@b@public class ClasspathResource extends AbstractResource@b@{@b@  public ClasspathResource(String location)@b@  {@b@    super(location);@b@  }@b@@b@  protected InputStream getActualInputStream() {@b@    InputStream is = ClasspathResource.class.getResourceAsStream(getLocation());@b@    if (is == null)@b@      throw new DbConException("Could not find resource " + getLocation());@b@@b@    return is;@b@  }@b@}