View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.hibernate;
2   
3   /*
4    * Copyright 2007 David Vicente
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.net.URL;
21  
22  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
23  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
24  import org.hibernate.HibernateException;
25  import org.hibernate.Session;
26  import org.hibernate.SessionFactory;
27  import org.hibernate.Transaction;
28  import org.hibernate.cfg.Configuration;
29  
30  /**
31   * Service Hibernate
32   * 
33   * @plexus.component role="org.codehaus.mojo.dashboard.report.plugin.hibernate.HibernateService"
34   *                   lifecycle-handler="plexus-configurable"
35   * 
36   * @author David Vicente
37   */
38  public class HibernateServiceImpl implements HibernateService, Initializable
39  {
40  
41      private SessionFactory sessionFactory;
42  
43      private Configuration hibConfig;
44  
45      private Session session;
46  
47      /**
48       * @plexus.configuration default-value="hibernate.cfg.xml"
49       */
50      private String mapping = "hibernate.cfg.xml";
51  
52      private String dialect;
53  
54      private String driverClass;
55  
56      private String url;
57  
58      private String username;
59  
60      private String password;
61  
62      /**
63       * @see org.codehaus.mojo.dashboard.report.plugin.hibernate.HibernateService#getSessionFactory()
64       */
65      public SessionFactory getSessionFactory()
66      {
67          if ( sessionFactory == null )
68          {
69              if ( dialect != null )
70              {
71                  hibConfig.setProperty( "hibernate.dialect", dialect );
72              }
73              if ( driverClass != null )
74              {
75                  hibConfig.setProperty( "hibernate.connection.driver_class", driverClass );
76              }
77              if ( url != null )
78              {
79                  hibConfig.setProperty( "hibernate.connection.url", url );
80              }
81              if ( username != null )
82              {
83                  hibConfig.setProperty( "hibernate.connection.username", username );
84              }
85              if ( password != null )
86              {
87                  hibConfig.setProperty( "hibernate.connection.password", password );
88              }             
89  
90              try
91              {
92                  sessionFactory = hibConfig.buildSessionFactory();
93              }
94              catch ( HibernateException e )
95              {
96                  throw new RuntimeException( "Problem creating session factory: ", e );
97              }
98          }
99          return sessionFactory;
100     }
101 
102     public Session getSession()
103     {
104         if ( session == null )
105         {
106             session = getSessionFactory().openSession();
107         }
108         return session;
109     }
110 
111     /**
112      * @see org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable#initialize()
113      */
114     public void initialize() throws InitializationException
115     {
116         hibConfig = new Configuration();
117 
118         try
119         {       
120             File file = new File( mapping );
121 
122             if ( file.exists() )
123             {
124                 hibConfig.configure( file );
125             }
126             else
127             {
128                 URL url = HibernateServiceImpl.class.getClassLoader().getResource( mapping );
129                 System.out.println( "url = " + url );
130                 if ( url != null )
131                 {
132                     hibConfig.configure( url );
133                 }
134                 else
135                 {
136                     throw new RuntimeException( "Couldn't find mapping file: " + mapping );
137                 }
138             }
139         }
140         catch ( HibernateException e )
141         {
142             throw new InitializationException( "Mapping problem.", e );
143         }
144     }
145 
146     public Configuration getConfiguration()
147     {
148         return hibConfig;
149     }
150 
151     /**
152      * @return the mapping
153      */
154     public String getMapping()
155     {
156         return mapping;
157     }
158 
159     /**
160      * @param mapping
161      *            the mapping to set
162      */
163     public void setMapping( String mapping )
164     {
165         this.mapping = mapping;
166     }
167 
168     public String getDialect()
169     {
170         return dialect;
171     }
172 
173     public void setDialect( String dialect )
174     {
175         this.dialect = dialect;
176     }
177 
178     public String getDriverClass()
179     {
180         return driverClass;
181     }
182 
183     public void setDriverClass( String driverClass )
184     {
185         this.driverClass = driverClass;
186     }
187 
188     public String getPassword()
189     {
190         return password == null ? "" : password;
191     }
192 
193     public void setPassword( String password )
194     {
195         this.password = password;
196     }
197 
198     public String getConnectionUrl()
199     {
200         return url;
201     }
202 
203     public void setConnectionUrl( String url )
204     {
205         this.url = url;
206     }
207 
208     public String getUsername()
209     {
210         return username;
211     }
212 
213     public void setUsername( String username )
214     {
215         this.username = username;
216     }
217 
218     public void save( Object obj )
219     {
220         Transaction transac = getSession().beginTransaction();
221         session.save( obj );
222         transac.commit();
223     }
224 
225     public void update( Object obj )
226     {
227         Transaction transac = getSession().beginTransaction();
228         session.update( obj );
229         transac.commit();
230     }
231 
232     public void saveOrUpdate( Object obj )
233     {
234         Transaction transac = getSession().beginTransaction();
235         session.saveOrUpdate( obj );
236         transac.commit();
237     }
238 }