Skip to content

Commit

Permalink
Fixed some hot reloading and SQL issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Betim Salihu committed Jun 21, 2016
1 parent a782e62 commit 98b862c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 26 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Lucy
A simple HTTP framework based on Netty for Java.
A micro HTTP framework based on Netty for Java.

## Requirements
- JDK8+
Expand Down Expand Up @@ -159,12 +159,13 @@ Method | Function
## Method Annotations
Annotation | Function
------------ | -------------
`@Api` | Marks method as an API method
`@Api("text/plain")` | Marks method as API and sets Content-Type
`@Api` | Marks method as an API method and ignores templating
`@Api("text/plain")` | Marks method as API and sets Content-Type and ignores templating
`@NoSession` | Does not bother with session stuff
`@Status(200)` | Sets HTTP response code
`@View("index1")` | Sets custom view
`@View("")` | Removes templating
`@NoAccess` | Does not check `hasAccess()` which returns boolean, for e.x: isLoggedIn

## Examples
Say you have an API with buyer and seller. You can have the code separately like this:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pring/lucy/server/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (cause instanceof HaltException)
Http.sendException(ctx, HttpResponseStatus.valueOf(((HaltException) cause).status), cause.getMessage());
else {
if (Server.developmentMode) {
if (true || Server.developmentMode) {
if (StringUtils.contains(cause.getMessage(), "cannot find symbol")) {
String ex[] = cause.getMessage().split("\n");

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/com/pring/lucy/server/HotReload.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,23 @@ class Reloader extends ClassLoader {
@SuppressWarnings("unchecked")
@Override
public Class<? extends HttpController> findClass(String path) {
byte[] bytes;
try {
bytes = loadClassData(path);

return
(Class<? extends HttpController>)
Class ret = null;

do {
byte[] bytes;
try {
bytes = loadClassData(path);

ret = (Class<? extends HttpController>)
defineClass(
path.split("/../")[1].replace('/', '.').replace(".class", ""),
bytes, 0, bytes.length);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} while(ret == null);

return null;
return ret;
}

private byte[] loadClassData(String classPath) throws IOException {
Expand All @@ -203,6 +206,7 @@ private byte[] loadClassData(String classPath) throws IOException {
e.printStackTrace();
}
}

return null;
}
}
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/com/pring/lucy/server/HttpController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.TreeSet;

import com.pring.lucy.annotations.Api;
import com.pring.lucy.annotations.NoAccess;
import com.pring.lucy.annotations.NoSession;
import com.pring.lucy.annotations.Status;
import com.pring.lucy.annotations.View;
Expand Down Expand Up @@ -166,16 +167,20 @@ else if (t.getTypeName().equals("double"))
idx++;
}

_method.invoke(this, parameters);

if (_method.getAnnotation(NoAccess.class) == null && hasAccess())
_method.invoke(this, parameters);
else if (_method.getAnnotation(NoAccess.class) != null)
_method.invoke(this, parameters);
else
halt = new HaltException("No access", 401);

if (halt != null) {
throw halt;
}

if (_method.getAnnotation(Api.class) != null)
if (_method.getAnnotation(Api.class) != null) {
response.headers().set(HeaderNames.CONTENT_TYPE, _method.getAnnotation(Api.class).value());

if (template.length() > 0) {
} else if (template.length() > 0) {
String[] elems = pkg.split("\\.");
elems[elems.length - 1] = template;

Expand Down Expand Up @@ -343,6 +348,10 @@ private List<String> formParams(String key, boolean number) {
return l;
}

public List<String> formParams(String key) {
return formParams(key, false);
}

public String POST(String key) {
return formParams(key, false).get(0);
}
Expand Down Expand Up @@ -426,7 +435,11 @@ public Map<String, File> files() {
}

public void sendFile(String filePath) throws Exception {
StaticFileHandler.sendFile(ctx, filePath, request, true);
StaticFileHandler.sendFile(ctx, filePath, request, true, null);
}

public void sendFile(String filePath, String fileName) throws Exception {
StaticFileHandler.sendFile(ctx, filePath, request, true, fileName);
}

public void sendFile(byte[] data, String fileName) throws Exception {
Expand Down Expand Up @@ -466,5 +479,9 @@ public void redirectWithTemplate(String to, String template) {
redirect(to);
}

public boolean hasAccess() {
return true;
}

public abstract void index() throws Exception;
}
7 changes: 5 additions & 2 deletions src/main/java/com/pring/lucy/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Server upload(String location, boolean yes) {
return this;
}

public Server database(String cs, String u, String p) {
public Server database(String cs, String u, String p, String... driver) {
config = new HikariConfig();

config.setJdbcUrl(cs);
Expand All @@ -114,7 +114,10 @@ public Server database(String cs, String u, String p) {

config.setInitializationFailFast(true);
config.setIdleTimeout(10000);
config.setMaximumPoolSize(10);
config.setMaximumPoolSize(2);

if (driver.length > 0)
config.setDriverClassName(driver[0]);

database = true;

Expand Down
43 changes: 42 additions & 1 deletion src/main/java/com/pring/lucy/server/SqlDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -67,14 +68,49 @@ public static CachedRowSet query(String query, Object... args) {
resultSet = statement.executeQuery();

result.populate(resultSet);
} catch (SQLException e) {
if (e.getMessage().contains("crashed")) {
String tokens[] = e.getMessage().split("\\s+");
System.out.println(e.getMessage() + ". Trying to auto-repair.");

Connection tempCon = null;
PreparedStatement tempStmt = null;
try {
tempCon = getConnection();
tempStmt = tempCon.prepareStatement("REPAIR TABLE "
+ tokens[1].replaceAll("\\'", "`") + ";");
tempStmt.executeQuery();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
tempStmt.close();
tempCon.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

return query(query, args);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
} catch (Exception e) {
e.printStackTrace();
}

try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}

try {
connection.close();
} catch (SQLException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -169,6 +205,7 @@ private ResultSetIterator(String query, Object[] args) {
setArgs(statement, args);
resultSet = statement.executeQuery();
} catch (Exception e) {
close();
e.printStackTrace();
}

Expand Down Expand Up @@ -219,6 +256,10 @@ public void close() {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (Exception e) { }
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/pring/lucy/server/StaticFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request)
return;
}

sendFile(ctx, Server.staticFileLocation + Http.sanitizeFileUri(request.getUri()), request, false);
sendFile(ctx, Server.staticFileLocation + Http.sanitizeFileUri(request.getUri()), request, false, null);
}

protected static void sendFile(ChannelHandlerContext ctx, String path,
FullHttpRequest request, boolean transfer) throws Exception {
FullHttpRequest request, boolean transfer, String sendName) throws Exception {
File file = new File(path);

if (file.isHidden() || file.exists() || file.isFile()) {
Expand Down Expand Up @@ -101,7 +101,7 @@ protected static void sendFile(ChannelHandlerContext ctx, String path,
if (transfer) {
response.headers().add("Content-Description", "File Transfer");
response.headers().add(HeaderNames.CONTENT_TYPE, "application/octet-stream");
response.headers().add("Content-Disposition", "attachment; filename=" + path);
response.headers().add("Content-Disposition", "attachment; filename=" + sendName != null ? sendName : path);
response.headers().add(HeaderNames.CONTENT_TRANSFER_ENCODING, "binary");
}

Expand Down

0 comments on commit 98b862c

Please sign in to comment.