View Javadoc

1   /*
2    * $Id: LegacyPropertiesConfigurationProvider.java 652734 2008-05-02 02:29:02Z mrdon $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.config;
23  
24  import java.util.Iterator;
25  import java.util.Locale;
26  import java.util.StringTokenizer;
27  
28  import com.opensymphony.xwork2.config.Configuration;
29  import com.opensymphony.xwork2.config.ConfigurationException;
30  import com.opensymphony.xwork2.config.ConfigurationProvider;
31  import com.opensymphony.xwork2.inject.ContainerBuilder;
32  import com.opensymphony.xwork2.inject.Context;
33  import com.opensymphony.xwork2.inject.Factory;
34  import com.opensymphony.xwork2.inject.Inject;
35  import com.opensymphony.xwork2.util.location.LocatableProperties;
36  import com.opensymphony.xwork2.util.logging.Logger;
37  import com.opensymphony.xwork2.util.logging.LoggerFactory;
38  import org.apache.struts2.StrutsConstants;
39  
40  public class LegacyPropertiesConfigurationProvider implements ConfigurationProvider {
41  
42      /***
43       * The Logging instance for this class.
44       */
45      private static final Logger LOG = LoggerFactory.getLogger(LegacyPropertiesConfigurationProvider.class);
46  
47      public void destroy() {
48          Settings.reset();
49      }
50  
51      public void init(Configuration configuration)
52          throws ConfigurationException {
53          Settings.reset();
54      }
55      
56      public void loadPackages()
57              throws ConfigurationException {
58      }
59  
60      public boolean needsReload() {
61          return false;
62      }
63  
64      public void register(ContainerBuilder builder, LocatableProperties props)
65              throws ConfigurationException {
66          
67          final Settings settings = Settings.getInstance();
68          
69          loadSettings(props, settings);
70          
71          // Set default locale by lazily resolving the locale property as needed into a Locale object
72          builder.factory(Locale.class, new Factory() {
73              private Locale locale;
74  
75              public synchronized Object create(Context context) throws Exception {
76                  if (locale == null) {
77                      String loc = context.getContainer().getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
78                      if (loc != null) {
79                          StringTokenizer localeTokens = new StringTokenizer(loc, "_");
80                          String lang = null;
81                          String country = null;
82  
83                          if (localeTokens.hasMoreTokens()) {
84                              lang = localeTokens.nextToken();
85                          }
86  
87                          if (localeTokens.hasMoreTokens()) {
88                              country = localeTokens.nextToken();
89                          }
90                          locale = new Locale(lang, country);
91                      } else {
92                          LOG.info("No locale define, substituting the default VM locale");
93                          locale = Locale.getDefault();
94                      }
95                  }
96                  return locale;
97              }
98          });
99      }
100 
101     /***
102      * @param props
103      * @param settings
104      */
105     protected void loadSettings(LocatableProperties props, final Settings settings) {
106         // We are calling the impl methods to get around the single instance of Settings that is expected
107         for (Iterator i = settings.listImpl(); i.hasNext(); ) {
108             String name = (String) i.next();
109             props.setProperty(name, settings.getImpl(name), settings.getLocationImpl(name));
110         }
111     }
112 }