Begin restructuring using Griffon
This commit is contained in:
21
src/main/com/jdbernard/timestamper/core/AuthenticationException.java
Executable file
21
src/main/com/jdbernard/timestamper/core/AuthenticationException.java
Executable file
@ -0,0 +1,21 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public class AuthenticationException extends IOException {
|
||||
|
||||
public AuthenticationException() { super(); }
|
||||
|
||||
public AuthenticationException(String message) { super(message); }
|
||||
|
||||
public AuthenticationException(Throwable t) { super(t); }
|
||||
|
||||
public AuthenticationException(String message, Throwable t) {
|
||||
super(message, t);
|
||||
}
|
||||
|
||||
}
|
64
src/main/com/jdbernard/timestamper/core/FileTimelineSource.java
Executable file
64
src/main/com/jdbernard/timestamper/core/FileTimelineSource.java
Executable file
@ -0,0 +1,64 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public class FileTimelineSource extends TimelineSource {
|
||||
|
||||
private File file;
|
||||
|
||||
public FileTimelineSource(URI uri) {
|
||||
super(uri);
|
||||
this.file = new File(uri);
|
||||
}
|
||||
|
||||
/** * {@inheritDoc } */
|
||||
public Timeline read() throws IOException {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
Timeline t = StreamBasedTimelineSource.readFromStream(fin);
|
||||
fin.close();
|
||||
return t;
|
||||
}
|
||||
|
||||
public Timeline readWithComments(OutputStream commentStream)
|
||||
throws IOException {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
Timeline t = StreamBasedTimelineSource.readFromStream(fin, commentStream);
|
||||
fin.close();
|
||||
return t;
|
||||
}
|
||||
|
||||
/** * {@inheritDoc } */
|
||||
public void persist(Timeline t) throws IOException {
|
||||
FileOutputStream fout = new FileOutputStream(file);
|
||||
StreamBasedTimelineSource.writeToStream(fout, t);
|
||||
fout.close();
|
||||
}
|
||||
|
||||
public boolean isAuthenticated() {
|
||||
File dir = file.getParentFile();
|
||||
return (dir.canRead() && dir.canWrite());
|
||||
}
|
||||
|
||||
public void authenticate() throws AuthenticationException {
|
||||
File dir = file.getParentFile();
|
||||
if (!dir.canRead())
|
||||
throw new AuthenticationException("This FileTimelineSource does not"
|
||||
+ " have read access to the parent directory for the "
|
||||
+ "given file (" + file.getAbsolutePath() + ".");
|
||||
|
||||
if (!dir.canWrite())
|
||||
throw new AuthenticationException("This FileTimelineSource does not"
|
||||
+ " have write access to the parent directory for the "
|
||||
+ "given file (" + file.getAbsolutePath() + ".");
|
||||
|
||||
}
|
||||
}
|
203
src/main/com/jdbernard/timestamper/core/StreamBasedTimelineSource.java
Executable file
203
src/main/com/jdbernard/timestamper/core/StreamBasedTimelineSource.java
Executable file
@ -0,0 +1,203 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Writer;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public class StreamBasedTimelineSource extends TimelineSource {
|
||||
|
||||
private static final int lineWrap = 78;
|
||||
|
||||
private static enum ReadingState {
|
||||
NewMarker,
|
||||
StartMark,
|
||||
ReadMark,
|
||||
StartNotes,
|
||||
ReadNotes,
|
||||
EndMarker
|
||||
};
|
||||
|
||||
|
||||
private InputStream in;
|
||||
private OutputStream out;
|
||||
private ByteArrayOutputStream comments;
|
||||
|
||||
public StreamBasedTimelineSource(InputStream inStream,
|
||||
OutputStream outStream) {
|
||||
super(null);
|
||||
this.in = inStream;
|
||||
this.out = outStream;
|
||||
this.comments = new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
/** {@inheritDoc } */
|
||||
public Timeline read() throws IOException {
|
||||
return readFromStream(in, comments);
|
||||
}
|
||||
|
||||
/** {@inheritDoc } */
|
||||
public void persist(Timeline t) throws IOException {
|
||||
writeToStream(out, t);
|
||||
}
|
||||
|
||||
public void authenticate() throws AuthenticationException { }
|
||||
public boolean isAuthenticated() { return true; }
|
||||
|
||||
/**
|
||||
* Allows a user to extract the comments from the last parsed file.
|
||||
* @return a <code>byte[]</code> representing the portions of the file
|
||||
* which were comments.
|
||||
*/
|
||||
public byte[] getCommentBytes() {
|
||||
return comments.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the a representation of the timeline to a stream. This method
|
||||
* flushes the stream after it finishes writing but does not close the
|
||||
* stream.
|
||||
* @param stream An open stream to write the timeline representation to.
|
||||
* @param timeline The timeline to write.
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public static void writeToStream(OutputStream stream, Timeline timeline)
|
||||
throws IOException {
|
||||
Writer out = new OutputStreamWriter(stream);
|
||||
for (TimelineMarker tm : timeline) {
|
||||
|
||||
// write timestamp
|
||||
out.write(Timeline.longFormat.format(tm.getTimestamp()) + "\n");
|
||||
|
||||
// write mark
|
||||
String mark = tm.getMark().replace('\n', '\u0000');
|
||||
if (mark.length() < lineWrap) out.write(mark + "\n");
|
||||
else {
|
||||
// wrap lines if neccessary
|
||||
int i;
|
||||
for (i = 0; (i + lineWrap) < mark.length(); i+=lineWrap)
|
||||
out.write(mark.substring(i, i+lineWrap) + "\\\n");
|
||||
if (i < mark.length())
|
||||
out.write(mark.substring(i, mark.length()) + "\n");
|
||||
}
|
||||
|
||||
// write notes
|
||||
String notes = tm.getNotes().replace('\n', '\u0000');
|
||||
if (notes.length() < lineWrap) out.write(notes + "\n");
|
||||
else {
|
||||
// wrap lines if neccessary
|
||||
int i;
|
||||
for (i = 0; (i + lineWrap) < notes.length(); i+=lineWrap)
|
||||
out.write(notes.substring(i, i+lineWrap) + "\\\n");
|
||||
if (i < notes.length())
|
||||
out.write(notes.substring(i, notes.length()) + "\n");
|
||||
}
|
||||
out.write("\n");
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a <b>Timeline</b> instance from a given stream.
|
||||
* @param stream The stream to read from.
|
||||
* @return A new <b>Timeline</b> instance specified by the input stream.
|
||||
* @throws java.io.IOException
|
||||
* @throws java.io.FileNotFoundException
|
||||
*/
|
||||
public static Timeline readFromStream(InputStream stream)
|
||||
throws IOException {
|
||||
return readFromStream(stream, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a <b>Timeline</b> instance from a given stream.
|
||||
* @param stream The stream to read from.
|
||||
* @param commentStream A stream to write comments found in the file. This
|
||||
* parameter may be <b>null</b>, in which case comments are ignored.
|
||||
* @return A new <b>Timeline</b> instance specified by the input stream.
|
||||
* @throws java.io.IOException
|
||||
* @throws java.io.FileNotFoundException
|
||||
*/
|
||||
public static Timeline readFromStream(InputStream stream,
|
||||
OutputStream commentStream) throws IOException {
|
||||
Scanner in = new Scanner(stream);
|
||||
Timeline timeline = new Timeline();
|
||||
PrintWriter commentsWriter = commentStream == null ?
|
||||
null : new PrintWriter(commentStream);
|
||||
|
||||
ReadingState readingState = ReadingState.NewMarker;
|
||||
Date d = null;
|
||||
StringBuilder mark = null;
|
||||
StringBuilder notes = null;
|
||||
String line;
|
||||
int lineNumber = 0;
|
||||
|
||||
while (in.hasNextLine()) {
|
||||
line = in.nextLine();
|
||||
lineNumber++;
|
||||
|
||||
// line is a comment
|
||||
if (line.startsWith("#")) {
|
||||
if (commentsWriter != null) commentsWriter.println(line);
|
||||
continue; // don't parse this line as part of the timeline
|
||||
}
|
||||
|
||||
switch (readingState) {
|
||||
|
||||
case NewMarker:
|
||||
try { d = Timeline.longFormat.parse(line); }
|
||||
catch (ParseException pe) {
|
||||
throw new IOException("Error parsing timeline file at line "
|
||||
+ lineNumber + ": expected a new marker date but could not parse"
|
||||
+ " the date. Error: " + pe.getLocalizedMessage());
|
||||
}
|
||||
readingState = ReadingState.StartMark;
|
||||
break;
|
||||
|
||||
case StartMark:
|
||||
mark = new StringBuilder();
|
||||
// fall through to ReadMark
|
||||
case ReadMark:
|
||||
if (line.endsWith("\\")) {
|
||||
readingState = ReadingState.ReadMark;
|
||||
line = line.substring(0, line.length() - 1);
|
||||
}
|
||||
else readingState = ReadingState.StartNotes;
|
||||
mark.append(line);
|
||||
break;
|
||||
|
||||
case StartNotes:
|
||||
notes = new StringBuilder();
|
||||
// fall through to ReadNotes
|
||||
case ReadNotes:
|
||||
if (line.endsWith("\\")) {
|
||||
readingState = ReadingState.ReadNotes;
|
||||
line = line.substring(0, line.length() - 1);
|
||||
}
|
||||
else readingState = ReadingState.EndMarker;
|
||||
notes.append(line);
|
||||
break;
|
||||
|
||||
case EndMarker:
|
||||
String sMark = mark.toString().replace('\u0000', '\n');
|
||||
String sNotes = notes.toString().replace('\u0000', '\n');
|
||||
timeline.addMarker(d, sMark, sNotes);
|
||||
readingState = ReadingState.NewMarker;
|
||||
}
|
||||
}
|
||||
|
||||
return timeline;
|
||||
}
|
||||
}
|
129
src/main/com/jdbernard/timestamper/core/SyncTarget.java
Executable file
129
src/main/com/jdbernard/timestamper/core/SyncTarget.java
Executable file
@ -0,0 +1,129 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public class SyncTarget {
|
||||
|
||||
protected final TimelineSource source;
|
||||
protected final Timeline localTimeline;
|
||||
protected final String name;
|
||||
|
||||
protected Timer syncTimer;
|
||||
protected SyncTask syncTask;
|
||||
|
||||
protected long syncInterval= 30 * 60 * 1000; // 30 minutes converted to ms
|
||||
protected boolean pushEnabled = true;
|
||||
protected boolean pullEnabled = true;
|
||||
protected boolean syncOnExit = true;
|
||||
|
||||
protected class SyncTask extends TimerTask {
|
||||
@Override public void run() {
|
||||
synchronized(this) {
|
||||
try { SyncTarget.this.sync(); }
|
||||
catch (IOException ioe) { /* TODO */ }
|
||||
}}}
|
||||
|
||||
protected SyncTarget(String name, TimelineSource source,
|
||||
Timeline localTimeline) {
|
||||
this.name = name;
|
||||
this.source = source;
|
||||
this.localTimeline = localTimeline;
|
||||
|
||||
syncTimer = new Timer(source.toString() + " sync-timer");
|
||||
syncTask = new SyncTask();
|
||||
syncTimer.schedule(syncTask, syncInterval, syncInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync the local timeline with the remote timeline represented by this
|
||||
* SyncTarget object.
|
||||
* @return <b>true</b> if the two timelines were out of sync and have
|
||||
* been put into synch, <b>false</b> if the timelines were already in
|
||||
* sync and no action was requried.
|
||||
* @throws IOException if there is an error communicating with the remote
|
||||
* timeline. This includes AuthenticationException.
|
||||
*/
|
||||
protected boolean sync() throws IOException {
|
||||
assert (pullEnabled || pushEnabled);
|
||||
|
||||
Timeline remoteTimeline;
|
||||
boolean syncPerformed = false;
|
||||
|
||||
// make sure we're authenticated to whatever source we're using
|
||||
if (!SyncTarget.this.source.isAuthenticated()) {
|
||||
SyncTarget.this.source.authenticate();
|
||||
}
|
||||
|
||||
// try to copy the remote timeline locally
|
||||
remoteTimeline = SyncTarget.this.source.read();
|
||||
|
||||
// if we are pulling markers from the remote line
|
||||
if (SyncTarget.this.pullEnabled) {
|
||||
// get all markers in the remote timeline not in the local one
|
||||
Collection<TimelineMarker> diffFromRemote =
|
||||
remoteTimeline.difference(localTimeline);
|
||||
|
||||
if (diffFromRemote.size() != 0) {
|
||||
// add all markers in the remote tline to the local one
|
||||
localTimeline.addAll(diffFromRemote);
|
||||
syncPerformed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// if we are pushing markers to the remote timeline
|
||||
if (SyncTarget.this.pushEnabled) {
|
||||
// get all markers in the local timeline but not in the remote
|
||||
Collection<TimelineMarker> diffFromLocal =
|
||||
localTimeline.difference(remoteTimeline);
|
||||
|
||||
if (diffFromLocal.size() != 0) {
|
||||
// add the difference to the remote timeline
|
||||
remoteTimeline.addAll(diffFromLocal);
|
||||
syncPerformed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return syncPerformed;
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
|
||||
public TimelineSource getSource() { return source; }
|
||||
|
||||
public synchronized void setSyncInterval(long syncInterval) {
|
||||
this.syncInterval= syncInterval;
|
||||
|
||||
syncTask.cancel();
|
||||
syncTask = new SyncTask();
|
||||
|
||||
syncTimer.purge();
|
||||
syncTimer.schedule(syncTask, syncInterval, syncInterval);
|
||||
}
|
||||
|
||||
public long getSyncInterval() { return syncInterval; }
|
||||
|
||||
public synchronized void enablePush(boolean enablePush) {
|
||||
this.pullEnabled = enablePush;
|
||||
}
|
||||
|
||||
public boolean isPushEnabled() { return pushEnabled; }
|
||||
|
||||
public synchronized void enablePull(boolean enablePull) {
|
||||
this.pullEnabled = enablePull;
|
||||
}
|
||||
|
||||
public boolean isPullEnabled() { return pullEnabled; }
|
||||
|
||||
public synchronized void enableSyncOnExit(boolean syncOnExit) {
|
||||
this.syncOnExit = syncOnExit;
|
||||
}
|
||||
|
||||
public boolean isSyncOnExitEnabled() { return syncOnExit; }
|
||||
}
|
95
src/main/com/jdbernard/timestamper/core/Timeline.java
Executable file
95
src/main/com/jdbernard/timestamper/core/Timeline.java
Executable file
@ -0,0 +1,95 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* @author Jonathan Bernard {@literal <jdbernard@gmail.com>}
|
||||
* A Timeline object represents a series of markers at specific points in time.
|
||||
* The markers have a name or symbol (the 'mark') and notes associated with that
|
||||
* mark.
|
||||
*/
|
||||
public class Timeline implements Iterable<TimelineMarker> {
|
||||
|
||||
public static SimpleDateFormat shortFormat = new SimpleDateFormat("HH:mm:ss");
|
||||
public static SimpleDateFormat longFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
|
||||
private TreeSet<TimelineMarker> timelineList;
|
||||
|
||||
public Timeline() {
|
||||
timelineList = new TreeSet<TimelineMarker>();
|
||||
}
|
||||
|
||||
public void addMarker(Date timestamp, String name, String notes) {
|
||||
timelineList.add(new TimelineMarker(timestamp, name, notes));
|
||||
}
|
||||
|
||||
public String getLastName(Date timestamp) {
|
||||
TimelineMarker lastMarker = getLastMarker(timestamp);
|
||||
return (lastMarker == null ?
|
||||
"No previous marker." :
|
||||
lastMarker.getMark());
|
||||
|
||||
}
|
||||
|
||||
public String getLastNotes(Date timestamp) {
|
||||
TimelineMarker lastMarker = getLastMarker(timestamp);
|
||||
return (lastMarker == null ?
|
||||
"No previous marker." :
|
||||
lastMarker.getNotes());
|
||||
}
|
||||
|
||||
public TimelineMarker getLastMarker(Date timestamp) {
|
||||
TimelineMarker lastMarker = null;
|
||||
for (TimelineMarker tm : timelineList) {
|
||||
if (tm.getTimestamp().after(timestamp))
|
||||
break;
|
||||
lastMarker = tm;
|
||||
}
|
||||
|
||||
return lastMarker;
|
||||
}
|
||||
|
||||
public void removeMarker(TimelineMarker marker) {
|
||||
timelineList.remove(marker);
|
||||
}
|
||||
|
||||
public Iterator<TimelineMarker> iterator() {
|
||||
return timelineList.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the difference of the this timeline relative to another timeline.
|
||||
* More specifically, return the set of all
|
||||
* {@link jdbernard.timestamper.core.TimelineMarker}s that are present in
|
||||
* the <b>this</b> timeline but not present in the given timeline.
|
||||
* timeline.
|
||||
* @param t
|
||||
* @return A collection representing the TimelineMarkers present in
|
||||
* <b>this</b> timeline but not in the given timeline.
|
||||
*/
|
||||
public Collection<TimelineMarker> difference(Timeline t) {
|
||||
TreeSet<TimelineMarker> difference = new TreeSet<TimelineMarker>();
|
||||
|
||||
for (TimelineMarker tm : timelineList) {
|
||||
if (!t.timelineList.contains(tm))
|
||||
difference.add(tm);
|
||||
}
|
||||
|
||||
return difference;
|
||||
}
|
||||
|
||||
public void addAll(Timeline t) {
|
||||
for (TimelineMarker tm : t) {
|
||||
if (!timelineList.contains(tm))
|
||||
timelineList.add(tm);
|
||||
}
|
||||
}
|
||||
|
||||
public void addAll(Collection<TimelineMarker> c) {
|
||||
timelineList.addAll(c);
|
||||
}
|
||||
}
|
63
src/main/com/jdbernard/timestamper/core/TimelineMarker.java
Normal file
63
src/main/com/jdbernard/timestamper/core/TimelineMarker.java
Normal file
@ -0,0 +1,63 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Jonathan Bernard {@literal <jdbernard@gmail.com>}
|
||||
* This represents a marker on the timeline.
|
||||
* The date of the marker and the mark cannot be changed once assigned.
|
||||
*/
|
||||
public class TimelineMarker implements Comparable<TimelineMarker> {
|
||||
|
||||
private final Date timestamp;
|
||||
private final String mark;
|
||||
private String notes;
|
||||
|
||||
public TimelineMarker(Date timestamp, String mark, String notes) {
|
||||
if (timestamp == null || mark == null)
|
||||
throw new IllegalArgumentException("Null timestamp or mark"
|
||||
+ " is not permitted.");
|
||||
|
||||
this.timestamp = timestamp;
|
||||
this.mark = mark;
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
public Date getTimestamp() { return timestamp; }
|
||||
|
||||
public String getMark() { return mark; }
|
||||
|
||||
public String getNotes() { return notes; }
|
||||
|
||||
public void setNotes(String notes) { this.notes = notes; }
|
||||
|
||||
@Override
|
||||
public int compareTo(TimelineMarker that) {
|
||||
if (that == null) return Integer.MAX_VALUE;
|
||||
|
||||
int val = this.timestamp.compareTo(that.timestamp);
|
||||
if (val == 0) val = this.mark.compareTo(that.mark);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) return false;
|
||||
if (!(o instanceof TimelineMarker)) return false;
|
||||
|
||||
TimelineMarker that = (TimelineMarker) o;
|
||||
return this.timestamp.equals(that.timestamp) &&
|
||||
this.mark.equals(that.mark);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 7;
|
||||
hash = 61 * hash + (this.timestamp != null ? this.timestamp.hashCode() : 0);
|
||||
hash = 61 * hash + (this.mark != null ? this.mark.hashCode() : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
||||
|
163
src/main/com/jdbernard/timestamper/core/TimelineProperties.java
Executable file
163
src/main/com/jdbernard/timestamper/core/TimelineProperties.java
Executable file
@ -0,0 +1,163 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public class TimelineProperties {
|
||||
|
||||
public static final String LOCAL_TIMELINE_URI = "timeline.uri";
|
||||
public static final String REMOTE_TIMELINE_BASE = "remote.timeline.";
|
||||
|
||||
private static final Pattern remoteTimelinePropPattern =
|
||||
Pattern.compile("\\Q" + REMOTE_TIMELINE_BASE + "\\E([^\\s\\.=]+?)[\\.=].*");
|
||||
|
||||
private File propertyFile;
|
||||
private Timeline timeline;
|
||||
private TimelineSource timelineSource;
|
||||
private LinkedList<SyncTarget> syncTargets = new LinkedList<SyncTarget>();
|
||||
|
||||
public TimelineProperties() {
|
||||
this.propertyFile = new File("timeline.default.properties");
|
||||
Properties config = new Properties();
|
||||
|
||||
File timelineFile = new File("timeline.default.txt");
|
||||
URI timelineURI = timelineFile.toURI();
|
||||
|
||||
timeline = new Timeline();
|
||||
timelineSource = TimelineSourceFactory.newInstance(timelineURI);
|
||||
try { timelineSource.persist(timeline); }
|
||||
catch (IOException ioe) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
config.setProperty(LOCAL_TIMELINE_URI, timelineURI.toString());
|
||||
|
||||
try { config.store(new FileOutputStream(propertyFile), ""); }
|
||||
catch (IOException ioe) {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
public TimelineProperties(File propertyFile) throws IOException {
|
||||
String strURI;
|
||||
URI timelineURI;
|
||||
|
||||
this.propertyFile = propertyFile;
|
||||
Properties config = new Properties();
|
||||
try {
|
||||
config.load(new InputStreamReader(new FileInputStream(propertyFile)));
|
||||
} catch (IOException ioe) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// load local timeline
|
||||
strURI = config.getProperty(LOCAL_TIMELINE_URI, "");
|
||||
if ("".equals(strURI)) {
|
||||
timelineURI = new File("timeline.default.txt").toURI();
|
||||
} else {
|
||||
try { timelineURI = new URI(strURI); }
|
||||
catch (URISyntaxException urise) {
|
||||
throw new IOException("Unable to load the timeline: the timeline "
|
||||
+ "URI is invalid.", urise);
|
||||
}
|
||||
}
|
||||
|
||||
timelineSource = TimelineSourceFactory.newInstance(timelineURI);
|
||||
timeline = timelineSource.read();
|
||||
|
||||
// search keys for remote timeline entries
|
||||
for (Object keyObj : config.keySet()) {
|
||||
if (!(keyObj instanceof String)) continue;
|
||||
|
||||
String key = (String) keyObj;
|
||||
String stName;
|
||||
String remoteBase;
|
||||
SyncTarget st;
|
||||
|
||||
Matcher m = remoteTimelinePropPattern.matcher(key);
|
||||
if (!m.matches()) continue;
|
||||
|
||||
stName = m.group(1);
|
||||
remoteBase = REMOTE_TIMELINE_BASE + stName;
|
||||
|
||||
strURI = config.getProperty(remoteBase + ".uri", "");
|
||||
try { timelineURI = new URI(strURI); }
|
||||
catch (URISyntaxException urise) { /* TODO */ }
|
||||
|
||||
// add a new SyncTarget to the list
|
||||
st = new SyncTarget(stName, TimelineSourceFactory
|
||||
.newInstance(timelineURI), timeline);
|
||||
syncTargets.add(st);
|
||||
|
||||
// check for synch options
|
||||
st.enablePull(Boolean.parseBoolean(
|
||||
config.getProperty(remoteBase + ".pull", "true")));
|
||||
st.enablePush(Boolean.parseBoolean(
|
||||
config.getProperty(remoteBase + ".push", "true")));
|
||||
st.enableSyncOnExit(Boolean.parseBoolean(
|
||||
config.getProperty(remoteBase + ".sync-on-exit", "true")));
|
||||
st.setSyncInterval(Long.parseLong(
|
||||
config.getProperty(remoteBase + ".update-interval",
|
||||
"1800000"))); // thirty minutes
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void save() throws IOException {
|
||||
Properties config = new Properties();
|
||||
timelineSource.persist(timeline);
|
||||
|
||||
config.setProperty(LOCAL_TIMELINE_URI,
|
||||
timelineSource.getURI().toString());
|
||||
|
||||
for (SyncTarget st : syncTargets) {
|
||||
String remoteBase = REMOTE_TIMELINE_BASE + st.getName();
|
||||
config.setProperty(remoteBase + ".uri",
|
||||
st.getSource().getURI().toString());
|
||||
config.setProperty(remoteBase + ".pull",
|
||||
Boolean.toString(st.isPullEnabled()));
|
||||
config.setProperty(remoteBase + ".push",
|
||||
Boolean.toString(st.isPushEnabled()));
|
||||
config.setProperty(remoteBase + ".sync-on-exit",
|
||||
Boolean.toString(st.isSyncOnExitEnabled()));
|
||||
config.setProperty(remoteBase + ".update-interval",
|
||||
Long.toString(st.getSyncInterval()));
|
||||
}
|
||||
|
||||
try {
|
||||
config.store(new FileOutputStream(propertyFile), "");
|
||||
} catch (IOException ioe) {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
public void save(File newFile) throws IOException {
|
||||
propertyFile = newFile;
|
||||
save();
|
||||
}
|
||||
|
||||
public Timeline getTimeline() { return timeline; }
|
||||
|
||||
public void setTimelineSource(TimelineSource newSource) {
|
||||
this.timelineSource = newSource;
|
||||
}
|
||||
|
||||
public TimelineSource getTimelineSource() { return timelineSource; }
|
||||
|
||||
public Collection<SyncTarget> getSyncTargets() { return syncTargets; }
|
||||
}
|
25
src/main/com/jdbernard/timestamper/core/TimelineSource.java
Executable file
25
src/main/com/jdbernard/timestamper/core/TimelineSource.java
Executable file
@ -0,0 +1,25 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public abstract class TimelineSource {
|
||||
|
||||
protected final URI uri;
|
||||
|
||||
public TimelineSource(URI uri) { this.uri = uri; }
|
||||
|
||||
public abstract Timeline read() throws IOException;
|
||||
public abstract void persist(Timeline t) throws IOException;
|
||||
public abstract boolean isAuthenticated();
|
||||
public abstract void authenticate() throws AuthenticationException;
|
||||
|
||||
public URI getURI() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
}
|
38
src/main/com/jdbernard/timestamper/core/TimelineSourceFactory.java
Executable file
38
src/main/com/jdbernard/timestamper/core/TimelineSourceFactory.java
Executable file
@ -0,0 +1,38 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public class TimelineSourceFactory {
|
||||
|
||||
public static TimelineSource newInstance(URI uri) {
|
||||
// File based
|
||||
if ("file".equalsIgnoreCase(uri.getScheme())) {
|
||||
return new FileTimelineSource(uri);
|
||||
}
|
||||
|
||||
// Twitter
|
||||
else if ("http".equalsIgnoreCase(uri.getScheme()) &&
|
||||
("twitter.com".equalsIgnoreCase(uri.getHost())
|
||||
|| "www.twitter.com".equalsIgnoreCase(uri.getHost()))) {
|
||||
throw new UnsupportedOperationException("Twitter based timeline "
|
||||
+ "sources are not yet supported.");
|
||||
}
|
||||
|
||||
// SSH
|
||||
else if ("ssh".equalsIgnoreCase(uri.getScheme())) {
|
||||
throw new UnsupportedOperationException("SSH based timeline sources"
|
||||
+ " are not yet supported.");
|
||||
}
|
||||
|
||||
// unknown
|
||||
else {
|
||||
throw new UnsupportedOperationException("Timeline sources for the"
|
||||
+ " " + uri.getScheme() + " are not currently supported.");
|
||||
}
|
||||
}
|
||||
}
|
34
src/main/com/jdbernard/timestamper/core/TwitterTimelineSource.java
Executable file
34
src/main/com/jdbernard/timestamper/core/TwitterTimelineSource.java
Executable file
@ -0,0 +1,34 @@
|
||||
package com.jdbernard.timestamper.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonathan Bernard ({@literal jonathan.bernard@gemalto.com})
|
||||
*/
|
||||
public class TwitterTimelineSource extends TimelineSource {
|
||||
|
||||
private String username;
|
||||
private char[] password;
|
||||
|
||||
public TwitterTimelineSource(URI uri) {
|
||||
super(uri);
|
||||
}
|
||||
|
||||
public Timeline read() throws IOException {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public void persist(Timeline t) throws IOException {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public boolean isAuthenticated() {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public void authenticate() throws AuthenticationException {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user