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 javax.annotation.CheckForNull;
19  
20  import org.jboss.logging.Logger;
21  import org.jboss.msc.service.Service;
22  import org.jboss.msc.service.ServiceName;
23  import org.jboss.msc.service.StartContext;
24  import org.jboss.msc.service.StartException;
25  import org.jboss.msc.service.StopContext;
26  
27  import de.smartics.properties.spi.config.config.ExtensionConfiguration;
28  import de.smartics.properties.spi.config.config.PropertiesConfiguration;
29  import de.smartics.util.lang.Arg;
30  
31  /**
32   * A service wrapper around counter instances.
33   */
34  public class ExtensionConfigurationService implements
35      Service<ExtensionConfiguration>
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * The name of the counter service collection.
43     */
44    public static final ServiceName SERVICE_NAME = ServiceName.of("de",
45        "smartics", "properties", "configuration");
46  
47    /**
48     * Reference to the logger for this class.
49     */
50    private static final Logger LOG = Logger
51        .getLogger(ExtensionConfigurationService.class);
52  
53    // --- members --------------------------------------------------------------
54  
55    /**
56     * The wrapped counter instance.
57     */
58    private final ExtensionConfiguration configurations;
59  
60    // ****************************** Initializer *******************************
61  
62    // ****************************** Constructors ******************************
63  
64    /**
65     * Default constructor.
66     *
67     * @param configuration the wrapped configurations instance.
68     * @throws NullPointerException if {@code configurations} is {@code null}.
69     */
70    public ExtensionConfigurationService(
71        final ExtensionConfiguration configurations) throws NullPointerException
72    {
73      this.configurations = Arg.checkNotNull("configurations", configurations);
74    }
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- init -----------------------------------------------------------------
81  
82    // --- get&set --------------------------------------------------------------
83  
84    // --- business -------------------------------------------------------------
85  
86    /**
87     * Returns the configuration stored with the given {@code name}.
88     *
89     * @param name the name of the configuration to return.
90     * @return the requested configuration or <code>null</code>, if no
91     *         configuration is registered with this name.
92     * @throws NullPointerException if {@code name} is <code>null</code>.
93     * @throws IllegalArgumentException if {@code name} is blank.
94     */
95    @CheckForNull
96    public PropertiesConfiguration getConfiguration(final String name)
97      throws NullPointerException, IllegalArgumentException
98    {
99      final PropertiesConfiguration configuration =
100         configurations.getConfiguration(name);
101     return configuration;
102   }
103 
104   /**
105    * Adds the given configuration.
106    *
107    * @param configuration the configuration to add.
108    * @throws NullPointerException if {@code configuration} is <code>null</code>.
109    */
110   public void addConfiguration(final PropertiesConfiguration configuration)
111     throws NullPointerException
112   {
113     LOG.debug("Adding configuration " + configuration.getName());
114     configurations.addConfiguration(configuration);
115   }
116 
117   /**
118    * Removes the configuration with the given name.
119    *
120    * @param name the name of the configuration to remove.
121    * @return the removed configuration, <code>null</code> if no has been stored
122    *         by this {@code name}.
123    * @throws NullPointerException if {@code name} is <code>null</code>.
124    * @throws IllegalArgumentException if {@code name} is blank.
125    */
126   @CheckForNull
127   public PropertiesConfiguration removeConfiguration(final String name)
128     throws NullPointerException, IllegalArgumentException
129   {
130     LOG.debug("Removing configuration " + name);
131     final PropertiesConfiguration configuration =
132         configurations.removeConfiguration(name);
133     return configuration;
134   }
135 
136 //  /**
137 //   * Creates the service name for the given configuration.
138 //   *
139 //   * @param name the name of a configuration.
140 //   * @return the full name of the configuration service.
141 //   */
142 //  public static ServiceName createServiceName(final String name)
143 //  {
144 //    return SERVICE_NAME.append(name);
145 //  }
146 
147   // ... service related ......................................................
148 
149   @Override
150   public ExtensionConfiguration getValue() throws IllegalStateException,
151     IllegalArgumentException
152   {
153     return configurations;
154   }
155 
156   @Override
157   public void start(final StartContext context) throws StartException
158   {
159     LOG.info("Starting properties configuration service");
160   }
161 
162   @Override
163   public void stop(final StopContext context)
164   {
165     LOG.info("Stopping properties configuration service");
166   }
167 
168   // --- object basics --------------------------------------------------------
169 
170 }