forked from megamsys/vertice
/
plugins.go
85 lines (74 loc) · 2.05 KB
/
plugins.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
/*
** Copyright [2013-2015] [Megam Systems]
**
** 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.
*/
package plugins
import (
//log "code.google.com/p/log4go"
"github.com/megamsys/megamd/global"
"fmt"
)
// Every Plugins must implement this interface.
type Plugins interface {
// Called when watching a Machine.
Watcher(*global.AssemblyWithComponents, *global.Operations, *global.Component) error
// Called when notifing a Machine.
Notify(*global.EventMessage) error
}
var plugs = make(map[string]Plugins)
var plug_names = []string{"cmp", "github", "gogs", "gitlab"}
/**
**register the all plugins to "plug" array
**/
func RegisterPlugins(name string, plugin Plugins) {
plugs[name] = plugin
}
func GetPlugin(name string) (Plugins, error) {
plugin, ok := plugs[name]
if !ok {
return nil, fmt.Errorf("plugins not registered")
}
return plugin, nil
}
func Watcher(asm *global.AssemblyWithComponents) error {
if len(asm.Components) > 0 {
for i := range asm.Components {
if asm.Components[i] != nil {
for j := range asm.Components[i].Operations {
for k := range plug_names {
p, err := GetPlugin(plug_names[k])
if err != nil {
return err
}
go p.Watcher(asm, asm.Components[i].Operations[j], asm.Components[i])
}
}
}
}
}
return nil
}
func Notify(m *global.EventMessage) error {
for i := range plug_names {
p, err := GetPlugin(plug_names[i])
if err != nil {
return err
}
perr := p.Notify(m)
if perr != nil {
return perr
}
}
return nil
}