Skip to content

Commit

Permalink
Test and controller cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed May 1, 2017
1 parent 1b34eb1 commit 6bf8028
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 172 deletions.
44 changes: 39 additions & 5 deletions src/main/java/io/sinistral/proteus/ProteusApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Service;
import com.google.common.util.concurrent.Service.State;
import com.google.common.util.concurrent.ServiceManager;
import com.google.common.util.concurrent.ServiceManager.Listener;
import com.google.inject.Guice;
Expand Down Expand Up @@ -104,7 +106,12 @@ public ProteusApplication(URL configURL)

public void start()
{

if(this.isRunning())
{
log.warn("Server has already started...");
return;
}

injector = injector.createChildInjector(registeredModules);

if( rootHandlerClass == null && rootHandler == null )
Expand Down Expand Up @@ -144,7 +151,7 @@ public void healthy()

public void failure(Service service)
{
log.error("Error on service: " + service);
log.error("Service failure: " + service);
}

}, MoreExecutors.directExecutor());
Expand All @@ -170,6 +177,13 @@ public void run()

public void shutdown() throws TimeoutException
{
if(!this.isRunning())
{
log.warn("Server is not running...");

return;
}

log.info("Shutting down...");

serviceManager.stopAsync().awaitStopped(5, TimeUnit.SECONDS);
Expand Down Expand Up @@ -199,7 +213,7 @@ public void buildServer()

} catch (Exception e)
{
log.error("Exception creating handlers for " + controllerClass.getName() + "!!!");
log.error("Exception creating handlers for " + controllerClass.getName() + "!!!\n" + e.getMessage(), e);
}

}
Expand Down Expand Up @@ -291,10 +305,30 @@ public void printStatus()

StringBuilder sb = new StringBuilder();

sb.append("\n\nUsing the following global headers: \n\n");
sb.append("\n\nUsing global headers: \n\n");
sb.append(globalHeadersParameters.entrySet().stream().map( e -> "\t" + e.getKey() + " = " + e.getValue() ).collect(Collectors.joining("\n")));
sb.append("\n\nRegistered the following endpoints: \n\n");
sb.append("\n\nRegistered endpoints: \n\n");
sb.append(this.registeredEndpoints.stream().sorted().map(EndpointInfo::toString).collect(Collectors.joining("\n")));
sb.append("\n\nRegistered services: \n\n");

ImmutableMultimap<State, Service> serviceStateMap = this.serviceManager.servicesByState();

String serviceStrings = serviceStateMap.asMap().entrySet().stream().sorted().flatMap( e -> {


return e.getValue().stream().map( s -> {
return "\t" + s.getClass().getSimpleName() + "\t" + e.getKey();
});


}).collect(Collectors.joining("\n"));

sb.append(serviceStrings);

sb.append("\n");

sb.append("\nListening on port " + config.getInt("application.port"));

sb.append("\n");

log.info(sb.toString());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -220,6 +221,7 @@ public ServerResponse<T> textHtml()
this.contentType = javax.ws.rs.core.MediaType.TEXT_HTML;
return this;
}


