Skip to content

Commit

Permalink
Merge pull request #497 from scireum/aha/code-cleanup
Browse files Browse the repository at this point in the history
Aha/code cleanup
  • Loading branch information
andyHa authored Sep 11, 2018
2 parents e9857a4 + a0b5439 commit a2d1e6a
Show file tree
Hide file tree
Showing 44 changed files with 353 additions and 342 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<parent>
<groupId>com.scireum</groupId>
<artifactId>sirius-parent</artifactId>
<version>5.1</version>
<version>5.8</version>
</parent>
<artifactId>sirius-web</artifactId>
<version>DEVELOPMENT-SNAPSHOT</version>
<name>SIRIUS web</name>
<description>Provides a modern and scalable web server as SIRIUS module</description>

<properties>
<sirius.kernel>12.0-rc16</sirius.kernel>
<sirius.kernel>12.0-rc19</sirius.kernel>
</properties>

<dependencies>
Expand Down Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>com.scireum</groupId>
<artifactId>server-sass</artifactId>
<version>4.2.1</version>
<version>5.0</version>
</dependency>

<!-- Used as chosen JSON implementation -->
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/sirius/tagliatelle/Tagliatelle.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ private Template resolveFromCache(String path, Resource resource) {
private Template compileTemplate(String path, Resource resource, @Nullable CompilationContext parentContext)
throws CompileException {
CompilationContext compilationContext = createCompilationContext(path, resource, parentContext);
new Compiler(compilationContext, resource.getContentAsString()).compile();
Compiler compiler = new Compiler(compilationContext, resource.getContentAsString());
compiler.compile();
return handleAliasing(compilationContext.getTemplate(), compilationContext);
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/sirius/tagliatelle/TagliatelleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public void tagInfo(WebContext ctx, String tagLib, String tag) throws Exception
}
}

