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 java.util.ArrayList;
19  import java.util.List;
20  
21  import de.smartics.properties.api.config.domain.key.ApplicationId;
22  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
23  import de.smartics.properties.api.config.domain.key.EnvironmentId;
24  import de.smartics.properties.api.config.domain.key.KeyListBuilder;
25  import de.smartics.util.lang.NullArgumentException;
26  
27  /**
28   * Explodes the key to its defaulting keys.
29   */
30  final class EnvAppKeyListBuilder implements KeyListBuilder
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    // --- members --------------------------------------------------------------
37  
38    // ****************************** Initializer *******************************
39  
40    // ****************************** Constructors ******************************
41  
42    /**
43     * Default constructor.
44     */
45    EnvAppKeyListBuilder()
46    {
47    }
48  
49    // ****************************** Inner Classes *****************************
50  
51    // ********************************* Methods ********************************
52  
53    // --- init -----------------------------------------------------------------
54  
55    // --- get&set --------------------------------------------------------------
56  
57    // --- business -------------------------------------------------------------
58  
59    @Override
60    public List<ConfigurationKey<?>> createKeyList(
61        final ConfigurationKey<?> key)
62    {
63      final List<ConfigurationKey<?>> list =
64          new ArrayList<ConfigurationKey<?>>();
65      list.add(key);
66  
67      final EnvAppConfigurationKey specific = (EnvAppConfigurationKey) key;
68      final EnvironmentId envId = specific.getEnvironmentId();
69      final ApplicationId appId = specific.getApplicationId();
70  
71      addAppKeys(list, envId, appId);
72  
73      if (envId.getNode() != null)
74      {
75        final EnvironmentId defaultEnv = new EnvironmentId(envId.getName());
76        if (!ApplicationId.ANY_APP.equals(appId))
77        {
78          if (appId.getVersion() != null)
79          {
80            final EnvAppConfigurationKey defaultKey =
81                new EnvAppConfigurationKey(defaultEnv, appId);
82            list.add(defaultKey);
83          }
84  
85          addAppKeys(list, defaultEnv, appId);
86        }
87        else
88        {
89          list.add(new EnvAppConfigurationKey(defaultEnv, ApplicationId.ANY_APP));
90        }
91      }
92  
93      addAppKeyNoEnv(list, appId);
94  
95      final EnvAppConfigurationKey defaultKey =
96          new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
97      list.add(defaultKey);
98  
99      return list;
100   }
101 
102   private void addAppKeys(final List<ConfigurationKey<?>> list,
103       final EnvironmentId envId, final ApplicationId appId)
104     throws IllegalArgumentException, NullArgumentException
105   {
106     if (appId.getVersion() != null)
107     {
108       final ApplicationId defaultAppId =
109           new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
110       final EnvAppConfigurationKey defaultKey =
111           new EnvAppConfigurationKey(envId, defaultAppId);
112       list.add(defaultKey);
113     }
114 
115     if (appId.getArtifactId() != null)
116     {
117       final ApplicationId defaultAppId =
118           new ApplicationId(appId.getGroupId(), null, null);
119       final EnvAppConfigurationKey defaultKey =
120           new EnvAppConfigurationKey(envId, defaultAppId);
121       list.add(defaultKey);
122     }
123 
124     if (appId.getGroupId() != null)
125     {
126       final EnvAppConfigurationKey defaultKey =
127           new EnvAppConfigurationKey(envId, ApplicationId.ANY_APP);
128       list.add(defaultKey);
129     }
130   }
131 
132   private void addAppKeyNoEnv(final List<ConfigurationKey<?>> list,
133       final ApplicationId appId) throws NullArgumentException,
134     IllegalArgumentException
135   {
136     if (appId.getVersion() != null)
137     {
138       final EnvAppConfigurationKey defaultAppKey =
139           new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, appId);
140       list.add(defaultAppKey);
141     }
142 
143     if (appId.getArtifactId() != null)
144     {
145       final ApplicationId noEnvAppId =
146           new ApplicationId(appId.getGroupId(), appId.getArtifactId(), null);
147       final EnvAppConfigurationKey defaultAppKey =
148           new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, noEnvAppId);
149       list.add(defaultAppKey);
150     }
151 
152     if (appId.getGroupId() != null)
153     {
154       final ApplicationId noEnvAppId =
155           new ApplicationId(appId.getGroupId(), null, null);
156       final EnvAppConfigurationKey defaultAppKey =
157           new EnvAppConfigurationKey(EnvironmentId.ANY_ENV, noEnvAppId);
158       list.add(defaultAppKey);
159     }
160   }
161 
162   // --- object basics --------------------------------------------------------
163 
164 }