Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
notthetup committed May 7, 2024
1 parent c1932ec commit 5997850
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 58 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ repositories {
}

dependencies {
api 'org.codehaus.groovy:groovy:2.5.20'
api 'org.jline:jline:3.21.0'
api 'org.apache.groovy:groovy:4.0.21'
api 'org.jline:jline:3.25.0'
api 'org.apache.commons:commons-lang3:3.12.0'
api 'commons-io:commons-io:2.11.0'
api 'uk.com.robust-it:cloning:1.9.12'
Expand Down
4 changes: 2 additions & 2 deletions gateways/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions src/main/groovy/org/arl/fjage/shell/GroovyScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GroovyScriptEngine implements ScriptEngine {

////// inner classes

class BlockingInput {
static class BlockingInput {

private int waiting = 0;
private String input = null;
Expand Down Expand Up @@ -78,13 +78,13 @@ synchronized boolean isWaiting() {
////// private attributes

private GroovyShell groovy;
private Binding binding;
private final Binding binding;
private ImportCustomizer imports;
private Shell out = null;
private Thread busy = null;
private Documentation doc = new Documentation();
private BlockingInput input = new BlockingInput();
private Logger log = Logger.getLogger(getClass().getName());
private final Documentation doc = new Documentation();
private final BlockingInput input = new BlockingInput();
private final Logger log = Logger.getLogger(getClass().getName());

////// constructor

Expand Down Expand Up @@ -135,16 +135,15 @@ public String getPrompt(boolean cont) {

@Override
public boolean isComplete(String cmd) {
if (cmd == null || cmd.trim().length() == 0) return true;
if (cmd == null || cmd.trim().isEmpty()) return true;
try {
groovy.parse(cmd);
return true;
} catch (MultipleCompilationErrorsException ex) {
String s = ex.getMessage();
if (s == null) return true;
if (s.contains("unexpected token")) return false;
if (s.contains("expecting")) return false;
return true;
return !s.contains("expecting");
} catch (Throwable ex) {
return true;
}
Expand Down Expand Up @@ -262,7 +261,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
30 changes: 11 additions & 19 deletions src/main/java/org/arl/fjage/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class Container {
protected Object cloner;
protected Method doClone;
protected boolean autoclone = false;
protected Set<AgentID> idle = new HashSet<AgentID>();
protected Set<MessageListener> listeners = new HashSet<MessageListener>();
protected final Set<AgentID> idle = new HashSet<AgentID>();
protected final Set<MessageListener> listeners = new HashSet<MessageListener>();

//////////// Interface methods

Expand Down Expand Up @@ -136,7 +136,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 Expand Up @@ -195,7 +195,7 @@ public boolean getAutoClone() {
* @return an agent id if successful, null on failure.
*/
public AgentID add(String name, Agent agent) {
if (name == null || name.length() == 0) {
if (name == null || name.isEmpty()) {
log.warning("Undefined agent name");
return null;
}
Expand Down Expand Up @@ -387,12 +387,8 @@ public synchronized boolean subscribe(AgentID aid, AgentID topic) {
log.warning("Unable to subscribe unknown agent "+aid+" to topic "+topic);
return false;
}
Set<Agent> subscribers = topics.get(topic);
if (subscribers == null) {
subscribers = new HashSet<Agent>();
topics.put(topic, subscribers);
}
subscribers.add(agent);
Set<Agent> subscribers = topics.computeIfAbsent(topic, k -> new HashSet<Agent>());
subscribers.add(agent);
return true;
}

Expand Down Expand Up @@ -434,12 +430,8 @@ public synchronized void unsubscribe(AgentID aid) {
* @return true on success, false on failure.
*/
public synchronized boolean register(AgentID aid, String service) {
Set<AgentID> providers = services.get(service);
if (providers == null) {
providers = new HashSet<AgentID>();
services.put(service, providers);
}
providers.add(aid);
Set<AgentID> providers = services.computeIfAbsent(service, k -> new HashSet<AgentID>());
providers.add(aid);
return true;
}

Expand All @@ -461,7 +453,7 @@ public synchronized String[] getServices() {
*/
public synchronized AgentID agentForService(String service) {
Set<AgentID> providers = services.get(service);
if (providers == null || providers.size() == 0) return null;
if (providers == null || providers.isEmpty()) return null;
return providers.iterator().next();
}

Expand All @@ -473,7 +465,7 @@ public synchronized AgentID agentForService(String service) {
*/
public synchronized AgentID[] agentsForService(String service) {
Set<AgentID> providers = services.get(service);
if (providers == null || providers.size() == 0) return null;
if (providers == null || providers.isEmpty()) return null;
return providers.toArray(new AgentID[0]);
}

Expand Down Expand Up @@ -532,7 +524,7 @@ void init() {
initing = false;
return;
}
if (agentsToAdd.size() > 0) {
if (!agentsToAdd.isEmpty()) {
synchronized (agents) {
agents.putAll(agentsToAdd);
SortedSet<AgentID> keys = new TreeSet<AgentID>(agentsToAdd.keySet());
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
5 changes: 3 additions & 2 deletions 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 All @@ -24,7 +25,7 @@ public class LogHandlerProxy extends Handler {

////////// Private attributes

private Handler delegate;
private final Handler delegate;
private TimestampProvider timesrc;

////////// Interface methods
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.ofEpochSecond(timesrc.currentTimeMillis()));
delegate.publish(rec);
}

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

package org.arl.fjage.param;

import java.io.Serial;
import java.util.*;
import java.util.Map.Entry;
import org.arl.fjage.*;
Expand All @@ -21,6 +22,7 @@
*/
public class ParameterRsp extends Message {

@Serial
private static final long serialVersionUID = 1L;

protected int index = -1;
Expand Down Expand Up @@ -102,7 +104,7 @@ public Object get(Parameter param) {
if (v == null) return null;
rv = v.getValue();
}
if (rv instanceof Double && ((Double)rv).intValue() == ((Double)rv).doubleValue()) rv = new Integer(((Double)rv).intValue());
if (rv instanceof Double && ((Double)rv).intValue() == (Double) rv) rv = ((Double) rv).intValue();
return rv;
}

Expand Down Expand Up @@ -162,7 +164,7 @@ protected Parameter resolve(Parameter param) {

@Override
public String toString() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append('[');
if (index >= 0) {
Expand All @@ -178,7 +180,7 @@ public String toString() {
sb.append(':');
if (value != null) {
v = value.getValue();
if (v instanceof Double && ((Double)v).intValue() == ((Double)v).doubleValue()) v = new Integer(((Double)v).intValue());
if (v instanceof Double && ((Double)v).intValue() == (Double) v) v = ((Double)v).intValue();
sb.append(v);
} else {
sb.append("null");
Expand All @@ -192,12 +194,12 @@ public String toString() {
GenericValue gv = e.getValue();
v = null;
if (gv != null) v = gv.getValue();
if (v instanceof Double && ((Double)v).intValue() == ((Double)v).doubleValue()) v = new Integer(((Double)v).intValue());
if (v instanceof Double && ((Double)v).intValue() == (Double) v) v = ((Double)v).intValue();
sb.append(v);
}
}
s = sb.toString();
if (s.length() == 0) s = null;
if (s.isEmpty()) s = null;
}
sb.append(']');
return sb.toString();
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/arl/fjage/remote/EnumTypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
public class EnumTypeAdapter extends TypeAdapter<Object> {

private static ClassLoader classloader = null;
private static Logger log;
private static final Logger log;

static {
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 Expand Up @@ -86,8 +86,6 @@ private static Class<? extends Enum> enumClass(String clsname) {
String value = s.substring(pos+1);
Class<? extends Enum> cls = enumClass(s.substring(0,pos));
if (cls != null) return Enum.valueOf(cls, value);
if (value != null) return new NamedParameter(s);
return null;
return new NamedParameter(s);
}

}
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MessageAdapterFactory implements TypeAdapterFactory {
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

0 comments on commit 5997850

Please sign in to comment.