Renamed SavedData to ManagedData

This commit is contained in:
Norbi Peti 2016-08-11 12:29:16 +02:00
parent c3fe5f2ebd
commit b81f1b2a81
19 changed files with 69 additions and 69 deletions

View file

@ -31,11 +31,11 @@ public class Main {
DataManager.init();
final GsonBuilder gsonBuilder = new GsonBuilder();
Reflections rf = new Reflections(
new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(SavedData.class.getClassLoader()))
.addClassLoader(SavedData.class.getClassLoader()).addScanners(new SubTypesScanner())
.filterInputsBy((String pkg) -> pkg.contains(SavedData.class.getPackage().getName())));
Set<Class<? extends SavedData>> datas = rf.getSubTypesOf(SavedData.class);
for (Class<? extends SavedData> data : datas) {
new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(ManagedData.class.getClassLoader()))
.addClassLoader(ManagedData.class.getClassLoader()).addScanners(new SubTypesScanner())
.filterInputsBy((String pkg) -> pkg.contains(ManagedData.class.getPackage().getName())));
Set<Class<? extends ManagedData>> datas = rf.getSubTypesOf(ManagedData.class);
for (Class<? extends ManagedData> data : datas) {
if (Modifier.isAbstract(data.getModifiers()))
continue;
gsonBuilder.registerTypeAdapter(new DataType(LoaderCollection.class, data),

View file

@ -12,7 +12,7 @@ import java.util.Map.Entry;
import com.google.common.io.Files;
import io.github.norbipeti.chat.server.Main;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
public final class DataManager {
private DataManager() {
@ -20,7 +20,7 @@ public final class DataManager {
private static final File datafolder = new File("data");
public static <T extends SavedData> void save(T object) {
public static <T extends ManagedData> void save(T object) {
try {
File file = new File(datafolder, getFileName(object.getClass(), object.getId()));
cache.put(file, object);
@ -30,11 +30,11 @@ public final class DataManager {
}
}
public static <T extends SavedData> T load(Class<T> cl, long id) {
public static <T extends ManagedData> T load(Class<T> cl, long id) {
return loadFromFile(new File(datafolder, getFileName(cl, id)), cl);
}
public static <T extends SavedData> LoaderCollection<T> getAll(Class<T> cl) {
public static <T extends ManagedData> LoaderCollection<T> getAll(Class<T> cl) {
String[] filenames = datafolder.list(new FilenameFilter() {
@Override
@ -59,10 +59,10 @@ public final class DataManager {
// TODO: Handle unloading of used objects (prevent detached objects)
@SuppressWarnings("unchecked")
private static <T extends SavedData> T loadFromFile(File file, Class<T> cl) {
private static <T extends ManagedData> T loadFromFile(File file, Class<T> cl) {
try {
if (!file.exists()) {
T obj = SavedData.create(cl);
T obj = ManagedData.create(cl);
return obj;
}
if (cache.containsKey(file))
@ -83,18 +83,18 @@ public final class DataManager {
return null;
}
public static <T extends SavedData> boolean remove(T obj) {
public static <T extends ManagedData> boolean remove(T obj) {
if (cache.containsValue(obj))
cache.values().remove(obj);
return new File(obj.getClass().getSimpleName() + "-" + obj.getId()).delete();
}
public static <T extends SavedData> boolean remove(Class<T> cl, Long id) {
public static <T extends ManagedData> boolean remove(Class<T> cl, Long id) {
return new File(cl.getName() + "-" + id).delete();
}
public static void init() {
packagename = SavedData.class.getPackage().getName();
packagename = ManagedData.class.getPackage().getName();
datafolder.mkdir();
nextids = loadNextIDs();
}
@ -108,32 +108,32 @@ public final class DataManager {
public static void save() {
saveNextIDs(nextids);
for (Entry<File, Object> item : cache.entrySet()) {
DataManager.save((SavedData) item.getValue());
DataManager.save((ManagedData) item.getValue());
}
}
private static HashMap<Class<? extends SavedData>, Long> nextids;
private static HashMap<Class<? extends ManagedData>, Long> nextids;
public static Map<Class<? extends SavedData>, Long> getNextIDs() {
public static Map<Class<? extends ManagedData>, Long> getNextIDs() {
return Collections.unmodifiableMap(nextids);
}
public static void setNextID(Class<? extends SavedData> cl, Long id) {
public static void setNextID(Class<? extends ManagedData> cl, Long id) {
nextids.put(cl, id);
}
@SuppressWarnings("unchecked")
private static HashMap<Class<? extends SavedData>, Long> loadNextIDs() {
private static HashMap<Class<? extends ManagedData>, Long> loadNextIDs() {
try {
File file = new File("data", "idlist.ini");
if (!file.exists())
return new HashMap<>();
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
String line;
HashMap<Class<? extends SavedData>, Long> ret = new HashMap<>();
HashMap<Class<? extends ManagedData>, Long> ret = new HashMap<>();
while ((line = reader.readLine()) != null) {
String[] spl = line.split("\\=");
ret.put((Class<? extends SavedData>) Class.forName(packagename + "." + spl[0]), Long.parseLong(spl[1]));
ret.put((Class<? extends ManagedData>) Class.forName(packagename + "." + spl[0]), Long.parseLong(spl[1]));
}
return ret;
} catch (Exception e) {
@ -142,11 +142,11 @@ public final class DataManager {
return new HashMap<>();
}
private static void saveNextIDs(HashMap<Class<? extends SavedData>, Long> ids) {
private static void saveNextIDs(HashMap<Class<? extends ManagedData>, Long> ids) {
try {
File file = new File("data", "idlist.ini");
String contents = "";
for (Entry<Class<? extends SavedData>, Long> item : ids.entrySet()) {
for (Entry<Class<? extends ManagedData>, Long> item : ids.entrySet()) {
contents += item.getKey().getSimpleName() + "=" + item.getValue() + "\n";
}
Files.write(contents, file, StandardCharsets.UTF_8);
@ -155,7 +155,7 @@ public final class DataManager {
}
}
private static <T extends SavedData> String getFileName(Class<T> cl, Long id) {
private static <T extends ManagedData> String getFileName(Class<T> cl, Long id) {
if (id != null)
return cl.getSimpleName() + "-" + id + ".json";
else

View file

@ -7,7 +7,7 @@ import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
/**
* <p>
@ -21,7 +21,7 @@ import io.github.norbipeti.chat.server.db.domain.SavedData;
*
* @param <T>
*/
public class LoaderCollection<T extends SavedData> extends Loader implements List<T> {
public class LoaderCollection<T extends ManagedData> extends Loader implements List<T> {
private static final long serialVersionUID = 5426152406394894301L;
List<Long> idlist;
Class<T> cl;
@ -69,7 +69,7 @@ public class LoaderCollection<T extends SavedData> extends Loader implements Lis
@Override
public boolean addAll(Collection<? extends T> c) {
return idlist.addAll(c.stream().map((data) -> {
SavedData cde = ((SavedData) data);
ManagedData cde = ((ManagedData) data);
DataManager.save(cde);
return cde.getId();
}).collect(Collectors.toList()));
@ -78,7 +78,7 @@ public class LoaderCollection<T extends SavedData> extends Loader implements Lis
@Override
public boolean addAll(int index, Collection<? extends T> c) {
return idlist.addAll(index, c.stream().map((data) -> {
SavedData cde = ((SavedData) data);
ManagedData cde = ((ManagedData) data);
DataManager.save(cde);
return cde.getId();
}).collect(Collectors.toList()));
@ -137,9 +137,9 @@ public class LoaderCollection<T extends SavedData> extends Loader implements Lis
*/
@Override
public boolean remove(Object o) {
if (SavedData.class.isAssignableFrom(o.getClass())) {
DataManager.remove((SavedData) o);
return idlist.remove(((SavedData) o).getId());
if (ManagedData.class.isAssignableFrom(o.getClass())) {
DataManager.remove((ManagedData) o);
return idlist.remove(((ManagedData) o).getId());
}
if (Long.class.isAssignableFrom(o.getClass()))
DataManager.remove(cl, (Long) o);
@ -155,8 +155,8 @@ public class LoaderCollection<T extends SavedData> extends Loader implements Lis
public boolean removeAll(Collection<?> c) {
boolean success = false;
for (Object item : c) {
if (SavedData.class.isAssignableFrom(item.getClass())) {
if (idlist.remove(((SavedData) item).getId())) {
if (ManagedData.class.isAssignableFrom(item.getClass())) {
if (idlist.remove(((ManagedData) item).getId())) {
success = true;
break;
}
@ -174,8 +174,8 @@ public class LoaderCollection<T extends SavedData> extends Loader implements Lis
public boolean retainAll(Collection<?> c) {
List<Long> list = new ArrayList<Long>();
for (Object item : c) {
if (SavedData.class.isAssignableFrom(item.getClass())) {
list.add(((SavedData) item).getId());
if (ManagedData.class.isAssignableFrom(item.getClass())) {
list.add(((ManagedData) item).getId());
} else if (Long.class.isAssignableFrom(item.getClass())) {
list.add((Long) item);
}

View file

@ -10,7 +10,7 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
// @SuppressWarnings("rawtypes")
public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<?>> {
@ -33,7 +33,7 @@ public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<?>>
@SuppressWarnings("unchecked")
@Override
public LoaderCollection<? extends SavedData> read(JsonReader in) throws IOException {
public LoaderCollection<? extends ManagedData> read(JsonReader in) throws IOException {
if (in.peek().equals(JsonToken.NULL)) {
in.nextNull();
return null;
@ -46,14 +46,14 @@ public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<?>>
new Exception("Error: Next isn't \"class\"").printStackTrace();
return null;
}
Class<? extends SavedData> cl;
Class<? extends ManagedData> cl;
try {
cl = (Class<? extends SavedData>) Class.forName(DataManager.getPackageName() + "." + in.nextString());
cl = (Class<? extends ManagedData>) Class.forName(DataManager.getPackageName() + "." + in.nextString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
LoaderCollection<? extends SavedData> col;
LoaderCollection<? extends ManagedData> col;
try {
col = LoaderCollection.class.getDeclaredConstructor(Class.class).newInstance(cl);
} catch (Exception e) {

View file

@ -1,9 +1,9 @@
package io.github.norbipeti.chat.server.data;
import java.util.Iterator;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
public final class LoaderIterator<T extends SavedData> implements Iterator<T> {
public final class LoaderIterator<T extends ManagedData> implements Iterator<T> {
private Iterator<Long> iterator;
private T lastitem;
private Class<T> cl;

View file

@ -2,9 +2,9 @@ package io.github.norbipeti.chat.server.data;
import java.util.ListIterator;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
public final class LoaderListIterator<T extends SavedData> implements ListIterator<T> {
public final class LoaderListIterator<T extends ManagedData> implements ListIterator<T> {
private ListIterator<Long> listiterator;
private T lastitem;
private Class<T> cl;

View file

@ -1,6 +1,6 @@
package io.github.norbipeti.chat.server.data;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
/**
* <p>
@ -13,7 +13,7 @@ import io.github.norbipeti.chat.server.db.domain.SavedData;
* @author Norbi
* @param <T> The type of the stored object
*/
public class LoaderRef<T extends SavedData> extends Loader {
public class LoaderRef<T extends ManagedData> extends Loader {
private static final long serialVersionUID = 8458570738734235320L;
Class<T> cl;
Long id;

View file

@ -6,7 +6,7 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
// @SuppressWarnings("rawtypes")
public class LoaderRefSerializer extends TypeAdapter<LoaderRef<?>> {
@ -39,14 +39,14 @@ public class LoaderRefSerializer extends TypeAdapter<LoaderRef<?>> {
new Exception("Error: Next isn't \"class\"").printStackTrace();
return null;
}
Class<? extends SavedData> cl;
Class<? extends ManagedData> cl;
try {
cl = (Class<? extends SavedData>) Class.forName(DataManager.getPackageName() + "." + in.nextString());
cl = (Class<? extends ManagedData>) Class.forName(DataManager.getPackageName() + "." + in.nextString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
LoaderRef<? extends SavedData> ref;
LoaderRef<? extends ManagedData> ref;
try {
ref = LoaderRef.class.getDeclaredConstructor(Class.class, Long.class).newInstance(cl, id);
} catch (Exception e) {

View file

@ -20,7 +20,7 @@ public class DataProvider implements AutoCloseable {
em.getTransaction().begin();
}
public <T extends SavedData> T save(T object) {
public <T extends ManagedData> T save(T object) {
T obj = em.merge(object);
return obj;
}

View file

@ -6,7 +6,7 @@ import io.github.norbipeti.chat.server.data.LoaderCollection;
@Entity
@Table(name = "CONVERSATION")
public class Conversation extends SavedData {
public class Conversation extends ManagedData {
private static final long serialVersionUID = 5058682475353799722L;
// @Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)

View file

@ -6,17 +6,17 @@ import java.lang.reflect.Constructor;
import io.github.norbipeti.chat.server.data.DataManager;
@SuppressWarnings("serial")
public abstract class SavedData implements Serializable {
public abstract class ManagedData implements Serializable {
public abstract long getId();
public abstract void setId(long id);
protected abstract void init();
protected SavedData() {
protected ManagedData() {
}
static <T extends SavedData> T create(Class<T> cl) {
static <T extends ManagedData> T create(Class<T> cl) {
T obj;
try {
Constructor<T> constructor = cl.getDeclaredConstructor(new Class<?>[0]);

View file

@ -82,7 +82,7 @@ public class Message implements Serializable {
int size = parent.getMesssageChunks().size();
MessageChunk chunk;
if (size == 0 || parent.getMesssageChunks().get(size - 1).getMessages().size() >= MESSAGE_LIMIT_PER_CHUNK) {
chunk = SavedData.create(MessageChunk.class);
chunk = ManagedData.create(MessageChunk.class);
chunk.setConversation(parent);
parent.getMesssageChunks().add(chunk);
} else

View file

@ -5,7 +5,7 @@ import java.util.List;
import io.github.norbipeti.chat.server.data.LoaderRef;
public class MessageChunk extends SavedData {
public class MessageChunk extends ManagedData {
private static final long serialVersionUID = -1665300779209348467L;
private Long id;

View file

@ -9,7 +9,7 @@ import io.github.norbipeti.chat.server.data.LoaderCollection;
@Entity
@Table(name = "\"USER\"")
public class User extends SavedData {
public class User extends ManagedData {
private static final long serialVersionUID = 2862762084164225666L;
private Long id;
private String name;
@ -110,6 +110,6 @@ public class User extends SavedData {
}
public static User createUser() {
return SavedData.create(User.class);
return ManagedData.create(User.class);
}
}

View file

@ -4,13 +4,13 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import io.github.norbipeti.chat.server.data.Loader;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
public final class DataType implements ParameterizedType {
private Class<? extends SavedData> datacl;
private Class<? extends ManagedData> datacl;
private Class<? extends Loader> loadercl;
public DataType(Class<? extends Loader> loadercl, Class<? extends SavedData> datacl) {
public DataType(Class<? extends Loader> loadercl, Class<? extends ManagedData> datacl) {
this.datacl = datacl;
this.loadercl = loadercl;
}

View file

@ -15,7 +15,7 @@ import io.github.norbipeti.chat.server.data.LoaderCollection;
import io.github.norbipeti.chat.server.db.domain.Conversation;
import io.github.norbipeti.chat.server.db.domain.Message;
import io.github.norbipeti.chat.server.db.domain.MessageChunk;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
import io.github.norbipeti.chat.server.db.domain.User;
import io.github.norbipeti.chat.server.io.IOHelper;

View file

@ -12,7 +12,7 @@ import io.github.norbipeti.chat.server.data.LoaderCollection;
import io.github.norbipeti.chat.server.db.domain.Conversation;
import io.github.norbipeti.chat.server.db.domain.Message;
import io.github.norbipeti.chat.server.db.domain.MessageChunk;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
import io.github.norbipeti.chat.server.db.domain.User;
import io.github.norbipeti.chat.server.io.IOHelper;
@ -52,7 +52,7 @@ public class ReceiveMessageAjaxPage extends Page {
+ conversation + " is not found.</p>", exchange);
return;
}
MessageChunk chunk = SavedData.create(MessageChunk.class);
MessageChunk chunk = ManagedData.create(MessageChunk.class);
chunk.setConversation(conv);
Message msg = new Message();
msg.setSender(user);

View file

@ -7,7 +7,7 @@ import com.google.gson.JsonObject;
import com.sun.net.httpserver.HttpExchange;
import io.github.norbipeti.chat.server.data.DataManager;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
import io.github.norbipeti.chat.server.db.domain.User;
import io.github.norbipeti.chat.server.io.IOHelper;
@ -35,7 +35,7 @@ public class RegisterAjaxPage extends Page {
IOHelper.SendResponse(200, (doc) -> doc.html(msg).ownerDocument(), exchange);
return;
}
User user = SavedData.create(User.class);
User user = ManagedData.create(User.class);
user.setName(post.get("name").getAsString());
user.setEmail(post.get("email").getAsString());
user.setSalt(BCrypt.gensalt()); // http://www.mindrot.org/projects/jBCrypt/

View file

@ -12,7 +12,7 @@ import io.github.norbipeti.chat.server.data.LoaderCollection;
import io.github.norbipeti.chat.server.db.domain.Conversation;
import io.github.norbipeti.chat.server.db.domain.Message;
import io.github.norbipeti.chat.server.db.domain.MessageChunk;
import io.github.norbipeti.chat.server.db.domain.SavedData;
import io.github.norbipeti.chat.server.db.domain.ManagedData;
import io.github.norbipeti.chat.server.db.domain.User;
import io.github.norbipeti.chat.server.io.IOHelper;
@ -64,7 +64,7 @@ public class SendMessageAjaxPage extends Page {
+ conversation + " is not found.</p>", exchange);
return;
}
MessageChunk chunk = SavedData.create(MessageChunk.class); // TODO: Automatize
MessageChunk chunk = ManagedData.create(MessageChunk.class); // TODO: Automatize
chunk.setConversation(conv);
Message msg = new Message();
msg.setSender(user);