KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > impl > config > HibernateConfigImpl


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.config;
10
11 import org.jboss.portal.setup.config.HibernateConfig;
12 import org.jboss.portal.setup.config.DbAccessConfig;
13 import org.jboss.portal.setup.config.Configuration;
14 import org.jboss.portal.setup.PortalSetupConstants;
15 import org.jboss.portal.setup.PortalSetupException;
16 import org.jboss.portal.setup.util.hibernate.HibernareDialectResolver;
17 import org.jboss.portal.common.util.XML;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.NodeList JavaDoc;
20
21 import java.util.Properties JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Map JavaDoc;
26
27
28
29
30 /**
31  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
32  * Date: Apr 13, 2005; Time: 1:51:37 PM
33  * @jmx.mbean
34  * @jboss.xmbean
35  * @since JBoss portal 2.0
36  * Class org.jboss.portal.setup.impl.config.HibernateConfigImpl
37  */

38 public class HibernateConfigImpl extends ConfigurationImpl implements HibernateConfig, PortalSetupConstants
39 {
40
41    private DbAccessConfig m_dbAccessConfig = null;
42    private List JavaDoc m_SchemaClassesList = new ArrayList JavaDoc();
43    private Properties JavaDoc m_HibernateProperties = new Properties JavaDoc();
44    private boolean m_validate = true;
45
46
47
48    /**
49     * @see HibernateConfig#setDbAccessConfig(DbAccessConfig)
50     * @jmx.managed-attribute
51     *
52     *
53     */

54    public void setDbAccessConfig(DbAccessConfig dbConfig)
55    {
56       m_dbAccessConfig = (DbAccessConfig)dbConfig.getConfiguration();
57
58       resolveHibernateDbProperies();
59    }
60
61    /**
62     * @see HibernateConfig#setDbAccessConfig(DbAccessConfig)
63     * @jmx.managed-attribute
64     *
65     *
66     */

67    public DbAccessConfig getDbAccessConfig()
68    {
69       return m_dbAccessConfig;
70    }
71
72
73    /**
74     * @return hibernate dialect class name
75     * @jmx.managed-attribute
76     * access="read-write"
77     */

78    public String JavaDoc getHibernateDialect() throws PortalSetupException
79    {
80       String JavaDoc dialect = (String JavaDoc)m_HibernateProperties.get(HIBERNATE_DIALECT);
81       if (null == dialect ||
82          AUTO_RESOLVE.equals(dialect)) {
83             dialect = resolveHbDialect();
84             m_HibernateProperties.put(HIBERNATE_DIALECT, dialect);
85          }
86       return dialect;
87    }
88
89    /**
90     * @param dialectClassName - hibernate dialect class name
91     * @jmx.managed-attribute
92     * access="read-write"
93     */

94    public void setHibernateDialect(String JavaDoc dialectClassName)
95    {
96
97       m_HibernateProperties.put(HIBERNATE_DIALECT, dialectClassName);
98    }
99
100
101    /**
102     * @param cacheProvider a hibernate cache provider class name
103     * @jmx.managed-attribute
104     * access="read-write"
105     */

106    public void setCacheProvider(String JavaDoc cacheProvider)
107    {
108       m_HibernateProperties.put(HIBERNATE_CACHE_PROVIDER, cacheProvider);
109    }
110
111    /**
112     * @return a cache provider class name
113     * @jmx.managed-attribute
114     * access="read-write"
115     */

116    public String JavaDoc getCacheProvider()
117    {
118       return (String JavaDoc)m_HibernateProperties.get(HIBERNATE_CACHE_PROVIDER);
119    }
120
121
122    /**
123     * @param classesElmt an XML elemnt that defines schema classes
124     * to use for hibernate configuration, session builder setup.
125     * @jmx.managed-attribute
126     * access="read-write"
127     */

128    public void setSchemaClasses(Element JavaDoc classesElmt)
129    {
130       List JavaDoc nodes = XML.getChildren(classesElmt, "db-schema-class-name");
131       if (null != nodes)
132       {
133          for (int i = 0, size = nodes.size(); i < size; i++)
134          {
135             Element JavaDoc classElmt = (Element JavaDoc)nodes.get(i);
136             if (classElmt != null)
137             {
138                String JavaDoc className = XML.asString(classElmt);
139                try
140                {
141                   Class JavaDoc clazz = Class.forName(className);
142                   m_SchemaClassesList.add(clazz);
143                }
144                catch (ClassNotFoundException JavaDoc cnfe)
145                {
146                   throw new IllegalArgumentException JavaDoc("Fail to load class: " + className + "due to" + cnfe.getMessage());
147                }
148             }
149          }
150       }
151    }
152
153    /**
154     * @return n XML elemnt that defines schema classes
155     * to use for hibernate configuration, session builder setup.
156     * @jmx.managed-attribute
157     * access="read-write"
158     */

159    public Element JavaDoc getSchemaClasses()
160    {
161       Element JavaDoc classes = null;
162       //XXX map back from a list to an element
163
return classes;
164    }
165
166
167    /**
168     * @return a set of hibernate configuration propertiies
169     * @see org.hibernate.cfg.Configuration
170     */

171    public Properties JavaDoc getHibernateProperties() throws PortalSetupException
172    {
173       String JavaDoc dialect = getHibernateDialect();
174       return m_HibernateProperties;
175    }
176
177
178    /**
179     * @return a list of schema classes that defines dbloader schema
180     */

181    public List JavaDoc getSchemaClassesList()
182    {
183       return m_SchemaClassesList;
184    }
185
186    /**
187     * @param props a set of hibernate configuration properties
188     * @see org.hibernate.cfg.Configuration
189     */

190    public void setHibernateProperties(Properties JavaDoc props)
191    {
192       if (props != null)
193       {
194          m_HibernateProperties.putAll(props);
195       }
196    }
197
198    /**
199     * @param key a property name to be added to a set of hibernate configuration properties
200     * @param value a proverty value to be added to a set of hibernate configuration properties
201     * @see org.hibernate.cfg.Configuration
202     */

203    public void addPropery(String JavaDoc key, String JavaDoc value)
204    {
205       m_HibernateProperties.put(key, value);
206    }
207
208
209    public void setSchemaClasses(List JavaDoc classes)
210    {
211       m_SchemaClassesList.addAll(classes);
212    }
213
214    public void addSchemaClass(Class JavaDoc schemaClass)
215    {
216       m_SchemaClassesList.add(schemaClass);
217    }
218
219
220    private void resolveHibernateDbProperies()
221    {
222
223       if (m_dbAccessConfig.getDataSourceName() != null)
224       {
225          //using datasource
226
m_HibernateProperties.put(DATA_SOURCE_NAME, m_dbAccessConfig.getDataSourceName());
227
228       }
229       else
230       {
231          //using jdbc driver , etc ...
232
m_HibernateProperties.put(DB_CONN_URL, m_dbAccessConfig.getConnUrl());
233          m_HibernateProperties.put(DB_CONN_DRIVER_CLASS, m_dbAccessConfig.getDriverClassName());
234          m_HibernateProperties.put(DB_USER_NAME, m_dbAccessConfig.getUserName());
235          m_HibernateProperties.put(DB_USER_PASSWORD, m_dbAccessConfig.getPassword());
236
237       }
238    }
239
240    private String JavaDoc resolveHbDialect() throws PortalSetupException {
241       String JavaDoc dialect = null;
242       String JavaDoc str =m_dbAccessConfig.getDataSourceName();
243       if(null != str) {
244            dialect = HibernareDialectResolver.getDialectFromDataSource(str);
245       } else {
246         str = m_dbAccessConfig.getConnUrl();
247          dialect = HibernareDialectResolver.getDialectFromConnURL(str);
248       }
249       return dialect;
250
251    }
252
253 }
254
Popular Tags