Skip to content
Snippets Groups Projects
Commit 106cd2fe authored by tonschnoer's avatar tonschnoer
Browse files

implemented Config

added packaging to pom
parent 1228e69c
No related branches found
No related tags found
No related merge requests found
......@@ -19,8 +19,33 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.apache.commons/commons-configuration2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.10.1</version>
</dependency>
<!--
https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Maven Repo</id>
......@@ -28,4 +53,33 @@
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.kmt.ndr.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package de.kmt.ndr;
import org.apache.commons.configuration2.*;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
public class Config {
public static int listenerport=0;
public static int serverport = 0;
public static String server ="";
public static String directory="";
public static String file="";
public static String hours="";
public static String minutes="";
public static String seconds="";
public static void readini() {
try {
Configurations c = new Configurations();
INIConfiguration ic = c.ini("MediaBackup.ini");
SubnodeConfiguration general = ic.getSection("general");
SubnodeConfiguration files = ic.getSection("file");
SubnodeConfiguration schedule = ic.getSection("schedule");
listenerport = general.getInt("listenerport");
serverport = general.getInt("serverport");
server = general.getString("server");
directory = files.getString("directory");
file = files.getString("file");
hours = schedule.getString("hours");
minutes = schedule.getString("minutes");
seconds = schedule.getString("seconds");
System.out.println("ini file read. schedule=" + hours + " " + minutes + " " + seconds);
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
......@@ -20,7 +20,9 @@ public class Main {
private TimerService timerService;
private void _setUP() {
System.out.println("Read ini file");
Config.readini();
}
// ***********************************************************************************
......@@ -29,10 +31,9 @@ public class Main {
@Timeout
public void scheduler(Timer timer) {
try {
_setUP();
System.out.println("Timer fired.");
} catch (Exception _ex) {
_ex.printStackTrace();
}
}
......@@ -43,12 +44,17 @@ public class Main {
public void initialize() {
try {
_setUP();
System.out.println("initialize application");
ScheduleExpression se = new ScheduleExpression();
se.hour("*").minute("0/1").second("0/1");
timerService.createCalendarTimer(se, new TimerConfig("Cleanup Service scheduled at ", false));
if (!Config.hours.equals("")) se = se.hour(Config.hours);
if (!Config.minutes.equals("")) se = se.minute(Config.minutes);
if (!Config.seconds.equals("")) se = se.second(Config.seconds);
timerService.createCalendarTimer(se, new TimerConfig("Next Backup Request scheduled at ", false));
} catch (Exception _ex) {
_ex.printStackTrace();
}
}
......@@ -59,13 +65,11 @@ public class Main {
public void stop() {
try {
_setUP();
for (Timer timer : timerService.getTimers()) {
timer.cancel();
}
} catch (Exception _ex) {
_ex.printStackTrace();
}
}
......
package de.kmt.ndr;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
// TODO: Auto-generated Javadoc
/**
* The Class TCPClient.
*/
public class TCPClient {
/** The logger. */
protected final Logger logger = LogManager.getLogger();
/**
* The main method.
*
* @param args the arguments
*/
public void main(String[] args) {
byte[] buffer = new byte[32768];
try {
logger.info("Startup TCPClient");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(Config.directory + Config.file));
Socket clientSocket = new Socket(Config.server,Config.serverport);
OutputStreamWriter out = new OutputStreamWriter(clientSocket.getOutputStream());
InputStream is = clientSocket.getInputStream();
while (true) {
// request file from server
out.write(Config.file);
out.flush();
while (true) {
// read up to 32768 bytes from stream
int readbytes = is.read(buffer,0,32768);
System.out.print(".");
// check if 32768 bytes where read (i.e. there are some more)
if (readbytes<32768) {
// less than 32768 bytes are read. Check if last 3 bytes are "END". If so: stop receiving data
if ((buffer[readbytes-3]==0x45) & (buffer[readbytes-2]==0x4e) & (buffer[readbytes-1]==0x44)) {
System.out.println("Schluss");
// if there is some interesting data, before END, write it to file
if (readbytes>3) {
dos.write(buffer,0,readbytes-3);
}
break;
} else {
// if bytesread < 32768, but no ending Tag "END" -> write buffer to file
dos.write(buffer,0,readbytes);
}
} else {
// if bytesread = 32768, assume there is more and write buffer to file
dos.write(buffer,0,readbytes);
}
}
// close output file
dos.close();
// Send a response back to the client
out.write(".");
// Close the socket and streams for this client
clientSocket.close();
// exit while(true)
break;
}
} catch (Exception e) {
System.out.println("An exception has occurred:");
e.printStackTrace();
}
}
}
[general]
listenerport = 4000
serverport = 12345
server = 127.0.0.1
[files]
directory=E:\\Daten\\Client\\
file=herz.mp4
[schedule]
hours=*
minutes=*
seconds=0/5
\ No newline at end of file
[general]
listenerport = 4000
serverport = 12345
server = 127.0.0.1
[files]
directory=E:\\Daten\\Client\\
file=herz.mp4
[schedule]
hours=*
minutes=0/1
seconds=
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment