Webwork learning path (4) Configuration explanation, qt learning path

Source: Internet
Author: User

Webwork learning path (4) Configuration explanation, qt learning path

Webwork is a classic Web MVC framework. I personally think that the implementation of the configuration file in the source code is very elegant, supports custom configuration files, custom configuration file reading classes, and custom internationalization. It can be used as a reference and introduced to other projects separately. The Inheritance relationships of Configuration-related classes are as follows:

 

 

1. Configuration
  • Configuration is the core class of the webwork Configuration file and serves as the portal for reading Configuration information. By default, the proxy class DelegatingConfiguration is introduced in the implementation class to completely decouple the underlying implementation of the read PropertiesConfiguration. When used in a project, you only need to introduce the Configuration class and the following code can obtain the Configuration information;
Configuration.getString("webwork.locale")
  • The getString method calls the get method of Configuration itself. The getConfiguration method is called in the get method:
1     public static String getString(String name) throws IllegalArgumentException {2         String val = get(name).toString();3         return val;4     }
1     public static Object get(String name) throws IllegalArgumentException {2         Object val = getConfiguration().getImpl(name);3         return val;4     }5 6     public static Configuration getConfiguration() {7         return configurationImpl == null ? getDefaultConfiguration() : configurationImpl;8     }
  • The two static variables defaultImpl and configurationImpl defined in Configuration, and the setConfiguration method is used to set configurationImpl;
  • DefaultImpl is a reference of the default implementation class instance of WebWork. Every time you read the configuration file, it will determine whether it is inWebwork. propertiesConfigured?Webwork. configurationParameter (in fact, the framework cannot implement hot-read configuration files. Next we will say that each judgment only determines whether to read the configuration information, and the default framework class used is the user-defined class );
  • If you call getDefaultConfiguration () to obtain the custom read class, reference configurationImpl; otherwise, WebWork's own Configuration implementation is returned.

(Let's talk about it here. You may be miserable if you change the configuration file on line at will. Remember to ask before the change)

 1     private static Configuration getDefaultConfiguration() { 2         if (defaultImpl == null) { 3             defaultImpl = new DefaultConfiguration(); 4             try { 5                 String className = getString("webwork.configuration"); 6                 if (!className.equals(defaultImpl.getClass().getName())) { 7                     try { 8                         defaultImpl = (Configuration) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className)); 9                     } catch (Exception e) {10                         log.error("Could not instantiate configuration", e);11                     }12                 }13                 return defaultImpl;14             } catch (IllegalArgumentException localIllegalArgumentException) {15             }16         }17         return defaultImpl;18     }
  • When you call the getDefaultConfiguration () method for the first time, ultimpl is empty by default. Then, you can create a WebWork instance that implements DefaultConfiguration and read the WebWork configuration information through this instance.
  • The above code has a special place in line 5th, which is very similar to the DelegatingConfiguration below. Let's talk about it together.
