1 32 33 package it.businesslogic.ireport.connection; 34 import it.businesslogic.ireport.*; 35 import it.businesslogic.ireport.util.*; 36 import java.net.URLClassLoader ; 37 import java.sql.*; 38 import java.util.HashMap ; 39 import java.util.List ; 40 import javax.swing.*; 41 42 import javax.persistence.EntityManager; 43 import javax.persistence.EntityManagerFactory; 44 import javax.persistence.Persistence; 45 46 50 public class EJBQLConnection extends it.businesslogic.ireport.IReportConnection { 51 52 private String name; 53 54 private java.util.HashMap map = null; 55 private String persistenceUnit; 56 57 private EntityManager em = null; 58 private EntityManagerFactory emf = null; 59 60 private int usedby = 0; 61 62 63 64 65 public EJBQLConnection() { 66 map = new java.util.HashMap (); 67 } 68 69 73 public java.sql.Connection 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 88 public java.util.HashMap getProperties() 89 { 90 return map; 91 } 92 93 public void loadProperties(java.util.HashMap map) 94 { 95 this.map = map; 96 } 97 98 public String getDescription(){ return "EJBQL connection"; } 99 100 101 105 public net.sf.jasperreports.engine.JRDataSource getJRDataSource() 106 { 107 return null; 108 } 109 110 public EntityManager getEntityManager() throws Exception 111 { 112 if (em == null) 113 { 114 if (emf == null) 115 { 116 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 117 if (cl instanceof ReportClassLoader) 118 { 119 List items = ((ReportClassLoader)cl).getCachedItems(); 120 121 java.net.URL [] urls = new java.net.URL [items.size()]; 122 for (int i=0; i<items.size(); ++i) 123 { 124 urls[i] = new java.io.File (""+items.get(i)).toURL(); 125 } 126 URLClassLoader urlClassLoader = new URLClassLoader (urls, cl ); 127 Thread.currentThread().setContextClassLoader(urlClassLoader ); 128 } 129 130 131 emf = Persistence.createEntityManagerFactory( 132 Misc.nvl(getProperties().get("PersistenceUnit"), null), new HashMap ()); 133 } 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 ex) 155 { 156 } 157 } 158 159 } 160 | Popular Tags |