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.ServiceVerificationHandler;
24  import org.jboss.as.naming.ManagedReferenceInjector;
25  import org.jboss.as.naming.ServiceBasedNamingStore;
26  import org.jboss.as.naming.deployment.ContextNames;
27  import org.jboss.as.naming.service.BinderService;
28  import org.jboss.as.naming.service.NamingService;
29  import org.jboss.dmr.ModelNode;
30  import org.jboss.logging.Logger;
31  import org.jboss.msc.service.ServiceController;
32  import org.jboss.msc.service.ServiceController.Mode;
33  import org.jboss.msc.service.ServiceTarget;
34  
35  import de.smartics.properties.api.core.app.JndiContext;
36  import de.smartics.properties.spi.config.config.ExtensionConfiguration;
37  
38  /**
39   * Adds the subsystem resource to the model.
40   */
41  final class SubsystemAdd extends AbstractAddStepHandler
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    /**
48     * The singleton instance of the operation.
49     */
50    static final SubsystemAdd INSTANCE = new SubsystemAdd();
51  
52    /**
53     * Reference to the logger for this class.
54     */
55    private static final Logger LOG = Logger.getLogger(SubsystemAdd.class);
56  
57    // --- members --------------------------------------------------------------
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    private SubsystemAdd()
64    {
65    }
66  
67    // ****************************** Inner Classes *****************************
68  
69    // ********************************* Methods ********************************
70  
71    // --- init -----------------------------------------------------------------
72  
73    // --- get&set --------------------------------------------------------------
74  
75    // --- business -------------------------------------------------------------
76  
77    @Override
78    protected void populateModel(final ModelNode operation, final ModelNode model)
79      throws OperationFailedException
80    {
81      model.setEmptyObject();
82    }
83  
84    @Override
85    protected void performRuntime(final OperationContext context,
86        final ModelNode operation, final ModelNode model,
87        final ServiceVerificationHandler verificationHandler,
88        final List<ServiceController<?>> newControllers)
89      throws OperationFailedException
90    {
91      LOG.debug("Properties configuration extension is being started ...");
92      newControllers.add(installService(context, model, verificationHandler,
93          newControllers));
94      newControllers.add(installJndi(context));
95      LOG.info("Properties configuration extension started successfully.");
96    }
97  
98    private ServiceController<ExtensionConfiguration> installService(
99        final OperationContext context, final ModelNode model,
100       final ServiceVerificationHandler verificationHandler,
101       final List<ServiceController<?>> newControllers)
102     throws OperationFailedException
103   {
104     LOG.debug("  Properties configuration extension is starting service ...");
105     final ExtensionConfiguration config = new ExtensionConfiguration();
106     final ExtensionConfigurationService service =
107         new ExtensionConfigurationService(config);
108     final ServiceController<ExtensionConfiguration> controller =
109         context.getServiceTarget()
110             .addService(ExtensionConfigurationService.SERVICE_NAME, service)
111             .addDependency(NamingService.SERVICE_NAME)
112             .addListener(verificationHandler).setInitialMode(Mode.ACTIVE)
113             .install();
114     LOG.debug("  Properties configuration extension started sucessfully.");
115     return controller;
116   }
117 
118   private ServiceController<?> installJndi(final OperationContext context)
119     throws OperationFailedException
120   {
121     LOG.debug("  Properties configuration extension is starting adding to JNDI ...");
122     final ServiceTarget target = context.getServiceTarget();
123     final String jndiName = JndiContext.extensionConfigPath();
124 
125     final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(jndiName);
126     final BinderService binder = new BinderService(bindInfo.getBindName());
127     final ServiceController<?> controller =
128         target
129             .addService(bindInfo.getBinderServiceName(), binder)
130             .addAliases(ContextNames.NAMING.append(jndiName))
131             .addDependency(
132                 ExtensionConfigurationService.SERVICE_NAME,
133                 ExtensionConfiguration.class,
134                 new ManagedReferenceInjector<ExtensionConfiguration>(binder
135                     .getManagedObjectInjector()))
136             .addDependency(bindInfo.getParentContextServiceName(),
137                 ServiceBasedNamingStore.class, binder.getNamingStoreInjector())
138             .setInitialMode(ServiceController.Mode.PASSIVE).install();
139     LOG.debug("  Properties configuration extension added to JNDI successfully.");
140     return controller;
141   }
142 
143   // --- object basics --------------------------------------------------------
144 }