001: package com.technoetic.xplanner.db.hibernate;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.InputStream;
006: import java.util.Properties;
007: import java.sql.Connection;
008: import java.sql.SQLException;
009: import javax.servlet.ServletRequest;
010: import javax.xml.parsers.ParserConfigurationException;
011: import javax.xml.transform.Source;
012: import javax.xml.transform.Templates;
013: import javax.xml.transform.Transformer;
014: import javax.xml.transform.TransformerConfigurationException;
015: import javax.xml.transform.TransformerException;
016: import javax.xml.transform.TransformerFactory;
017: import javax.xml.transform.sax.SAXSource;
018: import javax.xml.transform.stream.StreamResult;
019: import javax.xml.transform.stream.StreamSource;
020:
021: import net.sf.hibernate.HibernateException;
022: import net.sf.hibernate.Session;
023: import net.sf.hibernate.SessionFactory;
024: import net.sf.hibernate.connection.ConnectionProviderFactory;
025: import net.sf.hibernate.dialect.Dialect;
026: import net.sf.hibernate.cfg.Configuration;
027: import net.sf.hibernate.util.DTDEntityResolver;
028: import org.apache.log4j.Logger;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.XMLReader;
032: import org.xml.sax.helpers.XMLReaderFactory;
033:
034: import com.technoetic.xplanner.XPlannerProperties;
035: import com.technoetic.xplanner.actions.XPlannerInitializationPlugin;
036: import com.technoetic.xplanner.util.LogUtil;
037:
038: /** @noinspection ClassNamePrefixedWithPackageName*/
039: public class HibernateHelper {
040: private static final Logger LOG = LogUtil.getLogger();
041: private static final String SESSION_ATTRIBUTE_KEY = "HibernateSession";
042:
043: private HibernateHelper() {
044: }
045:
046: public static void initializeHibernate() throws HibernateException {
047: if (GlobalSessionFactory.get() == null) {
048: Configuration cfg = initializeConfiguration();
049: SessionFactory sessionFactory = cfg.buildSessionFactory();
050: // XPlanner uses a custom session factory so that all sessions will be
051: // automatically configured with an XPlanner-related Hibernate interceptor.
052: GlobalSessionFactory.set(new XPlannerSessionFactory(
053: sessionFactory));
054: }
055: }
056:
057: public static SessionFactory getSessionFactory()
058: throws HibernateException {
059: initializeHibernate();
060: return GlobalSessionFactory.get();
061: }
062:
063: public static Configuration initializeConfiguration()
064: throws HibernateException {
065: Transformer transformer = null;
066: Properties properties = getProperties();
067: try {
068: if (properties
069: .containsKey("xplanner.hibernate.mappingtransform")) {
070: String transformerFileName = properties
071: .getProperty("xplanner.hibernate.mappingtransform");
072: LOG.info("Using Hibernate mapping transformer: "
073: + transformerFileName);
074: transformer = createTransformer(transformerFileName);
075: }
076: Configuration cfg = new Configuration();
077: cfg.addInputStream(getMappingStream(
078: "mappings/Attribute.xml", transformer));
079: cfg.addInputStream(getMappingStream(
080: "mappings/Permission.xml", transformer));
081: cfg.addInputStream(getMappingStream(
082: "mappings/RoleAssociation.xml", transformer));
083: cfg.addInputStream(getMappingStream(
084: "mappings/DataSample.xml", transformer));
085: cfg.addInputStream(getMappingStream("mappings/Project.xml",
086: transformer));
087: cfg.addInputStream(getMappingStream(
088: "mappings/Iteration.xml", transformer));
089: cfg.addInputStream(getMappingStream(
090: "mappings/UserStory.xml", transformer));
091: cfg.addInputStream(getMappingStream("mappings/Task.xml",
092: transformer));
093: cfg.addInputStream(getMappingStream(
094: "mappings/TimeEntry.xml", transformer));
095: cfg.addInputStream(getMappingStream(
096: "mappings/Integration.xml", transformer));
097: cfg.addInputStream(getMappingStream("mappings/Note.xml",
098: transformer));
099: cfg.addInputStream(getMappingStream("mappings/Role.xml",
100: transformer));
101: cfg.addInputStream(getMappingStream("mappings/Person.xml",
102: transformer));
103: cfg.addInputStream(getMappingStream(
104: "mappings/HistoricalEvent.xml", transformer));
105: cfg.addInputStream(getMappingStream("mappings/File.xml",
106: transformer));
107: cfg.addInputStream(getMappingStream(
108: "mappings/Directory.xml", transformer));
109: cfg.addInputStream(getMappingStream("mappings/Metrics.xml",
110: transformer));
111: //FEATURE:
112: // cfg.addInputStream(getMappingStream("mappings/Feature.xml", transformer));
113: cfg.addProperties(properties);
114: EHCacheHelper.configure(cfg);
115: return cfg;
116: } catch (Exception e) {
117: throw new HibernateException(e);
118: }
119: }
120:
121: public static Properties getProperties() {
122: return new XPlannerProperties().get();
123: }
124:
125: private static Transformer createTransformer(String path)
126: throws TransformerConfigurationException {
127: InputStream xsltStream = XPlannerInitializationPlugin.class
128: .getClassLoader().getResourceAsStream(path);
129: Source xsltSource = new StreamSource(xsltStream);
130: TransformerFactory tf = TransformerFactory.newInstance();
131: Templates transformation = tf.newTemplates(xsltSource);
132: return transformation.newTransformer();
133: }
134:
135: private static InputStream getMappingStream(String path,
136: Transformer transformer) throws TransformerException,
137: SAXException {
138: InputStream in = XPlannerInitializationPlugin.class
139: .getClassLoader().getResourceAsStream(path);
140: if (transformer != null) {
141: XMLReader reader = XMLReaderFactory.createXMLReader();
142: reader.setEntityResolver(new DTDEntityResolver());
143: SAXSource saxSource = new SAXSource(reader,
144: new InputSource(in));
145: ByteArrayOutputStream transformedStream = new ByteArrayOutputStream();
146: transformer.transform(saxSource, new StreamResult(
147: transformedStream));
148: return new ByteArrayInputStream(transformedStream
149: .toByteArray());
150: } else {
151: return in;
152: }
153: }
154:
155: public static Session getSession(ServletRequest request) {
156: Session session = (Session) request
157: .getAttribute(SESSION_ATTRIBUTE_KEY);
158: if (LOG.isDebugEnabled()) {
159: if (session != ThreadSession.get()) {
160: LOG.info("Session storage mismatch thread="
161: + ThreadSession.get() + " HBHelper=" + session);
162: }
163: }
164: return session;
165: }
166:
167: public static void setSession(ServletRequest request,
168: Session session) {
169: request.setAttribute(SESSION_ATTRIBUTE_KEY, session);
170: }
171:
172: public static Dialect getDialect() throws HibernateException {
173: return Dialect.getDialect(getProperties());
174: }
175:
176: static public Connection getConnection() {
177: try {
178: Properties properties = new XPlannerProperties().get();
179: return ConnectionProviderFactory.newConnectionProvider(
180: properties).getConnection();
181: } catch (Exception e) {
182: throw new RuntimeException(e);
183: }
184: }
185: }
|