mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
No effective change - code cleanup - ResourceSource
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.resource;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
|
||||
public abstract class AbstractResourceSource {
|
||||
|
||||
|
||||
/**
|
||||
* Helper method to read String content.
|
||||
*/
|
||||
public String readString(ResourceContent content, int bufSize) throws IOException {
|
||||
|
||||
if (content == null) {
|
||||
throw new NullPointerException("content is null?");
|
||||
}
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
InputStream is = content.getInputStream();
|
||||
InputStreamReader reader = new InputStreamReader(is);
|
||||
|
||||
char[] buf = new char[bufSize];
|
||||
int len;
|
||||
while ((len = reader.read(buf, 0, buf.length)) != -1) {
|
||||
writer.write(buf, 0, len);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to read byte[] content.
|
||||
*/
|
||||
public byte[] readBytes(ResourceContent content, int bufSize) throws IOException {
|
||||
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
InputStream is = content.getInputStream();
|
||||
|
||||
byte[] buf = new byte[bufSize];
|
||||
int len;
|
||||
while ((len = is.read(buf, 0, buf.length)) != -1) {
|
||||
bytes.write(buf, 0, len);
|
||||
}
|
||||
|
||||
is.close();
|
||||
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
+47
-47
@@ -11,60 +11,60 @@ import java.util.Date;
|
||||
*/
|
||||
public class FileResourceContent implements ResourceContent {
|
||||
|
||||
/**
|
||||
* The underlying file.
|
||||
*/
|
||||
final File file;
|
||||
/**
|
||||
* The underlying file.
|
||||
*/
|
||||
final File file;
|
||||
|
||||
final String entryName;
|
||||
|
||||
/**
|
||||
* Create with a File and the entryName.
|
||||
*/
|
||||
public FileResourceContent(File file, String entryName) {
|
||||
this.file = file;
|
||||
this.entryName = entryName;
|
||||
}
|
||||
/**
|
||||
* Create with a File and the entryName.
|
||||
*/
|
||||
public FileResourceContent(File file, String entryName) {
|
||||
this.file = file;
|
||||
this.entryName = entryName;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[").append(getName());
|
||||
sb.append("] size[").append(size());
|
||||
sb.append("] lastModified[").append(new Date(lastModified()));
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[").append(getName());
|
||||
sb.append("] size[").append(size());
|
||||
sb.append("] lastModified[").append(new Date(lastModified()));
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entry name which contains the path from the base directory.
|
||||
* <p>
|
||||
* This does not return the full path of the file, but the path relative to
|
||||
* the FileIoSource directory.
|
||||
* </p>
|
||||
*/
|
||||
public String getName() {
|
||||
return entryName;
|
||||
}
|
||||
/**
|
||||
* Returns the entry name which contains the path from the base directory.
|
||||
* <p>
|
||||
* This does not return the full path of the file, but the path relative to
|
||||
* the FileIoSource directory.
|
||||
* </p>
|
||||
*/
|
||||
public String getName() {
|
||||
return entryName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time the file was last modified.
|
||||
*/
|
||||
public long lastModified() {
|
||||
return file.lastModified();
|
||||
}
|
||||
/**
|
||||
* Return the time the file was last modified.
|
||||
*/
|
||||
public long lastModified() {
|
||||
return file.lastModified();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the size of the file.
|
||||
*/
|
||||
public long size() {
|
||||
return file.length();
|
||||
}
|
||||
/**
|
||||
* Return the size of the file.
|
||||
*/
|
||||
public long size() {
|
||||
return file.length();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the input stream for this file.
|
||||
*/
|
||||
public InputStream getInputStream() throws IOException {
|
||||
/**
|
||||
* Return the input stream for this file.
|
||||
*/
|
||||
public InputStream getInputStream() throws IOException {
|
||||
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,46 +5,46 @@ import java.io.File;
|
||||
/**
|
||||
* A file system directory represented as a FileSource.
|
||||
*/
|
||||
public class FileResourceSource extends AbstractResourceSource implements ResourceSource {
|
||||
public class FileResourceSource implements ResourceSource {
|
||||
|
||||
/**
|
||||
* The directory name.
|
||||
*/
|
||||
final String directory;
|
||||
/**
|
||||
* The directory name.
|
||||
*/
|
||||
final String directory;
|
||||
|
||||
final String baseDir;
|
||||
|
||||
/**
|
||||
* Create the source based on a directory name.
|
||||
*/
|
||||
public FileResourceSource(String directory){
|
||||
this.directory = directory;
|
||||
this.baseDir = directory+File.separator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the source based on a directory file.
|
||||
*/
|
||||
public FileResourceSource(File dir){
|
||||
this(dir.getPath());
|
||||
}
|
||||
|
||||
|
||||
public String getRealPath() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the given file and return as IoContent.
|
||||
*/
|
||||
public ResourceContent getContent(String entry) {
|
||||
|
||||
String fullPath = baseDir+entry;
|
||||
|
||||
File f = new File(fullPath);
|
||||
if (f.exists()){
|
||||
return new FileResourceContent(f, entry);
|
||||
}
|
||||
return null;
|
||||
/**
|
||||
* Create the source based on a directory name.
|
||||
*/
|
||||
public FileResourceSource(String directory) {
|
||||
this.directory = directory;
|
||||
this.baseDir = directory + File.separator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the source based on a directory file.
|
||||
*/
|
||||
public FileResourceSource(File dir) {
|
||||
this(dir.getPath());
|
||||
}
|
||||
|
||||
|
||||
public String getRealPath() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the given file and return as IoContent.
|
||||
*/
|
||||
public ResourceContent getContent(String entry) {
|
||||
|
||||
String fullPath = baseDir + entry;
|
||||
|
||||
File f = new File(fullPath);
|
||||
if (f.exists()) {
|
||||
return new FileResourceContent(f, entry);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,24 +11,24 @@ import java.io.InputStream;
|
||||
*/
|
||||
public interface ResourceContent {
|
||||
|
||||
/**
|
||||
* The name of the content.
|
||||
*/
|
||||
String getName();
|
||||
/**
|
||||
* The name of the content.
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* The size of the content in bytes.
|
||||
*/
|
||||
long size();
|
||||
/**
|
||||
* The size of the content in bytes.
|
||||
*/
|
||||
long size();
|
||||
|
||||
/**
|
||||
* The last modified timestamp of the content.
|
||||
*/
|
||||
long lastModified();
|
||||
/**
|
||||
* The last modified timestamp of the content.
|
||||
*/
|
||||
long lastModified();
|
||||
|
||||
/**
|
||||
* The content itself.
|
||||
*/
|
||||
InputStream getInputStream() throws IOException;
|
||||
/**
|
||||
* The content itself.
|
||||
*/
|
||||
InputStream getInputStream() throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.lib.resource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A Source for ResourceManager.
|
||||
* <p>
|
||||
@@ -25,13 +23,4 @@ public interface ResourceSource {
|
||||
*/
|
||||
ResourceContent getContent(String entry);
|
||||
|
||||
/**
|
||||
* Return the content as a String.
|
||||
*/
|
||||
String readString(ResourceContent content, int bufSize) throws IOException;
|
||||
|
||||
/**
|
||||
* Return the content as a byte[].
|
||||
*/
|
||||
byte[] readBytes(ResourceContent content, int bufSize) throws IOException;
|
||||
}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Content from a URL Resource.
|
||||
*/
|
||||
public class UrlResourceContent implements ResourceContent {
|
||||
|
||||
final String entryName;
|
||||
|
||||
URLConnection con;
|
||||
|
||||
/**
|
||||
* Create with a File and the entryName.
|
||||
*/
|
||||
public UrlResourceContent(URL url, String entryName) {
|
||||
//this.url = url;
|
||||
this.entryName = entryName;
|
||||
try {
|
||||
con = url.openConnection();
|
||||
} catch (IOException ex){
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[").append(getName());
|
||||
sb.append("] size[").append(size());
|
||||
sb.append("] lastModified[").append(new Date(lastModified()));
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entry name which contains the path from the base directory.
|
||||
* <p>
|
||||
* This does not return the full path of the file, but the path relative to
|
||||
* the FileIoSource directory.
|
||||
* </p>
|
||||
*/
|
||||
public String getName() {
|
||||
return entryName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time the file was last modified.
|
||||
*/
|
||||
public long lastModified() {
|
||||
return con.getLastModified();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the size of the file.
|
||||
*/
|
||||
public long size() {
|
||||
return con.getContentLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the input stream for this file.
|
||||
*/
|
||||
public InputStream getInputStream() throws IOException {
|
||||
|
||||
return con.getInputStream();
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.lib.resource;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import com.avaje.ebeaninternal.server.lib.util.GeneralException;
|
||||
|
||||
/**
|
||||
* A file system directory represented as a FileSource.
|
||||
*/
|
||||
public class UrlResourceSource extends AbstractResourceSource implements ResourceSource {
|
||||
|
||||
|
||||
ServletContext sc;
|
||||
|
||||
String basePath;
|
||||
|
||||
String realPath;
|
||||
|
||||
/**
|
||||
* Create the source based on a directory name.
|
||||
*/
|
||||
public UrlResourceSource(ServletContext sc, String basePath){
|
||||
this.sc = sc;
|
||||
if (basePath == null){
|
||||
this.basePath = "/";
|
||||
} else {
|
||||
this.basePath = "/"+basePath+"/";
|
||||
}
|
||||
this.realPath = sc.getRealPath(basePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the "real path" from the ServletContext root.
|
||||
* <p>
|
||||
* This can be null for unpacked WAR deployment.
|
||||
* </p>
|
||||
*/
|
||||
public String getRealPath() {
|
||||
return realPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the given URL resource and return as ResourceContent.
|
||||
* <p>
|
||||
* Returns null if the resource is not found.
|
||||
* </p>
|
||||
*/
|
||||
public ResourceContent getContent(String entry) {
|
||||
|
||||
try {
|
||||
URL url = sc.getResource(basePath+entry);
|
||||
if (url != null){
|
||||
return new UrlResourceContent(url, entry);
|
||||
}
|
||||
return null;
|
||||
|
||||
} catch (MalformedURLException ex){
|
||||
throw new GeneralException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user