Skip to content

Commit

Permalink
Allow default to be runnable.
Browse files Browse the repository at this point in the history
  • Loading branch information
alesj committed Dec 18, 2011
1 parent d8fb95e commit fdf910c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@
*.ipr
api/target/
dist/target/
dist/lib/
dist/bin/jboss-modules.jar
dist/bin/ceylon-runtime-bootstrap.jar
dist/runtime-repo/ceylon/language/0.1/ceylon.language-0.1.car
Expand Down
27 changes: 14 additions & 13 deletions api/src/main/java/ceylon/modules/api/runtime/AbstractRuntime.java
Expand Up @@ -22,15 +22,14 @@

package ceylon.modules.api.runtime;

import java.util.logging.Logger;

import ceylon.language.descriptor.Module;
import ceylon.modules.Configuration;
import ceylon.modules.api.util.CeylonToJava;
import ceylon.modules.spi.Constants;

import com.redhat.ceylon.cmr.api.Repository;

import java.util.logging.Logger;

/**
* Abstract Ceylon Modules runtime.
* Useful for potential extension.
Expand Down Expand Up @@ -91,18 +90,20 @@ public void execute(Configuration conf) throws Exception {

ClassLoader cl = createClassLoader(name, mv, conf);
Module runtimeModule = loadModule(cl, name);
if (runtimeModule == null)
throw new IllegalArgumentException("Something went very wrong, missing runtime module!"); // TODO -- dump some more useful msg

String mn = CeylonToJava.toString(runtimeModule.getName());
if (name.equals(mn) == false)
throw new IllegalArgumentException("Input module name doesn't match module's name: " + name + " != " + mn);
String version = CeylonToJava.toString(runtimeModule.getVersion());
if (mv.equals(version) == false && Constants.DEFAULT.toString().equals(name) == false)
throw new IllegalArgumentException("Input module version doesn't match module's version: " + mv + " != " + version);
if (runtimeModule != null) {
final String mn = CeylonToJava.toString(runtimeModule.getName());
if (name.equals(mn) == false)
throw new IllegalArgumentException("Input module name doesn't match module's name: " + name + " != " + mn);

final String version = CeylonToJava.toString(runtimeModule.getVersion());
if (mv.equals(version) == false && Constants.DEFAULT.toString().equals(name) == false)
throw new IllegalArgumentException("Input module version doesn't match module's version: " + mv + " != " + version);
} else if (Constants.DEFAULT.toString().equals(name) == false) {
throw new IllegalArgumentException("Missing module.class info: " + name); // TODO -- dump some more useful msg
}

String runClassName = conf.run;
if(runClassName == null || runClassName.isEmpty())
if (runClassName == null || runClassName.isEmpty())
runClassName = name + RUN_INFO_CLASS;
invokeRun(cl, runClassName, conf.arguments);
}
Expand Down
Expand Up @@ -55,7 +55,7 @@ public ClassLoader createClassLoader(String name, String version, Configuration
/**
* Get repository extension.
*
* @param args the args
* @param conf the configuration
* @return repository extension
*/
protected Repository createRepository(Configuration conf) {
Expand Down Expand Up @@ -90,7 +90,7 @@ protected Repository createRepository(Configuration conf) {
* Get repository service.
*
* @param serviceType the service type
* @param args the args
* @param conf the configuration
* @return service instance or null
*/
protected <T> T getService(Class<T> serviceType, Configuration conf) {
Expand Down
95 changes: 57 additions & 38 deletions spi/src/main/java/ceylon/modules/Configuration.java
@@ -1,17 +1,17 @@
package ceylon.modules;

import ceylon.modules.spi.Argument;
import ceylon.modules.spi.ArgumentType;
import ceylon.modules.spi.Constants;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ceylon.modules.spi.Argument;
import ceylon.modules.spi.ArgumentType;
import ceylon.modules.spi.Constants;

/**
* Holds the configuration from the command-line arguments
*
*
* @author Stephane Epardaud <stef@epardaud.fr>
*/
public class Configuration {
Expand Down Expand Up @@ -44,68 +44,87 @@ public class Configuration {
// Non-Ceylon
public String executable;
public boolean cacheContent;
/** names of implementation classes by interface name */
public Map<String,String> impl = new HashMap<String,String>();
/**
* names of implementation classes by interface name
*/
public Map<String, String> impl = new HashMap<String, String>();

/**
* Sets an argument and checks its required number of parameters
* @param name argument name without the prefix -/+
* @param type type of option
*
* @param name argument name without the prefix -/+
* @param type type of option
* @param values list of arguments
* @param i index of the argument name
* @param i index of the argument name
* @return the index of the last argument required eaten by this argument
*/
public int setArgument(String name, ArgumentType type, String[] values, int i) {
Argument argument = Argument.forArgumentName(name, type);
if(argument == null)
throw new IllegalArgumentException("Unknown argument: "+name);
if (argument == null)
throw new IllegalArgumentException("Unknown argument: " + name);

if (i + argument.getRequiredValues() >= values.length)
throw new IllegalArgumentException("Missing argument value: " + name);

int arg = i+1;
switch(argument){
case EXECUTABLE: executable = values[arg]; break;
case CACHE_CONTENT: cacheContent = true; break;
case IMPLEMENTATION: impl.put(values[arg], values[arg+1]); break;
case SOURCE: src = values[arg]; break;
case RUN: run = values[arg]; break;
case REPOSITORY: repositories.add(values[arg]); break;
case HELP: printUsage(); break;
case VERSION: printVersion(); break;
int arg = i + 1;
switch (argument) {
case EXECUTABLE:
executable = values[arg];
break;
case CACHE_CONTENT:
cacheContent = true;
break;
case IMPLEMENTATION:
impl.put(values[arg], values[arg + 1]);
break;
case SOURCE:
src = values[arg];
break;
case RUN:
run = values[arg];
break;
case REPOSITORY:
repositories.add(values[arg]);
break;
case HELP:
printUsage();
break;
case VERSION:
printVersion();
break;
}

return i+argument.getRequiredValues();
return i + argument.getRequiredValues();
}

private void printVersion() {
System.out.println("Version: ceylon "+Constants.CEYLON_VERSION);
System.out.println("Version: ceylon " + Constants.CEYLON_VERSION);
System.exit(0);
}

public void printUsage() {
System.err.print("Usage: ceylon [options...] moduleName/version\n"
+"\n"
+" -run qualified-name: Name of a class or toplevel method to run\n"
+" (default: use the module descriptor)\n"
+" -rep path: Module repository path (can be specified more than once)\n"
+" (default: ...)\n"
+" -src path: Source path (default: source)\n"
+" -help: Prints help usage\n"
+" -version: Prints version number\n"
+" moduleName/version: Module name and version to run (required)\n"
);
+ "\n"
+ " -run qualified-name: Name of a class or toplevel method to run\n"
+ " (default: use the module descriptor)\n"
+ " -rep path: Module repository path (can be specified more than once)\n"
+ " (default: ...)\n"
+ " -src path: Source path (default: source)\n"
+ " -help: Prints help usage\n"
+ " -version: Prints version number\n"
+ " moduleName/version: Module name and version to run (required)\n"
);
System.exit(1);
}

public void check() {
if(executable == null
|| executable.isEmpty()){
if (executable == null
|| executable.isEmpty()) {
System.err.println("Missing +executable parameter\n");
printUsage();
}
if(module == null
|| module.isEmpty()){
if (module == null
|| module.isEmpty()) {
System.err.println("Missing module name\n");
printUsage();
}
Expand Down
Expand Up @@ -83,6 +83,10 @@ protected File createModuleFile(File tmpdir, Archive module) throws Exception {
}

protected void testArchive(Archive module, Archive... libs) throws Throwable {
testArchive(module, null, libs);
}

protected void testArchive(Archive module, String run, Archive... libs) throws Throwable {
File tmpdir = AccessController.doPrivileged(new PrivilegedAction<File>() {
public File run() {
return new File(System.getProperty("ceylon.repo", System.getProperty("java.io.tmpdir")));
Expand All @@ -95,17 +99,28 @@ public File run() {
for (Archive lib : libs)
files.add(createModuleFile(tmpdir, lib));

String fullName = module.getName();
int p = fullName.indexOf("-");
if (p < 0)
throw new IllegalArgumentException("No name and version split: " + fullName);
String name;
String version;

String name = fullName.substring(0, p);
String version = fullName.substring(p + 1, fullName.lastIndexOf("."));
String fullName = module.getName();
final boolean isDefault = (Constants.DEFAULT + ".car").equals(fullName);
if (isDefault) {
name = Constants.DEFAULT.toString();
version = Repository.NO_VERSION;
} else {
int p = fullName.indexOf("-");
if (p < 0)
throw new IllegalArgumentException("No name and version split: " + fullName);

name = fullName.substring(0, p);
version = fullName.substring(p + 1, fullName.lastIndexOf("."));
}

Map<String, String> args = new LinkedHashMap<String, String>();
args.put(Constants.IMPL_ARGUMENT_PREFIX+Argument.EXECUTABLE.toString(), RUNTIME_IMPL);
args.put(Constants.CEYLON_ARGUMENT_PREFIX+Argument.REPOSITORY.toString(), tmpdir.toString());
args.put(Constants.IMPL_ARGUMENT_PREFIX + Argument.EXECUTABLE.toString(), RUNTIME_IMPL);
args.put(Constants.CEYLON_ARGUMENT_PREFIX + Argument.REPOSITORY.toString(), tmpdir.toString());
if (run != null)
args.put(Constants.CEYLON_ARGUMENT_PREFIX + Argument.RUN.toString(), run);

execute(name + "/" + version, args);
} finally {
Expand All @@ -129,17 +144,17 @@ protected void src(String module, String src) throws Throwable {

protected void car(String module, Map<String, String> extra) throws Throwable {
Map<String, String> args = new LinkedHashMap<String, String>();
args.put(Constants.IMPL_ARGUMENT_PREFIX+Argument.EXECUTABLE.toString(), RUNTIME_IMPL);
args.put(Constants.CEYLON_ARGUMENT_PREFIX+Argument.REPOSITORY.toString(), getRepo().getPath());
args.put(Constants.IMPL_ARGUMENT_PREFIX + Argument.EXECUTABLE.toString(), RUNTIME_IMPL);
args.put(Constants.CEYLON_ARGUMENT_PREFIX + Argument.REPOSITORY.toString(), getRepo().getPath());
args.putAll(extra);

execute(module, args);
}

protected void src(String module, String src, Map<String, String> extra) throws Throwable {
Map<String, String> args = new LinkedHashMap<String, String>();
args.put(Constants.IMPL_ARGUMENT_PREFIX+Argument.EXECUTABLE.toString(), RUNTIME_IMPL);
args.put(Constants.CEYLON_ARGUMENT_PREFIX+Argument.SOURCE.toString(), src);
args.put(Constants.IMPL_ARGUMENT_PREFIX + Argument.EXECUTABLE.toString(), RUNTIME_IMPL);
args.put(Constants.CEYLON_ARGUMENT_PREFIX + Argument.SOURCE.toString(), src);
args.putAll(extra);

execute(module, args);
Expand Down
Expand Up @@ -53,4 +53,12 @@ public void testVisibility() throws Throwable {
testArchive(module, lib);
}

@Test
public void testDefaultRun() throws Throwable {
JavaArchive module = ShrinkWrap.create(JavaArchive.class, "default.car");
module.addClasses(org.jboss.acme.run.class);

testArchive(module, org.jboss.acme.run.class.getName());
}

}

0 comments on commit fdf910c

Please sign in to comment.