KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > connection > EJBQLConnection


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * EJBQLConnection.java
28  *
29  * Created on 4 giugno 2003, 18.15
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34 import it.businesslogic.ireport.*;
35 import it.businesslogic.ireport.util.*;
36 import java.net.URLClassLoader JavaDoc;
37 import java.sql.*;
38 import java.util.HashMap JavaDoc;
39 import java.util.List JavaDoc;
40 import javax.swing.*;
41
42 import javax.persistence.EntityManager;
43 import javax.persistence.EntityManagerFactory;
44 import javax.persistence.Persistence;
45
46 /**
47  *
48  * @author Administrator
49  */

50 public class EJBQLConnection extends it.businesslogic.ireport.IReportConnection {
51     
52     private String JavaDoc name;
53     
54     private java.util.HashMap JavaDoc map = null;
55     private String JavaDoc persistenceUnit;
56     
57     private EntityManager em = null;
58     private EntityManagerFactory emf = null;
59     
60     private int usedby = 0;
61     
62     /** Creates a new instance of JDBCConnection */
63     
64     
65     public EJBQLConnection() {
66         map = new java.util.HashMap JavaDoc();
67     }
68     
69     /** This method return an instanced connection to the database.
70      * If isJDBCConnection() return false => getConnection() return null
71      *
72      */

73     public java.sql.Connection JavaDoc getConnection() {
74             return null;
75     }
76     
77     public boolean isJDBCConnection() {
78         return false;
79     }
80     
81     public boolean isJRDataSource() {
82         return false;
83     }
84     
85     /*
86      * This method return all properties used by this connection
87      */

88     public java.util.HashMap JavaDoc getProperties()
89     {
90         return map;
91     }
92     
93     public void loadProperties(java.util.HashMap JavaDoc map)
94     {
95         this.map = map;
96     }
97     
98     public String JavaDoc getDescription(){ return "EJBQL connection"; }
99     
100     
101     /**
102      * This method return an instanced JRDataDource to the database.
103      * If isJDBCConnection() return true => getJRDataSource() return false
104      */

105     public net.sf.jasperreports.engine.JRDataSource getJRDataSource()
106     {
107         return null;
108     }
109     
110     public EntityManager getEntityManager() throws Exception JavaDoc
111     {
112             if (em == null)
113             {
114                 if (emf == null)
115                 {
116                     ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
117                     if (cl instanceof ReportClassLoader)
118                     {
119                         List JavaDoc items = ((ReportClassLoader)cl).getCachedItems();
120                         
121                         java.net.URL JavaDoc[] urls = new java.net.URL JavaDoc[items.size()];
122                         for (int i=0; i<items.size(); ++i)
123                         {
124                             urls[i] = new java.io.File JavaDoc(""+items.get(i)).toURL();
125                         }
126                         URLClassLoader JavaDoc urlClassLoader = new URLClassLoader JavaDoc(urls, cl );
127                         Thread.currentThread().setContextClassLoader(urlClassLoader );
128                     }
129                     
130                     
131                     emf = Persistence.createEntityManagerFactory(
132                             Misc.nvl(getProperties().get("PersistenceUnit"), null), new HashMap JavaDoc());
133                     //if (emf == null) throw new Exception("Unable to create the EntityManagerFactory for persistence unit " + Misc.nvl(getProperties().get("PersistenceUnit"), null));
134
//Thread.currentThread().setContextClassLoader().
135
}
136                 em = emf.createEntityManager();
137             }
138             usedby ++;
139             return em;
140     }
141     
142     public void closeEntityManager()
143     {
144         try {
145             if (em != null)
146             {
147                 usedby--;
148                 if (usedby == 0)
149                 {
150                     em.close();
151                     em = null;
152                 }
153             }
154         } catch (Exception JavaDoc ex)
155         {
156         }
157     }
158    
159 }
160
Popular Tags