Renamed FileSearch class and defined default values
This commit is contained in:
parent
e2e6a40ce9
commit
997ec1c52b
8 changed files with 286 additions and 280 deletions
|
|
@ -103,13 +103,15 @@ public class ClassUtil {
|
|||
* there is no generics or the super class is not found
|
||||
*/
|
||||
public static Class<?>[] getGenericClasses(Class<?> c, Class<?> superClass){
|
||||
if(superClass != null) {
|
||||
// Search for the super class
|
||||
while (c.getSuperclass() != Object.class && !superClass.isAssignableFrom(c.getSuperclass()))
|
||||
c = c.getSuperclass();
|
||||
while (c.getSuperclass() != null && c.getSuperclass() != Object.class) {
|
||||
// Did we find the super class?
|
||||
if (superClass.isAssignableFrom(c.getSuperclass()))
|
||||
return getGenericClasses(c.getGenericSuperclass());
|
||||
|
||||
c = c.getSuperclass();
|
||||
}
|
||||
}
|
||||
return new Class[0];
|
||||
}
|
||||
private static Class<?>[] getGenericClasses(Type genericType){
|
||||
|
|
@ -118,6 +120,7 @@ public class ClassUtil {
|
|||
Type[] argTypes = aType.getActualTypeArguments();
|
||||
Class<?>[] classArray = new Class<?>[argTypes.length];
|
||||
for(int i=0; i<classArray.length; ++i) {
|
||||
if(argTypes[i] instanceof Class)
|
||||
classArray[i] = (Class<?>) argTypes[i];
|
||||
}
|
||||
return classArray;
|
||||
|
|
|
|||
35
src/zutil/io/file/FileSearch.java → src/zutil/io/file/FileSearcher.java
Normal file → Executable file
35
src/zutil/io/file/FileSearch.java → src/zutil/io/file/FileSearcher.java
Normal file → Executable file
|
|
@ -34,7 +34,7 @@ import java.util.*;
|
|||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
|
||||
public class FileSearcher implements Iterable<FileSearcher.FileSearchItem>{
|
||||
// Constants
|
||||
private static final List<String> compressedFileExtensions = Arrays.asList(new String[]{
|
||||
"jar", "zip"
|
||||
|
|
@ -44,19 +44,17 @@ public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
|
|||
private File root;
|
||||
|
||||
// Search parameters
|
||||
private String fileName;
|
||||
private String extension;
|
||||
private boolean recursive;
|
||||
private String fileName = null;
|
||||
private String extension = null;
|
||||
private boolean recursive = false;
|
||||
//private int depth;
|
||||
private boolean searchFiles;
|
||||
private boolean searchCompressedFiles;
|
||||
private boolean searchFolders;
|
||||
private boolean searchFiles = true;
|
||||
private boolean searchFolders = true;
|
||||
private boolean searchCompressedFiles = false;
|
||||
|
||||
|
||||
public FileSearch(File root){
|
||||
public FileSearcher(File root){
|
||||
this.root = root;
|
||||
searchFiles = true;
|
||||
searchFolders = true;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -71,11 +69,11 @@ public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
|
|||
* Sets the file extensions to search for (should not include . at the beginning)
|
||||
*/
|
||||
public void setExtension(String ext){
|
||||
|
||||
extension = ext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets if the search should go into sub-folders
|
||||
* Defines if the search should go into sub-folders
|
||||
*/
|
||||
public void setRecursive(boolean recursive){
|
||||
this.recursive = recursive;
|
||||
|
|
@ -92,12 +90,12 @@ public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
|
|||
public void searchFiles(boolean searchFiles){
|
||||
this.searchFiles = searchFiles;
|
||||
}
|
||||
public void searchCompressedFiles(boolean searchCompressedFiles){
|
||||
this.searchCompressedFiles = searchCompressedFiles;
|
||||
}
|
||||
public void searchFolders(boolean searchFolders){
|
||||
this.searchFolders = searchFolders;
|
||||
}
|
||||
public void searchCompressedFiles(boolean searchCompressedFiles){
|
||||
this.searchCompressedFiles = searchCompressedFiles;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
|
@ -146,7 +144,8 @@ public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
|
|||
break;
|
||||
}
|
||||
else if(searchCompressedFiles && file.isFile() &&
|
||||
compressedFileExtensions.contains(FileUtil.getFileExtension(file.getName()).toLowerCase())){
|
||||
compressedFileExtensions.contains(
|
||||
FileUtil.getFileExtension(file.getName()).toLowerCase())){
|
||||
try {
|
||||
ZipFile zipFile = new ZipFile(file.getPath());
|
||||
Enumeration<? extends ZipEntry> e = zipFile.entries();
|
||||
|
|
@ -200,7 +199,7 @@ public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
|
|||
}
|
||||
|
||||
|
||||
public class FileSearchFileItem implements FileSearchItem{
|
||||
protected static class FileSearchFileItem implements FileSearchItem{
|
||||
private File file;
|
||||
|
||||
protected FileSearchFileItem(File file){
|
||||
|
|
@ -219,7 +218,7 @@ public class FileSearch implements Iterable<FileSearch.FileSearchItem>{
|
|||
|
||||
}
|
||||
|
||||
public class FileSearchZipItem implements FileSearchItem{
|
||||
protected static class FileSearchZipItem implements FileSearchItem{
|
||||
private String zipFile;
|
||||
private ZipEntry entry;
|
||||
private String fileName;
|
||||
|
|
@ -219,7 +219,7 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
|
|||
service.setExpirationTime(
|
||||
System.currentTimeMillis() + 1000 * getCacheTime(header.getHeader("Cache-Control")));
|
||||
}
|
||||
service.setHeaders(header.getHeaders());
|
||||
service.readHeaders(header);
|
||||
|
||||
if(listener != null && newService)
|
||||
listener.newService(service);
|
||||
|
|
|
|||
5
src/zutil/net/ssdp/SSDPCustomInfo.java
Normal file → Executable file
5
src/zutil/net/ssdp/SSDPCustomInfo.java
Normal file → Executable file
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
package zutil.net.ssdp;
|
||||
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
import zutil.net.http.HttpPrintStream;
|
||||
|
||||
/**
|
||||
|
|
@ -31,5 +32,7 @@ import zutil.net.http.HttpPrintStream;
|
|||
*/
|
||||
public interface SSDPCustomInfo extends SSDPServiceInfo{
|
||||
|
||||
public void setHeaders(HttpPrintStream http);
|
||||
public void readHeaders(HttpHeaderParser http);
|
||||
|
||||
public void writeHeaders(HttpPrintStream http);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -194,7 +194,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
|
|||
http.setHeader("EXT", "" );
|
||||
http.setHeader("Cache-Control", "max-age = "+ cache_time );
|
||||
if(services.get(st) instanceof SSDPCustomInfo)
|
||||
((SSDPCustomInfo)services.get(st)).setHeaders(http);
|
||||
((SSDPCustomInfo)services.get(st)).writeHeaders(http);
|
||||
logger.log(Level.FINEST, "Sending Response: "+ http);
|
||||
http.flush();
|
||||
|
||||
|
|
@ -265,7 +265,7 @@ public class SSDPServer extends ThreadedUDPNetwork implements ThreadedUDPNetwork
|
|||
http.setHeader("Cache-Control", "max-age = "+cache_time );
|
||||
http.setHeader("USN", service.getUSN() );
|
||||
if(service instanceof SSDPCustomInfo)
|
||||
((SSDPCustomInfo) service).setHeaders(http);
|
||||
((SSDPCustomInfo) service).writeHeaders(http);
|
||||
logger.log(Level.FINEST, "Sending Notification: " + http);
|
||||
http.flush();
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
package zutil.net.ssdp;
|
||||
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
import zutil.net.http.HttpPrintStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
@ -44,7 +45,7 @@ public class StandardSSDPInfo implements SSDPServiceInfo, SSDPCustomInfo{
|
|||
private String usn;
|
||||
private long expiration_time;
|
||||
// All header parameters
|
||||
private HashMap<String, String> headers;
|
||||
private HashMap<String, String> headers = new HashMap<>();
|
||||
private InetAddress inetAddress;
|
||||
|
||||
/**
|
||||
|
|
@ -127,27 +128,28 @@ public class StandardSSDPInfo implements SSDPServiceInfo, SSDPCustomInfo{
|
|||
return "USN: "+usn+"\nLocation: "+location+"\nST: "+st+"\nExpiration-Time: "+new Date(expiration_time);
|
||||
}
|
||||
|
||||
public void setHeaders(HashMap<String, String> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public void setHeader(String key, String value) {
|
||||
headers.put(key, value);
|
||||
}
|
||||
public String getHeader(String header){
|
||||
return headers.get(header.toUpperCase());
|
||||
return headers.get(header);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setHeaders(HttpPrintStream http) {
|
||||
public void writeHeaders(HttpPrintStream http) {
|
||||
try {
|
||||
if (headers != null) {
|
||||
for (String key : headers.keySet()) {
|
||||
for (String key : headers.keySet())
|
||||
http.setHeader(key, headers.get(key));
|
||||
}
|
||||
}
|
||||
}catch(IOException e){
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void readHeaders(HttpHeaderParser http) {
|
||||
HashMap<String,String> httpHeaders = http.getHeaders();
|
||||
for (String key : httpHeaders.keySet())
|
||||
headers.put(key, httpHeaders.get(key));
|
||||
}
|
||||
|
||||
public InetAddress getInetAddress(){
|
||||
return inetAddress;
|
||||
|
|
|
|||
6
src/zutil/plugin/PluginManager.java
Normal file → Executable file
6
src/zutil/plugin/PluginManager.java
Normal file → Executable file
|
|
@ -25,7 +25,7 @@
|
|||
package zutil.plugin;
|
||||
|
||||
import zutil.io.IOUtil;
|
||||
import zutil.io.file.FileSearch;
|
||||
import zutil.io.file.FileSearcher;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.json.JSONParser;
|
||||
|
|
@ -61,14 +61,14 @@ public class PluginManager<T> implements Iterable<PluginData>{
|
|||
public PluginManager(String path){
|
||||
plugins = new HashMap<String, PluginData>();
|
||||
|
||||
FileSearch search = new FileSearch(new File(path));
|
||||
FileSearcher search = new FileSearcher(new File(path));
|
||||
search.setRecursive(true);
|
||||
search.searchFolders(false);
|
||||
search.searchCompressedFiles(true);
|
||||
search.setFileName("plugin.json");
|
||||
|
||||
log.fine("Searching for plugins...");
|
||||
for(FileSearch.FileSearchItem file : search){
|
||||
for(FileSearcher.FileSearchItem file : search){
|
||||
try {
|
||||
DataNode node = JSONParser.read(IOUtil.getContentString(file.getInputStream()));
|
||||
log.fine("Found plugin: "+file.getPath());
|
||||
|
|
|
|||
7
test/zutil/test/SSDPServerTest.java
Normal file → Executable file
7
test/zutil/test/SSDPServerTest.java
Normal file → Executable file
|
|
@ -42,10 +42,9 @@ public class SSDPServerTest {
|
|||
StandardSSDPInfo service = new StandardSSDPInfo();
|
||||
service.setLocation("nowhere");
|
||||
service.setST("zep:discover");
|
||||
HashMap<String, String> headers = new HashMap<String, String>();
|
||||
headers.put("Alias", "Desktop");
|
||||
headers.put("PublicKey", "SuperDesktopKey");
|
||||
service.setHeaders(headers);
|
||||
service.setHeader("Alias", "Desktop");
|
||||
service.setHeader("PublicKey", "SuperDesktopKey");
|
||||
|
||||
|
||||
SSDPServer ssdp = new SSDPServer();
|
||||
ssdp.addService(service);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue