1 package org.jahia.resourcebundle; 14 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Vector ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.transform.OutputKeys ; 25 import javax.xml.transform.Transformer ; 26 import javax.xml.transform.TransformerFactory ; 27 import javax.xml.transform.dom.DOMSource ; 28 import javax.xml.transform.stream.StreamResult ; 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 ; 34 import org.w3c.dom.Element ; 35 36 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 resourceBundleRootDiskPath = ""; 49 private String resourceBundleConfigFile = ""; 50 private Map registry; 51 private ResourceBundlesDigester resBundleDigester; 52 private Document xmlDocument; 53 54 protected ResourceBundleBaseService() { 55 logger.debug( "***** Starting the ResourcebundleBaseService *****" ); 56 registry = new HashMap (); 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 buff = new StringBuffer (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 89 public Iterator getResourceBundles() 90 throws JahiaException 91 { 92 return this.registry.values().iterator(); 93 } 94 95 102 public ResourceBundleDefinition getResourceBundle(String key) 103 throws JahiaException 104 { 105 return (ResourceBundleDefinition)this.registry.get(key); 106 } 107 108 117 public synchronized ResourceBundleDefinition 118 declareResourceBundleDefinition (String key, String 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 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 factory = DocumentBuilderFactory.newInstance(); 148 DocumentBuilder builder = factory.newDocumentBuilder(); 149 150 if ( this.xmlDocument == null ){ 151 File bundleConfigFile = new File (this.resourceBundleConfigFile); 152 this.xmlDocument = builder.parse(bundleConfigFile); 153 } 154 if ( this.xmlDocument != null ){ 155 156 Element root = (Element ) this.xmlDocument.getDocumentElement(); 157 158 Element rbDefElement = (Element ) this.xmlDocument.createElement("resource-bundle"); 160 161 Element keyElement = this.xmlDocument.createElement("key"); 163 keyElement.appendChild(this.xmlDocument.createTextNode(rbDef.getResourceBundleID())); 164 rbDefElement.appendChild(keyElement); 165 166 Element fileElement = this.xmlDocument.createElement("file"); 168 fileElement.appendChild(this.xmlDocument.createTextNode(rbDef.getResourceBundleFile())); 169 rbDefElement.appendChild(fileElement); 170 171 root.appendChild(rbDefElement); 172 173 this.xmlDocument.normalize(); 176 TransformerFactory tfactory = TransformerFactory.newInstance(); 177 178 Transformer serializer = tfactory.newTransformer(); 181 182 serializer.setOutputProperty(OutputKeys.METHOD, "xml"); 183 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 184 FileOutputStream fileStream = new FileOutputStream (this.resourceBundleConfigFile); 185 serializer.transform(new DOMSource (this.xmlDocument), 186 new StreamResult (fileStream)); 187 } 188 } catch (Exception 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 |