KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > impl > PortalDbSetupImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

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 JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31 import org.apache.log4j.Logger;
32 import org.xml.sax.SAXException JavaDoc;
33
34 import javax.xml.parsers.DocumentBuilder JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41
42
43 /**
44  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
45  * Date: Apr 16, 2005; Time: 9:01:48 PM
46  * @jmx.mbean
47  * @jboss.xmbean
48  * @since JBoss portal 2.0
49  * Class org.jboss.portal.setup.impl.PortalDbSetupImpl
50  */

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       //initialize schema and data loaders
65
initializeSchemaLoader();
66       initializeDataLoader();
67       parseDataLoacation();
68    }
69
70    public void load() throws PortalSetupException
71    {
72       loadSchema();
73       //add chack for database data - chcke data query string
74
//we may want to move this string in the action class ?
75
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 JavaDoc className = getSchemaLoaderConfig().getSchemaLoaderClass();
96       if (null == className)
97       {
98          String JavaDoc msg = "Schema loader class name attribute cannot be null !";
99          m_log.error(msg);
100          throw new PortalSetupException(msg);
101       }
102       Class JavaDoc clazz = null;
103       try
104       {
105          clazz = Class.forName(className);
106       }
107       catch (ClassNotFoundException JavaDoc cnfe)
108       {
109          String JavaDoc 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 JavaDoc iae)
118       {
119          String JavaDoc msg = "Failed to instantiate class: " + className;
120          m_log.error(msg, iae);
121          throw new PortalSetupException(msg, iae);
122       }
123       catch (InstantiationException JavaDoc ie)
124       {
125          String JavaDoc msg = "Failed to instantiate class: " + className;
126          m_log.error(msg, ie);
127          throw new PortalSetupException(msg, ie);
128       }
129       catch (ClassCastException JavaDoc cce)
130       {
131          String JavaDoc 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 JavaDoc className = getDataLoaderConfig().getDataLoaderClass();
151       if (null == className)
152       {
153          String JavaDoc msg = "Data loader class name attribute cannot be null !";
154          m_log.error(msg);
155          throw new PortalSetupException(msg);
156       }
157       Class JavaDoc clazz = null;
158       try
159       {
160          clazz = Class.forName(className);
161       }
162       catch (ClassNotFoundException JavaDoc cnfe)
163       {
164          String JavaDoc 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 JavaDoc iae)
173       {
174          String JavaDoc msg = "Failed to instantiate class: " + className;
175          m_log.error(msg, iae);
176          throw new PortalSetupException(msg, iae);
177       }
178       catch (InstantiationException JavaDoc ie)
179       {
180          String JavaDoc msg = "Failed to instantiate class: " + className;
181          m_log.error(msg, ie);
182          throw new PortalSetupException(msg, ie);
183       }
184       catch (ClassCastException JavaDoc cce)
185       {
186          String JavaDoc 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             //drop first
218
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          //TODO add update to schema loader
229
return;
230       }
231
232       if (!schemaExists)
233       {
234          m_schemaLoader.loadSchema();
235          if (!m_schemaLoader.isSchemaExists())
236          {
237             String JavaDoc 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          //check vendor
246
boolean isMySql = ((HibernateSchemaLoaderConfig)m_schemaLoader.getSchemaLoaderConfig()).getHibernateConfig().getHibernateDialect().equalsIgnoreCase("net.sf.hibernate.dialect.MySQLDialect");
247
248          // if (DatabaseVendorType.parseVendorName(DatabaseVendorType.MY_SQL, false) ==
249
// ((HibernateSchemaLoaderConfig)m_schemaLoader.getSchemaLoaderConfig()).getHibernateConfig().getDbAccessConfig().getDbVendorType())
250
if (isMySql)
251          {
252             //workaround MSQL drop schema problems ( tables does not get deleted )
253
if (m_retryCount < 1)
254             {
255                m_retryCount++;
256                loadSchema();
257             }
258             else
259             {
260                String JavaDoc msg = "Failed to drop schema before loading!";
261                m_log.error(msg);
262                throw new PortalSetupException(msg);
263             }
264          }
265          else
266          {
267             String JavaDoc 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