2. defaultConfiguration 1 public DefaultConfiguration () {2 ArrayList list = new ArrayList (); 3 try {4 list. add (new PropertiesConfiguration ("webwork"); 5} catch (Exception e) {6 this. log. warn ("cocould not find webwork. properties "); 7} 8 try {9 list. add (new PropertiesConfiguration ("com/opensymphony/webwork/default"); 10} catch (Exception e) {11 this. log. error ("cocould not find com/opensymphony/webwork/default. properties ", e); 12} 13 Configuration [] configList = new Configuration [list. size ()]; 14 this. config = new DelegatingConfiguration (Configuration []) list. toArray (configList); 15 try {16 StringTokenizer configFiles = new StringTokenizer (String) this. config. getImpl ("webwork. custom. properties "),", "); 17 while (configFiles. hasMoreTokens () {18 String name = configFiles. nextToken (); 19 try {20 list. add (new PropertiesConfiguration (name); 21} catch (Exception e) {22 this. log. error ("cocould not find" + name + ". properties. skipping "); 23} 24} 25 configList = new Configuration [list. size ()]; 26 this. config = new DelegatingConfiguration (Configuration []) list. toArray (configList); 27} catch (IllegalArgumentException localIllegalArgumentException) {28} 29 try {30 StringTokenizer bundleFiles = new StringTokenizer (String) this. config. getImpl ("webwork. custom. i18n. resources "),", "); 31 while (bundleFiles. hasMoreTokens () {32 String name = bundleFiles. nextToken (); 33 try {34 this.log.info ("Loading global messages from" + name); 35 LocalizedTextUtil. addDefaultResourceBundle (name); 36} catch (Exception e) {37 this. log. error ("cocould not find" + name + ". properties. skipping "); 38} 39} 40} catch (IllegalArgumentException localIllegalArgumentException1) {41} 42}DefaultConfiguration Constructor

 

  • DefaultConfiguration does not directly read the properties file, but uses PropertiesConfiguration to read the properties file;
  • PropertiesConfiguration is also a subclass of Configuration. util. properties to parse the properties file, and assign its own Properties instance settings, and cover the four methods of the parent class setImpl, getImpl, isSetImpl, listImpl;
  • DefaultConfiguration is first loaded through PropertiesConfigurationWebwork. propertiesAnd then loadDefault. properties;

(Here, default. properties is the configuration file of the webwork framework. It is encapsulated in jar. properties and default. for the same properties parameters, see the loading sequence of the above program configuration file. The framework first loads your configuration file and then loads the default configuration file. You will find that your parameters will not work, if you want to overwrite your parameters in the framework, you need to customize the configuration file andWebwork. propertiesConfiguringWebwork. custom. propertiesParameters)

  • Add the PropertiesConfiguration instance to the List, create a Configuration [] array as large as the List, and convert the List to Configuration [] to assign the instantiated DelegatingConfiguration;
  • After loading two properties files and creating the DelegatingConfiguration instance, DefaultConfiguration starts to search for these two properties files.Webwork. custom. properties, Separated by commas. After finding the configuration, split the file name, create a PropertiesConfiguration instance, add the List, load all configuration files, and recreate the DelegatingConfiguration instance.
  • After loading all the WebWork attribute files, find the specified international resource files in the attribute file (the file names are also separated by commas (,). If yes, load the files to LocalizedTextUtil, for future use.

(After all, it is the source code compiled many years ago. The StringTokenizer here has been left over for compatibility reasons (although it is not encouraged to be used in new code ). In the API, it is recommended that all persons seeking this function use the String split method or java. util. regex package)

3. DelegatingConfiguration
  • DelegatingConfiguration is also a subclass of Configuration. It stores a Configuration [] array configList and overwrites the four methods of the parent class: setImpl, getImpl, isSetImpl, and listImpl, implement the configList operation;
  • If you do not specify your own Configuration implementation, Configuration. getString finally calls the getImpl of DelegatingConfiguration;
  • In the setImpl method implementation of DelegatingConfiguration, there is a special point, in fact, that is, the above Configuration mentioned f, the underlying implementation:
 1     public void setImpl(String name, Object value) throws IllegalArgumentException, UnsupportedOperationException { 2         IllegalArgumentException e = null; 3         for (int i = 0; i < this.configList.length; i++) { 4             try { 5                 this.configList[i].getImpl(name); 6  7                 this.configList[i].setImpl(name, value); 8  9                 return;10             } catch (IllegalArgumentException ex) {11                 e = ex;12             }13         }14         throw e;15     }

WebWork does not support adding attribute configuration dynamically, but allows modifying configured attributes. configList [I]. getImpl (name) callback calls the getImpl method of PropertiesConfiguration, which is implemented as follows:

1     public Object getImpl(String aName) throws IllegalArgumentException {2         Object setting = this.settings.get(aName);3         if (setting == null) {4             throw new IllegalArgumentException("No such setting:" + aName);5         }6         return setting;7     }

PropertiesConfiguration will find the name in settings. If it is found, the configuration information will be returned. In the setImpl method of DelegatingConfiguration, use configList [I]. setImpl (name, value) modifies the configuration of this attribute. Otherwise, an IllegalArgumentException exception is thrown. This exception is captured in the getImpl method of DelegatingConfiguration and continues to be thrown to the called function. In this case, configList [I] is not executed. setImpl (name, value) attributes ensure that only configured attributes can be modified, and no new attributes will be added during service running, all attributes are loaded when the Web service is started for the first time.

4. webwork [3] [4] Summary

The above analysis shows that when the Web service is started, ServletDispatcher first loads webwork through DefaultConfiguration. properties and default. properties, and find webwork. webwork in properties. custom. load other property files configured for properties. After loading, load the international resource file through webwork. custom. i18n. resources configured in the property for future use. Then look for the webwork. configuration Attribute to see if you have specified your own Configuration implementation. If so, use your own Configuration implementation. Otherwise, return the WebWork's own implementation (DelegatingConfiguration ). In most cases, WebWork is enough to implement your own Configuration, unless you want to load Configuration files in XML or other formats.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.