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.tutorial.config.key;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import de.smartics.projectdoc.annotations.DocCategory;
25  import de.smartics.projectdoc.annotations.Document;
26  import de.smartics.projectdoc.annotations.topic.DocChapter;
27  import de.smartics.projectdoc.annotations.topic.DocSection;
28  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
29  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactoryFactory;
30  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
31  import de.smartics.properties.api.config.domain.key.ApplicationId;
32  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
33  import de.smartics.properties.api.config.domain.key.EnvironmentId;
34  import de.smartics.properties.impl.config.domain.key.rtaware.TenantUserConfigurationKey;
35  import de.smartics.sandbox.mail.AnnouncementProperties;
36  import de.smartics.sandbox.mail.MailProperties;
37  
38  /**
39   * This tutorial shows how configurations for one environment with multiple
40   * nodes can be bundled. This may not be for everyone's taste since it implies
41   * that configurations not required by one node are nonetheless deployed. If you
42   * like to deploy only configurations specific to one node, you simply have to
43   * provide one project / artifact per node.
44   */
45  @Document(title = "Multiple Nodes", sortKey = "basics1010")
46  @DocCategory({ "basics" })
47  // @DocPrerequisites(ConfigurationKeyTutorial.class)
48  // @DocTopic(path="basics", step="1010")
49  public class MultiConfigurationKeyTutorial
50  {
51    private ConfigurationPropertiesFactory factory;
52  
53    @Before
54    public void setUp()
55    {
56      factory = ConfigurationPropertiesFactoryFactory.createDefaultFactory();
57    }
58  
59    /**
60     * <p>
61     * In this example we assume that the <code>test-module-announce</code> module
62     * is deployed on the main server while <code>test-module-mail</code> is part
63     * of a mail server. Both servers are part of the same environment and
64     * deployed to their own nodes.
65     * </p>
66     * <p>
67     * We simulate the two nodes in one project. In real life both will have their
68     * own projects and use only one of the following dependencies:
69     * </p>
70     *
71     * <pre>
72     *     <dependency>
73     *       <groupId>de.smartics.sandbox</groupId>
74     *       <artifactId>test-module-mail</artifactId>
75     *     </dependency>
76     *     <dependency>
77     *       <groupId>de.smartics.sandbox</groupId>
78     *       <artifactId>test-module-announce</artifactId>
79     *     </dependency>
80     * </pre>
81     * <p>
82     * The definition for these properties is provided by a dependency like this:
83     * </p>
84     *
85     * <pre>
86     *     <dependency>
87     *       <groupId>de.smartics.sandbox</groupId>
88     *       <artifactId>test-application-config-multinode</artifactId>
89     *     </dependency>
90     * </pre>
91     * <p>
92     * Now let's have a look how we can access information on the two nodes.
93     * </p>
94     */
95    @DocChapter
96    public void accessConfigurationByHardcodedKey()
97    {
98    }
99  
100   /**
101    * <p>
102    * Basically access information is purely a matter of creating the correct
103    * configuration key. This key identifies the application and the factory
104    * provides access to this information.
105    * </p>
106    * {@insertCode}
107    * <p>
108    * In {@$1}
109    * </p>
110    */
111   @DocSection
112   @Test
113   public void theMailServerConfiguration()
114   {
115     final ConfigurationKey<?> key =
116         new TenantUserConfigurationKey(new EnvironmentId("test", "mail"),
117             new ApplicationId("de.smartics.sandbox", "mail-server", "1.0")); // {1}
118 
119     final ConfigurationPropertiesManagement config =
120         factory.createManagement(key); // {2}
121 
122     final MailProperties properties =
123         config.getProperties(MailProperties.class); // {3}
124 
125     final int mailsPerPage = properties.mailsPerPage(); // {4}
126     assertThat(mailsPerPage, is(10)); // {5}
127   }
128 
129   /**
130    * <p>
131    * The main server simply changes the name of the node and the name of the
132    * application. This would allow the same application be deployed on multiple
133    * nodes. Note that a node is not required to be a physical instance. A node
134    * may refer to an EAR deployable that is deployed side by side to other EARs
135    * on the same application server.
136    * </p>
137    * {@insertCode}
138    * <p>
139    * In {@$1}
140    * </p>
141    */
142   @DocSection
143   @Test
144   public void theMainServerConfiguration()
145   {
146     final ConfigurationKey<?> key =
147         new TenantUserConfigurationKey(new EnvironmentId("test", "main"),
148             new ApplicationId("de.smartics.sandbox", "app-server", "1.0")); // {1}
149     final ConfigurationPropertiesManagement config =
150         factory.createManagement(key); // {2}
151 
152     final AnnouncementProperties properties =
153         config.getProperties(AnnouncementProperties.class); // {3}
154 
155     final int lookup = properties.lookupIntervalMs(); // {4}
156     assertThat(lookup, is(0)); // {5}
157   }
158 }