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