Commit b5e6a893 authored by SAPONE Alann's avatar SAPONE Alann

corrected importation from JAR resources access

parent ee7ecf81
......@@ -38,11 +38,20 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
......@@ -69,58 +78,46 @@ final class Installer {
return newDatabase;
}
private static void getResourceFiles(List<String> filenames, String path, Class<?> resourceClass) throws IOException {
try(
InputStream in = getResourceAsStream(path, resourceClass);
BufferedReader br = new BufferedReader( new InputStreamReader( in ) ) ) {
String resource;
while( (resource = br.readLine()) != null ) {
String fullPath = path + "/" + resource;
if (!fullPath.endsWith(".xml")) {
getResourceFiles(filenames, fullPath, resourceClass);
} else {
System.out.println(fullPath);
filenames.add( fullPath );
}
}
public static void getPresetsList(List<String> filenames, File dir) {
if (dir.isDirectory()) {
for (File entry : dir.listFiles()) {
if (entry.isFile())
filenames.add("/installdata/" + entry.getPath().split("/installdata/")[1]);
else
getPresetsList(filenames, entry);
}
}
}
private static InputStream getResourceAsStream(String resource, Class<?> resourceClass) {
final InputStream in = getContextClassLoader().getResourceAsStream( resource );
return in == null ? resourceClass.getResourceAsStream( resource ) : in;
}
private static void getResourceFiles(List<String> filenames, String path, Class<?> resourceClass) throws IOException {
private static ClassLoader getContextClassLoader() {
return Thread.currentThread().getContextClassLoader();
Enumeration<URL> en = resourceClass.getClassLoader().getResources(path);
if (en.hasMoreElements()) {
URL url = en.nextElement();
File rootPresetPath = new File(url.getFile());
getPresetsList(filenames, rootPresetPath);
// We are in the jar and can't get to path
if(filenames.size() == 0) {
JarURLConnection urlcon = (JarURLConnection) (url.openConnection());
try (JarFile jar = urlcon.getJarFile();) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String entry = entries.nextElement().getName();
if (entry.contains("installdata/presets/") && entry.endsWith(".xml")) {
filenames.add("/" + entry);
}
}
}
}
}
}
public static FileImportWorker importPresets() throws IOException {
List<String> presetsResources = new ArrayList<>();
getResourceFiles(presetsResources, "/installdata/presets", DbUpdater.class);
System.out.println(presetsResources);
/*
ArrayList<String> presetsResources = new ArrayList<String>();
// Sources presets
presetsResources.add("/installdata/presets/sources/BB-T10000K-MAGJ0-z0.xml");
presetsResources.add("/installdata/presets/sources/BB-T5000K-MAGJ20-z0.xml");
presetsResources.add("/installdata/presets/sources/BB-T5000K-MAGJ20-z1.xml");
presetsResources.add("/installdata/presets/sources/Sources-Halpha - CFRS-03.125.xml");
presetsResources.add("/installdata/presets/sources/Sources-Halpha - CFRS-03.1534.xml");
presetsResources.add("/installdata/presets/sources/Sources-Halpha - CFRS-14.0600.xml");
// Sites
presetsResources.add("/installdata/presets/sites/Demo - Ground site.xml");
// Instruments
presetsResources.add("/installdata/presets/instruments/Demo - Imaging Instrument 1m Telescope.xml");
presetsResources.add("/installdata/presets/instruments/Demo - Spectroscopic instrument 1m Telescope.xml");
// Obs params
presetsResources.add("/installdata/presets/obs_params/Demo - Obs Parameters - Int 1s.xml");
*/
// presetsResources.add("/installdata/presets/sources/Sources-Halpha - CFRS-14.0600.xml");
getResourceFiles(presetsResources, "installdata/presets/", DbUpdater.class);
FileImportWorker worker = ImportHelper.importPresets(presetsResources, DbUpdater.class);
return worker;
......
......@@ -81,6 +81,7 @@ final class SampControllerImpl implements SampController {
private Subscriptions getSubscription() {
Subscriptions subETC = new Subscriptions();
subETC.addMType("samp.app.ping");
subETC.addMType("spectrum.load.ssa-generic");
return subETC;
}
......
......@@ -75,7 +75,6 @@ public class ImportHelper {
for (String resource : presets) {
URL resourceURL = resourceClass.getResource(resource);
System.out.println(resource + " - " + resourceURL);
Triplet<ComponentType, String, String> infoFromUrl = ImportHelper.getInfoFromUrl(resourceURL);
switch (infoFromUrl.getValue0()) {
case SOURCE:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment