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 java.util.List;
19  
20  import org.jboss.as.controller.AbstractAddStepHandler;
21  import org.jboss.as.controller.OperationContext;
22  import org.jboss.as.controller.OperationFailedException;
23  import org.jboss.as.controller.PathAddress;
24  import org.jboss.as.controller.ServiceVerificationHandler;
25  import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
26  import org.jboss.dmr.ModelNode;
27  import org.jboss.logging.Logger;
28  import org.jboss.msc.service.ServiceController;
29  
30  import de.smartics.properties.api.core.app.JndiContext;
31  import de.smartics.properties.spi.config.config.PropertiesConfiguration;
32  
33  /**
34   * The operation to add a configuration resource instance.
35   */
36  public final class PropertiesConfigurationResourceAdd extends
37      AbstractAddStepHandler
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    /**
44     * The operation singleton.
45     */
46    public static final PropertiesConfigurationResourceAdd INSTANCE =
47        new PropertiesConfigurationResourceAdd();
48  
49    /**
50     * Reference to the logger for this class.
51     */
52    private static final Logger LOG = Logger
53        .getLogger(PropertiesConfigurationResourceAdd.class);
54  
55    // --- members --------------------------------------------------------------
56  
57    // ****************************** Initializer *******************************
58  
59    // ****************************** Constructors ******************************
60  
61    private PropertiesConfigurationResourceAdd()
62    {
63    }
64  
65    // ****************************** Inner Classes *****************************
66  
67    // ********************************* Methods ********************************
68  
69    // --- init -----------------------------------------------------------------
70  
71    // --- get&set --------------------------------------------------------------
72  
73    // --- business -------------------------------------------------------------
74  
75    @Override
76    protected void populateModel(final ModelNode operation, final ModelNode model)
77      throws OperationFailedException
78    {
79      PropertiesConfigurationResourceDefinition.NAME.validateAndSet(operation,
80          model);
81  
82      PropertiesConfigurationResourceDefinition.INSTANCE
83          .getDefinitionsComponent().populateModel(operation, model);
84      PropertiesConfigurationResourceDefinition.INSTANCE.getCacheComponent()
85          .populateModel(operation, model);
86      PropertiesConfigurationResourceDefinition.INSTANCE.getDataSourceComponent()
87          .populateModel(operation, model);
88      PropertiesConfigurationResourceDefinition.INSTANCE.getSecurityComponent()
89          .populateModel(operation, model);
90      PropertiesConfigurationResourceDefinition.INSTANCE.getFactoriesComponent()
91          .populateModel(operation, model);
92    }
93  
94    @Override
95    protected void performRuntime(final OperationContext context,
96        final ModelNode operation, final ModelNode model,
97        final ServiceVerificationHandler verificationHandler,
98        final List<ServiceController<?>> newControllers)
99      throws OperationFailedException
100   {
101     LOG.debug("Properties configuration started to be added ...");
102     final String configName =
103         PathAddress
104             .pathAddress(operation.get(ModelDescriptionConstants.ADDRESS))
105             .getLastElement().getValue();
106     addConfiguration(context, configName, model);
107     LOG.info("Properties configuration '" + configName
108              + "' added successfully.");
109   }
110 
111   private void addConfiguration(final OperationContext context,
112       final String configName, final ModelNode model)
113     throws OperationFailedException
114   {
115     final ServiceController<?> controller =
116         context.getServiceRegistry(false).getService(
117             ExtensionConfigurationService.SERVICE_NAME);
118     final ExtensionConfigurationService service =
119         (ExtensionConfigurationService) controller.getService();
120 
121     final PropertiesConfiguration propertiesConfig =
122         new PropertiesConfiguration();
123     propertiesConfig.setName(configName);
124     final String identifier =
125         JndiContext.extensionConfigPath() + ':' + configName;
126     PropertiesConfigurationResourceDefinition.INSTANCE.apply(identifier,
127         propertiesConfig, model);
128 
129     service.addConfiguration(propertiesConfig);
130   }
131 
132   // --- object basics --------------------------------------------------------
133 
134 }