View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * The attribute handler for the properties configuration resource.
28   */
29  public final class PropertiesConfigurationResourceAttributeHandler extends
30      AbstractWriteAttributeHandler<ExtensionConfiguration>
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    /**
37     * The attribute handler singleton.
38     */
39    public static final PropertiesConfigurationResourceAttributeHandler INSTANCE =
40        new PropertiesConfigurationResourceAttributeHandler();
41  
42    // --- members --------------------------------------------------------------
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    /**
49     * Default constructor.
50     */
51    private PropertiesConfigurationResourceAttributeHandler()
52    {
53    }
54  
55    // ****************************** Inner Classes *****************************
56  
57    // ********************************* Methods ********************************
58  
59    // --- init -----------------------------------------------------------------
60  
61    // --- get&set --------------------------------------------------------------
62  
63    // --- business -------------------------------------------------------------
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   // --- object basics --------------------------------------------------------
131 
132 }