blob: 61c5ad6083675f1a407b2bd0e8257cae7cc5a391 [file] [log] [blame]
<!--
/***************************************************************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
***************************************************************************************************************************/
-->
Section Interfaces
<p>
Config sections can also be accessed via interface proxies using
{@link oaj.config.Config#getSectionAsInterface(String,Class)}.
</p>
<p>
While section maps and beans retrieve copies of the configuration data at the time of the method
call, section interfaces can also be use to set values in the underlying configuration.
</p>
<h5 class='figure'>Example:</h5>
<p class='bpcode w800'>
<jc>// Example config file</jc>
<cs>[MySection]</cs>
<ck>string</ck> = <cv>foo</cv>
<ck>int</ck> = <cv>123</cv>
<ck>enum</ck> = <cv>ONE</cv>
<ck>bean</ck> = <cv>{foo:'bar',baz:123}</cv>
<ck>int3dArray</ck> = <cv>[[[123,null],null],null]</cv>
<ck>bean1d3dListMap</ck> = <cv>{key:[[[[{foo:'bar',baz:123}]]]]}</cv>
</p>
<p class='bpcode w800'>
<jc>// Example interface.</jc>
<jc>// Setters are optional.</jc>
<jk>public interface</jk> MyConfigInterface {
String getString();
<jk>void</jk> setString(String x);
<jk>int</jk> getInt();
<jk>void</jk> setInt(<jk>int</jk> x);
MyEnum getEnum();
<jk>void</jk> setEnum(MyEnum x);
MyBean getBean();
<jk>void</jk> setBean(MyBean x);
<jk>int</jk>[][][] getInt3dArray();
<jk>void</jk> setInt3dArray(<jk>int</jk>[][][] x);
Map&lt;String,List&lt;MyBean[][][]&gt;&gt; getBean1d3dListMap();
<jk>void</jk> setBean1d3dListMap(Map&lt;String,List&lt;MyBean[][][]&gt;&gt; x);
}
<jc>// Example usage.</jc>
Config c = Config.<jsm>create</jsm>(<js>"MyConfig.cfg"</js>).build();
MyConfigInterface ci = c.getSectionAsInterface(<js>"MySection"</js>, MyConfigInterface.<jk>class</jk>);
<jc>// Read a value.</jc>
<jk>int</jk> myInt = ci.getInt();
<jc>// Write a value.</jc>
ci.setBean(<jk>new</jk> MyBean());
<jc>// Commit your changes to the store.</jc>
c.commit();
</p>