Merge pull request #4 from informatik-ag-ngl/f/standalone_server_integration
Integrated Envoy Server Standalone
This commit is contained in:
commit
3a15946284
12
.classpath
12
.classpath
@ -24,18 +24,6 @@
|
||||
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/classes" path="target/generated-sources/jaxb">
|
||||
<attributes>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/xjb">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
|
@ -1,8 +1,6 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/main/resources=UTF-8
|
||||
encoding//src/main/xjb=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding//src/test/resources=UTF-8
|
||||
encoding//target/generated-sources/jaxb=UTF-8
|
||||
encoding/<project>=UTF-8
|
||||
|
@ -1,13 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||
|
||||
|
||||
|
||||
<wb-module deploy-name="envoy-common">
|
||||
|
||||
|
||||
|
||||
<wb-resource deploy-path="/" source-path="/src/main/java"/>
|
||||
|
||||
<wb-resource deploy-path="/" source-path="/target/generated-sources/jaxb"/>
|
||||
<wb-resource deploy-path="/" source-path="/src/main/xjb"/>
|
||||
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
|
||||
|
||||
</wb-module>
|
||||
|
||||
|
||||
</wb-module>
|
||||
|
||||
|
||||
</project-modules>
|
||||
|
39
pom.xml
39
pom.xml
@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>informatik-ag-ngl</groupId>
|
||||
<artifactId>envoy-common</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>0.2-alpha</version>
|
||||
|
||||
<name>Envoy Common</name>
|
||||
<url>https://github.com/informatik-ag-ngl/envoy-common</url>
|
||||
@ -16,44 +16,7 @@
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>jaxb2-maven-plugin</artifactId>
|
||||
<version>2.5.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>envoy-common</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/xjb</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>jaxb2-maven-plugin</artifactId>
|
||||
<version>2.5.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>xjc</id>
|
||||
<goals>
|
||||
<goal>xjc</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<packageName>envoy.schema</packageName>
|
||||
<sources>
|
||||
<source>src/main/resources</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
72
src/main/java/envoy/data/LoginCredentials.java
Normal file
72
src/main/java/envoy/data/LoginCredentials.java
Normal file
@ -0,0 +1,72 @@
|
||||
package envoy.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Formatter;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>LoginCredentials.java</strong><br>
|
||||
* Created: <strong>29.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public class LoginCredentials implements Serializable {
|
||||
|
||||
private final String name;
|
||||
private final byte[] passwordHash;
|
||||
|
||||
private static final long serialVersionUID = -7395245059059523314L;
|
||||
|
||||
/**
|
||||
* Creates an in stance of {@link LoginCredentials}.
|
||||
*
|
||||
* @param name the name of the user
|
||||
* @param password the password of the user (will be converted to a hash)
|
||||
* @throws NoSuchAlgorithmException
|
||||
*/
|
||||
public LoginCredentials(String name, char[] password) throws NoSuchAlgorithmException {
|
||||
this.name = name;
|
||||
passwordHash = getSha256(toByteArray(password));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
Formatter form = new Formatter();
|
||||
form.format("LoginCredentials[name=%s,passwordHash=", name);
|
||||
for (int i = 0; i < passwordHash.length; i++)
|
||||
form.format("%02x", passwordHash[i]);
|
||||
form.format("]");
|
||||
String str = form.toString();
|
||||
form.close();
|
||||
return str;
|
||||
}
|
||||
|
||||
private byte[] toByteArray(char[] chars) {
|
||||
byte[] bytes = new byte[chars.length * 2];
|
||||
for (int i = 0; i < chars.length; ++i) {
|
||||
bytes[i * 2] = (byte) (chars[i] >> 8);
|
||||
bytes[i * 2 + 1] = (byte) (chars[i]);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
return md.digest(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name of the user performing the login
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public String getName() { return name; }
|
||||
|
||||
/**
|
||||
* @return the password hash of the user performing the login
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public byte[] getPasswordHash() { return passwordHash; }
|
||||
}
|
97
src/main/java/envoy/data/Message.java
Normal file
97
src/main/java/envoy/data/Message.java
Normal file
@ -0,0 +1,97 @@
|
||||
package envoy.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Represents a unique message with a unique, numeric ID. Further metadata
|
||||
* includes the sender and recipient {@link User}s, as well as the creation
|
||||
* date and the current {@link MessageStatus}.<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>Message.java</strong><br>
|
||||
* Created: <strong>28.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public abstract class Message implements Serializable {
|
||||
|
||||
private final long id;
|
||||
private final User sender, recipient;
|
||||
private final Date date;
|
||||
|
||||
private MessageStatus status;
|
||||
|
||||
private static final long serialVersionUID = -4393477412979594435L;
|
||||
|
||||
/**
|
||||
* Initializes a {@link Message} from the client's perspective. The current date
|
||||
* is used as the message date and the status is set to
|
||||
* {@link MessageStatus#WAITING}.
|
||||
*
|
||||
* @param id unique ID
|
||||
* @param sender the user who sends the message
|
||||
* @param recipient the user who receives the message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public Message(long id, User sender, User recipient) {
|
||||
this.id = id;
|
||||
this.sender = sender;
|
||||
this.recipient = recipient;
|
||||
date = new Date();
|
||||
status = MessageStatus.WAITING;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the ID of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public long getId() { return id; }
|
||||
|
||||
/**
|
||||
* @return the sender of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public User getSender() { return sender; }
|
||||
|
||||
/**
|
||||
* @return the recipient of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public User getRecipient() { return recipient; }
|
||||
|
||||
/**
|
||||
* @return the date at which this message was created
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public Date getDate() { return date; }
|
||||
|
||||
/**
|
||||
* @return the current status of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public MessageStatus getStatus() { return status; }
|
||||
|
||||
/**
|
||||
* Changes the current {@link MessageStatus} to the next logical status.<br>
|
||||
* <br>
|
||||
* The underlying order is as follows:
|
||||
* <ol>
|
||||
* <li>{@link MessageStatus#WAITING}
|
||||
* <li>{@link MessageStatus#SENT}
|
||||
* <li>{@link MessageStatus#RECEIVED}
|
||||
* <li>{@link MessageStatus#READ}
|
||||
* </ol>
|
||||
*
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public void nextStatus() {
|
||||
if (status == MessageStatus.READ) throw new IllegalStateException("Message status READ is already reached");
|
||||
status = MessageStatus.values()[status.ordinal() + 1];
|
||||
}
|
||||
|
||||
public static enum MessageStatus {
|
||||
WAITING, SENT, RECEIVED, READ
|
||||
}
|
||||
}
|
54
src/main/java/envoy/data/TextMessage.java
Normal file
54
src/main/java/envoy/data/TextMessage.java
Normal file
@ -0,0 +1,54 @@
|
||||
package envoy.data;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* Represents a {@link Message} with content that is comprised of text.<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>TextMessage.java</strong><br>
|
||||
* Created: <strong>28.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public class TextMessage extends Message {
|
||||
|
||||
private final String content;
|
||||
|
||||
private static final long serialVersionUID = 1538164720632899917L;
|
||||
|
||||
/**
|
||||
* Initializes a {@link TextMessage} from the client's perspective. The current
|
||||
* date
|
||||
* is used as the message date and the status is set to
|
||||
* {@link envoy.data.Message.MessageStatus#WAITING}.
|
||||
*
|
||||
* @param id unique ID
|
||||
* @param sender the user who sends the message
|
||||
* @param recipient the user who receives the message
|
||||
* @param content the content of the message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public TextMessage(long id, User sender, User recipient, String content) {
|
||||
super(id, sender, recipient);
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,content=%s]",
|
||||
getId(),
|
||||
getSender(),
|
||||
getRecipient(),
|
||||
new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(getDate()),
|
||||
getStatus(),
|
||||
content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the content of this {@link TextMessage}
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public String getContent() { return content; }
|
||||
}
|
71
src/main/java/envoy/data/User.java
Normal file
71
src/main/java/envoy/data/User.java
Normal file
@ -0,0 +1,71 @@
|
||||
package envoy.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Represents a unique user with a unique, numeric ID, a name and a current
|
||||
* {@link UserStatus}.<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>User.java</strong><br>
|
||||
* Created: <strong>28.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public class User implements Serializable {
|
||||
|
||||
private final long id;
|
||||
private final String name;
|
||||
|
||||
private UserStatus status;
|
||||
|
||||
private static final long serialVersionUID = 3530947374856708236L;
|
||||
|
||||
/**
|
||||
* Initializes a {@link User}. The {@link UserStatus} is set to
|
||||
* {@link UserStatus#OFFLINE}.
|
||||
*
|
||||
* @param id unique ID
|
||||
* @param name user name
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public User(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
status = UserStatus.OFFLINE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() { return String.format("User[id=%d,name=%s,status=%s]", id, name, status); }
|
||||
|
||||
/**
|
||||
* @return the ID of this {@link User}
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public long getId() { return id; }
|
||||
|
||||
/**
|
||||
* @return the name of this {@link User}
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public String getName() { return name; }
|
||||
|
||||
/**
|
||||
* @return the current status of this user
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public UserStatus getStatus() { return status; }
|
||||
|
||||
/**
|
||||
* Sets the current status of this user
|
||||
*
|
||||
* @param status the status to set
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public void setStatus(UserStatus status) { this.status = status; }
|
||||
|
||||
public static enum UserStatus {
|
||||
ONLINE, AWAY, BUSY, OFFLINE;
|
||||
}
|
||||
}
|
19
src/main/java/envoy/event/Event.java
Normal file
19
src/main/java/envoy/event/Event.java
Normal file
@ -0,0 +1,19 @@
|
||||
package envoy.event;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>Event.java</strong><br>
|
||||
* Created: <strong>04.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @param <T> the type of the Event
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public interface Event<T> {
|
||||
|
||||
/**
|
||||
* @return the data associated with this event
|
||||
*/
|
||||
T get();
|
||||
}
|
||||
|
81
src/main/java/envoy/event/EventBus.java
Normal file
81
src/main/java/envoy/event/EventBus.java
Normal file
@ -0,0 +1,81 @@
|
||||
package envoy.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This class handles events by allowing {@link EventHandler} object to register
|
||||
* themselves and then be notified about certain events dispatched by the event
|
||||
* bus.<br>
|
||||
* <br>
|
||||
* The event bus is a singleton and can be used across the entire application to
|
||||
* guarantee the propagation of events.<br>
|
||||
*
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>EventBus.java</strong><br>
|
||||
* Created: <strong>04.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public class EventBus {
|
||||
|
||||
/**
|
||||
* Contains all {@link EventHandler} instances registered at this
|
||||
* {@link EventBus} as values mapped to by their supported {@link Event}
|
||||
* classes.
|
||||
*/
|
||||
private Map<Class<? extends Event<?>>, List<EventHandler>> handlers = new HashMap<>();
|
||||
|
||||
/**
|
||||
* The singleton instance of this {@link EventBus} that is used across the
|
||||
* entire application.
|
||||
*/
|
||||
private static EventBus eventBus = new EventBus();
|
||||
|
||||
/**
|
||||
* This constructor is not accessible from outside this class because a
|
||||
* singleton instance of it is provided by the {@link EventBus#getInstance()}
|
||||
* method.
|
||||
*/
|
||||
private EventBus() {}
|
||||
|
||||
/**
|
||||
* @return the singleton instance of the {@link EventBus}
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public static EventBus getInstance() { return eventBus; }
|
||||
|
||||
/**
|
||||
* Registers an {@link EventHandler} to be notified when a
|
||||
* {@link Event} of a certain type is dispatched.
|
||||
*
|
||||
* @param eventClass the class which the {@link EventHandler} is subscribed to
|
||||
* @param handler the {@link EventHandler} to register
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public void register(Class<? extends Event<?>> eventClass, EventHandler handler) {
|
||||
if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>());
|
||||
handlers.get(eventClass).add(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a {@link Event} to every {@link EventHandler} subscribed to it.
|
||||
*
|
||||
* @param event the {@link Event} to dispatch
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public void dispatch(Event<?> event) {
|
||||
handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.handle(event));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a map of all {@link EventHandler} instances currently registered at
|
||||
* this {@link EventBus} with the {@link Event} classes they are
|
||||
* subscribed to as keys
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public Map<Class<? extends Event<?>>, List<EventHandler>> getHandlers() { return handlers; }
|
||||
}
|
18
src/main/java/envoy/event/EventHandler.java
Normal file
18
src/main/java/envoy/event/EventHandler.java
Normal file
@ -0,0 +1,18 @@
|
||||
package envoy.event;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>EventHandler.java</strong><br>
|
||||
* Created: <strong>04.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
*/
|
||||
public interface EventHandler {
|
||||
|
||||
/**
|
||||
* Consumes an event dispatched by the event bus.
|
||||
*
|
||||
* @param event The event dispatched by the event bus, only of supported type
|
||||
*/
|
||||
void handle(Event<?> event);
|
||||
}
|
27
src/main/java/envoy/event/MessageEvent.java
Normal file
27
src/main/java/envoy/event/MessageEvent.java
Normal file
@ -0,0 +1,27 @@
|
||||
package envoy.event;
|
||||
|
||||
import envoy.data.Message;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>MessageCreationEvent.java</strong><br>
|
||||
* Created: <strong>4 Dec 2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
*/
|
||||
public class MessageEvent implements Event<Message> {
|
||||
|
||||
protected final Message message;
|
||||
|
||||
/**
|
||||
* Initializes a {@link MessageEvent} conveying information about a
|
||||
* {@link Message} object.
|
||||
*
|
||||
* @param message the {@link Message} object to attach to this event
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public MessageEvent(Message message) { this.message = message; }
|
||||
|
||||
@Override
|
||||
public Message get() { return message; }
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package envoy.schema;
|
||||
|
||||
import javax.xml.bind.annotation.XmlEnum;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>MessageState.java</strong><br>
|
||||
* Created: <strong>11 Oct 2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
*/
|
||||
@XmlType(name = "")
|
||||
@XmlEnum
|
||||
public enum MessageState {
|
||||
|
||||
Waiting, Sent, Received, Read;
|
||||
|
||||
public String value() { return name(); }
|
||||
|
||||
public static MessageState fromValue(String v) { return valueOf(v); }
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package envoy.schema;
|
||||
|
||||
import javax.xml.bind.annotation.XmlEnum;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>UserStatus.java</strong><br>
|
||||
* Created: <strong>27 Oct 2019</strong><br>
|
||||
*
|
||||
* @author Maximilian Käfer
|
||||
*/
|
||||
@XmlType(name = "")
|
||||
@XmlEnum
|
||||
public enum UserStatus {
|
||||
|
||||
Online, DoNotDisturb, AFK, Offline;
|
||||
|
||||
public String value() { return name(); }
|
||||
|
||||
public static UserStatus fromValue(String v) { return valueOf(v); }
|
||||
}
|
94
src/main/java/envoy/util/SerializationUtils.java
Normal file
94
src/main/java/envoy/util/SerializationUtils.java
Normal file
@ -0,0 +1,94 @@
|
||||
package envoy.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
import envoy.exception.EnvoyException;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>SerializationUtils.java</strong><br>
|
||||
* Created: <strong>23.12.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public class SerializationUtils {
|
||||
|
||||
private SerializationUtils() {}
|
||||
|
||||
/**
|
||||
* Deserializes an arbitrary {@link Serializable} object from a file.
|
||||
*
|
||||
* @param <T> the type of the object to deserialize
|
||||
* @param file the file deserialize from
|
||||
* @param serializedClass the class of the object to deserialize
|
||||
* @return the deserialized object
|
||||
* @throws EnvoyException if an error occurred during deserialization
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public static <T extends Serializable> T read(File file, Class<T> serializedClass) throws IOException, ClassNotFoundException {
|
||||
if (file == null) throw new NullPointerException("File is null");
|
||||
return read(new FileInputStream(file), serializedClass);
|
||||
}
|
||||
|
||||
public static <T extends Serializable> T read(InputStream in, Class<T> serializedClass) throws IOException, ClassNotFoundException {
|
||||
try (ObjectInputStream oin = new ObjectInputStream(in)) {
|
||||
return serializedClass.cast(oin.readObject());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes an arbitrary object to a file.
|
||||
*
|
||||
* @param file the file to serialize to
|
||||
* @param obj the object to serialize
|
||||
* @throws IOException if an error occurred during serialization
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public static void write(File file, Object obj) throws IOException {
|
||||
if (file == null) throw new NullPointerException("File is null");
|
||||
if (obj == null) throw new NullPointerException("Object to serialize is null");
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
}
|
||||
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
|
||||
out.writeObject(obj);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes an object and writes it into an output stream preceded by 4 bytes
|
||||
* containing the number of serialized bytes.
|
||||
*
|
||||
* @param obj the object to serialize
|
||||
* @param out the output stream to serialize to
|
||||
* @throws IOException if an error occurred during serialization
|
||||
*/
|
||||
public static void writeBytesWithLength(Object obj, OutputStream out) throws IOException {
|
||||
// Serialize object to byte array
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
|
||||
oout.writeObject(obj);
|
||||
}
|
||||
byte[] objBytes = baos.toByteArray();
|
||||
|
||||
// Get length of byte array in bytes
|
||||
byte[] objLen = intToBytes(objBytes.length);
|
||||
|
||||
// Write length and byte array
|
||||
out.write(objLen);
|
||||
out.write(objBytes);
|
||||
}
|
||||
|
||||
private static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; }
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="https://github.com/informatik-ag-ngl"
|
||||
xmlns="https://github.com/informatik-ag-ngl"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<xs:element name="Sync">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Message" type="Message" minOccurs="0"
|
||||
maxOccurs="unbounded" />
|
||||
<xs:element name="User" type="User" minOccurs="0"
|
||||
maxOccurs="unbounded" />
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
|
||||
<xs:complexType name="Message">
|
||||
<xs:sequence>
|
||||
<xs:element name="Metadata">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Sender" type="xs:long" />
|
||||
<xs:element name="Recipient" type="xs:long" />
|
||||
<xs:element name="Date" type="xs:dateTime" />
|
||||
<xs:element name="MessageId" type="xs:long" />
|
||||
<xs:element name="State">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:enumeration value="Waiting" />
|
||||
<xs:enumeration value="Sent" />
|
||||
<xs:enumeration value="Received" />
|
||||
<xs:enumeration value="Read" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
<xs:element name="Content" maxOccurs="unbounded">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="text" type="xs:string" />
|
||||
</xs:sequence>
|
||||
<xs:attribute name="type" type="xs:string" />
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
|
||||
|
||||
<xs:complexType name="User">
|
||||
<xs:sequence>
|
||||
<xs:element name="Name" type="xs:string" />
|
||||
<xs:element name="ID" type="xs:long" />
|
||||
<xs:element name="Status">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:enumeration value="Online" />
|
||||
<xs:enumeration value="DoNotDisturb" />
|
||||
<xs:enumeration value="AFK" />
|
||||
<xs:enumeration value="Offline" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
|
||||
</xs:schema>
|
@ -1,33 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE xml>
|
||||
<jaxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
|
||||
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
|
||||
jaxb:extensionBindingPrefixes="xjc" version="2.1">
|
||||
<jaxb:globalBindings generateIsSetMethod="true"
|
||||
fixedAttributeAsConstantProperty="true">
|
||||
<xjc:serializable />
|
||||
</jaxb:globalBindings>
|
||||
<jaxb:bindings
|
||||
schemaLocation="../resources/sync_schema.xsd">
|
||||
|
||||
<jaxb:bindings node="//xs:element[@name='Message']">
|
||||
<jaxb:property name="Messages"/>
|
||||
</jaxb:bindings>
|
||||
|
||||
<jaxb:bindings node="//xs:element[@name='User']">
|
||||
<jaxb:property name="Users"/>
|
||||
</jaxb:bindings>
|
||||
|
||||
<jaxb:bindings
|
||||
node="//xs:element[@name='State']/xs:simpleType">
|
||||
<jaxb:typesafeEnumClass name="MessageState" />
|
||||
</jaxb:bindings>
|
||||
|
||||
<jaxb:bindings
|
||||
node="//xs:element[@name='Status']/xs:simpleType">
|
||||
<jaxb:typesafeEnumClass name="UserStatus" />
|
||||
</jaxb:bindings>
|
||||
|
||||
</jaxb:bindings>
|
||||
</jaxb:bindings>
|
Reference in New Issue
Block a user