Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Configuration for Java EE
Dmitry Kornilov
@m0mus
Wern...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
The following is intended to ou...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 3
Dmitry Kornilov
• Software Developer @ Oracle
• JSO...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 4
Werner Keil
• Consultant – Coach
• Creative Cosmopo...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Program Agenda
Introduction
Problem Definition
JSR Pr...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Introduction
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Oracle Java EE 8 Survey 2014
Confidential – Oracle
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 8
DZone and Java EE Guardians Survey Results
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 9
Java EE 7
Connector JAXBJSP Debugging
Managed Beans...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Problem Definition
10
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
What is Configuration?
11
Application server setup?
R...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Problems
• Lack of standard configuration API
• Confi...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Problems
• Lack of standard configuration API
• Confi...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Problems
• Lack of standard configuration API
• Confi...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Problems
• Lack of standard configuration API
• Confi...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Problems
• Lack of standard configuration API
• Confi...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JSR Proposal
17
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Configuration Definition
• Application centric
• Not ...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Java EE Configuration
• Unified API
• Externalized co...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Java EE Configuration
• Unified API
• Externalized co...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JSR Features (Java EE 8)
21
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
API
22
a=JavaOne
b=9
c=2016
Config config = ConfigPro...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Converters
23
• Type safe access to values
• Built-in...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Configuration Sources
24
• Multiple configuration sou...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Default Configuration Sources
25
Java EE Config
• Sys...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Defining Configuration Sources
26
• Using config.sour...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
config-sources.xml
27
• File with defined schema usin...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Sample config-sources.xml
28
Simple
With ordinals
<co...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Sample config-sources.xml
29
Custom
<config-sources>
...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
JSR Features (Java EE 9)
30
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Integration With Other Java EE Frameworks
• Read conf...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Configuration Descriptor
32
• File with defined forma...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Dynamic Configuration
33
• Polling framework
• Expres...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Polling
34
// Defining polling using API
Config cfg =...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Polling
35
// Defining polling using API
Config cfg =...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Polling
36
<config-sources refresh-rate="300000">
<so...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Polling
37
<config-sources refresh-rate="300000">
<so...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Expressions
38
• EL like expressions evaluated at run...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Property Resolvers
39
• Flexible mechanism allowing
e...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Configuration Context
40
• Allows setting application...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Tamaya
41
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
• Configuration = ordered list of PropertySources
• P...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 43
Requirements
• Developer‘s Perspective
• Architect...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 44
Developer‘s Requirements
• Easy to use.
• Develope...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 45
Architectural/Design Requirements
• Decouple code ...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 46
Operational's Requirements
• Enable Transparency:
...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 47
Other Aspects
• Support Access Constraints and Vie...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 48
API Requirements
• Leverage existing functionality...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 49
Existing Mechanisms
• Environment Properties
• Sys...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 50
Dependencies - API & Core
<dependency>
<groupId>or...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 51
Programmatic API
Configuration config = Configurat...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 52
Dependencies – Injection SE
<dependency>
<groupId>...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 53
Dependencies – Injection SE
@Config(value="admin.s...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 54
Configuration Backends
• Support existing mechanis...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 55
PropertySource
public interface PropertySource {
P...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 56
Predefined Property Sources
• System & Environment...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 57
Property Sources
• Mostly map to exact one file, r...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 58
PropertySource - Example
public class MyPropertySo...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 59
PropertySource - Registration
• By default, regist...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 60
PropertySource Provider
• Allow dynamic registrati...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 61
More SPI artifacts
• Filters for filtering values ...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Q & A
62
Upcoming SlideShare
Loading in …5
×

Configuration for Java EE: Config JSR and Tamaya

2,160 views

Published on

Slides from our joint talk with Werner Keil about configuration proposal for Java EE and Tamaya we've done on 16 Mov 2016 in Sofia on Java2Days conference.

Published in: Technology
  • Be the first to comment

Configuration for Java EE: Config JSR and Tamaya

  1. 1. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Configuration for Java EE Dmitry Kornilov @m0mus Werner Keil @wernerkeil November 16, 2016 Config JSR and Tamaya
  2. 2. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
  3. 3. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 3 Dmitry Kornilov • Software Developer @ Oracle • JSON-B (JSR-367) spec lead • JSON-P (JSR-374) spec lead • Outstanding Spec Lead 2016 • EclipseLink project committer
  4. 4. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 4 Werner Keil • Consultant – Coach • Creative Cosmopolitan • Open Source Evangelist • Software Architect • Spec Lead – JSR363 • Individual JCP Executive Committee Member
  5. 5. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Program Agenda Introduction Problem Definition JSR Proposal & Features Tamaya Q & A 1 2 3 4 5 5
  6. 6. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Introduction 6
  7. 7. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Oracle Java EE 8 Survey 2014 Confidential – Oracle
  8. 8. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 8 DZone and Java EE Guardians Survey Results
  9. 9. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 9 Java EE 7 Connector JAXBJSP Debugging Managed BeansJSPConcurrency EE Interceptors JAX-WS WebSocket Bean Validation JASPIC ServletJMS JTADeployment Batch JACCDependency Injection JAXR JSTL Management CDI EJB JAX-RPC Web ServicesJSF Java Persistence JSON-PCommon Annotations EL JAX-RS Web Services MetadataJavaMail CDI JSON-P Security Bean Validation JSF JAX-RS JSP Servlet Health CheckConfiguration Java EE 8 (Revised Proposal, 2016) JSON-B
  10. 10. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Problem Definition 10
  11. 11. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. What is Configuration? 11 Application server setup? Runtime parameters? Deployment descriptors? Parameters of used frameworks? Deployment scripts? Used resources?
  12. 12. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Problems • Lack of standard configuration API • Configuring multiple instances • Deploying on different environments • Change configuration without redeployment • Configuration of decoupled microservices 12
  13. 13. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Problems • Lack of standard configuration API • Configuring multiple instances • Deploying on different environments • Change configuration without redeployment • Configuration of decoupled microservices 13
  14. 14. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Problems • Lack of standard configuration API • Configuring multiple instances • Deploying on different environments • Change configuration without redeployment • Configuration of decoupled microservices 14
  15. 15. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Problems • Lack of standard configuration API • Configuring multiple instances • Deploying on different environments • Change configuration without redeployment • Configuration of decoupled microservices 15
  16. 16. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Problems • Lack of standard configuration API • Configuring multiple instances • Deploying on different environments • Change configuration without redeployment • Configuration of decoupled microservices 16
  17. 17. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. JSR Proposal 17
  18. 18. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Configuration Definition • Application centric • Not modifiable by application • Consists of key/value pairs • Keys and values are strings • Flat structure 18
  19. 19. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Java EE Configuration • Unified API • Externalized configuration • Support of multiple configuration sources – Properties, xml and json formats support out of the box • Layering and overrides • Optional configuration descriptor • Dynamic configuration • Integration with other Java EE frameworks 19
  20. 20. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Java EE Configuration • Unified API • Externalized configuration • Support of multiple configuration sources – Properties, xml and json formats support out of the box • Layering and overrides 20 Java EE 8 • Optional configuration descriptor • Dynamic configuration • Integration with other Java EE frameworks Java EE 9
  21. 21. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. JSR Features (Java EE 8) 21
  22. 22. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. API 22 a=JavaOne b=9 c=2016 Config config = ConfigProvider.getConfig(); // Returns "JavaOne" String a = config.getProperty("a"); // Returns string "9" String b = config.getProperty("b"); // Returns string "default" String defaultValue = config.getProperty("not.exist", "default"); // Returns number 2016 Long c = config.getProperty("c", Long.class);
  23. 23. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Converters 23 • Type safe access to values • Built-in converters for: – Primitive Wrappers – BigDecimal, BigInteger, URL, URI – Date, Calendar – java.time.* public interface Converter<Target> { Target convert(String value, Config c); } public class FooConverter implements Converter<Foo> { public Foo convert(String value, Config c) { ... } } Config cfg = ConfigProvider.builder() .withConverters(new FooConverter()) .build(); Foo foo = cfg.getProperty("foo", Foo.class);
  24. 24. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Configuration Sources 24 • Multiple configuration sources • Supported configuration sources: – System properties – Runtime parameters – File (Properties, xml, json) – Resource on a web server • Pluggable architecture – Custom sources (like DB) • Configuration sources are ordered Java EE Config XML JSONprop DBweb Application
  25. 25. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Default Configuration Sources 25 Java EE Config • System properties (ordinal=400) • Environment properties (ordinal=300) • /META-INF/config.properties (ordinal=100) • /META-INF/config.xml (ordinal=100) • /META-INF/config.json (ordinal=100)
  26. 26. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Defining Configuration Sources 26 • Using config.sources runtime parameter • Using API • Using config-sources.xml file java –jar my.jar –Dconfig.source=http://shared/global.xml,/conf/my.json Config config = ConfigProvider.builder() .addSource(new WebSource("http://shared:8080/global.xml"), 200) .addSource(new FileSource("/conf/my.json"), 100) .addSource(new MyCustomSource()) .build();
  27. 27. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. config-sources.xml 27 • File with defined schema using to define configuration sources and their metadata • Default location /META-INF/config-sources.xml • Can be placed outside of the application package • Define using runtime parameter • Define using API java –jar my.jar –Dconfig.sources=http://sharedhost/config-sources.xml Config c = ConfigProvider.builder() .withSources("/cfg/config-sources.xml") .build();
  28. 28. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Sample config-sources.xml 28 Simple With ordinals <config-sources> <source>http://shared:8080/config.xml</source> <source>/cfg/myconf.json</source> </config-sources> <config-sources> <source ordinal="500">http://shared:8080/config.xml</source> <source ordinal="450">/cfg/myconf.json</source> </config-sources>
  29. 29. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Sample config-sources.xml 29 Custom <config-sources> <source>http://shared:8080/config.xml</source> <source type="com.oracle.config.CloudConfig"> <user>user</user> <password>secret</password> </source> </config-sources>
  30. 30. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. JSR Features (Java EE 9) 30
  31. 31. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Integration With Other Java EE Frameworks • Read configuration from Java EE Config • Store configuration as part of whole application configuration • Use standard API 31 Java EE Config Application Configuration Application JPA Configuration JAX-RS Configuration
  32. 32. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Configuration Descriptor 32 • File with defined format • Defines all configurable properties and metadata • Optional • It’s not XML-Schema! <config-descriptor> <property name="a"/> <property name="b" default=”valueB"/> <property name="c" mutable="false"/> <property name="d"/> </config-descriptor>
  33. 33. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Dynamic Configuration 33 • Polling framework • Expressions • Property Resolvers • Configuration Context
  34. 34. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Polling 34 // Defining polling using API Config cfg = ConfigProvider.builder() .withSource(new FileSource("/cfg/config.xml"), 200, Duration.ofSeconds(30)) .withSource(new WebSource("http://shared/config.xml"), 100, Duration.ofMinutes(1)) .build();
  35. 35. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Polling 35 // Defining polling using API Config cfg = ConfigProvider.builder() .withSource(new FileSource("/cfg/config.xml"), 200, Duration.ofSeconds(30)) .withSource(new WebSource("http://shared/config.xml"), 100, Duration.ofMinutes(1)) .build(); Ordinal Refresh IntervalLocation
  36. 36. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Polling 36 <config-sources refresh-rate="300000"> <source ordinal="200" refresh-rate="30000">/cfg/config.xml</source> <source ordinal="100" refresh-rate="60000">http://shared/config.xml</source> </config-sources>
  37. 37. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Polling 37 <config-sources refresh-rate="300000"> <source ordinal="200" refresh-rate="30000">/cfg/config.xml</source> <source ordinal="100" refresh-rate="60000">http://shared/config.xml</source> </config-sources> Sources list refresh interval (5 min) Source 1 refresh interval (30 sec) Source 2 refresh interval (1 min)
  38. 38. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Expressions 38 • EL like expressions evaluated at runtime to a property value • Property substitution • Conditional configuration sources foo=${some.other.value} bar=${foo + 10} baz=${foo * bar} <config-sources> <source>//cfg/config.properties</source> <source enabled=”${app==‘ios’}”> //cfg/cust_ios.properties </source> </config-sources>
  39. 39. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Property Resolvers 39 • Flexible mechanism allowing executing user code in configuration expressions • Can be used to inject cloud resources rating.service.url=${eureka:rating.url} cust.db=${cloud:cust.db} <config-sources> <resolvers> <resolver name=”cloud”> <class>com.example.CloudResolver</class> <username>user</username> <password>secret</password> </resolver> </resolvers> <!-- ... --> </config-sources>
  40. 40. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Configuration Context 40 • Allows setting application variables which can be used in configuration expressions • Example: geographical zone, application type, etc. <config-sources> <source>/cfg/config.properties</source> <source enabled=”${app==‘ios’}”>cust_ios.properties</source> </config-sources> Config config = ConfigProvider.getConfig(); ConfigContext context = ConfigContext.builder().addProperty("app", "ios").build(); Long prop = config.getPropertyWithContext("prop", context);
  41. 41. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Tamaya 41
  42. 42. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. • Configuration = ordered list of PropertySources • Properties found are combined using a CombinationPolicy • Raw properties are filtered by PropertyFilter • For typed access PropertyConverters have to do work • Extensions add more features • Component Lifecycle is controlled by the ServiceContextManager ConfigurationContext PropertyFilters PropertySource PropertySource PropertySource PropertySource Configuration CombinationPolicy PropertyProviders <provides> PropertyConverter 42 Apache Tamaya in 120 seconds...
  43. 43. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 43 Requirements • Developer‘s Perspective • Architectural/Design Requirements • Operational Aspects • Other Aspects
  44. 44. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 44 Developer‘s Requirements • Easy to use. • Developers want defaults. • Developers don‘t care about the runtime (for configuration only). • Developers are the ultimate source of truth • Type Safety
  45. 45. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 45 Architectural/Design Requirements • Decouple code that consumes configuration from – Backends Used – Storage Format – Distribution – Lifecycle and versioning – Security Aspects
  46. 46. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 46 Operational's Requirements • Enable Transparency: – What configuration is available ? – What are the current values and which sources provided the value ? – Documentation • Manageable: – Configuration changes without redeployment or restart. – Solution must integrate with existing environment
  47. 47. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 47 Other Aspects • Support Access Constraints and Views • No accidental logging of secrets • Dynamic changes • Configuration Validation
  48. 48. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 48 API Requirements • Leverage existing functionality where useful • Only one uniform API for access on all platforms! • Defaults provided by developer during development (no interaction with operations or external dependencies)
  49. 49. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 49 Existing Mechanisms • Environment Properties • System Properties • CLI arguments • Properties, xml-Properties
  50. 50. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 50 Dependencies - API & Core <dependency> <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-api</artifactId> <version>0.3-incubating-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-core</artifactId> <version>0.3-incubating-SNAPSHOT</version> </dependency>
  51. 51. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 51 Programmatic API Configuration config = ConfigurationProvider.getConfiguration(); // single property access String name = config.getOrDefault("name", "John"); int ChildNum = config.get("childNum", int.class); // Multi property access Map<String,String> properties = config.getProperties(); // Templates (provided by extension) MyConfig config = ConfigurationInjection.getConfigurationInjector() .getConfig(MyConfig.class);
  52. 52. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 52 Dependencies – Injection SE <dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-injection-api</artifactId> <version>0.3-incubating-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-injection</artifactId> <version>0.3-incubating-SNAPSHOT</version> </dependency>
  53. 53. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 53 Dependencies – Injection SE @Config(value="admin.server", defaultValue="127.0.0.1") private String server; @Config(value="admin.port", defaultValue="8080") private int port; @Config(value="admin.connections") private int connections = 5; @Config("address") private Address address; MyTenant t = new MyTenant(); ConfigurationInjection .getConfigurationInjector() .configure(t);
  54. 54. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 54 Configuration Backends • Support existing mechanisms OOTB • Provide a simple SPI for (multiple) property sources • Define a mechanism to prioritize different property sources • Allow different strategies to combine values • Support Filtering • Support Type Conversion
  55. 55. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 55 PropertySource public interface PropertySource { PropertyValue get(String key); Map<String, String> getProperties(); boolean isScannable(); String getName(); int getOrdinal(); } public final class PropertyValue{ public String getKey(); public String getValue(); public String get(String key); public Map<String, String> getConfigEntries(); ... }
  56. 56. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 56 Predefined Property Sources • System & Environment Properties • (CLI Arguments) • Files – ${configDir}/*.properties • Classpath Resources – /META-INF/javaconfiguration.properties
  57. 57. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 57 Property Sources • Mostly map to exact one file, resource or backend • Have a unique name • Must be thread safe • Can be dynamic • Provide an ordinal • Can be scannable
  58. 58. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 58 PropertySource - Example public class MyPropertySource extends BasePropertySource { private Map<String, String> props = new HashMap<>(); public SimplePropertySource() throws IOException { URL url = getClass().getClassLoader().getResource( "/META-INF/myFancyConfig.xml"); // read config properties into props ... } @Override public String getName() { return "/META-INF/myFancyConfig.xml"; }; @Override public Map<String, String> getProperties() { return props; } }
  59. 59. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 59 PropertySource - Registration • By default, register it using the java.util.ServiceLoader – → /META-INF/services/org.apache.tamaya.spi.PropertySource – MyPropertySource
  60. 60. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 60 PropertySource Provider • Allow dynamic registration of multiple property sources • E.g. all files found in a config directory • Are evaluated once and then discarded • Are also registered using the ServiceLoader. public interface PropertySourceProvider{ public Collection<PropertySource> getPropertySources(); }
  61. 61. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 61 More SPI artifacts • Filters for filtering values evaluated (remove, map, change) • Converters for converting String values to non-String types • A ValueCombinationPolicy – determines how values evaluated are combined to a final value (defaults to overriding)
  62. 62. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Q & A 62

×