Mercurial Hosting > luan
annotate src/luan/modules/Boot.luan @ 1424:9ab267b9427c
better load_file()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 22 Nov 2019 22:58:39 -0700 |
parents | 836e00bf7ce2 |
children | 94a6a209d4e2 |
rev | line source |
---|---|
1353
8d95711f6615
replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 require "java" |
1280 | 2 local System = require "java:java.lang.System" |
3 local URL = require "java:java.net.URL" | |
4 local BasicLuan = require "java:luan.modules.BasicLuan" | |
5 local new_error = BasicLuan.new_error | |
6 local ipairs = BasicLuan.ipairs | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
7 local set_metatable = BasicLuan.set_metatable |
1330 | 8 local try = BasicLuan.try_ |
1424 | 9 local load = BasicLuan.load |
10 local type = BasicLuan.type | |
1280 | 11 local StringLuan = require "java:luan.modules.StringLuan" |
12 local match = StringLuan.match -- String.match | |
1424 | 13 local matches = StringLuan.matches -- String.matches |
1280 | 14 local IoLuan = require "java:luan.modules.IoLuan" |
15 local LuanUrl = require "java:luan.modules.url.LuanUrl" | |
1330 | 16 local LuanJava = require "java:luan.Luan" |
17 local LuanTable = require "java:luan.LuanTable" | |
1280 | 18 |
19 | |
20 local Boot = {} | |
21 | |
22 | |
23 local function error(message) | |
24 new_error(message).throw() | |
25 end | |
26 Boot.error = error | |
27 | |
1330 | 28 local function no_security(fn) |
29 LuanJava.checkCallerSecurity("no_security") | |
30 return function(...) | |
31 local security = LuanJava.setSecurity(nil) | |
32 return try( { | |
33 fn | |
34 finally = function() | |
35 security and LuanJava.setSecurity(security) | |
36 end | |
37 }, ... ) | |
38 end | |
39 end | |
40 Boot.no_security = no_security | |
41 | |
1280 | 42 |
43 local function new_LuanIn(io) | |
44 local this = {} | |
45 this.java = io | |
46 this.to_string = io.to_string | |
47 this.read_text = io.read_text | |
48 this.read_binary = io.read_binary | |
49 this.read_lines = io.read_lines | |
50 this.read_blocks = io.read_blocks | |
51 this.exists = io.exists | |
52 this.checksum = io.checksum | |
53 this.charset = io.charset | |
54 this.set_charset = io.set_charset | |
1424 | 55 |
56 function this.to_uri_string() | |
57 return this.uri_string or io.to_uri_string() | |
58 end | |
59 | |
1280 | 60 return this |
61 end | |
62 Boot.new_LuanIn = new_LuanIn | |
63 | |
64 local function new_writer(writer) | |
65 local this = {} | |
66 this.java = writer | |
67 this.write = writer.write | |
68 this.close = writer.close | |
69 return this | |
70 end | |
71 | |
72 function Boot.text_writer(out) | |
73 return new_writer( IoLuan.luanWriter(out) ) | |
74 end | |
75 | |
76 Boot.binary_writer = new_writer | |
77 | |
78 local function new_LuanIO(io) | |
79 local this = new_LuanIn(io) | |
80 this.write = io.write | |
81 this.write_text = io.write_text | |
82 | |
83 function this.text_writer() | |
84 return new_writer( io.text_writer() ) | |
85 end | |
86 | |
87 function this.binary_writer() | |
88 return new_writer( io.binary_writer() ) | |
89 end | |
90 | |
91 return this | |
92 end | |
93 | |
94 local schemes = {} | |
1330 | 95 LuanTable.setSecurity(schemes,"schemes") |
1280 | 96 |
97 function schemes.null(path) | |
98 return new_LuanIO( IoLuan.nullIO ) | |
99 end | |
100 | |
101 function schemes.string(path) | |
102 return new_LuanIO( IoLuan.LuanString.new(path) ) | |
103 end | |
104 | |
105 function schemes.classpath(path) | |
106 local cp = IoLuan.classpath(path) | |
107 return cp and new_LuanIn(cp) | |
108 end | |
109 | |
110 function schemes.luan(path) | |
111 return schemes.classpath("luan/modules/"..path) | |
112 end | |
113 | |
114 function schemes.stdin(path) | |
115 local Io = require "luan:Io.luan" | |
116 return Io.stdin | |
117 end | |
118 | |
1364 | 119 |
120 local function new_LuanUrl(io) | |
121 local this = new_LuanIn(io) | |
122 | |
123 local function check() | |
124 io.httpCon or error "must open first" | |
125 end | |
126 | |
127 function this.get_header(name) | |
128 check() | |
129 return io.httpCon.getHeaderField(name) | |
130 end | |
131 | |
132 function this.get_response_code() | |
133 check() | |
134 return io.httpCon.getResponseCode() | |
135 end | |
136 | |
137 function this.get_response_message() | |
138 check() | |
139 return io.httpCon.getResponseMessage() | |
140 end | |
141 | |
142 return this | |
143 end | |
144 | |
1280 | 145 local function url(path,options) |
1364 | 146 return new_LuanUrl( LuanUrl.new(URL.new(path),options) ) |
1280 | 147 end |
148 | |
149 function schemes.http(path,options) | |
150 return url( "http:"..path, options ) | |
151 end | |
152 | |
153 function schemes.https(path,options) | |
154 return url( "https:"..path, options ) | |
155 end | |
156 | |
157 local function new_BaseOs(io) | |
158 local this = new_LuanIO(io) | |
159 this.wait_for = io.wait_for | |
160 return this | |
161 end | |
162 | |
163 function schemes.os(path,options) | |
164 return new_BaseOs( IoLuan.LuanOs.new(path,options) ) | |
165 end | |
166 | |
167 function schemes.bash(path,options) | |
168 return new_BaseOs( IoLuan.LuanBash.new(path,options) ) | |
169 end | |
170 | |
171 local function new_LuanFile(io) | |
172 local this = new_LuanIO(io) | |
173 this.name = io.file.getName | |
174 this.is_directory = io.file.isDirectory | |
175 this.is_file = io.file.isFile | |
176 this.delete = io.delete | |
177 this.delete_on_exit = io.file.deleteOnExit | |
178 this.mkdir = io.mkdir | |
179 this.last_modified = io.file.lastModified | |
180 this.set_last_modified = io.set_last_modified | |
181 this.length = io.file.length | |
182 this.rename_to = io.rename_to | |
1367
836e00bf7ce2
add Lucene backup_to
Franklin Schmidt <fschmidt@gmail.com>
parents:
1364
diff
changeset
|
183 this.link_to = io.link_to |
836e00bf7ce2
add Lucene backup_to
Franklin Schmidt <fschmidt@gmail.com>
parents:
1364
diff
changeset
|
184 this.symlink_to = io.symlink_to |
1311
ca742d51b31f
don't delete into symbolic links
Franklin Schmidt <fschmidt@gmail.com>
parents:
1286
diff
changeset
|
185 this.is_symbolic_link = io.is_symbolic_link |
1280 | 186 |
187 function this.child(name) | |
188 return new_LuanFile( io.child(name) ) | |
189 end | |
190 | |
191 function this.children() | |
192 local raw = io.children() | |
193 if raw == nil then | |
194 return nil | |
195 end | |
196 local rtn = {} | |
197 for _, child in ipairs(raw) do | |
198 rtn[#rtn+1] = new_LuanFile(child) | |
199 end | |
200 return rtn | |
201 end | |
202 | |
203 function this.parent() | |
204 return new_LuanFile( io.parent() ) | |
205 end | |
206 | |
207 function this.canonical() | |
208 return new_LuanFile( io.canonical() ) | |
209 end | |
210 | |
211 function this.create_temp_file(prefix,suffix) | |
212 return new_LuanFile( io.create_temp_file(prefix,suffix) ) | |
213 end | |
214 | |
215 return this | |
216 end | |
217 | |
218 function schemes.file(path) | |
219 return new_LuanFile( IoLuan.LuanFile.new(path) ) | |
220 end | |
221 | |
222 Boot.schemes = schemes | |
223 | |
224 | |
225 local function uri(name,options) | |
1286 | 226 local scheme, location = match( name, "(?s)^([^:]+):(.*)$" ) |
1280 | 227 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" ) |
1286 | 228 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" ) |
1280 | 229 return opener(location,options) |
230 end | |
231 Boot.uri = uri | |
232 | |
233 | |
234 function Boot.read(uri_str) -- for PackageLuan.java | |
235 local u = uri(uri_str) | |
236 if u==nil or not u.exists() then | |
237 return nil | |
238 end | |
239 return u.read_text() | |
240 end | |
241 | |
1424 | 242 function Boot.load_file(file) |
243 if type(file) == "string" then | |
244 if not matches(file,":") then | |
245 file = "file:"..file | |
246 end | |
247 local u = uri(file) | |
248 if u==nil or not u.exists() then | |
249 return nil | |
250 end | |
251 local src = u.read_text() | |
252 return load(src,file) | |
253 elseif type(file) == "table" and file.read_text ~= nil then | |
254 local src = file.read_text() | |
255 return load(src,file.to_uri_string()) | |
256 else | |
257 error("bad argument, expected string or uri table but got "..type(file)) | |
258 end | |
259 end | |
260 | |
1280 | 261 |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
262 local error_mt = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
263 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
264 function error_mt.__to_string(t) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
265 return t.java.getLuanStackTraceString() |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
266 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
267 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
268 function Boot.new_error_table(ex) -- for LuanException.java |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
269 local this = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
270 set_metatable(this,error_mt) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
271 this.java = ex |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
272 this.get_message = ex.getMessage |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
273 this.throw = ex.throwThis |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
274 this.get_stack_trace_string = ex.getLuanStackTraceString |
1347
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
275 this.get_java_stack_trace_string = ex.getJavaStackTraceString |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
276 return this |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
277 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
278 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
279 |
1280 | 280 return Boot |