Skip to content
Draft
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
17 changes: 12 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ repositories {
}

dependencies {
api 'org.codehaus.groovy:groovy:2.5.23'
api 'org.apache.groovy:groovy:4.0.28'
api 'org.jline:jline:3.30.4'
api 'org.apache.commons:commons-lang3:3.17.0'
api 'commons-io:commons-io:2.19.0'
api 'uk.com.robust-it:cloning:1.9.12'
api 'org.apache.commons:commons-lang3:3.18.0'
api 'commons-io:commons-io:2.20.0'
api 'io.github.kostaskougios:cloning:1.12.0'
api 'org.eclipse.jetty:jetty-server:9.4.57.v20241219'
api 'org.eclipse.jetty:jetty-servlet:9.4.57.v20241219'
api 'org.eclipse.jetty:jetty-rewrite:9.4.57.v20241219'
api 'org.eclipse.jetty.websocket:websocket-server:9.4.57.v20241219'
api 'com.google.code.gson:gson:2.13.0'
api 'com.google.code.gson:gson:2.13.1'
api 'com.fazecast:jSerialComm:2.9.3'
testImplementation 'junit:junit:4.13.2'
testImplementation 'net.jodah:concurrentunit:0.4.6'
Expand All @@ -55,6 +55,7 @@ dependencies {
compileJava {
options.compilerArgs << "-Xlint:all"
options.compilerArgs << "-Xlint:-options"
options.compilerArgs << "-Xlint:-removal"
}

compileTestJava {
Expand Down Expand Up @@ -157,6 +158,12 @@ processResources {
from { tasks.named('buildjs').get()?.outputs?.files?.asFileTree?.matching {include 'dist/fjage.js'} ?: [] }
into('org/arl/fjage/web/shell')
}
doLast {
copy {
into "$buildDir/libs"
from configurations.runtimeClasspath
}
}
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public boolean exec(final Class<?> script, final List<String> args) {
binding.setVariable("out", out);
binding.setVariable("script", script.getName());
binding.setVariable("args", args);
Script gs = (Script)script.newInstance();
Script gs = (Script)script.getDeclaredConstructor().newInstance();
gs.setBinding(binding);
gs.run();
} catch (Throwable ex) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/arl/fjage/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void setCloner(String name) {
doClone = Class.forName(SERIAL_CLONER).getDeclaredMethod("clone", Serializable.class);
} else if (name.equals(FAST_CLONER)) {
Class<?> cls = Class.forName(FAST_CLONER);
cloner = cls.newInstance();
cloner = cls.getDeclaredConstructor().newInstance();
doClone = cls.getMethod("deepClone", Object.class);
} else {
cloner = null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/arl/fjage/LogFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class LogFormatter extends Formatter {

// list of packages to ignore in stack trace display
private static String[] ignorePkgList = {
private static final String[] ignorePkgList = {
"org.codehaus.groovy.",
"java.",
"groovy.",
Expand All @@ -45,7 +45,7 @@ public String format(LogRecord record) {
s.append('|');
s.append(record.getLoggerName());
s.append('@');
s.append(record.getThreadID());
s.append(record.getLongThreadID());
s.append(':');
s.append(record.getSourceMethodName());
s.append('|');
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/arl/fjage/LogHandlerProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.arl.fjage;

import java.time.Instant;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void setTimestampProvider(TimestampProvider timesrc) {
*/
@Override
public void publish(LogRecord rec) {
if (timesrc != null) rec.setMillis(timesrc.currentTimeMillis());
if (timesrc != null) rec.setInstant(Instant.ofEpochMilli(timesrc.currentTimeMillis()));
delegate.publish(rec);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/arl/fjage/auth/SimpleFirewallSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package org.arl.fjage.auth;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.arl.fjage.AgentID;
import org.arl.fjage.remote.Action;
import org.arl.fjage.remote.JsonMessage;
Expand Down Expand Up @@ -73,7 +73,7 @@ public SimpleFirewallSupplier addUser(String username, String credentials, Strin
* @return This <code>SimpleFirewallSupplier</code> instance.
*/
public SimpleFirewallSupplier addPolicy(String policyId, Consumer<Policy> policyConsumer) {
if (StringUtils.equals(policyId, POLICY_ID_ALLOW_ALL)) {
if (Strings.CS.equals(policyId, POLICY_ID_ALLOW_ALL)) {
throw new IllegalArgumentException(String.format("%s is a reserved policy ID", policyId));
}

Expand All @@ -98,7 +98,7 @@ public void removeUser(String username) {
* @param policyId Policy ID.
*/
public void removePolicy(String policyId) {
if (StringUtils.equals(policyId, POLICY_ID_ALLOW_ALL)) {
if (Strings.CS.equals(policyId, POLICY_ID_ALLOW_ALL)) {
throw new IllegalArgumentException(String.format("%s is a reserved policy ID", policyId));
}
policyMap.remove(policyId);
Expand All @@ -115,7 +115,7 @@ public User findUserByCredentials(String credentials) {
return null;
}
for (final User user : userMap.values()) {
if (StringUtils.equals(user.getCredentials(), credentials)) {
if (Strings.CS.equals(user.getCredentials(), credentials)) {
return user;
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ public boolean authenticate(String creds) {
return false;
}
policy = findPolicy(user.getPolicyId());
allowAll = StringUtils.equals(user.getPolicyId(), POLICY_ID_ALLOW_ALL);
allowAll = Strings.CS.equals(user.getPolicyId(), POLICY_ID_ALLOW_ALL);
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/arl/fjage/remote/ConnectionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.arl.fjage.AgentID;
import org.arl.fjage.auth.*;
Expand Down Expand Up @@ -153,8 +154,10 @@ public void run() {
else if (fw.permit(rq)) pool.execute(new RemoteTask(rq));
else respondAuth(rq, false);
}
} catch(com.google.gson.JsonIOException ex) {
log.log(Level.WARNING, "Bad JSON message: " + s, ex);
} catch(Exception ex) {
log.warning("Bad JSON request: "+ex.toString() + " in " + s);
log.log(Level.WARNING, "Error processing request: " + s, ex);
}
}
fw.signoff();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/arl/fjage/remote/EnumTypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class EnumTypeAdapter extends TypeAdapter<Object> {
log = Logger.getLogger(EnumTypeAdapter.class.getName());
try {
Class<?> cls = Class.forName("groovy.lang.GroovyClassLoader");
classloader = (ClassLoader)cls.newInstance();
classloader = (ClassLoader)cls.getDeclaredConstructor().newInstance();
log.info("Groovy detected, using GroovyClassLoader");
} catch (Exception ex) {
// do nothing
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/arl/fjage/remote/FileAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.arl.fjage.remote;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.File;
import java.io.IOException;

public class FileAdapter extends TypeAdapter<File> {

@Override
public void write(JsonWriter out, File value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(value.getAbsolutePath());
}
}

@Override
public File read(JsonReader in) throws IOException {
if (in.peek() == com.google.gson.stream.JsonToken.NULL) {
in.nextNull();
return null;
}
String path = in.nextString();
return new File(path);
}
}
14 changes: 3 additions & 11 deletions src/main/java/org/arl/fjage/remote/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Gateway(Container container) {
protected void init() {
agent = new Agent() {
private Message rsp;
private Object sync = new Object();
private final Object sync = new Object();
@Override
public Message receive(final MessageFilter filter, long timeout) {
if (Thread.currentThread().getId() == tid) return super.receive(filter, timeout);
Expand Down Expand Up @@ -235,7 +235,7 @@ public final Message receive() {

@Override
public Message receive(final Class<?> cls, long timeout) {
return receive(m -> cls.isInstance(m), timeout);
return receive(cls::isInstance, timeout);
}

@Override
Expand All @@ -248,7 +248,7 @@ public Message receive(final Message m, long timeout) {
if (container instanceof SlaveContainer)
((SlaveContainer)container).checkAuthFailure(m.getMessageID());
Message rsp = receive(new MessageFilter() {
private String mid = m.getMessageID();
private final String mid = m.getMessageID();
@Override
public boolean matches(Message m) {
String s = m.getInReplyTo();
Expand Down Expand Up @@ -434,12 +434,4 @@ public AgentID[] agentsForService(Enum<?> service) {
t[i] = new AgentID(t[i], this);
return t;
}

////////////// Private methods

@Override
public void finalize() {
close();
}

}
2 changes: 2 additions & 0 deletions src/main/java/org/arl/fjage/remote/JsonMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.arl.fjage.remote;

import java.io.File;
import java.util.Date;
import com.google.gson.*;
import org.arl.fjage.AgentID;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class JsonMessage {
.registerTypeAdapter(Double.class, (JsonSerializer<Double>) (value, type, jsonSerializationContext) -> value.isNaN()?null:new JsonPrimitive(value))
.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()))
.registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) -> new JsonPrimitive(date.getTime()))
.registerTypeAdapter(File.class, new FileAdapter())
.registerTypeHierarchyAdapter(AgentID.class, new AgentIDAdapter())
.registerTypeHierarchyAdapter(Parameter.class, new EnumTypeAdapter())
.registerTypeAdapterFactory(new MessageAdapterFactory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
class MessageAdapterFactory implements TypeAdapterFactory {

private static ClassLoader classloader = null;
private static final Logger log = Logger.getLogger(MessageAdapterFactory.class.getName());

static {
try {
Class<?> cls = Class.forName("groovy.lang.GroovyClassLoader");
classloader = (ClassLoader)cls.newInstance();
classloader = (ClassLoader)cls.getDeclaredConstructor().newInstance();
Logger log = Logger.getLogger(MessageAdapterFactory.class.getName());
log.info("Groovy detected, using GroovyClassLoader");
} catch (Exception ex) {
Expand Down Expand Up @@ -147,6 +148,7 @@ else if (cls.equals(GenericMessage.class)) {
rv = (T) msg;
} else {
TypeAdapter<?> delegate1 = gson.getDelegateAdapter(parent, TypeToken.get(cls));
log.info("Inflating JSONMessage with adapter" + delegate1.getClass().getName() + " for class " + cls.getName());
rv = (T)delegate1.read(in);
}
} else in.skipValue();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/arl/fjage/test/AbstractSimulatorTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.arl.fjage.test;

import net.jodah.concurrentunit.Waiter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.arl.fjage.Container;
import org.arl.fjage.DiscreteEventSimulator;
Expand Down Expand Up @@ -219,7 +219,7 @@ private static abstract class AbstractExpectation

protected Stream<TestEvent> findEventById(List<TestEvent> testEvents, String id) {
return testEvents.stream()
.filter(event -> StringUtils.equals(event.getId(), id));
.filter(event -> Strings.CS.equals(event.getId(), id));
}
}

Expand Down
Loading