Skip to content

Commit

Permalink
A few tweaks to compiling Java verticle pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
purplefox committed May 17, 2012
1 parent 7fd3b8f commit f3397a8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 14 deletions.
41 changes: 41 additions & 0 deletions src/examples/java_source/echo/EchoSourceServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.vertx.java.core.Handler;
import org.vertx.java.core.net.NetSocket;
import org.vertx.java.core.streams.Pump;
import org.vertx.java.deploy.Verticle;

/**
* An echo server that gets compiled from source
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class EchoSourceServer extends Verticle {

public void start() {

// Reference some other class to show that it gets compiled too

System.out.println(new SomeOtherClass().foo());

vertx.createNetServer().connectHandler(new Handler<NetSocket>() {
public void handle(final NetSocket socket) {
Pump.createPump(socket, socket).start();
}
}).listen(1234);
}
}
24 changes: 24 additions & 0 deletions src/examples/java_source/echo/SomeOtherClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class SomeOtherClass {
public String foo() {
return "foo";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.vertx.java.deploy.impl.java;

import java.io.File;
import java.util.Collections;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.core.logging.impl.LoggerFactory;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
Expand All @@ -27,6 +27,8 @@
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import java.io.File;
import java.util.Collections;

/**
*
Expand All @@ -35,17 +37,20 @@
* @author Janne Hietam&auml;ki
*/
public class CompilingClassLoader extends ClassLoader {

private static final Logger log = LoggerFactory.getLogger(CompilingClassLoader.class);

private final File sourceFile;
private final MemoryFileManager fileManager;

public CompilingClassLoader(ClassLoader loader, String sourceName) {
super(loader);
this.sourceFile = new File(sourceName).getAbsoluteFile();
if(!this.sourceFile.canRead()) {
if (!this.sourceFile.canRead()) {
throw new RuntimeException("File not found: " + sourceName);
}
try {
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(null, null, null);

Expand All @@ -57,7 +62,7 @@ public CompilingClassLoader(ClassLoader loader, String sourceName) {
boolean valid = task.call();

for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
System.out.println(d);
log.debug(d);
}
if (!valid) {
throw new RuntimeException("Compilation failed!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private boolean isJavaSource(String main) {
public Verticle createVerticle(String main, ClassLoader loader) throws Exception {
ClassLoader cl = loader;
String className = main;
if(isJavaSource(main)) {
if (isJavaSource(main)) {
CompilingClassLoader compilingLoader = new CompilingClassLoader(loader, main);
className = compilingLoader.resolveMainClassName();
cl = compilingLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package org.vertx.java.deploy.impl.java;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -24,27 +29,22 @@
import java.util.HashMap;
import java.util.Map;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;

/**
* Java in-memory file manager used by {@link CompilingClassLoader} to handle
* compiled classes
*
* @author Janne Hietam&auml;ki
*/
public class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
private final Map<String, ByteArrayOutputStream> compiledClasses = new HashMap<String, ByteArrayOutputStream>();
private final Map<String, ByteArrayOutputStream> compiledClasses = new HashMap<>();

public MemoryFileManager(JavaFileManager fileManager) {
super(fileManager);
}

@Override
public JavaFileObject getJavaFileForOutput(Location location, final String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
public JavaFileObject getJavaFileForOutput(Location location, final String className,
JavaFileObject.Kind kind, FileObject sibling) throws IOException {
try {
return new SimpleJavaFileObject(new URI(""), kind) {
public OutputStream openOutputStream() throws IOException {
Expand Down

0 comments on commit f3397a8

Please sign in to comment.