KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > resourcebundle > ResourceBundleBaseService


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
package org.jahia.resourcebundle;
14
15 import java.io.File JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import javax.xml.transform.OutputKeys JavaDoc;
25 import javax.xml.transform.Transformer JavaDoc;
26 import javax.xml.transform.TransformerFactory JavaDoc;
27 import javax.xml.transform.dom.DOMSource JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29
30 import org.jahia.exceptions.JahiaException;
31 import org.jahia.exceptions.JahiaInitializationException;
32 import org.jahia.settings.SettingsBean;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35
36 /**
37  * Resource Bundle Service.
38  * Maintain a registry of resource bundles that can be referred by dynamic content ( JahiaField content ).
39  *
40  * @author Khue Nguyen
41  */

42 public class ResourceBundleBaseService extends ResourceBundleService {
43
44     private static org.apache.log4j.Logger logger =
45             org.apache.log4j.Logger.getLogger(ResourceBundleBaseService.class);
46
47     static private ResourceBundleBaseService instance = null;
48     private String JavaDoc resourceBundleRootDiskPath = "";
49     private String JavaDoc resourceBundleConfigFile = "";
50     private Map JavaDoc registry;
51     private ResourceBundlesDigester resBundleDigester;
52     private Document JavaDoc xmlDocument;
53
54     protected ResourceBundleBaseService() {
55         logger.debug( "***** Starting the ResourcebundleBaseService *****" );
56         registry = new HashMap JavaDoc();
57         resBundleDigester = new ResourceBundlesDigester();
58     }
59
60      public static ResourceBundleBaseService getInstance() {
61          if (instance == null) {
62              synchronized ( ResourceBundleBaseService.class ){
63                  if (instance == null) {
64                      instance = new ResourceBundleBaseService();
65                  }
66              }
67          }
68          return instance;
69      }
70
71      public void init( SettingsBean jSettings )
72      throws JahiaInitializationException {
73
74          StringBuffer JavaDoc buff = new StringBuffer JavaDoc(jSettings.getJahiaEtcDiskPath());
75          buff.append(File.separator);
76          buff.append("config");
77          buff.append(File.separator);
78          buff.append(this.configFileName);
79          this.resourceBundleConfigFile = buff.toString();
80          loadAllResourceBundles();
81      }
82
83     /**
84      * Returns an enumeration of all Resource Bundle Definition.
85      *
86      * @return all ResourceBundle registered in the system
87      * @throws JahiaException
88      */

89     public Iterator JavaDoc getResourceBundles()
90     throws JahiaException
91     {
92         return this.registry.values().iterator();
93     }
94
95     /**
96      * Return a resource bundle definition looking at it key.
97      *
98      * @param key
99      * @return ResourceBundleDefinition
100      * @throws JahiaException
101      */

102     public ResourceBundleDefinition getResourceBundle(String JavaDoc key)
103     throws JahiaException
104     {
105         return (ResourceBundleDefinition)this.registry.get(key);
106     }
107
108     /**
109      * Add a resource Bundle.
110      * If a resource bundle already exists for this key, this is this one that is returned !
111      * The existing resource bundle definition won't be overriden.
112      *
113      * @param key ResourceBundle Key
114      * @return an Resource Bundle looking at it key
115      * @throws JahiaException
116      */

117     public synchronized ResourceBundleDefinition
118     declareResourceBundleDefinition (String JavaDoc key, String JavaDoc resourceBundleName)
119     throws JahiaException {
120         ResourceBundleDefinition rbDef = this.getResourceBundle(key);
121         if ( rbDef != null ){
122             return rbDef;
123         }
124         rbDef = new ResourceBundleDefinition(key,resourceBundleName);
125         this.registry.put(key,rbDef);
126         this.storeResourceBundleDefinition(rbDef);
127         return rbDef;
128     }
129
130     private void loadAllResourceBundles(){
131
132         this.resBundleDigester
133                 .loadResourceBundleDefinitions(this.resourceBundleConfigFile);
134         Vector JavaDoc v = this.resBundleDigester.getResourceBundleDefinitions();
135         int size = v.size();
136         for ( int i=0 ; i<size ; i++ )
137         {
138             ResourceBundleDefinition rbDef = (ResourceBundleDefinition)v.get(i);
139             this.registry.put(rbDef.getResourceBundleID(),rbDef);
140         }
141     }
142
143     private synchronized void storeResourceBundleDefinition(ResourceBundleDefinition rbDef){
144
145
146         try {
147             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
148             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
149
150             if ( this.xmlDocument == null ){
151                 File JavaDoc bundleConfigFile = new File JavaDoc(this.resourceBundleConfigFile);
152                 this.xmlDocument = builder.parse(bundleConfigFile);
153             }
154             if ( this.xmlDocument != null ){
155
156                 Element JavaDoc root = (Element JavaDoc) this.xmlDocument.getDocumentElement();
157
158                 // resource bundle element
159
Element JavaDoc rbDefElement = (Element JavaDoc) this.xmlDocument.createElement("resource-bundle");
160
161                 // key element
162
Element JavaDoc keyElement = this.xmlDocument.createElement("key");
163                 keyElement.appendChild(this.xmlDocument.createTextNode(rbDef.getResourceBundleID()));
164                 rbDefElement.appendChild(keyElement);
165
166                 // file element
167
Element JavaDoc fileElement = this.xmlDocument.createElement("file");
168                 fileElement.appendChild(this.xmlDocument.createTextNode(rbDef.getResourceBundleFile()));
169                 rbDefElement.appendChild(fileElement);
170
171                 root.appendChild(rbDefElement);
172
173                 // write out to the file
174
this.xmlDocument.normalize(); // cleanup DOM tree a little
175

176                 TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
177
178                 // This creates a transformer that does a simple identity transform,
179
// and thus can be used for all intents and purposes as a serializer.
180
Transformer JavaDoc serializer = tfactory.newTransformer();
181
182                 serializer.setOutputProperty(OutputKeys.METHOD, "xml");
183                 serializer.setOutputProperty(OutputKeys.INDENT, "yes");
184                 FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(this.resourceBundleConfigFile);
185                 serializer.transform(new DOMSource JavaDoc(this.xmlDocument),
186                                      new StreamResult JavaDoc(fileStream));
187             }
188         } catch (Exception JavaDoc e) {
189             logger.error(
190                 "Error while reading or writing resource bundle configuration file : " +
191                 this.resourceBundleConfigFile, e);
192         }
193     }
194
195 }
196
197
Popular Tags