feature/config #78

Merged
CodeSteak merged 10 commits from feature/config into master 2020-06-18 14:09:47 +02:00
5 changed files with 161 additions and 52 deletions
Showing only changes of commit 774e62f892 - Show all commits

View File

@ -6,3 +6,16 @@ installationName: Fast-Web-Shop
numberOfStorageSpaces: 128
parcelServiceApiURL: https://dhl.api.ecommerce.mosad.xyz
parcelServiceName: DHL International GmbH
suppliers:
- apiURL: https://reichelt.api.ecommerce.mosad.xyz
companyAddress: {cityName: Sande, countryName: Germany, houseNumber: '1', streetName: Elektronikring,
zipCode: '26452'}
deliveryTime: 4
id: d41d8cd98f00b204e9800998ecf8427e
name: Reichelt elektronik GmbH & Co. KG
- apiURL: https://conrad.api.ecommerce.mosad.xyz
companyAddress: {cityName: Hirschau, countryName: Germany, houseNumber: '1', streetName: Klaus-Conrad-Straße,
zipCode: '92240'}
deliveryTime: 5
id: 18a17da5bac1cf00551b08c3e98720f5
name: Conrad Electronic SE

View File

@ -1,21 +0,0 @@
package org.hso.ecommerce.app;
import org.hso.ecommerce.app.config.AppSettings;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class InitController {
@PostConstruct
public void init() {
System.out.println("Initialize!");
AppSettings appSettings = AppSettings.getInstance();
//appSettings.writeDemoConfig();
appSettings.readConfig();
}
}

View File

