-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
336 lines (301 loc) · 9.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2019 Tamás Gulácsi
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate env OHOME=/oracle/fmw12c/product sh -c "set -x; javac -cp classes:${DOLLAR}OHOME/jlib/frmjdapi.jar:${DOLLAR}OHOME/jlib/frmxmltools.jar:${DOLLAR}OHOME/oracle_common/modules/oracle.xdk/xmlparserv2.jar -d classes src/unosoft/forms/Serve.java"
//go:generate statik -m -f -p statik -src classes
package main
import (
"bytes"
"context"
"io"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/rjeczalik/notify"
"golang.org/x/sync/errgroup"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/UNO-SOFT/forms2xml/transform"
)
func main() {
if err := Main(); err != nil {
log.Fatalf("%+v", err)
}
}
var jdapiURLs = []string{os.Getenv("BRUNO_ID")}
func Main() error {
if len(jdapiURLs) == 1 {
jdapiURLs = append(jdapiURLs, jdapiURLs[0])
}
var concurrency = 4
formsLibPath, display := os.Getenv("FORMS_PATH"), os.Getenv("DISPLAY")
if formsLibPath == "" {
formsLibPath = filepath.Join(filepath.Dir(os.Getenv("BRUNO_HOME")), "lib")
}
app := kingpin.New("forms2xml", "Oracle Forms .fmb <-> .xml with optional conversion")
app.Flag("jdapi-src", "SRC Form JDAPI helper HTTP listener URL").
Default(jdapiURLs[0]).StringVar(&jdapiURLs[0])
app.Flag("jdapi-dst", "DEST Form JDAPI helper HTTP listener URL").
Default(jdapiURLs[1]).StringVar(&jdapiURLs[1])
app.Flag("forms.lib.path", "FORMS_PATH").Default(formsLibPath).StringVar(&formsLibPath)
app.Flag("display", "DISPLAY").Default(display).StringVar(&display)
cmdXML := app.Command("xml", "convert to-from XML").Default()
xmlSrc := cmdXML.Arg("src", "source file").ExistingFile()
xmlDst := cmdXML.Arg("dst", "destination file").String()
cmdServe := app.Command("serve", "serve (start java only)")
cmdServeAddress := cmdServe.Arg("address", "address to listen on").Required().String()
cmdTransform := app.Command("transform", "transform the XML")
tranSrc := cmdTransform.Arg("src", "source file").ExistingFile()
tranDst := cmdTransform.Arg("dst", "destination file").String()
fileSuffix := "-v11"
cmd6211 := app.Command("6to11", "convert from Forms v6 to v11").Alias("6211").Alias("convert")
upNoTransform := cmd6211.Flag("no-transform", "don't transform").Default("false").Bool()
upSrc := cmd6211.Arg("src", "source file").ExistingFile()
upDst := cmd6211.Arg("dst", "destination file").String()
upSuffix := cmd6211.Flag("suffix", "suffix of converted files").Default(fileSuffix).String()
var watchSrc, watchDst string
cmdWatch := app.Command("watch", "watch a directory and transform all appearing files")
cmdWatch.Arg("src", "source path to watch").ExistingDirVar(&watchSrc)
cmdWatch.Arg("dst", "destination path").ExistingDirVar(&watchDst)
cmdWatch.Flag("suffix", "suffix of converted files").Default(fileSuffix).StringVar(&fileSuffix)
watchNoTransform := cmdWatch.Flag("no-transform", "don't transform").Default("false").Bool()
cmdWatch.Flag("concurrency", "maximum number of conversions running in parallel").Default(strconv.Itoa(concurrency)).IntVar(&concurrency)
watchServeAddress := cmdWatch.Flag("http", "HTTP address to listen on").String()
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCh := make(chan os.Signal, 1)
go func() {
<-sigCh
cancel()
time.Sleep(time.Second)
os.Exit(1)
}()
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
jr := newJavaRunner(ctx, jdapiURLs[0], formsLibPath, display, 0, concurrency)
jr.MaxRetries = 2
converter := Converter(jr)
log.Println("converter:", converter)
switch cmd {
case cmdXML.FullCommand():
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
err := convertFiles(ctx, converter, *xmlDst, *xmlSrc)
cancel()
return err
case cmdServe.FullCommand():
http.Handle("/", jr)
log.Println("Listening on " + *cmdServeAddress)
return http.ListenAndServe(*cmdServeAddress, nil)
case cmdTransform.FullCommand():
return transformFiles(*tranDst, *tranSrc)
case cmd6211.FullCommand():
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
err := convertFiles6to11(ctx, converter, *upDst, *upSrc, !*upNoTransform, *upSuffix)
cancel()
return err
case cmdWatch.FullCommand():
http.Handle("/", jr)
grp, ctx := errgroup.WithContext(ctx)
if *watchServeAddress != "" {
grp.Go(func() error {
log.Println("Listening on " + *watchServeAddress)
return http.ListenAndServe(*watchServeAddress, nil)
})
}
grp.Go(func() error {
return watchConvert(ctx, converter, watchDst, watchSrc, !*watchNoTransform, fileSuffix, concurrency)
})
return grp.Wait()
}
return nil
}
func watchConvert(ctx context.Context, converter Converter, dstDir, srcDir string, doTransform bool, suffix string, concurrency int) error {
tokens := make(chan struct{}, concurrency)
eventCh := make(chan notify.EventInfo, 16)
if err := notify.Watch(srcDir, eventCh, eventsToWatch...); err != nil {
return errors.Wrap(err, "watch")
}
for evt := range eventCh {
fn := evt.Path()
bn := filepath.Base(fn)
if !strings.HasSuffix(bn, ".fmb") {
continue
}
go func() {
time.Sleep(time.Second)
tokens <- struct{}{}
defer func() { <-tokens }()
for i := 0; i < 10; i++ {
err := convertFiles6to11(
ctx, converter,
filepath.Join(dstDir, bn), fn, doTransform, suffix,
)
if err == nil {
break
}
log.Println(err)
time.Sleep(time.Duration(i) * time.Second)
}
}()
}
return nil
}
func transformFiles(dst, src string) error {
inp := os.Stdin
if !(src == "" || src == "-") {
var err error
if inp, err = os.Open(src); err != nil {
return errors.Wrap(err, "open "+src)
}
}
defer inp.Close()
out := os.Stdout
if !(dst == "" || dst == "-") {
var err error
if out, err = os.Create(dst); err != nil {
return errors.Wrap(err, "create "+dst)
}
}
defer out.Close()
var P transform.FormsXMLProcessor
if err := P.ProcessStream(out, inp); err != nil {
return errors.WithMessage(err, "processStream")
}
return out.Close()
}
func convertFiles6to11(ctx context.Context, converter Converter, dst, src string, doTransform bool, suffix string) error {
if dst == "" {
dst = strings.TrimSuffix(src, ".fmb") + suffix + ".fmb"
}
if dst == src {
return errors.Wrap(errors.New("overwrite source file"), src)
}
log.Printf("Convert %q to %q.", src, dst)
inp, err := os.Open(src)
if err != nil {
return errors.Wrap(err, "open "+src)
}
defer inp.Close()
if dfi, err := os.Stat(dst); err == nil {
sfi, err := inp.Stat()
if err != nil {
return errors.Wrap(err, "stat "+dst)
}
if os.SameFile(sfi, dfi) {
return errors.Wrap(errors.New("overwrite source file"), sfi.Name())
}
}
out, err := os.Create(dst)
if err != nil {
return errors.Wrap(err, "create "+dst)
}
defer out.Close()
xr, xw := io.Pipe()
var P transform.FormsXMLProcessor
var grp errgroup.Group
grp.Go(func() error {
xmlSource := xr
if doTransform {
xmlR := io.ReadCloser(xr)
tr, tw := io.Pipe()
xmlW := io.WriteCloser(tw)
xmlSrcFn := strings.TrimSuffix(src, ".fmb") + ".xml"
if xmlSrcFh, err := os.Create(xmlSrcFn); err != nil {
log.Println(err)
} else {
defer xmlSrcFh.Close()
xmlR = struct {
io.Reader
io.Closer
}{io.TeeReader(xr, xmlSrcFh), xr}
}
xmlFn := strings.TrimSuffix(dst, ".fmb") + ".xml"
if xmlFh, err := os.Create(xmlFn); err != nil {
log.Println(err)
} else {
defer xmlFh.Close()
xmlW = struct {
io.Writer
io.Closer
}{io.MultiWriter(tw, xmlFh), tw}
}
xmlSource = tr
grp.Go(func() error {
log.Println("start transform")
err := P.ProcessStream(xmlW, xmlR)
log.Printf("xml->xml: %+v", err)
tw.CloseWithError(err)
return errors.WithMessage(err, "processStream")
})
}
log.Println("start convert")
err := converter.Convert(ctx, out, xmlSource, "application/xml")
log.Printf("xml->fmb: %+v", err)
xr.CloseWithError(err)
return errors.WithMessage(err, "convert")
})
err = converter.Convert(ctx, xw, inp, "application/x-oracle-forms")
log.Printf("fmb->xml: %+v", err)
xw.CloseWithError(err)
if err != nil {
return errors.WithMessage(err, "convert")
}
if err = grp.Wait(); err != nil {
return errors.WithMessage(err, "convertFiles6to11")
}
return out.Close()
}
func convertFiles(ctx context.Context, converter Converter, dst, src string) error {
mimeType := "application/x-oracle-forms"
inp := io.ReadCloser(os.Stdin)
var err error
if src != "" && src != "-" {
return converter.ConvertFiles(ctx, dst, src)
}
var a [1024]byte
n, err := io.ReadAtLeast(inp, a[:], 4)
if err != nil {
return errors.Wrap(err, "readAtLeast stdin")
}
if bytes.HasPrefix(bytes.TrimSpace(a[:n]), []byte("<?xml")) {
mimeType = "application/xml"
}
inp = struct {
io.Reader
io.Closer
}{io.MultiReader(bytes.NewReader(a[:n]), inp), inp}
defer inp.Close()
log.Println(src, mimeType)
out := os.Stdout
if dst != "" && dst != "-" {
if out, err = os.Create(dst); err != nil {
return errors.Wrap(err, "create "+dst)
}
defer out.Close()
}
if err = converter.Convert(ctx, out, inp, mimeType); err != nil {
return errors.WithMessage(err, "convertFiles")
}
return out.Close()
}
type Converter interface {
Convert(ctx context.Context, w io.Writer, r io.Reader, mimeType string) error
ConvertFiles(ctx context.Context, dst, src string) error
}