1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.jboss.extension.resources;
17
18 import org.jboss.as.controller.AbstractWriteAttributeHandler;
19 import org.jboss.as.controller.OperationContext;
20 import org.jboss.as.controller.OperationFailedException;
21 import org.jboss.dmr.ModelNode;
22
23 import de.smartics.properties.spi.config.config.ExtensionConfiguration;
24 import de.smartics.properties.spi.config.config.PropertiesConfiguration;
25
26
27
28
29 public final class PropertiesConfigurationResourceAttributeHandler extends
30 AbstractWriteAttributeHandler<ExtensionConfiguration>
31 {
32
33
34
35
36
37
38
39 public static final PropertiesConfigurationResourceAttributeHandler INSTANCE =
40 new PropertiesConfigurationResourceAttributeHandler();
41
42
43
44
45
46
47
48
49
50
51 private PropertiesConfigurationResourceAttributeHandler()
52 {
53 }
54
55
56
57
58
59
60
61
62
63
64
65 @Override
66 protected boolean applyUpdateToRuntime(
67 final OperationContext context,
68 final ModelNode operation,
69 final String attributeName,
70 final ModelNode resolvedValue,
71 final ModelNode currentValue,
72 final AbstractWriteAttributeHandler.HandbackHolder<ExtensionConfiguration> handbackHolder)
73 throws OperationFailedException
74 {
75 if (attributeName.equals(PropertiesConfigurationResourceDefinition.AN_NAME))
76 {
77 final ExtensionConfigurationService service =
78 (ExtensionConfigurationService) context.getServiceRegistry(true)
79 .getRequiredService(ExtensionConfigurationService.SERVICE_NAME)
80 .getValue();
81
82 final String name = currentValue.asString();
83
84 final String newValue = resolvedValue.asString();
85 final PropertiesConfiguration configuration =
86 service.removeConfiguration(name);
87 if (configuration == null)
88 {
89 throw new OperationFailedException(
90 "Cannot find smartics-properties configuration with name '" + name
91 + "'.");
92 }
93
94 configuration.setName(newValue);
95 service.addConfiguration(configuration);
96
97 handbackHolder.setHandback(service.getValue());
98 context.completeStep();
99 }
100
101 return false;
102 }
103
104 @Override
105 protected void revertUpdateToRuntime(final OperationContext context,
106 final ModelNode operation, final String attributeName,
107 final ModelNode valueToRestore, final ModelNode valueToRevert,
108 final ExtensionConfiguration handback) throws OperationFailedException
109 {
110 if (handback != null)
111 {
112 final String oldValue = valueToRestore.asString();
113
114 final String newValue = valueToRevert.asString();
115 final PropertiesConfiguration configuration =
116 handback.removeConfiguration(oldValue);
117 if (configuration == null)
118 {
119 throw new OperationFailedException(String.format(
120 "Cannot find smartics-properties configuration with"
121 + " name '%' to revert to '%s'.", oldValue, newValue));
122 }
123 configuration.setName(newValue);
124 handback.addConfiguration(configuration);
125
126 context.completeStep();
127 }
128 }
129
130
131
132 }