Mercurial Hosting > luan
changeset 264:9e0d4452e649
implement URL style module names
git-svn-id: https://luan-java.googlecode.com/svn/trunk@265 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 29 Oct 2014 03:50:59 +0000 |
parents | 54873a389f80 |
children | 454a486d9c19 |
files | core/src/luan/cmd_line.luan core/src/luan/init.luan core/src/luan/modules/BasicLuan.java core/src/luan/modules/IoLuan.java core/src/luan/modules/JavaLuan.java core/src/luan/modules/PackageLuan.java core/src/luan/modules/StringLuan.java core/src/luan/modules/Time.luan dist/jars/luan-core-trunk.jar dist/jars/luan-logging-trunk.jar dist/jars/luan-lucene-trunk.jar dist/jars/luan-mail-trunk.jar dist/jars/luan-web-trunk.jar logging/src/luan/modules/logging/Logging.luan lucene/src/luan/modules/lucene/Lucene.luan web/src/luan/modules/web/Http.luan web/src/luan/modules/web/HttpServicer.java web/src/luan/modules/web/LuanHandler.java web/src/luan/modules/web/Web_server.luan |
diffstat | 19 files changed, 159 insertions(+), 194 deletions(-) [+] |
line wrap: on
line diff
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/cmd_line.luan --- a/core/src/luan/cmd_line.luan Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/cmd_line.luan Wed Oct 29 03:50:59 2014 +0000 @@ -1,6 +1,6 @@ -import "String" -import "Table" -import "Io" +import "luan:String" +import "luan:Table" +import "luan:Io" local standalone_usage = [=[
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/init.luan --- a/core/src/luan/init.luan Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/init.luan Wed Oct 29 03:50:59 2014 +0000 @@ -1,7 +1,7 @@ -import "Package" -import "Basic" -import "Table" -import "Io" +import "luan:Package" +import "luan:Basic" +import "luan:Table" +import "luan:Io" function Package.global(module,fn_name) local function fn(...)
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/modules/BasicLuan.java --- a/core/src/luan/modules/BasicLuan.java Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/modules/BasicLuan.java Wed Oct 29 03:50:59 2014 +0000 @@ -74,21 +74,11 @@ } public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { - try { - String src; - if( fileName==null ) { - src = Utils.readAll(new InputStreamReader(System.in)); - } else { - LuanTable t = IoLuan.get(luan,fileName); - if( t == null ) - throw luan.exception( "file '"+fileName+"' not found" ); - LuanFunction fn = (LuanFunction)t.get("read_text"); - src = (String)Luan.first(luan.call(fn)); - } - return load(luan,src,fileName,null,false); - } catch(IOException e) { - throw luan.exception(e); - } + LuanTable t = fileName==null ? IoLuan.stdin.table() : IoLuan.get(luan,fileName,false); + if( t == null ) + throw luan.exception( "file '"+fileName+"' not found" ); + LuanFunction loader = (LuanFunction)t.get("loader"); + return (LuanFunction)Luan.first(luan.call(loader,new Object[]{fileName})); } public static Object do_file(LuanState luan,String fileName) throws LuanException {
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/modules/IoLuan.java --- a/core/src/luan/modules/IoLuan.java Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/modules/IoLuan.java Wed Oct 29 03:50:59 2014 +0000 @@ -40,7 +40,7 @@ try { add( module, "read_console_line", String.class ); module.put( "protocols", newProtocols() ); - add( module, "get", LuanState.class, String.class ); + add( module, "get", LuanState.class, String.class, Boolean.class ); module.put( "stdin", stdin.table() ); add( module, "socket_server", Integer.TYPE ); } catch(NoSuchMethodException e) { @@ -206,6 +206,15 @@ return blocks(inputStream(),n); } + public LuanFunction loader(LuanState luan,String name) throws LuanException { + try { + String src = read_text(); + return BasicLuan.load(luan,src,name,null,false); + } catch(IOException e) { + throw luan.exception(e); + } + } + LuanTable table() { LuanTable tbl = Luan.newTable(); try { @@ -224,6 +233,9 @@ tbl.put( "read_blocks", new LuanJavaFunction( LuanIn.class.getMethod( "read_blocks", Integer.class ), this ) ); + tbl.put( "loader", new LuanJavaFunction( + LuanIn.class.getMethod( "loader", LuanState.class, String.class ), this + ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -231,7 +243,7 @@ } } - private static final LuanIn stdin = new LuanIn() { + static final LuanIn stdin = new LuanIn() { @Override InputStream inputStream() { return System.in; @@ -397,16 +409,22 @@ } } - public static LuanTable file(LuanState luan,String name) throws LuanException { + public static LuanTable file(LuanState luan,String name,Boolean loading) throws LuanException { + if( Boolean.TRUE.equals(loading) ) + name += ".luan"; File file = new File(name); if( !file.exists() ) return null; return new LuanFile(file).table(); } - public static LuanTable classpath(LuanState luan,String path) throws LuanException { - if( path.contains("//") ) + public static LuanTable classpath(LuanState luan,String name,Boolean loading) throws LuanException { + if( name.contains("//") ) return null; + String path = name; + boolean isLoading = Boolean.TRUE.equals(loading); + if( isLoading ) + path += ".luan"; URL url; if( !path.contains("#") ) { url = ClassLoader.getSystemResource(path); @@ -427,31 +445,77 @@ } } } - if( url == null ) + if( url != null ) + return new LuanUrl(url).table(); + + // try java + if( !isLoading ) return null; - return new LuanUrl(url).table(); + String modName = name.replace('/','.') + "Luan.LOADER"; + try { +//System.out.println("modName = "+modName); + final LuanFunction fn = PackageLuan.load_lib(luan,modName); // throws exception if not found + LuanFunction loader = new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) { + return fn; + } + }; + LuanTable tbl = Luan.newTable(); + tbl.put( "loader", loader ); + return tbl; + } catch(ClassNotFoundException e) { + } catch(NoSuchFieldException e) { + } catch(IllegalAccessException e) { + } + return null; } - private static LuanTable url(String url) throws IOException { + private static LuanTable url(String url,Boolean loading) throws IOException { + if( Boolean.TRUE.equals(loading) ) + url += ".luan"; return new LuanUrl(new URL(url)).table(); } - public static LuanTable http(String path) throws IOException { - return url("http:"+path); + public static LuanTable http(String path,Boolean loading) throws IOException { + return url("http:"+path,loading); + } + + public static LuanTable https(String path,Boolean loading) throws IOException { + return url("https:"+path,loading); } - public static LuanTable https(String path) throws IOException { - return url("https:"+path); + public static LuanTable _class(LuanState luan,String path,Boolean loading) throws LuanException { + if( !Boolean.TRUE.equals(loading) ) + return null; + final LuanFunction fn = JavaLuan.javaLoader(luan,path); + if( fn==null ) + return null; + LuanFunction loader = new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) { + return fn; + } + }; + LuanTable tbl = Luan.newTable(); + tbl.put( "loader", loader ); + return tbl; + } + + public static LuanTable luan(LuanState luan,String path,Boolean loading) throws LuanException { + return classpath( luan, "luan/modules/" + path, loading ); } private static LuanTable newProtocols() { LuanTable protocols = Luan.newTable(); try { - add( protocols, "file", LuanState.class, String.class ); - add( protocols, "classpath", LuanState.class, String.class ); + add( protocols, "file", LuanState.class, String.class, Boolean.class ); + add( protocols, "classpath", LuanState.class, String.class, Boolean.class ); add( protocols, "socket", LuanState.class, String.class ); - add( protocols, "http", String.class ); - add( protocols, "https", String.class ); + add( protocols, "http", String.class, Boolean.class ); + add( protocols, "https", String.class, Boolean.class ); + protocols.put( "class", new LuanJavaFunction( + IoLuan.class.getMethod( "_class", LuanState.class, String.class, Boolean.class ), null + ) ); + add( protocols, "luan", LuanState.class, String.class, Boolean.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -459,7 +523,7 @@ } private static LuanTable protocols(LuanState luan) { - LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("Io"); + LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); if( t == null ) return newProtocols(); t = (LuanTable)t.get("protocols"); @@ -468,17 +532,17 @@ return t; } - public static LuanTable get(LuanState luan,String name) throws LuanException { + public static LuanTable get(LuanState luan,String name,Boolean loading) throws LuanException { int i = name.indexOf(':'); if( i == -1 ) - throw luan.exception( "invalid IO name '"+name+"', missing protocol" ); + throw luan.exception( "invalid Io name '"+name+"', missing protocol" ); String protocol = name.substring(0,i); String location = name.substring(i+1); LuanTable protocols = protocols(luan); LuanFunction opener = (LuanFunction)protocols.get(protocol); if( opener == null ) throw luan.exception( "invalid protocol '"+protocol+"' in '"+name+"'" ); - return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location})); + return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location,loading})); } public static final class LuanSocket extends LuanIO {
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/modules/JavaLuan.java --- a/core/src/luan/modules/JavaLuan.java Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/modules/JavaLuan.java Wed Oct 29 03:50:59 2014 +0000 @@ -44,25 +44,22 @@ }; private static boolean isLoaded(LuanState luan) { - return PackageLuan.loaded(luan).get("Java") != null; + return PackageLuan.loaded(luan).get("luan:Java") != null; } - public static final LuanFunction javaSearcher = new LuanFunction() { - @Override public Object call(LuanState luan,Object[] args) throws LuanException { - if( !isLoaded(luan) ) - return LuanFunction.NOTHING; - String modName = (String)args[0]; - final Static s = JavaLuan.getClass(luan,modName); - if( s==null ) - return null; - LuanFunction loader = new LuanFunction() { - @Override public Object call(LuanState luan,Object[] args) { - return s; - } - }; - return loader; - } - }; + static LuanFunction javaLoader(LuanState luan,String modName) throws LuanException { + if( !isLoaded(luan) ) + return null; + final Static s = JavaLuan.getClass(luan,modName); + if( s==null ) + return null; + LuanFunction loader = new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) { + return s; + } + }; + return loader; + } private static void add(LuanTable t,String method,Class<?>... parameterTypes) { try {
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/modules/PackageLuan.java --- a/core/src/luan/modules/PackageLuan.java Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/modules/PackageLuan.java Wed Oct 29 03:50:59 2014 +0000 @@ -15,24 +15,18 @@ public final class PackageLuan { - private static final String jpath = "luan.modules.?Luan.LOADER"; - public static final LuanFunction LOADER = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) { LuanTable module = Luan.newTable(); module.put( "loaded", loaded(luan) ); - module.put( "path", "?.luan;classpath:luan/modules/?.luan" ); - module.put( "jpath", jpath ); try { module.put("require",requireFn); add( module, "load", LuanState.class, String.class ); add( module, "load_lib", LuanState.class, String.class ); - add( module, "search_path", LuanState.class, String.class, String.class ); add( module, "search", LuanState.class, String.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } - module.put( "searchers", searchers(luan) ); return module; } }; @@ -63,19 +57,6 @@ return t==null ? null : t.get(key); } - private static LuanTable searchers(LuanState luan) { - String key = "Package.searchers"; - LuanTable tbl = (LuanTable)luan.registry().get(key); - if( tbl == null ) { - tbl = Luan.newTable(); - tbl.add(fileSearcher); - tbl.add(javaSearcher); - tbl.add(JavaLuan.javaSearcher); - luan.registry().put(key,tbl); - } - return tbl; - } - public static Object require(LuanState luan,String modName) throws LuanException { Object mod = load(luan,modName); if( mod==null ) @@ -107,83 +88,14 @@ } public static Object[] search(LuanState luan,String modName) throws LuanException { - for( Object s : searchers(luan).asList() ) { - LuanFunction searcher = (LuanFunction)s; - Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName})); - if( a.length >= 1 && a[0] instanceof LuanFunction ) - return a; - } - return null; - } - - public static String search_path(LuanState luan,String name,String path) throws LuanException { - for( String s : path.split(";") ) { - String file = s.replaceAll("\\?",name); - if( file.indexOf(':') > 0 && IoLuan.get(luan,file) != null ) - return file; - } - return null; + LuanTable t = IoLuan.get(luan,modName,true); + if( t == null ) + return null; + LuanFunction loader = (LuanFunction)t.get("loader"); + LuanFunction fn = (LuanFunction)Luan.first(luan.call(loader,new Object[]{modName})); + return new Object[]{fn,modName}; } - public static final LuanFunction fileLoader = new LuanFunction() { - @Override public Object call(LuanState luan,Object[] args) throws LuanException { - String fileName = (String)args[1]; - LuanFunction fn = BasicLuan.load_file(luan,fileName); - return fn.call(luan,args); - } - }; - - public static final LuanFunction fileSearcher = new LuanFunction() { - @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { - String modName = (String)args[0]; - String path = (String)pkg(luan,"path"); - if( path==null ) - return LuanFunction.NOTHING; - String file = search_path(luan,modName,path); - return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file}; - } - }; - - - public static final LuanFunction javaLoader = new LuanFunction() { - @Override public Object call(LuanState luan,Object[] args) throws LuanException { - try { - String objName = (String)args[1]; - LuanFunction fn = load_lib(luan,objName); - return fn.call(luan,args); - } catch(ClassNotFoundException e) { - throw new RuntimeException(e); - } catch(NoSuchFieldException e) { - throw new RuntimeException(e); - } catch(IllegalAccessException e) { - throw new RuntimeException(e); - } - } - }; - - public static final LuanFunction javaSearcher = new LuanFunction() { - @Override public Object[] call(LuanState luan,Object[] args) - throws LuanException - { - String modName = (String)args[0]; - modName = modName.replace('/','.'); - String path = (String)pkg(luan,"jpath"); - if( path==null ) - path = jpath; - for( String s : path.split(";") ) { - String objName = s.replaceAll("\\?",modName); - try { - load_lib(luan,objName); // throws exception if not found - return new Object[]{javaLoader,objName}; - } catch(ClassNotFoundException e) { - } catch(NoSuchFieldException e) { - } catch(IllegalAccessException e) { - } - } - return LuanFunction.NOTHING; - } - }; - public static void block(LuanState luan,String key) { blocked(luan).put(key,true);
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/modules/StringLuan.java --- a/core/src/luan/modules/StringLuan.java Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/modules/StringLuan.java Wed Oct 29 03:50:59 2014 +0000 @@ -43,7 +43,7 @@ } public static Object __index(LuanState luan,final String s,Object key) throws LuanException { - LuanTable mod = (LuanTable)PackageLuan.loaded(luan).get("String"); + LuanTable mod = (LuanTable)PackageLuan.loaded(luan).get("luan:String"); if( mod!=null ) { Object obj = mod.get(key); if( obj instanceof LuanFunction ) {
diff -r 54873a389f80 -r 9e0d4452e649 core/src/luan/modules/Time.luan --- a/core/src/luan/modules/Time.luan Wed Oct 29 00:02:14 2014 +0000 +++ b/core/src/luan/modules/Time.luan Wed Oct 29 03:50:59 2014 +0000 @@ -1,12 +1,12 @@ -- incomplete, will add as needed -import "String" -import "Table" -import "Java" -import "java.lang.System" -import "java.util.Calendar" -import "java.util.Date" -import "java.text.SimpleDateFormat" +import "luan:String" +import "luan:Table" +import "luan:Java" +import "class:java.lang.System" +import "class:java.util.Calendar" +import "class:java.util.Date" +import "class:java.text.SimpleDateFormat" function now()
diff -r 54873a389f80 -r 9e0d4452e649 dist/jars/luan-core-trunk.jar Binary file dist/jars/luan-core-trunk.jar has changed
diff -r 54873a389f80 -r 9e0d4452e649 dist/jars/luan-logging-trunk.jar Binary file dist/jars/luan-logging-trunk.jar has changed
diff -r 54873a389f80 -r 9e0d4452e649 dist/jars/luan-lucene-trunk.jar Binary file dist/jars/luan-lucene-trunk.jar has changed
diff -r 54873a389f80 -r 9e0d4452e649 dist/jars/luan-mail-trunk.jar Binary file dist/jars/luan-mail-trunk.jar has changed
diff -r 54873a389f80 -r 9e0d4452e649 dist/jars/luan-web-trunk.jar Binary file dist/jars/luan-web-trunk.jar has changed
diff -r 54873a389f80 -r 9e0d4452e649 logging/src/luan/modules/logging/Logging.luan --- a/logging/src/luan/modules/logging/Logging.luan Wed Oct 29 00:02:14 2014 +0000 +++ b/logging/src/luan/modules/logging/Logging.luan Wed Oct 29 03:50:59 2014 +0000 @@ -1,10 +1,10 @@ -import "Java" -import "org.apache.log4j.Logger" -import "org.apache.log4j.EnhancedPatternLayout" -import "org.apache.log4j.ConsoleAppender" -import "org.apache.log4j.Level" -import "org.apache.log4j.RollingFileAppender" -import "luan.modules.logging.LuanLogger" +import "luan:Java" +import "class:org.apache.log4j.Logger" +import "class:org.apache.log4j.EnhancedPatternLayout" +import "class:org.apache.log4j.ConsoleAppender" +import "class:org.apache.log4j.Level" +import "class:org.apache.log4j.RollingFileAppender" +import "class:luan.modules.logging.LuanLogger" layout = "%d %-5p %c - %m%n"
diff -r 54873a389f80 -r 9e0d4452e649 lucene/src/luan/modules/lucene/Lucene.luan --- a/lucene/src/luan/modules/lucene/Lucene.luan Wed Oct 29 00:02:14 2014 +0000 +++ b/lucene/src/luan/modules/lucene/Lucene.luan Wed Oct 29 03:50:59 2014 +0000 @@ -1,5 +1,5 @@ -import "Java" -import "luan.modules.lucene.LuceneIndex" +import "luan:Java" +import "class:luan.modules.lucene.LuceneIndex" function Index(indexDir)
diff -r 54873a389f80 -r 9e0d4452e649 web/src/luan/modules/web/Http.luan --- a/web/src/luan/modules/web/Http.luan Wed Oct 29 00:02:14 2014 +0000 +++ b/web/src/luan/modules/web/Http.luan Wed Oct 29 03:50:59 2014 +0000 @@ -1,6 +1,6 @@ -import "Java" -import "Table" -import "luan.modules.web.LuanHandler" +import "luan:Java" +import "luan:Table" +import "class:luan.modules.web.LuanHandler" function new_luan_handler() return LuanHandler.new() @@ -11,7 +11,7 @@ function init_for_test() function get_page(mod_name) - local mod = require(mod_name) + local mod = require("site:"..mod_name) mod.service() return Table.concat(result) end
diff -r 54873a389f80 -r 9e0d4452e649 web/src/luan/modules/web/HttpServicer.java --- a/web/src/luan/modules/web/HttpServicer.java Wed Oct 29 00:02:14 2014 +0000 +++ b/web/src/luan/modules/web/HttpServicer.java Wed Oct 29 03:50:59 2014 +0000 @@ -64,7 +64,7 @@ } } - LuanTable module = (LuanTable)PackageLuan.loaded(luan).get("web/Http"); + LuanTable module = (LuanTable)PackageLuan.loaded(luan).get("luan:web/Http"); if( module == null ) throw luan.exception( "module 'web/Http' not defined" ); HttpServicer lib = new HttpServicer(request,response);
diff -r 54873a389f80 -r 9e0d4452e649 web/src/luan/modules/web/LuanHandler.java --- a/web/src/luan/modules/web/LuanHandler.java Wed Oct 29 00:02:14 2014 +0000 +++ b/web/src/luan/modules/web/LuanHandler.java Wed Oct 29 03:50:59 2014 +0000 @@ -25,7 +25,7 @@ if( target.endsWith("/") ) target += welcomeFile; try { - if( !HttpServicer.service(luan,request,response,target) ) + if( !HttpServicer.service(luan,request,response,"site:"+target) ) return; response.setStatus(HttpServletResponse.SC_OK); } catch(LuanException e) {
diff -r 54873a389f80 -r 9e0d4452e649 web/src/luan/modules/web/Web_server.luan --- a/web/src/luan/modules/web/Web_server.luan Wed Oct 29 00:02:14 2014 +0000 +++ b/web/src/luan/modules/web/Web_server.luan Wed Oct 29 03:50:59 2014 +0000 @@ -1,21 +1,21 @@ -import "Java" -import "String" -import "Io" -import "Package" -import "web/Http" +import "luan:Java" +import "luan:String" +import "luan:Io" +import "luan:Package" +import "luan:web/Http" -import "org.eclipse.jetty.server.Server" -import "org.eclipse.jetty.server.NCSARequestLog" -import "org.eclipse.jetty.server.handler.DefaultHandler" -import "org.eclipse.jetty.server.handler.HandlerList" -import "org.eclipse.jetty.server.handler.HandlerCollection" -import "org.eclipse.jetty.server.handler.ResourceHandler" -import "org.eclipse.jetty.server.handler.RequestLogHandler" -import "org.eclipse.jetty.server.handler.ContextHandler" -import "org.eclipse.jetty.server.handler.GzipHandler" -import "org.eclipse.jetty.server.handler.HandlerWrapper" -import "org.eclipse.jetty.server.session.SessionHandler" -import "luan.modules.web.AuthenticationHandler" +import "class:org.eclipse.jetty.server.Server" +import "class:org.eclipse.jetty.server.NCSARequestLog" +import "class:org.eclipse.jetty.server.handler.DefaultHandler" +import "class:org.eclipse.jetty.server.handler.HandlerList" +import "class:org.eclipse.jetty.server.handler.HandlerCollection" +import "class:org.eclipse.jetty.server.handler.ResourceHandler" +import "class:org.eclipse.jetty.server.handler.RequestLogHandler" +import "class:org.eclipse.jetty.server.handler.ContextHandler" +import "class:org.eclipse.jetty.server.handler.GzipHandler" +import "class:org.eclipse.jetty.server.handler.HandlerWrapper" +import "class:org.eclipse.jetty.server.session.SessionHandler" +import "class:luan.modules.web.AuthenticationHandler" port = 8080 @@ -73,7 +73,9 @@ function serve(dir) dir = dir.gsub("/$","") -- remove trailing '/' if any Http.dir = dir - Package.path = dir .. "?.luan;classpath:luan/modules/?.luan" + function Io.protocols.site(path,loading) + return Io.get( dir..path, loading ) + end authentication_handler.setPassword(private_password) local base = dir if base.match("^classpath:") ~= nil then