-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrigger.go
241 lines (188 loc) · 6.03 KB
/
trigger.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
package legacybridge
import (
"context"
"github.com/project-flogo/core/data/schema"
legacyData "github.com/TIBCOSoftware/flogo-lib/core/data"
legacyTrigger "github.com/TIBCOSoftware/flogo-lib/core/trigger"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/support"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/trigger"
)
func RegisterLegacyTriggerFactory(ref string, factory legacyTrigger.Factory) {
err := trigger.LegacyRegister(ref, wrapTriggerFactory(factory))
if err != nil {
log.RootLogger().Warnf("Error registering legacy trigger '%s': %v", ref, err)
}
}
func GetTrigger(trg legacyTrigger.Trigger) trigger.Trigger {
ref := support.GetRef(trg)
return &triggerWrapper{legacyTrigger: trg, ref: ref}
}
func wrapTriggerFactory(lFactory legacyTrigger.Factory) trigger.Factory {
lTrigger := lFactory.New(nil)
newMd := toNewMetadata(lTrigger.Metadata())
w := &triggerFactoryWrapper{lFactory: lFactory, newMd: newMd, lTrigger: lTrigger}
return w
}
type triggerFactoryWrapper struct {
lFactory legacyTrigger.Factory
lTrigger legacyTrigger.Trigger
newMd *trigger.Metadata
}
func (w *triggerFactoryWrapper) Metadata() *trigger.Metadata {
return w.newMd
}
func (w *triggerFactoryWrapper) New(config *trigger.Config) (trg trigger.Trigger, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
lConfig := &legacyTrigger.Config{}
lConfig.Name = config.Id
lConfig.Id = config.Id
lConfig.Ref = config.Ref
lConfig.Settings = config.Settings
lConfig.Handlers = make([]*legacyTrigger.HandlerConfig, len(config.Handlers))
for i, hConfig := range config.Handlers {
lHandleConfig := &legacyTrigger.HandlerConfig{}
lHandleConfig.Name = hConfig.Name
lHandleConfig.Settings = hConfig.Settings
lHandleConfig.Output = make(map[string]interface{})
if hConfig.Schemas != nil {
for name, sche := range hConfig.Schemas.Output {
attr, ok := w.lTrigger.Metadata().Output[name]
if ok && attr.Type() == legacyData.TypeComplexObject {
s, err := schema.FindOrCreate(sche)
if err != nil {
return nil, err
}
lHandleConfig.Output[name] = &legacyData.ComplexObject{Metadata: s.Value(), Value: nil}
}
}
}
lConfig.Handlers[i] = lHandleConfig
}
//translate config
legacyTrg := w.lFactory.New(lConfig)
trg = &triggerWrapper{legacyTrigger: legacyTrg, ref: lConfig.Ref, legacyHandlers: lConfig.Handlers}
return trg, nil
}
type triggerWrapper struct {
ref string
legacyTrigger legacyTrigger.Trigger
legacyHandlers []*legacyTrigger.HandlerConfig
}
func (w *triggerWrapper) Ref() string {
return w.ref
}
func (w *triggerWrapper) Start() error {
return w.legacyTrigger.Start()
}
func (w *triggerWrapper) Stop() error {
return w.legacyTrigger.Stop()
}
func (w *triggerWrapper) Resume() error {
fc, ok := w.legacyTrigger.(trigger.EventFlowControlAware)
if ok {
return fc.Resume()
}
return nil
}
func (w *triggerWrapper) Pause() error {
fc, ok := w.legacyTrigger.(trigger.EventFlowControlAware)
if ok {
return fc.Pause()
}
return nil
}
func toNewMetadata(lMd *legacyTrigger.Metadata) *trigger.Metadata {
newMd := &trigger.Metadata{}
newMd.Settings = make(map[string]data.TypedValue, len(lMd.Settings))
newMd.Reply = make(map[string]data.TypedValue, len(lMd.Reply))
newMd.Output = make(map[string]data.TypedValue, len(lMd.Output))
for name, attr := range lMd.Settings {
newType, _ := ToNewTypeFromLegacy(attr.Type())
newMd.Settings[name] = data.NewTypedValue(newType, attr.Value())
}
if lMd.Handler != nil {
newMd.HandlerSettings = make(map[string]data.TypedValue, len(lMd.Handler.Settings))
for _, attr := range lMd.Handler.Settings {
newType, _ := ToNewTypeFromLegacy(attr.Type())
newMd.HandlerSettings[attr.Name()] = data.NewTypedValue(newType, attr.Value())
}
}
for name, attr := range lMd.Reply {
newType, _ := ToNewTypeFromLegacy(attr.Type())
newMd.Reply[name] = data.NewTypedValue(newType, attr.Value())
}
for name, attr := range lMd.Output {
newType, _ := ToNewTypeFromLegacy(attr.Type())
newMd.Output[name] = data.NewTypedValue(newType, attr.Value())
}
return newMd
}
func (w *triggerWrapper) Initialize(ctx trigger.InitContext) error {
//wrap init ctx
if iTrg, ok := w.legacyTrigger.(legacyTrigger.Initializable); ok {
wCtx := &triggerInitCtxWrapper{ctx: ctx, lHandlers: w.legacyHandlers}
return iTrg.Initialize(wCtx)
}
return nil
}
type triggerInitCtxWrapper struct {
ctx trigger.InitContext
lHandlers []*legacyTrigger.HandlerConfig
}
func (w *triggerInitCtxWrapper) GetHandlers() []*legacyTrigger.Handler {
handlers := w.ctx.GetHandlers()
lHandlers := make([]*legacyTrigger.Handler, len(handlers))
for i, handler := range handlers {
wrapHandler := &wrapperHandlerInf{handler: handler, lHandler: w.lHandlers[i]}
lHandler := legacyTrigger.NewHandlerAlt(wrapHandler)
lHandlers[i] = lHandler
}
return lHandlers
}
type wrapperHandlerInf struct {
handler trigger.Handler
lHandler *legacyTrigger.HandlerConfig
}
func (w *wrapperHandlerInf) Handle(ctx context.Context, triggerData map[string]interface{}) (map[string]*legacyData.Attribute, error) {
for name, td := range triggerData {
value, _, ok := GetComplexObjectInfo(td)
if ok {
triggerData[name] = value
}
}
ret, err := w.handler.Handle(ctx, triggerData)
if err != nil {
return nil, err
}
attrs := make(map[string]*legacyData.Attribute)
for name, value := range ret {
attr, _ := legacyData.NewAttribute(name, legacyData.TypeAny, value)
attrs[name] = attr
}
return attrs, nil
}
func (w *wrapperHandlerInf) GetSetting(setting string) (interface{}, bool) {
val, exists := w.handler.Settings()[setting]
return val, exists
}
func (w *wrapperHandlerInf) GetOutput() map[string]interface{} {
return w.lHandler.Output
}
func (w *wrapperHandlerInf) GetStringSetting(setting string) string {
val, exists := w.handler.Settings()[setting]
if !exists {
return ""
}
retVal, _ := coerce.ToString(val)
return retVal
}
func (w *wrapperHandlerInf) String() string {
return ""
}