KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > language > LanguageCache


1 /*
2  * Created on Jan 6, 2005
3  */

4 package com.nightlabs.ipanema.language;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import com.nightlabs.ipanema.base.login.Login;
13 import com.nightlabs.ipanema.language.Language;
14 import com.nightlabs.ipanema.language.LanguageManager;
15 import com.nightlabs.ipanema.language.LanguageManagerUtil;
16 import com.nightlabs.rcp.exceptionhandler.ExceptionHandlerRegistry;
17
18 /**
19  * Because languages don't change so often, we use a singleton shared instance of
20  * this class in the client to cache the languages.
21  *
22  * @author Marco Schulze - marco at nightlabs dot de
23  */

24 public class LanguageCache
25 {
26     private static LanguageCache _sharedInstance = null;
27     public static LanguageCache sharedInstance()
28     {
29         if (_sharedInstance == null) {
30             _sharedInstance = new LanguageCache();
31         }
32         return _sharedInstance;
33     }
34
35     protected List JavaDoc languages = new ArrayList JavaDoc();
36     protected Map JavaDoc languagesByLanguageID = new HashMap JavaDoc();
37
38     public LanguageCache()
39     {
40         try {
41           LanguageManager languageManager = LanguageManagerUtil.getHome(
42                 Login.getLogin().getInitialContextProperties()).create();
43
44           for (Iterator JavaDoc it = languageManager.getLanguages().iterator(); it.hasNext(); ) {
45             Language language = (Language) it.next();
46             languages.add(language);
47             languagesByLanguageID.put(language.getLanguageID(), language);
48           }
49
50             languageManager.remove();
51
52             if (languages.size() < 1)
53                 throw new IllegalStateException JavaDoc("There is no language registered in the server!");
54         } catch (RuntimeException JavaDoc e) {
55             ExceptionHandlerRegistry.asyncHandleException(e);
56             throw e;
57         } catch (Exception JavaDoc e) {
58             ExceptionHandlerRegistry.asyncHandleException(e);
59             throw new RuntimeException JavaDoc(e);
60         }
61     }
62
63     /**
64      * @return Returns the languages (a List of instances of type Language).
65      *
66      * @see Language
67      */

68     public List JavaDoc getLanguages()
69     {
70         return languages;
71     }
72
73     /**
74      * This method returns an instance of Language for the given languageID.
75      * If there is no language for the given ID existing, an UnknownLanguageException
76      * is thrown.
77      *
78      * @param languageID
79      * @return Returns the desired instance of Language - never <tt>null</tt>
80      *
81      * @see #getLanguage(String, boolean)
82      */

83     public Language getLanguage(String JavaDoc languageID)
84         throws UnknownLanguageException
85     {
86         return getLanguage(languageID, true);
87     }
88
89     /**
90      * This method returns an instance of Language for the given languageID.
91      *
92      * @param languageID The id of the desired language.
93      * @param throwExceptionIfNotExistent Whether or not to throw an exception if the language does not exist.
94      * @return Returns the desired instance of Language or <tt>null</tt> if it
95      * does not exist and <tt>throwExceptionIfNotExistent</tt> is <tt>false</tt>.
96      *
97      * @see #getLanguage(String)
98      */

99     public Language getLanguage(String JavaDoc languageID, boolean throwExceptionIfNotExistent)
100         throws UnknownLanguageException
101     {
102         Language l = (Language) languagesByLanguageID.get(languageID);
103         if (l == null && throwExceptionIfNotExistent)
104             throw new UnknownLanguageException("Language \""+languageID+"\" does not exist!");
105         return l;
106     }
107 }
108
Popular Tags