1 19 20 package org.apache.cayenne.jpa; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 29 import javax.persistence.spi.ClassTransformer; 30 import javax.persistence.spi.PersistenceUnitInfo; 31 import javax.persistence.spi.PersistenceUnitTransactionType; 32 import javax.sql.DataSource ; 33 34 import org.apache.cayenne.jpa.conf.JpaDataSourceFactory; 35 36 42 public abstract class JpaUnit implements PersistenceUnitInfo { 43 44 protected String persistenceUnitName; 45 protected List <String > mappingFileNames; 46 protected List <URL > jarFileUrls; 47 protected List <String > managedClassNames; 48 protected URL persistenceUnitRootUrl; 49 protected boolean excludeUnlistedClasses; 50 protected Properties properties; 51 protected String description; 52 53 protected ClassLoader classLoader; 55 56 public JpaUnit() { 57 58 this.mappingFileNames = new ArrayList <String >(2); 59 this.jarFileUrls = new ArrayList <URL >(2); 60 this.managedClassNames = new ArrayList <String >(30); 61 this.properties = new Properties (); 62 63 setDefaultClassLoader(); 64 } 65 66 public String getPersistenceUnitName() { 67 return persistenceUnitName; 68 } 69 70 public String getPersistenceProviderClassName() { 71 return getProperty(Provider.PROVIDER_PROPERTY); 72 } 73 74 87 public abstract void addTransformer(ClassTransformer transformer); 88 89 public PersistenceUnitTransactionType getTransactionType() { 90 String type = getProperty(Provider.TRANSACTION_TYPE_PROPERTY); 91 92 return type != null 96 ? PersistenceUnitTransactionType.valueOf(type) 97 : PersistenceUnitTransactionType.JTA; 98 } 99 100 String getProperty(String key) { 101 return properties.getProperty(key); 102 } 103 104 JpaDataSourceFactory getJpaDataSourceFactory() { 105 String factory = getProperty(Provider.DATA_SOURCE_FACTORY_PROPERTY); 106 107 if (factory == null) { 108 throw new JpaProviderException("No value for '" 109 + Provider.DATA_SOURCE_FACTORY_PROPERTY 110 + "' property - can't build DataSource factory."); 111 } 112 113 try { 114 return (JpaDataSourceFactory) Class.forName( 116 factory, 117 true, 118 Thread.currentThread().getContextClassLoader()).newInstance(); 119 } 120 catch (Throwable th) { 121 throw new JpaProviderException("Error instantiating a JPADataSourceFactory: " 122 + factory, th); 123 } 124 } 125 126 public DataSource getJtaDataSource() { 127 String name = getProperty(Provider.JTA_DATA_SOURCE_PROPERTY); 128 return getJpaDataSourceFactory().getJtaDataSource(name, this); 129 } 130 131 public DataSource getNonJtaDataSource() { 132 String name = getProperty(Provider.NON_JTA_DATA_SOURCE_PROPERTY); 133 return getJpaDataSourceFactory().getNonJtaDataSource(name, this); 134 } 135 136 public List <String > getMappingFileNames() { 137 return mappingFileNames; 138 } 139 140 public List <URL > getJarFileUrls() { 141 return jarFileUrls; 142 } 143 144 public URL getPersistenceUnitRootUrl() { 145 return persistenceUnitRootUrl; 146 } 147 148 public List <String > getManagedClassNames() { 149 return managedClassNames; 150 } 151 152 156 public boolean excludeUnlistedClasses() { 157 return excludeUnlistedClasses; 158 } 159 160 public Properties getProperties() { 161 return properties; 162 } 163 164 public ClassLoader getClassLoader() { 165 return classLoader; 166 } 167 168 171 public ClassLoader getNewTempClassLoader() { 172 return new JpaUnitClassLoader(Thread.currentThread().getContextClassLoader()); 173 } 174 175 public void setExcludeUnlistedClasses(boolean excludeUnlistedClasses) { 176 this.excludeUnlistedClasses = excludeUnlistedClasses; 177 } 178 179 public void addJarFileUrl(String jarName) { 180 182 if (persistenceUnitRootUrl == null) { 183 throw new IllegalStateException ("Persistence Unit Root URL is not set"); 184 } 185 186 try { 187 this.jarFileUrls.add(new URL (persistenceUnitRootUrl, jarName)); 188 } 189 catch (MalformedURLException e) { 190 throw new IllegalArgumentException ("Invalid Jar file name:" + jarName, e); 191 } 192 } 193 194 public void setPersistenceUnitName(String persistenceUnitName) { 195 this.persistenceUnitName = persistenceUnitName; 196 } 197 198 201 public void setClassLoader(ClassLoader classLoader) { 202 if (classLoader == null) { 203 setDefaultClassLoader(); 204 } 205 else { 206 this.classLoader = classLoader; 207 } 208 } 209 210 protected void setDefaultClassLoader() { 211 this.classLoader = Thread.currentThread().getContextClassLoader(); 212 } 213 214 public void addManagedClassName(String managedClassName) { 215 this.managedClassNames.add(managedClassName); 216 } 217 218 public void addMappingFileName(String mappingFileName) { 219 this.mappingFileNames.add(mappingFileName); 220 } 221 222 public void setPersistenceUnitRootUrl(URL persistenceUnitRootUrl) { 223 this.persistenceUnitRootUrl = persistenceUnitRootUrl; 224 } 225 226 public void addProperties(Map properties) { 227 this.properties.putAll(properties); 228 } 229 230 public void putProperty(String key, String value) { 231 this.properties.put(key, value); 232 } 233 234 public String getDescription() { 235 return description; 236 } 237 238 public void setDescription(String description) { 239 this.description = description; 240 } 241 242 } 243 | Popular Tags |