/**
* Provides some statistics and metrics for the internals of tagliatelle.
*
* @param ctx the request being handled
*/
@Routed("/system/tags/state")
@Permission(PERMISSION_SYSTEM_TAGS_STATE)
public void tagState(WebContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package sirius.tagliatelle.compiler;

import org.jetbrains.annotations.NotNull;
import parsii.tokenizer.Position;
import sirius.kernel.di.std.Register;
import sirius.tagliatelle.emitter.CompositeEmitter;
Expand All @@ -22,6 +21,8 @@
import sirius.tagliatelle.expression.Expression;
import sirius.tagliatelle.expression.ReadLocal;

import javax.annotation.Nonnull;

/**
* Parses an attribute expression.
* <p>
Expand Down Expand Up @@ -67,7 +68,7 @@ public Emitter process(Compiler compiler) {
return translateLiteralExpression(compiler, start, attributeName, condition);
}

@NotNull
@Nonnull
private Emitter translateLiteralExpression(Compiler compiler,
Position start,
String attributeName,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/tagliatelle/compiler/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ private void handleTag(TagHandler handler, CompositeEmitter block) {
private void parseAttributes(TagHandler handler) {
while (true) {
skipWhitespaces();
if (reader.current().isEndOfInput() || reader.current().is('>') || reader.current().is('/')) {
if (reader.current().isEndOfInput() || reader.current().is('>','/')) {
break;
}
String name = parseName();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/sirius/tagliatelle/compiler/InputProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public void consumeExpectedCharacter(char expectedCharacter) {
}
}

/**
* Determines if the parser is currently at the given text / keyword.
*
* @param offset the offset to add to the parsers position
* @param text the text or keyword to check for
* @return <tt>true</tt> if the parser is currently at the given text, <tt>false</tt> otherwise
*/
public boolean isAtText(int offset, String text) {
for (int i = 0; i < text.length(); i++) {
if (!reader.next(offset + i).is(text.charAt(i))) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/sirius/tagliatelle/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private Expression tryClassOrEnumLiteral(String id) {
int offset = 0;
while (true) {
Char current = reader.next(offset);
if (!(current.isLetter() || current.is('_') || current.is('.') || current.is('$') || current.isDigit())) {
if (!isValidClassNameChar(current)) {
break;
}
sb.append(current.getValue());
Expand All @@ -556,6 +556,10 @@ private Expression tryClassOrEnumLiteral(String id) {
return new ConstantClass(context.resolveClass(pos, literal));
}

private boolean isValidClassNameChar(Char current) {
return current.isLetter() || current.isDigit() || current.is('_', '.', '$');
}

private Expression tryEnumLiteral(String literal, int offset) {
Tuple<String, String> typeNameName = Strings.splitAtLast(literal, ".");
Class<?> enumType = context.tryResolveClass(typeNameName.getFirst()).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ public void visitExpressions(Function<Position, ExpressionVisitor> visitorSuppli
public String toString() {
return "INLINE: " + template.getName() + " { " + body + " }\n";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public ConcatExpression(Expression... expressions) {
stringExpressions.addAll(Arrays.asList(expressions));
}

/**
* Adds a new expression.
*
* @param expr the expression to add
*/
public void add(Expression expr) {
stringExpressions.add(expr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private Method findMethod(Class<?> type, String name, Class<?>[] parameterTypes)
}

// Try to find an appropriate method using coercions known to the system...
for (Method m: type.getMethods()) {
for (Method m : type.getMethods()) {
if (signatureMatch(m, name, parameterTypes)) {
if (checkSandbox(m)) {
return m;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/sirius/tagliatelle/expression/Negation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
import sirius.tagliatelle.rendering.LocalRenderContext;

/**
* Represents a boolean <tt>and</tt>.
* Represents a boolean <tt>not</tt>.
*/
public class Negation implements Expression {

private Expression expression;

/**
* Creates a new negation.
*
* @param expresion the expression to negate
*/
public Negation(Expression expresion) {
this.expression = expresion;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/sirius/tagliatelle/expression/ReadLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package sirius.tagliatelle.expression;

import sirius.tagliatelle.rendering.LocalRenderContext;

/**
* Performs a read on the local stack.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class RenderEmitterExpression implements Expression {

private InlineTemplateEmitter emitter;

/**
* Creates an expression which evaluates to the result of the given emitter.
*
* @param emitter the emitter to wrap
*/
public RenderEmitterExpression(InlineTemplateEmitter emitter) {
this.emitter = emitter;
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/sirius/tagliatelle/macros/FormatDateMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* Represents <tt>formatDate(…)</tt>.
*
* <p>
* The macro supports the datatypes {@code long} for epoch milliseconds, {@link Date} for legacy applications, and
* {@link TemporalAccessor}. The invocation is forwarded to {@link NLS#toUserString(Object)} via
* {@link Value#asLocalDate(LocalDate)}.
Expand All @@ -41,9 +41,11 @@ public void verifyArguments(List<Expression> args) {
throw new IllegalArgumentException("One parameter is expected");
}

if (!Tagliatelle.isAssignableTo(args.get(0).getType(), Long.class) &&
!Tagliatelle.isAssignableTo(args.get(0).getType(), Date.class) &&
!Tagliatelle.isAssignableTo(args.get(0).getType(), TemporalAccessor.class)) {
if (!Tagliatelle.isAssignableTo(args.get(0).getType(), Long.class)
&& !Tagliatelle.isAssignableTo(args.get(0)
.getType(),
Date.class)
&& !Tagliatelle.isAssignableTo(args.get(0).getType(), TemporalAccessor.class)) {
throw new IllegalArgumentException("Illegal parameter type");
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/sirius/tagliatelle/macros/FormatTimeMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* Represents <tt>formatTime(…)</tt>.
*
* <p>
* The macro supports the datatypes {@code long} for epoch milliseconds, {@link Date} for legacy applications, and
* {@link TemporalAccessor}. The invocation is forwarded to {@link NLS#toUserString(Object)} via
* {@link Value#asLocalTime(LocalTime)}.
Expand All @@ -41,9 +41,11 @@ public void verifyArguments(List<Expression> args) {
throw new IllegalArgumentException("One parameter is expected");
}

if (!Tagliatelle.isAssignableTo(args.get(0).getType(), Long.class) &&
!Tagliatelle.isAssignableTo(args.get(0).getType(), Date.class) &&
!Tagliatelle.isAssignableTo(args.get(0).getType(), TemporalAccessor.class)) {
if (!Tagliatelle.isAssignableTo(args.get(0).getType(), Long.class)
&& !Tagliatelle.isAssignableTo(args.get(0)
.getType(),
Date.class)
&& !Tagliatelle.isAssignableTo(args.get(0).getType(), TemporalAccessor.class)) {
throw new IllegalArgumentException("Illegal parameter type");
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/sirius/tagliatelle/macros/LeftPadMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public Class<?> getType() {

@Override
public void verifyArguments(List<Expression> args) {
if (args.size() != 3 || !Tagliatelle.isAssignableTo(args.get(0).getType(), String.class)
|| !Tagliatelle.isAssignableTo(args.get(1).getType(), String.class)
|| !Tagliatelle.isAssignableTo(args.get(2).getType(), int.class)) {
if (args.size() != 3
|| !Tagliatelle.isAssignableTo(args.get(0).getType(), String.class)
|| !Tagliatelle.isAssignableTo(args.get(1).getType(), String.class)
|| !Tagliatelle.isAssignableTo(args.get(2).getType(), int.class)) {
throw new IllegalArgumentException("Expected the first and second argument to be a string "
+ "and the third to be a integer.");
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/sirius/tagliatelle/macros/ToListMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public Class<?> getType() {
@Override
public void verifyArguments(List<Expression> args) {
if (args.isEmpty()) {
throw new IllegalArgumentException(
"Expected at least one parameter");
throw new IllegalArgumentException("Expected at least one parameter");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public String getExtraBlock(String name) {
/**
* Emits everything which is invoked from within the callback into an unescaped string.
*
* @param callback the callback which will invoke emitters.
* @param callback the callback which will invoke emitters.
* @return the contents which were emitted within the <tt>callback</tt>
*/
public String emitToString(RenderCall callback) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/sirius/web/controller/AutocompleteHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ private void writeTo(StructuredOutput out) {
* Called to generate completions for a given query.
*/
public interface ItemSearch {
/**
* Invoked to generate completions.
*
* @param query the input provided by the user
* @param result the consumer to be supplied with completions for the autocomplete
*/
void search(String query, Consumer<Completion> result);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/sirius/web/controller/FacetRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class FacetRange {

/**
* Creates a new range with the given min and max values.
*
* @param min smallest available value
* @param max the largest available value
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/sirius/web/crunchlog/Crunchlog.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public LogBuilder withURIAndUserAgent(WebContext ctx) {
*/
@CheckReturnValue
public LogBuilder set(String key, Object value) {
if (context == null) {
throw new IllegalArgumentException("LogBuilder of Crunchlog was already submitted.");
}
if (context == null) {
throw new IllegalArgumentException("LogBuilder of Crunchlog was already submitted.");
}

context.set(key, value);
return this;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/sirius/web/crunchlog/CrunchlogKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,27 @@ protected double maxCallFrequency() {
}

@Override
protected void doWork() throws Exception {
protected String doWork() throws Exception {
if (buffer.isEmpty()) {
return;
return null;
}

if (!ensureWriterIsReady()) {
buffer.clear();
return;
return null;
}

try {
int numWritten = 0;
while (TaskContext.get().isActive()) {
Context line = buffer.poll();
if (line == null) {
return;
return "Crunchlogs written: " + numWritten;
}
String lineAsString = JSON.toJSONString(line);
currentWriter.write(lineAsString);
currentWriter.write("\n");
numWritten++;
}

currentWriter.flush();
Expand All @@ -140,6 +142,7 @@ protected void doWork() throws Exception {
Exceptions.handle(Crunchlog.LOG, e);
buffer.clear();
}
return null;
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/sirius/web/health/Cluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@

package sirius.web.health;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import sirius.kernel.async.Tasks;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.Log;
import sirius.kernel.health.metrics.Metric;
import sirius.kernel.health.metrics.MetricState;
import sirius.kernel.health.metrics.Metrics;
import sirius.kernel.timer.EveryMinute;
import sirius.kernel.timer.EveryTenSeconds;
import sirius.web.services.JSONCall;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -151,5 +144,4 @@ private MetricState computeNodeState() {

return newNodeState;
}

}
6 changes: 5 additions & 1 deletion src/main/java/sirius/web/health/SystemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import sirius.web.security.Permission;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -65,6 +64,11 @@ public class SystemController extends BasicController {
*/
public static final String PERMISSION_SYSTEM_STATE = "permission-system-state";

/**
* Renders the UI for the system console.
*
* @param ctx the request being handled
*/
@Routed("/system/console")
@Permission(PERMISSION_SYSTEM_CONSOLE)
public void console(WebContext ctx) {
Expand Down
Loading

0 comments on commit a2d1e6a

Please sign in to comment.