📄 index.xtp
字号:
<document><header><product>resin</product><type>tutorial</type><title>Application configuration files using a WebBeans object</title><description><p>Applications often need to read, and possibly write,configuration files. An excellent way toaccomplish this is to implement a custom singleton, which iseasily configured and easily obtained from anywhere in theapplication.</p><p>This implementation of the concept allows you to configure a basedirectory for configuration files. An object of type<code>AppConfig</code> is obtained with a <code>javax.webbeans.In</code>injection. It is used to openfiles relative to the base directory.</p></description> <tutorial-startpage>index.jsp</tutorial-startpage></header><body><summary/><s1 title="Files in this tutorial"><deftable><tr> <th>File</th> <th>Description</th></tr><tr> <td><viewfile-link file="WEB-INF/web.xml"/></td> <td>Configure the AppConfig object as a resource</td></tr><tr> <td><viewfile-link file="WEB-INF/classes/example/AppConfig.java"/></td> <td>The AppConfig object provides input and output streams to configuration files</td></tr><tr> <td><viewfile-link file="WEB-INF/classes/example/TestServlet.java"/></td> <td>A simple example usage of AppConfig that reads and writes a file</td></tr><tr> <td><viewfile-link file="index.jsp"/></td> <td>The starting page for the tutorial</td></tr></deftable></s1><s1 title="The java code for a custom Singleton"><p>A custom singleton is a standard java-bean (see <a href="doc|resin-ioc.xtp">Resin-IoC</a>). Setter methods like <code>setFoo(String foo)</code> areused to set valuesthat are specified in the configuration.</p><p>In this case, a single setter is provided that matches theconfiguration parameter "config-files-location". The<code>@PostConstruct</code> annotation tells Resin to call the<code>init()</code> method after all of the settershave been called.</p><example title="AppConfig.java">import javax.annotations.PostConstruct;public class AppConfig { ConfigFilesLocation _cfl = null; /** * Set the base for subsequent call's to openConfigFileRead() * and openConfigFileWrite() * * @param location a file path or url */ public void setConfigFilesLocation(String location) throws Exception { _cfl = new ConfigFilesLocation(); _cfl.setLocation(location); } @PostConstruct public void init() throws Exception { if (_cfl == null) throw new Exception("'config-files-location' must be set"); } ...</example></s1><s1 title="Configuring the custom singleton"><p>Configuration of the singleton is done with the<a config-tag="bean"/> tag.</p><p>The example here configures the location of the configuration filesas <code>WEB-INF/config</code> (which means you need to makesure the directory exists for the example to work). It is good tohide the files somewhere under <code>WEB-INF</code>, because a browserwill not be able to read the files, just the application.</p><p>The <a href="doc|el-var.xtp">EL configuration</a> variable <var>webApp.root</var> is used.</p><example title="Configuring the AppConfig singleton in resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.AppConfig"> <init> <config-files-location>${webApp.root}/WEB-INF/config</config-files-location> </init> </bean></web-app></example></s1><s1 title="Obtaining and using the object"><p>An instance of the object is retrieved in the application usingdependency injection. In this example servlet, we'll use field-basedinjection, marked by the <code>@javax.webbeans.In</code> annotation.We could also use method injection.</p><p>Resin will look in the WebBeans registry for the <code>AppConfig</code>object that we've configured in the resin.conf, and inject it into theservlet. Resin will report any errors in looking upthe <code>AppConfig</code> object, e.g. if it's not configured in theresin.conf or if you've configured multiple <code>AppConfig</code>instances.</p><example title="Obtaining the AppConfig object">import javax.webbeans.In;public class TestServlet extends GenericServlet { @In AppConfig _appConfig;}</example><p><code>_appConfig</code> is used to open theconfiguration files for reading and writing.</p><example title="Using the AppConfig object">... InputStream is = _appConfig.openConfigFileRead(inputFile);... OutputStream os = _appConfig.openConfigFileWrite(outputFile);...</example></s1><s1 title="Variation - Hiding the configuration file with getters"><p>The example in this tutorial is easily modified to allow the hiding of theconfiguration file behind <code>get</code> methods of the bean. Implementinggetters on the configuration bean abstracts the configuration information,protecting code which uses the configuration information from implementationdetails of how the configuration information is read and stored.</p><example title="Hiding the configuration file with getters">package example;import java.util.*;import java.io.*;import javax.webbeans.*;@Componentpublic class AppConfig { private final static String DEFAULT_PROPERTIES = "example/AppConfig.properties"; private String _configFile; private Properties _properties; /** * Optionally set the name of a file that provides properties that override * the defaults. The defaults are obtained from a file in the classpath * named 'example/AppConfig.properties' * * For example, the file containing default properties might be in * WEB-INF/classes/example/AppConfig.properties, * or if AppConfig.class is in a jar, the AppConfig.properties * could be in the jar file alongside the AppConfig.class file. * * AppConfig.properties contains values placed there by the developer. * The <config-file> is used to indicate a file that specifies properties * that override the defaults, perhaps properties that change depending * on the deployment environment. */ public void setConfigFile(String configFile) throws Exception { _configFile = configFile; } @PostConstruct public void init() throws Exception { InputStream is = null; if (_configFile != null) { // the properties in _configFile override the defaults is = new FileInputStream(_configFile); _properties = new Properties(defaults); _properties.load(is); } else { // try to find a default configuration file in the classpath ClassLoader loader = Thread.currentThread().getContextClassLoader(); is = loader.getResourceAsStream(DEFAULT_PROPERTIES); if (is != null) _properties = new Properties(); _properties.load(is); } else { // throw an exception here to make the defaults required throw new FileNotFoundException(DEFAULT_PROPERTIES); } } } public String getFoo() { return _properties.getProperty("foo"); } public String getBar() { return _properties.getProperty("bar"); }}</example><example><web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.AppConfig"/> </resource></web-app>or<web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.AppConfig"> <init> <config-file>${webApp.root}/WEB-INF/AppConfig-override.properties</config-file> </init> </bean></web-app></example><example>package example;import javax.servlet.*;import javax.servlet.http.*;import javax.webbeans.In;import java.io.*;import java.util.*;public class TestServlet extends HttpServlet { @In AppConfig _appConfig; ... String foo = _appConfig.getFoo(); String bar = _appConfig.getBar(); ...}</example></s1><s1 title="Availability of AppConfig from different web-apps"><p>The availability of AppConfig to different web-apps depends upon the contextthat the <bean ...> configuration is placed within.</p><p>If the configuration is placed as a child of <web-app>, then thatinstance of AppConfig is available only to that web-app.</p><example title="WEB-INF/resin-web.xml local to web-app"><web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.AppConfig"/></web-app></example><p>If it is placed as a child of <host>, that instance of AppConfig isavailable to all web-apps within the host.</p><example title="shared host configuration in resin.conf"><resin xmlns="http://caucho.com/ns/resin"><cluster id=""> <host> <bean class="example.AppConfig"/> ... </host></cluster></resin></example><p>If the <bean> is placed as a child of <cluster>, that instance ofAppConfig is available to all web-apps within all hosts within that cluster.</p><example title="shared cluster configuration in resin.conf"><resin xmlns="http://caucho.com/ns/resin"><cluster id=""> <bean class="example.AppConfig"> <host id=""> ... </host id=""> ...</cluster></resin></example><p>In the case of <cluster> or <host>, the example.AppConfig classneeds to be available in the classpath. The easiest way to accomplish that isto place a jar with that class in $RESIN_HOME/lib, or you can use an explicit <a config-tag="class-loader"/>.</p></s1></body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -