-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
CompManager.groovy
332 lines (285 loc) · 10.4 KB
/
CompManager.groovy
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
/*
* Tigase XMPP Server - The instant messaging server
* Copyright (C) 2004 Tigase, Inc. (office@tigase.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. Look for COPYING file in the top folder.
* If not, see http://www.gnu.org/licenses/.
*/
/*
Manage active server components
AS:Description: Manage active server components
AS:CommandId: comp-manager
AS:Component: basic-conf
AS:Group: Configuration
*/
package tigase.admin
import tigase.conf.ConfigRepositoryIfc
import tigase.conf.Configurable
import tigase.conf.Configurator
import tigase.osgi.ModulesManagerImpl
import tigase.server.Command
import tigase.server.Iq
import tigase.server.MessageRouterConfig
import tigase.server.XMPPServer
class DelayedReloadTask
extends Thread {
void run() {
Thread.sleep(5000);
((Configurator) XMPPServer.getConfigurator()).updateMessageRouter();
}
}
try {
def ACTION = "action";
def ACTION_ADD = "Add";
def ACTION_EDIT = "Edit";
def ACTION_REMOVE = "Remove";
def ACTION_LIST = "List";
def COMP_NAME = "comp-name";
def COMP_CLASS = "comp-class";
def SUBMIT = "confirm";
def getComponentProperties = { comp_name, comp_class ->
def conf = XMPPServer.getConfigurator();
def prop = conf.getProperties(comp_name);
try {
def comp = ModulesManagerImpl.getInstance().getServerComponent(comp_class);
if (!comp) {
def INTERNAL_COMPONENTS = [ ];
INTERNAL_COMPONENTS.addAll(MessageRouterConfig.COMPONENT_CLASSES.values());
INTERNAL_COMPONENTS.addAll(MessageRouterConfig.COMP_CLUS_MAP.values());
if (!XMPPServer.isOSGi() || INTERNAL_COMPONENTS.contains(comp_class)) {
comp = XMPPServer.class.getClassLoader().loadClass(comp_class).newInstance()
}
}
if (comp) {
comp.setName(comp_name)
def defProp = comp.getDefaults(conf.getDefConfigParams() ?: [ : ]) ?: [ : ];
defProp.putAll(prop);
return defProp;
} else {
return [ : ];
}
} catch (Exception ex) {
ex.printStackTrace();
return [ : ];
}
};
def prepareComponentConfigForAdHoc = { res, comp_name, comp_class ->
def prop = getComponentProperties(comp_name, comp_class);
def keys = [ ];
keys.addAll(prop.keySet());
keys.sort { it }.each { key ->
def val = prop[key];
if (val.getClass().isArray()) {
def tmp = [ ];
for (def x : val) {
tmp.add(String.valueOf(x));
}
val = tmp.join(',');
}
if (!(val instanceof String)) {
val = val.toString();
}
Command.addFieldValue(res, key, val);
}
};
def getConfigFromAdHoc = { p, comp_name, comp_class ->
def defProp = getComponentProperties(comp_name, comp_class);
def props = [ : ];
defProp.each { key, defVal ->
def val = Command.getFieldValue(p, key);
def tmpVal = defVal;
if (tmpVal.getClass().isArray()) {
def tmp = [ ];
for (def x : tmpVal) {
tmp.add(String.valueOf(x));
}
tmpVal = tmp.join(',');
}
if (val && !val.equals(tmpVal)) {
def cls = defVal.getClass();
if (val == "null") {
val = null
};
if (cls == Long[].class) {
def out = [ ];
val.split(',').each { out.add(Long.parseLong(it)) };
val = (out as Long[]);
} else if (cls == Integer[].class) {
def out = [ ];
val.split(',').each { out.add(Integer.parseInt(it)) };
val = (out as Integer[]);
} else if (cls == String[].class) {
def out = [ ];
val.split(',').each { out.add(it) };
val = (out as String[]);
} else if (cls == Long.class) {
val = Long.parseLong(val);
} else if (cls == Integer.class) {
val = Integer.parseInt(val);
} else if (cls == Boolean.class) {
val = Boolean.parseBoolean(val);
} else {
println "unknown convertion for key = " + key + " , " + cls.getName()
}
props.put(key, val);
}
}
return props;
};
def conf_repo = (ConfigRepositoryIfc) comp_repo
def p = (Iq) packet
// check permission
def admins = (Set) adminsSet
def stanzaFromBare = p.getStanzaFrom().getBareJID()
def isServiceAdmin = admins.contains(stanzaFromBare)
if (!isServiceAdmin) {
def result = p.commandResult(Command.DataType.result)
Command.addTextField(result, "Error", "You do not have enough permissions to access this data.");
return result
}
def comp_name = Command.getFieldValue(p, COMP_NAME)
def action = Command.getFieldValue(p, ACTION);
def submit = Command.getFieldValue(p, SUBMIT);
if (action == null) {
def res = (Iq) p.commandResult(Command.DataType.form)
def actions = [ ACTION_LIST, ACTION_ADD, ACTION_EDIT, ACTION_REMOVE ];
Command.addFieldValue(res, ACTION, actions[0], "Action",
(String[]) actions, (String[]) actions)
return res;
}
if (action == ACTION_ADD && comp_name == null) {
def res = (Iq) p.commandResult(Command.DataType.form)
Command.addFieldValue(res, COMP_NAME, "", "text-single", "Component name");
Command.addFieldValue(res, COMP_CLASS, "", "text-single", "Component class");
Command.addHiddenField(res, ACTION, ACTION_ADD);
return res;
} else if (action == ACTION_LIST && comp_name == null) {
def res = (Iq) p.commandResult(Command.DataType.form)
def compNames = [ ]
def conf = XMPPServer.getConfigurator();
def mrProps = conf.getProperties("message-router");
compNames = mrProps.get(MessageRouterConfig.MSG_RECEIVERS_NAMES_PROP_KEY).toList();
compNames += mrProps.get(MessageRouterConfig.REGISTRATOR_NAMES_PROP_KEY).toList();
compNames.each {
def mr = conf.getComponent(it);
if (mr) {
Command.addFieldMultiValue(res, it, Arrays.asList(mr.getComponentInfo().toString()))
}
}
Command.addHiddenField(res, ACTION, action);
return res
} else if (comp_name == null) {
def res = (Iq) p.commandResult(Command.DataType.form)
def compNames = [ ]
def conf = XMPPServer.getConfigurator();
conf_repo.getCompNames().findAll { conf.getComponent(it) != null }.each { compNames += it }
compNames.sort();
Command.addFieldValue(res, COMP_NAME, comp_name ?: compNames[0], "Components",
(String[]) compNames, (String[]) compNames)
Command.addHiddenField(res, ACTION, action ?: "");
return res
} else {
if (action == ACTION_REMOVE) {
def res = (Iq) p.commandResult(Command.DataType.result)
def conf = XMPPServer.getConfigurator();
def initProps = conf.getDefConfigParams().
findAll { return it.key.startsWith(Configurable.GEN_COMP_NAME) && it.value.equals(comp_name); };
def suffix = null;
initProps.each {
suffix = it.key.substring(Configurable.GEN_COMP_NAME.length());
}
conf.getDefConfigParams().remove(Configurable.GEN_COMP_NAME + suffix);
conf.getDefConfigParams().remove(Configurable.GEN_COMP_CLASS + suffix);
def mrProps = conf.getProperties("message-router");
def compNames = mrProps.get(MessageRouterConfig.MSG_RECEIVERS_NAMES_PROP_KEY).toList();
if (compNames.contains(comp_name)) {
compNames.remove(comp_name);
}
mrProps = [ : ];
mrProps.put(MessageRouterConfig.MSG_RECEIVERS_NAMES_PROP_KEY, (compNames as String[]));
conf.putProperties("message-router", mrProps);
new DelayedReloadTask().start();
Command.addTextField(res, "Note", "Operation successful.")
return res;
} else if (!submit) {
def comp_class = Command.getFieldValue(p, COMP_CLASS);
if (comp_class == null) {
def conf = XMPPServer.getConfigurator();
def initProps = conf.getDefConfigParams().
findAll { return it.key.startsWith(Configurable.GEN_COMP_NAME) && it.value.equals(comp_name); };
def suffix = null;
initProps.each {
suffix = it.key.substring(Configurable.GEN_COMP_NAME.length());
}
comp_class = conf.getDefConfigParams().get(Configurable.GEN_COMP_CLASS + suffix);
if (!comp_class) {
comp_class = MessageRouterConfig.COMPONENT_CLASSES.get(comp_name)
};
if (!comp_class) {
comp_class = MessageRouterConfig.COMP_CLUS_MAP.get(comp_name)
};
if (!comp_class && comp_name == "basic-conf") {
comp_class = XMPPServer.getConfigurator().
getClass().
getCanonicalName()
};
if (!comp_class) {
throw new Exception("Could not find component class for component: " + comp_name)
};
}
def res = (Iq) p.commandResult(Command.DataType.form)
Command.addFieldValue(res, COMP_NAME, comp_name, "fixed", "Component name");
Command.addHiddenField(res, COMP_CLASS, comp_class);
Command.addHiddenField(res, ACTION, action);
Command.addHiddenField(res, SUBMIT, SUBMIT);
prepareComponentConfigForAdHoc(res, comp_name, comp_class);
return res;
} else if (submit) {
def comp_class = Command.getFieldValue(p, COMP_CLASS);
// here we should apply results of new or edit
def res = (Iq) p.commandResult(Command.DataType.result)
def props = getConfigFromAdHoc(p, comp_name, comp_class);
def conf = XMPPServer.getConfigurator();
conf.putProperties(comp_name, props);
def initProps = conf.getDefConfigParams().
findAll { return it.key.startsWith(Configurable.GEN_COMP_NAME) && it.value.equals(comp_name); };
if (initProps.isEmpty()) {
def suffix = 1;
while (!(conf.getDefConfigParams().
findAll {
return it.key.startsWith(Configurable.GEN_COMP_NAME) && it.key.endsWith("-" + suffix);
}.
isEmpty())) {
suffix += 1;
}
conf.getDefConfigParams().put(Configurable.GEN_COMP_NAME + "-" + suffix, comp_name);
conf.getDefConfigParams().put(Configurable.GEN_COMP_CLASS + "-" + suffix, comp_class);
}
def mrProps = conf.getProperties("message-router");
def compNames = mrProps.get(MessageRouterConfig.MSG_RECEIVERS_NAMES_PROP_KEY).toList();
if (!compNames.contains(comp_name)) {
compNames.add(comp_name);
}
mrProps = [ : ];
mrProps.put(MessageRouterConfig.MSG_RECEIVERS_NAMES_PROP_KEY, (compNames as String[]));
mrProps.put(MessageRouterConfig.MSG_RECEIVERS_PROP_KEY + comp_name + ".class", comp_class)
conf.putProperties("message-router", mrProps);
new DelayedReloadTask().start();
Command.addTextField(res, "Note", "Operation successful.");
return res;
}
}
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}