View Javadoc

1   /*
2    * Copyright 2012-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.impl.config.domain.key.envapp;
17  
18  import static org.apache.commons.lang.StringUtils.defaultIfBlank;
19  import de.smartics.properties.api.config.domain.key.ApplicationId;
20  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
21  import de.smartics.properties.api.config.domain.key.ConfigurationKeyFactory;
22  import de.smartics.properties.api.config.domain.key.EnvironmentId;
23  
24  /**
25   * Factory for configuration keys.
26   */
27  final class EnvAppConfigurationKeyFactory implements
28      ConfigurationKeyFactory<EnvAppConfigurationKey>
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The default key referencing any environment and any application.
36     */
37    private static final EnvAppConfigurationKey DEFAULT =
38        new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
39  
40    // --- members --------------------------------------------------------------
41  
42    // ****************************** Initializer *******************************
43  
44    // ****************************** Constructors ******************************
45  
46    /**
47     * Default constructor.
48     */
49    EnvAppConfigurationKeyFactory()
50    {
51    }
52  
53    // ****************************** Inner Classes *****************************
54  
55    // ********************************* Methods ********************************
56  
57    // --- init -----------------------------------------------------------------
58  
59    // --- get&set --------------------------------------------------------------
60  
61    // --- business -------------------------------------------------------------
62  
63    @Override
64    public EnvAppConfigurationKey createDefaultKey()
65    {
66      return DEFAULT;
67    }
68  
69    @Override
70    public ConfigurationKey<?> createKey(final ApplicationId applicationId)
71    {
72      final ConfigurationKey<?> key =
73          new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, applicationId);
74      return key;
75    }
76  
77    @Override
78    public ConfigurationKey<?> createUniqueKey(final String id)
79    {
80      return new EnvAppConfigurationKey(new EnvironmentId(id),
81          ApplicationId.ANY_APP);
82    }
83  
84    @Override
85    public ConfigurationKey<?> createKey(final ConfigurationKey<?> key,
86        final ApplicationId appId)
87    {
88      final ConfigurationKey<?> newKey =
89          new EnvAppConfigurationKey((EnvAppConfigurationKey) key, appId);
90      return newKey;
91    }
92  
93    @Override
94    public ConfigurationKey<?> createKeyFromString(final String key)
95      throws IllegalArgumentException
96    {
97  
98      if (key == null)
99      {
100       throw new IllegalArgumentException(String.format(
101           "Cannot parse key '%s', since key is null", key));
102 
103     }
104     final String[] keyArray = key.split("/", -1);
105     final int count = keyArray.length;
106     final int expectedCount = 2;
107     if (count != expectedCount)
108     {
109       throw new IllegalArgumentException(String.format(
110           "Cannot parse key '%s', since %s slash-separated elements are expected,"
111               + " but found only %s.", key, expectedCount, count));
112     }
113 
114     final EnvironmentId environmentId =
115         EnvironmentId.valueOf(defaultIfBlank(keyArray[0], null));
116     final ApplicationId applicationId =
117         ApplicationId.valueOf(defaultIfBlank(keyArray[1], null));
118 
119     final ConfigurationKey<?> newKey =
120         new EnvAppConfigurationKey(environmentId, applicationId);
121     return newKey;
122   }
123 
124   @Override
125   public ConfigurationKey<?> createStaticKey(final ConfigurationKey<?> key)
126   {
127     return new EnvAppConfigurationKey((EnvAppConfigurationKey) key,
128         ((EnvAppConfigurationKey) key).getApplicationId());
129   }
130 
131   // --- object basics --------------------------------------------------------
132 
133 }