1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.StringReader ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 36 import javax.xml.transform.TransformerException ; 37 38 import org.apache.log4j.Logger; 39 import org.apache.xerces.parsers.DOMParser; 40 import org.apache.xpath.XPathAPI; 41 import org.exolab.castor.jdo.Database; 42 import org.exolab.castor.jdo.OQLQuery; 43 import org.exolab.castor.jdo.QueryResults; 44 import org.infoglue.cms.entities.content.ContentVersion; 45 import org.infoglue.cms.entities.kernel.BaseEntityVO; 46 import org.infoglue.cms.entities.management.CategoryAttribute; 47 import org.infoglue.cms.entities.management.Repository; 48 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl; 49 import org.infoglue.cms.entities.structure.SiteNodeVersion; 50 import org.infoglue.cms.entities.workflow.Event; 51 import org.infoglue.cms.entities.workflow.EventVO; 52 import org.infoglue.cms.entities.workflow.impl.simple.EventImpl; 53 import org.infoglue.cms.exception.Bug; 54 import org.infoglue.cms.exception.SystemException; 55 import org.infoglue.cms.io.FileHelper; 56 import org.infoglue.cms.security.InfoGluePrincipal; 57 import org.infoglue.cms.util.CmsPropertyHandler; 58 import org.w3c.dom.Document ; 59 import org.w3c.dom.Element ; 60 import org.w3c.dom.Node ; 61 import org.w3c.dom.NodeList ; 62 import org.xml.sax.InputSource ; 63 64 69 70 public class DatabaseDefinitionsController 71 { 72 private final static Logger logger = Logger.getLogger(DatabaseDefinitionsController.class.getName()); 73 74 public static DatabaseDefinitionsController getController() 75 { 76 return new DatabaseDefinitionsController(); 77 } 78 79 82 private Document getDatabaseDefinitionsDocument() 83 { 84 Document document = null; 85 86 try 87 { 88 String databaseDefinitions = CmsPropertyHandler.getContextRootPath() + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "databaseDefinitions.xml"; 89 90 File file = new File (databaseDefinitions); 91 92 if(file != null && file.exists()) 93 { 94 InputSource xmlSource = new InputSource (new FileInputStream (file)); 95 96 DOMParser parser = new DOMParser(); 97 parser.parse(xmlSource); 98 document = parser.getDocument(); 99 } 100 } 101 catch(Exception e) 102 { 103 logger.warn("An error occurred when trying to fetch the asset keys:" + e.getMessage(), e); 104 } 105 106 return document; 107 } 108 109 112 113 public Map getDatabaseDefinitions() 114 { 115 Map databases = new HashMap (); 116 117 Document document = getDatabaseDefinitionsDocument(); 118 119 if(document != null) 120 { 121 NodeList databaseNodeList = document.getDocumentElement().getElementsByTagName("database"); 122 123 for(int i=0; i < databaseNodeList.getLength(); i++) 124 { 125 Element databaseElement = (Element )databaseNodeList.item(i); 126 String id = databaseElement.getAttribute("id"); 127 NodeList propertyNodeList = databaseElement.getElementsByTagName("property"); 128 129 Map database = new HashMap (); 130 database.put("id", id); 131 132 for(int j=0; j < propertyNodeList.getLength(); j++) 133 { 134 Element propertyElement = (Element )propertyNodeList.item(j); 135 String name = propertyElement.getAttribute("name"); 136 String value = propertyElement.getAttribute("value"); 137 138 database.put(name, value); 139 } 140 141 databases.put(id, database); 142 } 143 } 144 145 return databases; 146 } 147 148 151 152 public Map getDatabaseDefinition(String id) 153 { 154 Map definitions = getDatabaseDefinitions(); 155 156 return (Map )definitions.get(id); 157 } 158 159 160 163 164 public File getCastorDatabaseDefinitionFile(String id) 165 { 166 Map databaseDefinition = DatabaseDefinitionsController.getController().getDatabaseDefinition("default"); 167 String engine = (String )databaseDefinition.get("driverEngine"); 168 String driverClass = (String )databaseDefinition.get("driverClass"); 169 String url = (String )databaseDefinition.get("url"); 170 String user = (String )databaseDefinition.get("user"); 171 String password = (String )databaseDefinition.get("password"); 172 String useUnicode = (String )databaseDefinition.get("useUnicode"); 173 String encoding = (String )databaseDefinition.get("encoding"); 174 String maxConnections = (String )databaseDefinition.get("maxConnections"); 175 String mapping = (String )databaseDefinition.get("mapping"); 176 177 url = url.replaceAll("&", "&"); 178 179 187 188 StringBuffer sb = new StringBuffer (); 189 sb.append("<jdo-conf>"); 190 sb.append(" <database name=\"INFOGLUE_CMS\" engine=\"" + engine + "\">"); 191 sb.append(" <data-source class-name=\"org.apache.commons.dbcp.BasicDataSource\">"); 192 sb.append(" <param name=\"driver-class-name\" value=\"" + driverClass + "\"/>"); 193 sb.append(" <param name=\"username\" value=\"" + user + "\"/>"); 194 sb.append(" <param name=\"password\" value=\"" + password + "\"/>"); 195 sb.append(" <param name=\"url\" value=\"" + url + "\"/>"); 196 sb.append(" <param name=\"max-active\" value=\"" + maxConnections + "\"/>"); 197 sb.append(" <param name=\"connection-properties\" value=\"useUnicode=" + useUnicode + ";characterEncoding=" + encoding + "\"/>"); 198 sb.append(" </data-source>"); 199 sb.append(" <mapping HREF=\"classes/" + mapping + "\"/>"); 200 sb.append(" </database>"); 201 sb.append(" <transaction-demarcation mode=\"local\"/>"); 202 sb.append("</jdo-conf>"); 203 204 String xml = sb.toString(); 205 206 String databaseDefinitions = CmsPropertyHandler.getContextRootPath() + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "currentDatabase.xml"; 207 208 File file = new File (databaseDefinitions); 209 210 try 211 { 212 FileHelper.writeToFile(file, xml, false); 213 } 214 catch (Exception e) 215 { 216 logger.error("Could not write currentDatabase.xml:" + e.getMessage(), e); 217 } 218 219 return file; 220 } 221 } 222 | Popular Tags |