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