1 9 package org.jboss.portal.setup.impl; 10 11 import org.jboss.system.ServiceMBeanSupport; 12 import org.jboss.portal.setup.dl.DataLoaderConfig; 13 import org.jboss.portal.setup.dl.DataLoader; 14 import org.jboss.portal.setup.dl.HibernateDataLoaderConfig; 15 import org.jboss.portal.setup.sl.SchemaLoaderConfig; 16 import org.jboss.portal.setup.sl.SchemaLoader; 17 import org.jboss.portal.setup.sl.SchemaLoadType; 18 import org.jboss.portal.setup.sl.HibernateSchemaLoaderConfig; 19 import org.jboss.portal.setup.PortalSetupException; 20 import org.jboss.portal.setup.DatabaseVendorType; 21 import org.jboss.portal.setup.SetupService; 22 import org.jboss.portal.setup.impl.pm.HibernatePersistenceManager; 23 import org.jboss.portal.setup.pm.PersistenceManager; 24 import org.jboss.portal.setup.config.HibernateConfig; 25 import org.jboss.portal.setup.config.Configuration; 26 import org.jboss.portal.common.util.XML; 27 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.NodeList ; 30 import org.w3c.dom.Document ; 31 import org.apache.log4j.Logger; 32 import org.xml.sax.SAXException ; 33 34 import javax.xml.parsers.DocumentBuilder ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import java.util.List ; 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.io.InputStream ; 40 import java.io.IOException ; 41 42 43 51 public class PortalDbSetupImpl extends SetupServiceImpl implements SetupService 52 { 53 54 protected int m_retryCount = 0; 55 56 public PortalDbSetupImpl() 57 { 58 super(); 59 DEFAULT_DATA_URI = "org/jboss/portal/setup/impl/dl/dbloader/portal-dl.xml"; 60 } 61 62 public void initializeLoaders() throws PortalSetupException 63 { 64 initializeSchemaLoader(); 66 initializeDataLoader(); 67 parseDataLoacation(); 68 } 69 70 public void load() throws PortalSetupException 71 { 72 loadSchema(); 73 if (!m_dataLoader.checkDataInitialized()) 76 { 77 loadData(); 78 } 79 else 80 { 81 if (m_log.isInfoEnabled()) 82 { 83 m_log.info("Portal database data has been initilized before!"); 84 } 85 } 86 } 87 88 89 private void initializeSchemaLoader() throws PortalSetupException 90 { 91 if (m_log.isInfoEnabled()) 92 { 93 m_log.info("PortalDbSetupImpl:Initializing schema loader"); 94 } 95 String className = getSchemaLoaderConfig().getSchemaLoaderClass(); 96 if (null == className) 97 { 98 String msg = "Schema loader class name attribute cannot be null !"; 99 m_log.error(msg); 100 throw new PortalSetupException(msg); 101 } 102 Class clazz = null; 103 try 104 { 105 clazz = Class.forName(className); 106 } 107 catch (ClassNotFoundException cnfe) 108 { 109 String msg = "Failed to load class: " + className; 110 m_log.error(msg, cnfe); 111 throw new PortalSetupException(msg, cnfe); 112 } 113 try 114 { 115 m_schemaLoader = (SchemaLoader)clazz.newInstance(); 116 } 117 catch (IllegalAccessException iae) 118 { 119 String msg = "Failed to instantiate class: " + className; 120 m_log.error(msg, iae); 121 throw new PortalSetupException(msg, iae); 122 } 123 catch (InstantiationException ie) 124 { 125 String msg = "Failed to instantiate class: " + className; 126 m_log.error(msg, ie); 127 throw new PortalSetupException(msg, ie); 128 } 129 catch (ClassCastException cce) 130 { 131 String msg = "Failed to instantiate class: " + className + 132 ". Class should be instance of org.jboss.portal.setup.sl.SchemaLoader"; 133 m_log.error(msg, cce); 134 throw new PortalSetupException(msg, cce); 135 } 136 m_schemaLoader.setSchemaLoaderConfig(getSchemaLoaderConfig()); 137 138 if (m_log.isInfoEnabled()) 139 { 140 m_log.info("PortalDbSetupImpl:Schema loader have been initialized"); 141 } 142 } 143 144 private void initializeDataLoader() throws PortalSetupException 145 { 146 if (m_log.isInfoEnabled()) 147 { 148 m_log.info("PortalDbSetupImpl:Initializing schema loader"); 149 } 150 String className = getDataLoaderConfig().getDataLoaderClass(); 151 if (null == className) 152 { 153 String msg = "Data loader class name attribute cannot be null !"; 154 m_log.error(msg); 155 throw new PortalSetupException(msg); 156 } 157 Class clazz = null; 158 try 159 { 160 clazz = Class.forName(className); 161 } 162 catch (ClassNotFoundException cnfe) 163 { 164 String msg = "Failed to load class: " + className; 165 m_log.error(msg, cnfe); 166 throw new PortalSetupException(msg, cnfe); 167 } 168 try 169 { 170 m_dataLoader = (DataLoader)clazz.newInstance(); 171 } 172 catch (IllegalAccessException iae) 173 { 174 String msg = "Failed to instantiate class: " + className; 175 m_log.error(msg, iae); 176 throw new PortalSetupException(msg, iae); 177 } 178 catch (InstantiationException ie) 179 { 180 String msg = "Failed to instantiate class: " + className; 181 m_log.error(msg, ie); 182 throw new PortalSetupException(msg, ie); 183 } 184 catch (ClassCastException cce) 185 { 186 String msg = "Failed to instantiate class: " + className + 187 ". Class should be instance of org.jboss.portal.setup.dl.DataLoader"; 188 m_log.error(msg, cce); 189 throw new PortalSetupException(msg, cce); 190 } 191 m_dataLoader.setDataLoaderConfiguration(getDataLoaderConfig()); 192 if (m_log.isInfoEnabled()) 193 { 194 m_log.info("PortalDbSetupImpl:Data loader have been initialized"); 195 } 196 } 197 198 private void loadSchema() throws PortalSetupException 199 { 200 SchemaLoadType type = getSchemaLoaderConfig().getSchemaLoadType(); 201 boolean schemaExists = m_schemaLoader.isSchemaExists(); 202 if (SchemaLoadType.NEWONLY == type) 203 { 204 if (schemaExists) 205 { 206 if (m_log.isInfoEnabled()) 207 { 208 m_log.info("Did not perform dbloader schama load, because schema already exists!"); 209 } 210 return; 211 } 212 } 213 else if (SchemaLoadType.ALWAYS == type) 214 { 215 if (schemaExists) 216 { 217 m_schemaLoader.destroySchema(); 219 schemaExists = m_schemaLoader.isSchemaExists(); 220 } 221 } 222 else if (SchemaLoadType.NEVER == type) 223 { 224 return; 225 } 226 else if (SchemaLoadType.UPDATE == type) 227 { 228 return; 230 } 231 232 if (!schemaExists) 233 { 234 m_schemaLoader.loadSchema(); 235 if (!m_schemaLoader.isSchemaExists()) 236 { 237 String msg = "Schema verification failed after loading"; 238 m_log.error(msg); 239 throw new PortalSetupException(msg); 240 } 241 m_retryCount = 0; 242 } 243 else 244 { 245 boolean isMySql = ((HibernateSchemaLoaderConfig)m_schemaLoader.getSchemaLoaderConfig()).getHibernateConfig().getHibernateDialect().equalsIgnoreCase("net.sf.hibernate.dialect.MySQLDialect"); 247 248 if (isMySql) 251 { 252 if (m_retryCount < 1) 254 { 255 m_retryCount++; 256 loadSchema(); 257 } 258 else 259 { 260 String msg = "Failed to drop schema before loading!"; 261 m_log.error(msg); 262 throw new PortalSetupException(msg); 263 } 264 } 265 else 266 { 267 String msg = "Failed to drop schema before loading!"; 268 m_log.error(msg); 269 throw new PortalSetupException(msg); 270 } 271 } 272 } 273 274 275 } 276 | Popular Tags |