@ -9,21 +9,42 @@ import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import javax.annotation.PostConstruct;
@Component
public class AppSettings {
private static AppSettings instance;
private AppSettings() {
// constructor
}
public static AppSettings getInstance() {
if (instance == null) {
instance = new AppSettings();
}
return instance;
private static YAMLData data;
private static String installationName;
private static String companyName;
private static Address companyAddress;
private static int numberOfStorageSpaces;
private static List<YAMLData.Supplier> suppliers;
private static String parcelServiceName;
private static String parcelServiceApiURL;
@PostConstruct
/**
* on initialization read the config and store the data in static objects
*/
public void init() {
data = readConfig();
installationName = data.getInstallationName();
companyName = data.getCompanyName();
companyAddress = data.getCompanyAddress();
numberOfStorageSpaces = data.getNumberOfStorageSpaces();
suppliers = data.getSuppliers();
parcelServiceName = data.getParcelServiceName();
parcelServiceApiURL = data.getParcelServiceApiURL();
System.out.println("Initialised Settings!");
}
/**
* write a demo config file
*/
public void writeDemoConfig() {
YAMLData data = new YAMLData();
data.setInstallationName("Fast-Web-Shop");
@ -37,15 +58,34 @@ public class AppSettings {
));
data.setNumberOfStorageSpaces(128);
// List<YAMLData.Supplier> suppliers = new ArrayList<YAMLData.Supplier>();
// suppliers.add(new YAMLData.Supplier("Reichelt elektronik GmbH & Co. KG", "d41d8cd98f00b204e9800998ecf8427e",
// "https://reichelt.api.ecommerce.mosad.xyz",
// new Address("Elektronikring", "1", "26452", "Sande", "Germany"), 4));
// suppliers.add(new YAMLData.Supplier("Conrad Electronic SE", "18a17da5bac1cf00551b08c3e98720f5",
// "https://conrad.api.ecommerce.mosad.xyz",
// new Address("Klaus-Conrad-Straße", "1", "92240", "Hirschau", "Germany"), 5));
//
// data.setSuppliers(suppliers);
List<YAMLData.Supplier> suppliers = new ArrayList<>();
suppliers.add(new YAMLData.Supplier(
"Reichelt elektronik GmbH & Co. KG",
"d41d8cd98f00b204e9800998ecf8427e",
"https://reichelt.api.ecommerce.mosad.xyz",
4,
new Address(
"Elektronikring",
"1",
"26452",
"Sande",
"Germany"
)
));
suppliers.add(new YAMLData.Supplier(
"Conrad Electronic SE",
"18a17da5bac1cf00551b08c3e98720f5",
"https://conrad.api.ecommerce.mosad.xyz",
5,
new Address(
"Klaus-Conrad-Straße",
"1",
"92240",
"Hirschau",
"Germany"
)
));
data.setSuppliers(suppliers);
data.setParcelServiceName("DHL International GmbH");
data.setParcelServiceApiURL("https://dhl.api.ecommerce.mosad.xyz");
@ -59,14 +99,19 @@ public class AppSettings {
}
/**
* read a config file named "eCommerce_config.yml" from the applications root directory
* @return the settings as YAMLData object
*/
public YAMLData readConfig() {
YAMLData data = new YAMLData();
try (InputStream inputStream = new FileInputStream("./eCommerce_config.yml")) {

Vlcht besser als private/public static final String in der Klasse.

wollen wir wirklich "./eCommerce_config.yml" als Dateinamen? Hat merkwürdiges Caseing (underscore und Groß/klein-schreibung.).

Vlcht besser als private/public static final String in der Klasse. wollen wir wirklich "./eCommerce_config.yml" als Dateinamen? Hat merkwürdiges Caseing (underscore und Groß/klein-schreibung.).
Yaml yaml = new Yaml(new Constructor(YAMLData.class));
data = yaml.load(inputStream);
System.out.println(data.getCompanyName());
} catch (FileNotFoundException e) {
System.err.println("The file \"eCommerce_config.yml\" has not been found, please create a valid Configuration file.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
@ -74,4 +119,35 @@ public class AppSettings {
return data;

Führt nach im catch(...) Fall evtl zu null pointer deref. Vlcht im catch leere werte setzen oder app beenden.

Führt nach im catch(...) Fall evtl zu null pointer deref. Vlcht im catch leere werte setzen oder app beenden.
Outdated
Review

Wollen wir mit leeren Werten starten oder beenden? Ich hab jetzt mal ein System.exit() eingebaut.

Wollen wir mit leeren Werten starten oder beenden? Ich hab jetzt mal ein `System.exit()` eingebaut.
}
public static YAMLData getData() {
return data;
}
public static String getInstallationName() {

In den Getter ist nicht sichergestellt, dass die werte schon initialisiert sind. Andere Komponenten könnten schon vorher PostConstruct ausgeführt haben.

In den Getter ist nicht sichergestellt, dass die werte schon initialisiert sind. Andere Komponenten könnten schon vorher PostConstruct ausgeführt haben.
return installationName;
}
public static String getCompanyName() {
return companyName;
}
public static Address getCompanyAddress() {
return companyAddress;
}
public static int getNumberOfStorageSpaces() {
return numberOfStorageSpaces;
}
public static List<YAMLData.Supplier> getSuppliers() {
return suppliers;
}
public static String getParcelServiceName() {
return parcelServiceName;
}
public static String getParcelServiceApiURL() {
return parcelServiceApiURL;
}
}

View File

@ -1,12 +1,14 @@
package org.hso.ecommerce.app.config;
import java.util.List;
public class YAMLData {
private String installationName;
private String companyName;
private Address companyAddress;
private int numberOfStorageSpaces;
//private List<Supplier> suppliers;
private List<Supplier> suppliers;
private String parcelServiceName;
private String parcelServiceApiURL;
@ -35,14 +37,12 @@ public class YAMLData {
public void setNumberOfStorageSpaces(int numberOfStorageSpaces) {
this.numberOfStorageSpaces = numberOfStorageSpaces;
}
/*
public List<Supplier> getSuppliers() {
return suppliers;
}
public void setSuppliers(List<Supplier> suppliers) {
this.suppliers = suppliers;
}
*/
public String getParcelServiceName() {
return parcelServiceName;
}
@ -116,27 +116,68 @@ public class YAMLData {
this.countryName = countryName;
}
}
/*
public static class Supplier {
public String name;
public String id;
public String apiURL;
public Address companyAddress;
public int deliveryTime;
public Address companyAddress;
public Supplier() {
// needed by snakeyaml
}
public Supplier(String name, String id, String apiURL, Address companyAddress, int deliveryTime) {
public Supplier(String name, String id, String apiURL, int deliveryTime, Address companyAddress) {
this.name = name;
this.id = id;
this.apiURL = apiURL;
this.deliveryTime = deliveryTime;
this.companyAddress = companyAddress;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getApiURL() {
return apiURL;
}
public void setApiURL(String apiURL) {
this.apiURL = apiURL;
}
public int getDeliveryTime() {
return deliveryTime;
}
public void setDeliveryTime(int deliveryTime) {
this.deliveryTime = deliveryTime;
}
public Address getCompanyAddress() {
return companyAddress;
}
*/
public void setCompanyAddress(Address companyAddress) {
this.companyAddress = companyAddress;
}
}
}

View File

@ -1,5 +1,6 @@
package org.hso.ecommerce.components;
import org.hso.ecommerce.app.config.AppSettings;
import org.hso.ecommerce.entities.warehouse.Slot;
import org.hso.ecommerce.repos.warehouse.SlotRepository;
import org.springframework.beans.factory.annotation.Autowired;
@ -13,8 +14,7 @@ public class SlotInitializer {
@Autowired
private final SlotRepository slotRepository = null;
// TODO: use values form cfg.
private final int NUM_SLOTS = 50;
private final int NUM_SLOTS = AppSettings.getNumberOfStorageSpaces();

Muss in init() eingelesen werden wegen obigen Problem

Muss in init() eingelesen werden wegen obigen Problem
@PostConstruct
public void init() {