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.sl.SchemaLoaderConfig; 15 import org.jboss.portal.setup.sl.SchemaLoader; 16 import org.jboss.portal.setup.PortalSetupException; 17 import org.jboss.portal.setup.SetupService; 18 import org.jboss.portal.common.util.XML; 19 import org.apache.log4j.Logger; 20 import org.w3c.dom.Element ; 21 import org.w3c.dom.Document ; 22 import org.xml.sax.SAXException ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 32 40 public class SetupServiceImpl extends ServiceMBeanSupport implements SetupService 41 { 42 protected String DEFAULT_DATA_URI = null; 43 protected final Logger m_log; 44 private boolean m_performLoad = true; 45 private Element m_dataFileLocation = null; 46 47 private DataLoaderConfig m_dataLoaderConfig = null; 48 private SchemaLoaderConfig m_schemaLoaderConfig = null; 49 50 protected SchemaLoader m_schemaLoader = null; 51 protected DataLoader m_dataLoader = null; 52 protected List m_dataUriList = new ArrayList (); 53 54 55 public SetupServiceImpl() 56 { 57 super(); 58 m_log = Logger.getLogger(getClass().getName()); 59 } 60 61 64 public SetupService getSetupService() 65 { 66 return this; 67 } 68 69 70 73 public int getState() 74 { 75 return super.getState(); 76 } 77 78 81 public String getStateString() 82 { 83 return super.getStateString(); 84 } 85 86 89 public void setDataFileLocation(Element dataFileLocation) 90 { 91 m_dataFileLocation = dataFileLocation; 92 } 93 94 97 public Element getDataFileLocation() 98 { 99 return m_dataFileLocation; 100 } 101 102 103 106 public void setDataLoaderConfig(DataLoaderConfig dataLoaderConfig) 107 { 108 m_dataLoaderConfig = (DataLoaderConfig)dataLoaderConfig.getConfiguration(); 109 } 110 111 114 public void setSchemaLoaderConfig(SchemaLoaderConfig schemaLoaderConfig) 115 { 116 m_schemaLoaderConfig = (SchemaLoaderConfig)schemaLoaderConfig.getConfiguration(); 117 } 118 119 122 public void create() throws Exception 123 { 124 super.create(); 125 } 126 127 130 public void start() throws Exception 131 { 132 super.start(); 133 } 134 135 138 public void stop() 139 { 140 super.stop(); 141 } 142 143 146 public void destroy() 147 { 148 super.destroy(); 149 } 150 151 152 public boolean getPerformLoad() 153 { 154 return m_performLoad; 155 } 156 157 public void setPerformLoad(boolean performLoad) 158 { 159 m_performLoad = performLoad; 160 } 161 162 163 public DataLoaderConfig getDataLoaderConfig() 164 { 165 return m_dataLoaderConfig; 166 } 167 168 169 public SchemaLoaderConfig getSchemaLoaderConfig() 170 { 171 return m_schemaLoaderConfig; 172 } 173 174 175 public void initializeLoaders() throws PortalSetupException 176 { 177 178 } 179 180 181 public void load() throws PortalSetupException 182 { 183 184 } 185 186 187 public void clearLoaders() throws PortalSetupException 188 { 189 if (null != m_dataLoader) 190 { 191 m_dataLoader.destroy(); 192 } 193 if (null != m_schemaLoader) 194 { 195 m_schemaLoader.destroy(); 196 } 197 } 198 199 204 protected void destroyService() throws Exception 205 { 206 if (m_log.isInfoEnabled()) 207 { 208 m_log.info("Processing request to destroy service for: " + getClass().getName()); 209 } 210 clearLoaders(); 211 super.destroyService(); 212 213 if (m_log.isInfoEnabled()) 214 { 215 m_log.info("Finished process request to destroy service for: " + getClass().getName()); 216 } 217 } 218 219 protected void stopService() throws Exception 220 { 221 if (m_log.isInfoEnabled()) 222 { 223 m_log.info("Processing request to stop service for: " + getClass().getName()); 224 } 225 super.stopService(); 226 clearLoaders(); 227 if (m_log.isInfoEnabled()) 228 { 229 m_log.info("Finished process request to stop service for: " + getClass().getName()); 230 } 231 232 } 233 234 240 protected void createService() throws Exception 241 { 242 243 if (m_log.isInfoEnabled()) 244 { 245 m_log.info("Processing request to create service for: " + getClass().getName()); 246 } 247 super.createService(); 248 if (m_log.isInfoEnabled()) 249 { 250 m_log.info("Finished process request to create service for: " + getClass().getName()); 251 } 252 } 253 254 255 protected void startService() throws Exception 256 { 257 if (m_log.isInfoEnabled()) 258 { 259 m_log.info("Processing request to start service for: " + getClass().getName()); 260 } 261 if (m_performLoad) 262 { 263 initializeLoaders(); 264 load(); 265 } 266 267 268 super.startService(); 269 if (m_log.isInfoEnabled()) 270 { 271 m_log.info("Finished process request to start service for: " + getClass().getName()); 272 } 273 } 274 275 protected void parseDataLoacation() throws PortalSetupException 276 { 277 278 Element files = getDataFileLocation(); 279 if (null == files) 280 { 281 282 setDefaultDataUri(); 283 } 284 else 285 { 286 List fileElements = XML.getChildren(files, "data-uri"); 287 if (null == fileElements) 288 { 289 setDefaultDataUri(); 290 } 291 else 292 { 293 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 294 295 for (int i = 0, size = fileElements.size(); i < size; i++) 296 { 297 Element fileElmt = (Element )fileElements.get(i); 298 String fileUri = XML.asString(fileElmt); 299 300 if (null == cl.getResource(fileUri)) 301 { 302 String msg = "Failed to load resource: " + fileUri; 303 m_log.error(msg); 304 throw new PortalSetupException(msg); 305 } 306 else 307 { 308 m_dataUriList.add(fileUri); 309 } 310 } 311 } 312 } 313 } 314 315 protected void loadData() throws PortalSetupException 316 { 317 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 318 DocumentBuilder db = null; 319 320 try 321 { 322 db = XML.getDocumentBuilderFactory().newDocumentBuilder(); 323 } 324 catch (ParserConfigurationException pce) 325 { 326 String msg = "Failed to create a document builder!"; 327 m_log.error(msg, pce); 328 throw new PortalSetupException(msg, pce); 329 } 330 Iterator iter = m_dataUriList.iterator(); 331 while (iter.hasNext()) 332 { 333 String uri = (String )iter.next(); 334 InputStream is = cl.getResourceAsStream(uri); 335 if (null == is) 336 { 337 String msg = "Failed to load resource: " + uri; 338 m_log.error(msg); 339 throw new PortalSetupException(msg); 340 } 341 Document doc = null; 342 try 343 { 344 doc = db.parse(is); 345 } 346 catch (SAXException se) 347 { 348 throw new PortalSetupException(se); 349 } 350 catch (IOException ioe) 351 { 352 throw new PortalSetupException(ioe); 353 } 354 355 Element dataLoadElmt = doc.getDocumentElement(); 356 List actions = XML.getChildren(dataLoadElmt, "action"); 357 for (int j = 0; j < actions.size(); j++) 358 { 359 Element action = (Element )actions.get(j); 360 { 361 m_dataLoader.loadData(action); 362 } 363 } 364 365 } 366 } 367 368 protected void setDefaultDataUri() throws PortalSetupException 369 { 370 if (m_log.isInfoEnabled()) 371 { 372 m_log.info("No data load file uri have been scpecified; Trying to use default URI + " + DEFAULT_DATA_URI + "'."); 373 } 374 if (null == DEFAULT_DATA_URI) 375 { 376 throw new PortalSetupException("DefaultUri is undefoned!"); 377 } 378 m_dataUriList.add(DEFAULT_DATA_URI); 379 } 380 381 382 } 383 | Popular Tags |