1 package org.tanukisoftware.wrapper.resources; 2 3 45 46 import java.util.Hashtable ; 47 import java.util.MissingResourceException ; 48 import java.util.ResourceBundle ; 49 import java.text.MessageFormat ; 50 51 76 public class ResourceManager 77 { 78 private static Hashtable m_resources = new Hashtable (); 79 80 87 private int m_refreshCounter; 88 private static int m_staticRefreshCounter = 0; 89 90 93 private ResourceBundle m_bundle; 94 private String m_bundleName; 95 96 99 private ResourceManager( String resourceName ) 100 { 101 String className = this.getClass().getName(); 103 int pos = className.lastIndexOf( '.' ); 105 if ( pos > 0 ) 106 { 107 m_bundleName = className.substring( 0, pos + 1 ); 108 } 109 else 110 { 111 m_bundleName = ""; 112 } 113 114 m_bundleName += resourceName; 115 116 refreshBundle(); 118 } 119 120 private void refreshBundle() 121 { 122 try 123 { 124 m_bundle = ResourceBundle.getBundle( m_bundleName ); 125 } 126 catch ( MissingResourceException e ) 127 { 128 System.out.println( e ); 129 } 130 131 m_refreshCounter = m_staticRefreshCounter; 132 } 133 134 139 public static ResourceManager getResourceManager() 140 { 141 return getResourceManager( null ); 142 } 143 144 151 public static synchronized ResourceManager getResourceManager( String resourceName ) 152 { 153 if ( resourceName == null ) 154 { 155 resourceName = "resource"; 156 } 157 ResourceManager resource = (ResourceManager)m_resources.get( resourceName ); 158 if ( resource == null ) 159 { 160 resource = new ResourceManager( resourceName ); 161 m_resources.put( resourceName, resource ); 162 } 163 return resource; 164 } 165 166 170 public static synchronized void refresh() 171 { 172 m_resources = new Hashtable (); 173 m_staticRefreshCounter++; 174 } 175 176 private String getString( String key ) 177 { 178 synchronized(this) 180 { 181 if ( m_refreshCounter != m_staticRefreshCounter ) 182 { 183 refreshBundle(); 184 } 185 } 186 187 String msg; 188 if ( m_bundle == null ) 189 { 190 msg = key; 191 } 192 else 193 { 194 try 195 { 196 msg = m_bundle.getString( key ); 197 } 198 catch ( MissingResourceException ex ) 199 { 200 msg = key; 201 System.out.println( key + " is missing from resource bundle \"" + m_bundleName 202 + "\"" ); 203 } 204 } 205 206 return msg; 207 } 208 209 215 public String format( String key ) 216 { 217 return getString( key ); 218 } 219 220 228 public String format( String pattern, Object o0 ) 229 { 230 return MessageFormat.format( getString( pattern ), new Object [] { o0 } ); 231 } 232 233 242 public String format( String pattern, Object o0, Object o1 ) 243 { 244 return MessageFormat.format( getString( pattern ), new Object [] { o0,o1 } ); 245 } 246 247 257 public String format( String pattern, Object o0, Object o1, Object o2 ) 258 { 259 return MessageFormat.format( getString( pattern ), new Object [] { o0,o1,o2 } ); 260 } 261 262 273 public String format( String pattern, Object o0, Object o1, Object o2, Object o3 ) 274 { 275 return MessageFormat.format( getString( pattern ), new Object [] { o0,o1,o2,o3 } ); 276 } 277 278 } 280 281 | Popular Tags |