public ServerResponse<T> applicationOctetStream()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.server.endpoints.EndpointInfo;
import io.swagger.annotations.Api;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;
import io.undertow.util.Headers;
Expand Down Expand Up @@ -91,7 +92,7 @@ public enum TypeHandler
FilePathType("$T $L = $T.filePath(exchange,$S)", true, java.nio.file.Path.class, StatementParameterType.LITERAL,io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING),
AnyType("$T $L = $T.any(exchange)", true, com.jsoniter.any.Any.class, StatementParameterType.LITERAL,io.sinistral.proteus.server.Extractors.class),
JsonIteratorType("$T $L = $T.jsonIterator(exchange)", true, com.jsoniter.JsonIterator.class, StatementParameterType.LITERAL,io.sinistral.proteus.server.Extractors.class),
ModelType("$T $L = $T.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.LITERAL),
ModelType("$T $L = io.sinistral.proteus.server.Extractors.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.LITERAL),

//EnumType("$T $L = $T.enumValue(exchange,$T.class,$S)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL,io.sinistral.proteus.server.Extractors.class, StatementParameterType.TYPE, StatementParameterType.STRING),
ByteBufferType("$T $L = $T.byteBuffer(exchange,$S)", true, java.nio.ByteBuffer.class, StatementParameterType.LITERAL,io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING),
Expand Down Expand Up @@ -793,7 +794,7 @@ else if(t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))
for (Parameter p : m.getParameters())
{

if (p.getParameterizedType().equals(ServerRequest.class) || p.getParameterizedType().equals(HttpServerExchange.class))
if (p.getParameterizedType().equals(ServerRequest.class) || p.getParameterizedType().equals(HttpServerExchange.class) || p.getParameterizedType().equals(HttpHandler.class))
{
continue;
}
Expand Down Expand Up @@ -840,6 +841,11 @@ else if (p.getType().equals(HttpServerExchange.class))
{
methodBuilder.addCode("$L", "\n");
}
else if (p.getType().equals(HttpHandler.class))
{
methodBuilder.addStatement("$T $L = this", HttpHandler.class, p.getName());
methodBuilder.addCode("$L", "\n");
}
else
{
if (p.isAnnotationPresent(HeaderParam.class))
Expand Down Expand Up @@ -907,8 +913,11 @@ else if( handler.equals(TypeHandler.FromStringType) )
String interfaceType = parameterizedLiteralsNameMap.get(type);

String pType = interfaceType != null ? interfaceType + "TypeLiteral" : type.getTypeName() + ".class";

methodBuilder.addStatement(t.statement, type, p.getName(), pType);


methodBuilder.addStatement(t.statement, type, p.getName(), pType);


}
else if (t.equals(TypeHandler.OptionalFromStringType) || t.equals(TypeHandler.OptionalValueOfType))
{
Expand Down Expand Up @@ -1029,11 +1038,22 @@ else if(producesContentType.contains(MediaType.TEXT_HTML))
}
else
{
functionBlockBuilder.add("$L.$L($L);", controllerName, m.getName(), "exchange");

functionBlockBuilder.add("$L.$L($L);", controllerName, m.getName(), controllerMethodArgs);


methodBuilder.addCode(functionBlockBuilder.build());

methodBuilder.addCode("$L", "\n");


handlerClassBuilder.addMethod(methodBuilder.build());

// functionBlockBuilder.add("$L.$L($L);", controllerName, m.getName(), "exchange");
//
// methodBuilder.addCode(functionBlockBuilder.build());
//
// handlerClassBuilder.addMethod(methodBuilder.build());

}

Expand Down
16 changes: 15 additions & 1 deletion src/main/java/io/sinistral/proteus/services/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import org.slf4j.LoggerFactory;

import com.google.common.util.concurrent.AbstractIdleService;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.typesafe.config.Config;

import io.sinistral.proteus.modules.ConfigModule;
Expand All @@ -16,7 +19,8 @@
* @author jbauer
*
*/
public class BaseService extends AbstractIdleService
@Singleton
public abstract class BaseService extends AbstractIdleService implements Module
{
private static Logger log = LoggerFactory.getLogger(BaseService.class.getCanonicalName());

Expand Down Expand Up @@ -52,6 +56,16 @@ protected void shutDown() throws Exception
{
log.info("Stopping " + this.getClass().getSimpleName() );
}


/* (non-Javadoc)
* @see com.google.inject.Module#configure(com.google.inject.Binder)
*/
public void configure(Binder binder)
{


}


}
7 changes: 1 addition & 6 deletions src/test/java/io/sinistral/proteus/controllers/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

import static io.sinistral.proteus.server.ServerResponse.response;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,15 +29,14 @@
import com.google.common.io.Files;
import com.google.inject.Singleton;
import com.jsoniter.output.JsonStream;

import io.sinistral.proteus.models.User;
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;

/**
* @author jbauer
Expand Down
12 changes: 2 additions & 10 deletions src/test/java/io/sinistral/proteus/server/DefaultServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,18 @@ public void run(final RunNotifier notifier)
public void testStarted(Description description) throws Exception
{

System.out.println("testStarted: " + description.getClassName() + "." + description.getMethodName());

super.testStarted(description);
}

@Override
public void testFinished(Description description) throws Exception
{

System.out.println("testFinished: " + description.getClassName() + "." + description.getMethodName());

super.testFinished(description);
}
});

runInternal(notifier);



super.run(notifier);
}
Expand All @@ -79,12 +73,10 @@ private static void runInternal(final RunNotifier notifier)
app.addService(SwaggerService.class);
app.addService(AssetsService.class);
app.addController(Tests.class);



app.start();
while(!app.isRunning())

while (!app.isRunning())
{
try
{
Expand Down
Loading

0 comments on commit 6bf8028

Please sign in to comment.