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