-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from jeffreybakker/feature/extension_7
Feature/extension 7
- Loading branch information
Showing
12 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package honours.ing.banq.log; | ||
|
||
import honours.ing.banq.time.TimeServiceImpl; | ||
|
||
import javax.persistence.*; | ||
|
||
/** | ||
* The model for a log message that is saved in the database. | ||
* @author Jeffrey Bakker | ||
* @since 6-8-17 | ||
*/ | ||
@Entity | ||
public class Log { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.TABLE) | ||
private Long id; | ||
|
||
private long timeStamp; | ||
|
||
@Column(columnDefinition = "TEXT") | ||
private String eventLog; | ||
|
||
/** | ||
* @deprecated empty constructor for Hibernate ORM | ||
*/ | ||
@Deprecated | ||
public Log() { } | ||
|
||
/** | ||
* Creates a new {@link Log} object with the given parameters. | ||
* @param message the message for the log | ||
*/ | ||
public Log(String message) { | ||
this(TimeServiceImpl.currentTimeMillis(), message); | ||
} | ||
|
||
/** | ||
* Creates a new {@link Log} object with the given parameters. | ||
* @param timeStamp the timeStamp for the log | ||
* @param message the message for the log | ||
*/ | ||
public Log(long timeStamp, String message) { | ||
this.timeStamp = timeStamp; | ||
this.eventLog = message; | ||
} | ||
|
||
/** | ||
* Returns the {@link Log}'s ID. | ||
* @return the id | ||
*/ | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
/** | ||
* Returns the time stamp in milliseconds. | ||
* @return the time stamp | ||
*/ | ||
public long getTimeStamp() { | ||
return timeStamp; | ||
} | ||
|
||
/** | ||
* Sets the time for the time stamp. | ||
* @param timeStamp the time in milliseconds | ||
*/ | ||
public void setTimeStamp(long timeStamp) { | ||
this.timeStamp = timeStamp; | ||
} | ||
|
||
/** | ||
* Returns the message for the log. | ||
* @return the message | ||
*/ | ||
public String getMessage() { | ||
return eventLog; | ||
} | ||
|
||
/** | ||
* Sets the message for the log. | ||
* @param message the message | ||
*/ | ||
public void setMessage(String message) { | ||
this.eventLog = message; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Log log = (Log) o; | ||
|
||
if (timeStamp != log.timeStamp) return false; | ||
return id != null ? id.equals(log.id) : log.id == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = id != null ? id.hashCode() : 0; | ||
result = 31 * result + (int) (timeStamp ^ (timeStamp >>> 32)); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package honours.ing.banq.log; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author jeffrey | ||
* @since 6-8-17 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface LogIgnore { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package honours.ing.banq.log; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* @author jeffrey | ||
* @since 6-8-17 | ||
*/ | ||
@Aspect | ||
@Component | ||
public class LogInterceptor implements ApplicationContextAware { | ||
|
||
private ApplicationContext context; | ||
private LogService log; | ||
|
||
private final ThreadLocal<AtomicBoolean> running; | ||
|
||
@Autowired | ||
public LogInterceptor(LogService logService) { | ||
this.log = logService; | ||
|
||
running = ThreadLocal.withInitial(() -> new AtomicBoolean(false)); | ||
} | ||
|
||
@Around("within(honours.ing..*) && @annotation(org.springframework.transaction.annotation.Transactional)") | ||
public Object intercept(ProceedingJoinPoint pjp) throws Throwable { | ||
boolean mayLog = false; | ||
|
||
MethodSignature signature = (MethodSignature) pjp.getSignature(); | ||
Method method = signature.getMethod(); | ||
LogIgnore annotation = method.getAnnotation(LogIgnore.class); | ||
|
||
if (annotation == null) { | ||
synchronized (running) { | ||
if (!running.get().get()) { | ||
running.get().set(true); | ||
mayLog = true; | ||
} | ||
} | ||
} | ||
|
||
try { | ||
Object res = pjp.proceed(); | ||
|
||
if (mayLog) { | ||
log.log(String.format("%s%s", method.getName(), Arrays.deepToString(pjp.getArgs()))); | ||
} | ||
|
||
return res; | ||
} catch (Throwable t) { | ||
if (mayLog) { | ||
log.log(String.format("[%s] %s%s", t.getClass().getSimpleName(), method.getName(), Arrays.deepToString(pjp.getArgs()))); | ||
} | ||
throw t; | ||
} finally { | ||
if (mayLog) { | ||
running.get().set(false); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package honours.ing.banq.log; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author jeffrey | ||
* @since 6-8-17 | ||
*/ | ||
public interface LogRepository extends JpaRepository<Log, Long> { | ||
|
||
List<Log> findLogsByTimeStampBetween(long a, long b); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package honours.ing.banq.log; | ||
|
||
import com.googlecode.jsonrpc4j.JsonRpcParam; | ||
import com.googlecode.jsonrpc4j.JsonRpcService; | ||
import honours.ing.banq.log.bean.LogBean; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* This service provides access to the log messages that have been made by the application. | ||
* @author Jeffrey Bakker | ||
* @since 6-8-17 | ||
*/ | ||
@JsonRpcService("/api/log") | ||
public interface LogService { | ||
|
||
void log(String message); | ||
|
||
/** | ||
* Returns all event logs from a given time period. | ||
* @param beginDate the begin of time period | ||
* @param endDate the end of the time period | ||
* @return a list of all event logs made in the given time period | ||
*/ | ||
List<LogBean> getEventLogs(@JsonRpcParam("beginDate") String beginDate, @JsonRpcParam("endDate") String endDate); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package honours.ing.banq.log; | ||
|
||
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl; | ||
import honours.ing.banq.Application; | ||
import honours.ing.banq.InvalidParamValueError; | ||
import honours.ing.banq.log.bean.LogBean; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.List; | ||
|
||
/** | ||
* An implementation for the {@link LogService}. | ||
* @author Jeffrey Bakker | ||
* @since 6-8-17 | ||
*/ | ||
@Service | ||
@AutoJsonRpcServiceImpl | ||
@Transactional(readOnly = true) | ||
public class LogServiceImpl implements LogService { | ||
|
||
private static final DateFormat INPUT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
private Logger logger = Application.getLogger(); | ||
|
||
// Repositories | ||
private LogRepository repository; | ||
|
||
@Autowired | ||
public LogServiceImpl(LogRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
@LogIgnore | ||
@Transactional | ||
@Override | ||
public void log(String message) { | ||
logger.info(message); | ||
|
||
Log log = new Log(message); | ||
repository.save(log); | ||
} | ||
|
||
@Override | ||
public List<LogBean> getEventLogs(String beginDate, String endDate) { | ||
long a, b; | ||
try { | ||
a = INPUT_DATE_FORMAT.parse(beginDate).getTime(); | ||
b = INPUT_DATE_FORMAT.parse(endDate).getTime(); | ||
} catch (ParseException e) { | ||
throw new InvalidParamValueError("Either the beginDate or the endDate was in an invalid format"); | ||
} | ||
|
||
return LogBean.generate(repository.findLogsByTimeStampBetween(a, b)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package honours.ing.banq.log.bean; | ||
|
||
import honours.ing.banq.log.Log; | ||
import honours.ing.banq.log.LogService; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* This is a bean that will be returned in some of the {@link LogService}'s methods | ||
* @author Jeffrey Bakker | ||
* @since 6-8-17 | ||
*/ | ||
public class LogBean { | ||
|
||
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | ||
|
||
private String timeStamp; | ||
private String eventLog; | ||
|
||
public LogBean(Log log) { | ||
this.timeStamp = DATE_FORMAT.format(new Date(log.getTimeStamp())); | ||
this.eventLog = log.getMessage(); | ||
} | ||
|
||
public static List<LogBean> generate(List<Log> logs) { | ||
List<LogBean> res = new ArrayList<>(); | ||
logs.forEach((l) -> res.add(new LogBean(l))); | ||
return res; | ||
} | ||
|
||
public String getTimeStamp() { | ||
return timeStamp; | ||
} | ||
|
||
public void setTimeStamp(String timeStamp) { | ||
this.timeStamp = timeStamp; | ||
} | ||
|
||
public String getEventLog() { | ||
return eventLog; | ||
} | ||
|
||
public void setEventLog(String eventLog) { | ||
this.eventLog = eventLog; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.