Skip to content

Commit

Permalink
final stuff for hashing files and checking for file existence
Browse files Browse the repository at this point in the history
  • Loading branch information
deanhiller committed Mar 28, 2017
1 parent a652905 commit 66c0f20
Show file tree
Hide file tree
Showing 46 changed files with 397 additions and 224 deletions.
2 changes: 1 addition & 1 deletion runAllTesting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ unzip mytest.zip
test_result=$?
if [ $test_result -eq 0 ]
then
echo "Successfully Unzipped Production Server "
echo "Successfully Unzipped Production Server to `pwd`"
else
echo "Unzip Production server FAILED"
exit $test_result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.google.inject.Guice;
import com.google.inject.Injector;

import groovy.lang.GroovyClassLoader;

public class TemplateCompilerTask extends AbstractCompile {

// private TemplateCompileOptions options = new TemplateCompileOptions();
Expand Down Expand Up @@ -69,8 +71,6 @@ public void compileImpl(TemplateCompileOptions options) throws IOException {
config.setGroovySrcWriteDirectory(groovySrcGen);
System.out.println("custom tags="+options.getCustomTags());
config.setCustomTagsFromPlugin(options.getCustomTags());
Injector injector = Guice.createInjector(new RouterLookupModule(), new DevTemplateModule(config));
HtmlToJavaClassCompiler compiler = injector.getInstance(HtmlToJavaClassCompiler.class);

LogLevel logLevel = getProject().getGradle().getStartParameter().getLogLevel();

Expand All @@ -92,6 +92,14 @@ public void compileImpl(TemplateCompileOptions options) throws IOException {
OutputStreamWriter write = new OutputStreamWriter(routeOut, encoding.name());
BufferedWriter bufWrite = new BufferedWriter(write)
) {

Injector injector = Guice.createInjector(
new RouterLookupModule(),
new DevTemplateModule(config, new PluginCompileCallback(destinationDir, bufWrite))
);
HtmlToJavaClassCompiler compiler = injector.getInstance(HtmlToJavaClassCompiler.class);
GroovyClassLoader cl = new GroovyClassLoader();

for(File f : files) {
System.out.println("file="+f);

Expand All @@ -100,7 +108,7 @@ public void compileImpl(TemplateCompileOptions options) throws IOException {

String source = readSource(f);

compiler.compile(fullName, source, new PluginCompileCallback(destinationDir, bufWrite));
compiler.compile(cl, fullName, source);
}
}

Expand Down Expand Up @@ -164,8 +172,8 @@ public PluginCompileCallback(File destinationDir, BufferedWriter bufWrite) {
this.destinationDir = destinationDir;
this.routeOut = bufWrite;
}
public void compiledGroovyClass(GroovyClass clazz) {

public void compiledGroovyClass(GroovyClassLoader groovyCl, GroovyClass clazz) {
String name = clazz.getName();
String path = name.replace('.', '/');
String fullPathName = path+".class";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.webpieces.util.logging.LoggerFactory;
import org.webpieces.router.impl.StaticRoute;
import org.webpieces.router.impl.compression.CompressionCacheSetup;
import org.webpieces.router.impl.compression.FileMeta;

public class DevCompressionCacheSetup implements CompressionCacheSetup {

Expand All @@ -17,7 +18,7 @@ public void setupCache(List<StaticRoute> staticRoutes) {
}

@Override
public String relativeUrlToHash(String path) {
public FileMeta relativeUrlToHash(String path) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;

import org.webpieces.ctx.api.RouterRequest;
import org.webpieces.router.impl.compression.FileMeta;

public interface RoutingService {

Expand All @@ -22,5 +23,5 @@ public interface RoutingService {
*/
String convertToUrl(String routeId, Map<String, String> notUrlEncodedArgs);

String relativeUrlToHash(String urlPath);
FileMeta relativeUrlToHash(String urlPath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.webpieces.router.api.RoutingService;
import org.webpieces.router.api.Startable;
import org.webpieces.router.api.exceptions.BadCookieException;
import org.webpieces.router.impl.compression.FileMeta;
import org.webpieces.util.logging.Logger;
import org.webpieces.util.logging.LoggerFactory;

Expand Down Expand Up @@ -48,7 +49,7 @@ public String convertToUrl(String routeId, Map<String, String> args) {
}

@Override
public String relativeUrlToHash(String urlPath) {
public FileMeta relativeUrlToHash(String urlPath) {
if(!urlPath.startsWith("/"))
urlPath = "/"+urlPath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.webpieces.router.api.routing.RouteModule;
import org.webpieces.router.api.routing.WebAppMeta;
import org.webpieces.router.impl.compression.CompressionCacheSetup;
import org.webpieces.router.impl.compression.FileMeta;
import org.webpieces.router.impl.hooks.ClassForName;
import org.webpieces.router.impl.loader.ControllerLoader;
import org.webpieces.router.impl.model.AbstractRouteBuilder;
Expand Down Expand Up @@ -231,7 +232,7 @@ public String convertToUrl(String routeId, Map<String, String> args) {
return invoker.convertToUrl(routeId, args);
}

public String relativeUrlToHash(String urlPath) {
public FileMeta relativeUrlToHash(String urlPath) {
return compressionCacheSetup.relativeUrlToHash(urlPath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public interface CompressionCacheSetup {

void setupCache(List<StaticRoute> staticRoutes);

String relativeUrlToHash(String path);
FileMeta relativeUrlToHash(String path);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.webpieces.router.impl.compression;

public class FileMeta {

private String hash;

public FileMeta() {
this(null);
}

public FileMeta(String hash) {
this.hash = hash;
}

public String getHash() {
return hash;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ProdCompressionCacheSetup implements CompressionCacheSetup {
private MimeTypes mimeTypes;
private List<String> encodings = new ArrayList<>();
private FileUtil fileUtil;
private Map<String, String> pathToHash = new HashMap<>();
private Map<String, FileMeta> pathToFileMeta = new HashMap<>();

@Inject
public ProdCompressionCacheSetup(CompressionLookup lookup, RouterConfig config, MimeTypes mimeTypes, FileUtil fileUtil) {
Expand Down Expand Up @@ -116,14 +116,18 @@ private void transferAndCompress(Properties p, File directory, File destination,
private void maybeAddFileToCache(Properties properties, File src, File destination, String urlPath) {
String name = src.getName();
int indexOf = name.lastIndexOf(".");
if(indexOf < 0)
if(indexOf < 0) {
pathToFileMeta.put(urlPath, new FileMeta());
return; //do nothing
}
String extension = name.substring(indexOf+1);

MimeTypeResult mimeType = mimeTypes.extensionToContentType(extension, "application/octet-stream");
Compression compression = lookup.createCompressionStream(encodings, extension, mimeType);
if(compression == null)
if(compression == null) {
pathToFileMeta.put(urlPath, new FileMeta());
return;
}

//before we do the below, do a quick timestamp check to avoid reading in the files when not necessary
long lastModifiedSrc = src.lastModified();
Expand All @@ -133,7 +137,7 @@ private void maybeAddFileToCache(Properties properties, File src, File destinati
String previousHash = properties.getProperty(urlPath);
if(lastModified > lastModifiedSrc && previousHash != null) {
log.info("timestamp later than src so skipping writing to="+destination);
pathToHash.put(urlPath, previousHash);
pathToFileMeta.put(urlPath, new FileMeta(previousHash));
return; //no need to check anything as destination was written after this source file
}

Expand All @@ -148,7 +152,7 @@ private void maybeAddFileToCache(Properties properties, File src, File destinati
+ "corrupted. You will need to delete the whole cache directory");

log.info("Previous file is the same, no need to compress to="+destination+" hash="+hash);
pathToHash.put(urlPath, previousHash);
pathToFileMeta.put(urlPath, new FileMeta(previousHash));
return;
}
}
Expand All @@ -158,12 +162,12 @@ private void maybeAddFileToCache(Properties properties, File src, File destinati
//if file writing succeeded, set the hash
properties.setProperty(urlPath, hash);

String existing = pathToHash.get(urlPath);
FileMeta existing = pathToFileMeta.get(urlPath);
if(existing != null)
throw new IllegalStateException("this urlpath="+urlPath+" is referencing two files. hash1="+existing+" hash2="+hash
throw new IllegalStateException("this urlpath="+urlPath+" is referencing two files. hash1="+existing.getHash()+" hash2="+hash
+" You should search your logs for this hash");

pathToHash.put(urlPath, hash);
pathToFileMeta.put(urlPath, new FileMeta(hash));

log.info("compressed "+src.length()+" bytes to="+destination.length()+" to file="+destination+" hash="+hash);

Expand Down Expand Up @@ -196,7 +200,7 @@ private void createDirectory(File directoryToCreate) {
}

@Override
public String relativeUrlToHash(String path) {
return pathToHash.get(path);
public FileMeta relativeUrlToHash(String path) {
return pathToFileMeta.get(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public abstract class AbstractTag implements GroovyGen {

@Override
public void generateStartAndEnd(ScriptOutput sourceCode, Token token, int uniqueId, CompileCallback callback) {
public void generateStartAndEnd(ScriptOutput sourceCode, Token token, int uniqueId) {
String name = getName();
throw new IllegalArgumentException(name+" tag can only be used with a body so"
+ " #{"+name+"/} is not usable. "+token.getSourceLocation(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import org.codehaus.groovy.tools.GroovyClass;

public interface CompileCallback {
import groovy.lang.GroovyClassLoader;

void compiledGroovyClass(GroovyClass clazz);
public interface CompileCallback {

void compiledGroovyClass(GroovyClassLoader groovyCl, GroovyClass clazz);

/**
* Allows the compiler to write a file out with all routeids from html files that can be used at startup time
* to validate all routes so we catch any errors on mistyped route ids before going to production (ie. build time as your
Expand All @@ -20,5 +22,4 @@ public interface CompileCallback {
void recordRouteId(String routeId, List<String> argNames, String sourceLocation);

void recordPath(String relativeUrlPath, String sourceLocation);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.webpieces.templating.api;

import org.webpieces.templating.impl.DevTemplateCompileCallback;
import org.webpieces.templating.impl.DevTemplateService;

import com.google.inject.Binder;
Expand All @@ -8,14 +9,21 @@
public class DevTemplateModule implements Module {

private TemplateCompileConfig config;
private CompileCallback callback;

public DevTemplateModule(TemplateCompileConfig config) {
this(config, new DevTemplateCompileCallback());
}

public DevTemplateModule(TemplateCompileConfig config, CompileCallback callback) {
this.config = config;
this.callback = callback;
}

@Override
public void configure(Binder binder) {
binder.bind(TemplateService.class).to(DevTemplateService.class).asEagerSingleton();
binder.bind(TemplateCompileConfig.class).toInstance(config);
binder.bind(CompileCallback.class).toInstance(callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public interface GroovyGen {

String getName();

void generateStartAndEnd(ScriptOutput sourceCode, Token token, int uniqueId, CompileCallback callback);
void generateStartAndEnd(ScriptOutput sourceCode, Token token, int uniqueId);

void generateStart(ScriptOutput sourceCode, Token token, int uniqueId, CompileCallback callback);
void generateStart(ScriptOutput sourceCode, Token token, int uniqueId);

void generateEnd(ScriptOutput sourceCode, Token token, int uniqueId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.inject.Module;

public class RouterLookupModule implements Module {

@Override
public void configure(Binder binder) {
binder.bind(RouterLookup.class).to(NullRouterLookup.class).asEagerSingleton();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.webpieces.templating.impl;

import java.util.List;

import org.codehaus.groovy.tools.GroovyClass;
import org.webpieces.templating.api.CompileCallback;

import groovy.lang.GroovyClassLoader;

public class DevTemplateCompileCallback implements CompileCallback {

@Override
public void compiledGroovyClass(GroovyClassLoader cl, GroovyClass groovyClass) {
cl.defineClass(groovyClass.getName(), groovyClass.getBytes());
}

@Override
public void recordRouteId(String routeId, List<String> argNames, String sourceLocation) {
}

@Override
public void recordPath(String relativeUrlPath, String sourceLocation) {
}

}
Loading

0 comments on commit 66c0f20

Please sign in to comment.