View Javadoc
1   /*
2    * Copyright (C) 2003-2013 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5   * modify it under the terms of the GNU Affero General Public License
6   * as published by the Free Software Foundation; either version 3
7   * of the License, or (at your option) any later version.
8   *
9   * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.social.notification.mock;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.ArrayList;
22  import java.util.Enumeration;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.Properties;
29  import java.util.ResourceBundle;
30  import java.util.Scanner;
31  import java.util.Set;
32  
33  import org.apache.commons.beanutils.PropertyUtils;
34  
35  import org.exoplatform.commons.utils.CommonsUtils;
36  import org.exoplatform.commons.utils.PageList;
37  import org.exoplatform.container.configuration.ConfigurationManager;
38  import org.exoplatform.services.log.ExoLogger;
39  import org.exoplatform.services.log.Log;
40  import org.exoplatform.services.resources.Query;
41  import org.exoplatform.services.resources.ResourceBundleData;
42  import org.exoplatform.services.resources.ResourceBundleService;
43  
44  
45  public class MockResourceBundleService implements ResourceBundleService {
46    private static Log LOG = ExoLogger.getLogger(MockResourceBundleService.class);
47    private static final String RESOURCE_LOCATION = "jar:/";
48    ConfigurationManager configurationService;
49    ResourceBundle resourceBundle;
50    List<String> locals = new ArrayList<String>();
51    public MockResourceBundleService() {
52      configurationService = CommonsUtils.getService(ConfigurationManager.class);
53    }
54    
55    private ResourceBundle getResourceBundle() {
56      if (resourceBundle == null) {
57        resourceBundle = new TestResourceBundle();
58      }
59      return resourceBundle;
60    }
61    
62    private static List<String> readTextECToListByInput(InputStream input) throws IOException {
63      List<String> list = new ArrayList<String>();
64      Scanner scanner = new Scanner(input, "UTF-8");
65      try {
66        while (scanner.hasNextLine()) {
67          String s = scanner.nextLine();
68          list.add(s);
69        }
70      } catch (Exception e) {
71      } finally {
72        scanner.close();
73        input.close();
74      }
75      return list;
76    }
77  
78    @Override
79    public ResourceBundle getResourceBundle(String name, Locale locale) {
80      String id = name.replace(".", "/") + "_" + locale.getLanguage() + ".properties";
81      String defaultId = name.replace(".", "/") + "_" + Locale.ENGLISH + ".properties";
82      ResourceBundle resourceBundle = getResourceBundle();
83      if(!locals.contains(id)) {
84        InputStream inputStream = null;
85        Properties list = null;
86        try {
87          inputStream = configurationService.getInputStream(RESOURCE_LOCATION + id);
88          list = new Properties();
89          list.load(inputStream);
90        } catch (Exception e) {
91          LOG.warn("Could not find the resource bundle for the language {}, falling back to the default language {}",locale.getLanguage(), Locale.ENGLISH);
92          try {
93            inputStream = configurationService.getInputStream(RESOURCE_LOCATION + defaultId);
94            list = new Properties();
95            list.load(inputStream);
96          } catch (Exception e1) {
97            e1.printStackTrace();
98          }
99        } finally {
100         if(inputStream != null) {
101           try {
102             inputStream.close();
103           } catch (IOException e) {
104             e.printStackTrace();
105           }
106         }
107       }
108       if (list != null) {
109         Set<Object> keys = list.keySet();
110         for (Object key : keys) {
111           ((TestResourceBundle)resourceBundle).addData(key.toString(), list.get(key).toString());
112         }
113       }
114       locals.add(id);
115     }
116     
117     return resourceBundle;
118   }
119 
120   @Override
121   public ResourceBundle getResourceBundle(String name, Locale locale, ClassLoader cl) {
122     return null;
123   }
124 
125   @Override
126   public ResourceBundle getResourceBundle(String[] name, Locale locale) {
127     return null;
128   }
129 
130   @Override
131   public ResourceBundle getResourceBundle(String[] name, Locale locale, ClassLoader cl) {
132     return null;
133   }
134 
135   @Override
136   public ResourceBundleData getResourceBundleData(String id) {
137     // TODO Auto-generated method stub
138     return null;
139   }
140 
141   @Override
142   public ResourceBundleData removeResourceBundleData(String id) {
143     return null;
144   }
145 
146   @Override
147   public void saveResourceBundle(ResourceBundleData data) {
148     
149   }
150 
151   @Override
152   public PageList<ResourceBundleData> findResourceDescriptions(Query q) {
153     return null;
154   }
155 
156   @Override
157   public ResourceBundleData createResourceBundleDataInstance() {
158     return null;
159   }
160 
161   @Override
162   public String[] getSharedResourceBundleNames() {
163     return null;
164   }
165   
166   
167   private class TestResourceBundle extends ResourceBundle {
168     private Map<String, String> data = new HashMap<String, String>();
169 
170     public void addData(String key, String value) {
171       this.data.put(key, value);
172     }
173 
174     @Override
175     protected Object handleGetObject(String key) {
176       return data.get(key);
177     }
178 
179     @Override
180     protected Set<String> handleKeySet() {
181       return data.keySet();
182     }
183     @Override
184     public Enumeration<String> getKeys() {
185       return new Enumeration<String>() {
186         Iterator<String> it = data.keySet().iterator();
187 
188         @Override
189         public boolean hasMoreElements() {
190           return it.hasNext();
191         }
192 
193         @Override
194         public String nextElement() {
195           return it.next();
196         }
197       };
198     }
199     
200   }
201 
202 }