Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #66

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.oneteme</groupId>
<artifactId>inspect-core</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
<packaging>jar</packaging>
<name>inspect-core</name>
<description>INtegrated System Performance Evaluation and Communication Tracking core libray</description>
Expand Down Expand Up @@ -63,9 +63,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>3.2.0</version>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>3.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/usf/inspect/core/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import static java.lang.Math.min;
import static java.lang.Thread.currentThread;
import static java.util.Collections.synchronizedList;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.Optional.empty;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -31,7 +33,7 @@ public final class Helper {
static {
var p = Helper.class.getPackageName();
ROOT_PACKAGE = p.substring(0, p.lastIndexOf(".")); //root
log = getLogger(ROOT_PACKAGE + ".Collector");
log = getLogger(ROOT_PACKAGE + ".collector");
}

public static String threadName() {
Expand All @@ -52,7 +54,7 @@ public static <T> Optional<T> newInstance(Class<? extends T> clazz) {
try {
return Optional.of(clazz.getDeclaredConstructor().newInstance());
} catch (Exception e) {
log.warn("cannot instantiate class " + clazz.getName(), e);
log.warn("cannot instantiate class '{}', exception={}", clazz.getName(), e.getMessage());
return empty();
}
}
Expand All @@ -64,12 +66,12 @@ public static Optional<StackTraceElement> outerStackTraceElement() {
return i<arr.length ? Optional.of(arr[i]) : empty();
}

public static void warnNoActiveSession() {
log.warn("no active session");
public static void warnStackTrace(String msg) {
log.warn(msg);
var arr = currentThread().getStackTrace();
var i = 1; //skip this method call
while(i<arr.length && arr[i].getClassName().startsWith(ROOT_PACKAGE)) {i++;}
var max = min(arr.length, --i+MAX_STACK); //first JQuery method call
var max = min(arr.length, --i+MAX_STACK); //first inspect method call
while(i<max) {
log.warn("\tat {}", arr[i++]);
}
Expand All @@ -81,7 +83,7 @@ public static void warnNoActiveSession() {
public static String prettyURLFormat(String user, String protocol, String host, int port, String path) {
var s = isNull(user) ? "" : '<' + user + '>';
if(nonNull(protocol)) {
s += protocol + "://";
s+= protocol + "://";
}
if(nonNull(host)) {
s+= host;
Expand All @@ -97,4 +99,8 @@ public static String prettyURLFormat(String user, String protocol, String host,
}
return s;
}

static <T> List<T> synchronizedArrayList() {
return synchronizedList(new ArrayList<>());
}
}
33 changes: 16 additions & 17 deletions src/main/java/org/usf/inspect/core/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public interface Session extends Metric {

void setId(String id); //used in server side

List<RestRequest> getRestRequests(); // rename to getApiRequests
List<RestRequest> getRestRequests();

List<DatabaseRequest> getDatabaseRequests(); //rename to getDatabaseRequests
List<DatabaseRequest> getDatabaseRequests();

List<LocalRequest> getLocalRequests();

Expand All @@ -38,28 +38,27 @@ public interface Session extends Metric {

AtomicInteger getLock();

default void append(SessionStage stage) {
default boolean append(SessionStage stage) {
if(stage instanceof RestRequest req) {
getRestRequests().add(req);
return getRestRequests().add(req);
}
else if(stage instanceof DatabaseRequest req) {
getDatabaseRequests().add(req);
if(stage instanceof DatabaseRequest req) {
return getDatabaseRequests().add(req);
}
else if(stage instanceof FtpRequest req) {
getFtpRequests().add(req);
if(stage instanceof FtpRequest req) {
return getFtpRequests().add(req);
}
else if(stage instanceof MailRequest req) {
getMailRequests().add(req);
if(stage instanceof MailRequest req) {
return getMailRequests().add(req);
}
else if(stage instanceof NamingRequest req) {
getLdapRequests().add(req);
if(stage instanceof NamingRequest req) {
return getLdapRequests().add(req);
}
else if(stage instanceof LocalRequest req) {
getLocalRequests().add(req);
}
else {
log.warn("unsupported session stage {}", stage);
if(stage instanceof LocalRequest req) {
return getLocalRequests().add(req);
}
log.warn("unsupported session stage {}", stage);
return false;
}

default void lock(){
Expand Down
40 changes: 13 additions & 27 deletions src/main/java/org/usf/inspect/core/SessionManager.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package org.usf.inspect.core;

import static java.util.Collections.synchronizedList;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.usf.inspect.core.ExceptionInfo.mainCauseException;
import static org.usf.inspect.core.Helper.log;
import static org.usf.inspect.core.Helper.outerStackTraceElement;
import static org.usf.inspect.core.Helper.warnNoActiveSession;
import static org.usf.inspect.core.Helper.synchronizedArrayList;
import static org.usf.inspect.core.Helper.warnStackTrace;
import static org.usf.inspect.core.MainSessionType.BATCH;
import static org.usf.inspect.core.MainSessionType.STARTUP;
import static org.usf.inspect.core.Session.nextId;
import static org.usf.inspect.core.StageTracker.call;
import static org.usf.inspect.core.StageTracker.exec;

import java.util.ArrayList;
import java.util.List;

import org.usf.inspect.core.SafeCallable.SafeRunnable;
import org.usf.inspect.core.StageTracker.StageConsumer;

Expand All @@ -38,27 +35,24 @@ public static <S extends Session> S requireCurrentSession(Class<S> clazz) {
if(clazz.isInstance(ses)) { //nullable
return clazz.cast(ses);
}
log.warn("unexpected session type expected={}, but was {}", clazz.getSimpleName(), ses);
log.warn("unexpected session type: expected={}, but was={}", clazz.getSimpleName(), ses);
return null;
}

public static Session requireCurrentSession() {
var ses = currentSession();
if(isNull(ses)) {
warnNoActiveSession();
warnStackTrace("no active session");
}
else if(ses.completed()) {
warnStackTrace("current session already completed: " + ses);
}
return ses;
}

public static Session currentSession() {
var ses = localTrace.get(); // priority
if(isNull(ses)) {
ses = startupSession;
}
if(nonNull(ses) && ses.completed()) {
log.warn("current session was completed {}", ses);
}
return ses;
return nonNull(ses) ? ses : startupSession;
}

public static void updateCurrentSession(Session s) {
Expand Down Expand Up @@ -115,7 +109,7 @@ public static Session endSession() {
localTrace.remove();
}
else {
warnNoActiveSession();
warnStackTrace("no active session");
}
return ses;
}
Expand All @@ -126,18 +120,14 @@ public static MainSession endStatupSession() {
startupSession = null;
}
else {
warnNoActiveSession();
warnStackTrace("no startup session");
}
return ses;
}

public static boolean appendSessionStage(SessionStage stg) {
var session = requireCurrentSession();
if(nonNull(session)) {
session.append(stg);
return true;
}
return false;
var ses = requireCurrentSession();
return nonNull(ses) && ses.append(stg);
}

public static <E extends Throwable> void trackRunnable(String name, SafeRunnable<E> fn) throws E {
Expand All @@ -164,8 +154,4 @@ static StageConsumer<Object> localRequestAppender(String name) {
appendSessionStage(stg);
};
}

static <T> List<T> synchronizedArrayList() {
return synchronizedList(new ArrayList<>());
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/usf/inspect/core/SessionPublisher.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.usf.inspect.core;

import static java.util.Collections.synchronizedList;
import static org.usf.inspect.core.Helper.log;
import static org.usf.inspect.core.Helper.synchronizedArrayList;

import java.util.ArrayList;
import java.util.List;

import lombok.AccessLevel;
Expand All @@ -20,7 +19,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SessionPublisher {

static final List<SessionHandler<Session>> handlers = synchronizedList(new ArrayList<>());
static final List<SessionHandler<Session>> handlers = synchronizedArrayList();

public static void register(@NonNull SessionHandler<Session> sender) {
handlers.add(sender);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/usf/inspect/rest/RestSessionFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Obj
private static String defaultEndpointName(HttpServletRequest req) {
var arr = req.getRequestURI().substring(1).split("/");
var map = (Map<String, String>) req.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE);
return map == null ? join("_", arr) : Stream.of(arr)
return isNull(map) ? join("_", arr) : Stream.of(arr)
.filter(not(map.values()::contains))
.collect(joiner);
}
Expand Down
Loading