001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.orm.jpa.persistenceunit; 018 019import java.net.URL; 020import java.util.ArrayList; 021import java.util.List; 022import java.util.Properties; 023 024import javax.persistence.SharedCacheMode; 025import javax.persistence.ValidationMode; 026import javax.persistence.spi.ClassTransformer; 027import javax.persistence.spi.PersistenceUnitTransactionType; 028import javax.sql.DataSource; 029 030import org.springframework.lang.Nullable; 031import org.springframework.util.Assert; 032import org.springframework.util.ClassUtils; 033 034/** 035 * Spring's base implementation of the JPA 036 * {@link javax.persistence.spi.PersistenceUnitInfo} interface, 037 * used to bootstrap an {@code EntityManagerFactory} in a container. 038 * 039 * <p>This implementation is largely a JavaBean, offering mutators 040 * for all standard {@code PersistenceUnitInfo} properties. 041 * 042 * @author Rod Johnson 043 * @author Juergen Hoeller 044 * @author Costin Leau 045 * @since 2.0 046 */ 047public class MutablePersistenceUnitInfo implements SmartPersistenceUnitInfo { 048 049 @Nullable 050 private String persistenceUnitName; 051 052 @Nullable 053 private String persistenceProviderClassName; 054 055 @Nullable 056 private PersistenceUnitTransactionType transactionType; 057 058 @Nullable 059 private DataSource nonJtaDataSource; 060 061 @Nullable 062 private DataSource jtaDataSource; 063 064 private final List<String> mappingFileNames = new ArrayList<>(); 065 066 private final List<URL> jarFileUrls = new ArrayList<>(); 067 068 @Nullable 069 private URL persistenceUnitRootUrl; 070 071 private final List<String> managedClassNames = new ArrayList<>(); 072 073 private final List<String> managedPackages = new ArrayList<>(); 074 075 private boolean excludeUnlistedClasses = false; 076 077 private SharedCacheMode sharedCacheMode = SharedCacheMode.UNSPECIFIED; 078 079 private ValidationMode validationMode = ValidationMode.AUTO; 080 081 private Properties properties = new Properties(); 082 083 private String persistenceXMLSchemaVersion = "2.0"; 084 085 @Nullable 086 private String persistenceProviderPackageName; 087 088 089 public void setPersistenceUnitName(@Nullable String persistenceUnitName) { 090 this.persistenceUnitName = persistenceUnitName; 091 } 092 093 @Override 094 @Nullable 095 public String getPersistenceUnitName() { 096 return this.persistenceUnitName; 097 } 098 099 public void setPersistenceProviderClassName(@Nullable String persistenceProviderClassName) { 100 this.persistenceProviderClassName = persistenceProviderClassName; 101 } 102 103 @Override 104 @Nullable 105 public String getPersistenceProviderClassName() { 106 return this.persistenceProviderClassName; 107 } 108 109 public void setTransactionType(PersistenceUnitTransactionType transactionType) { 110 this.transactionType = transactionType; 111 } 112 113 @Override 114 public PersistenceUnitTransactionType getTransactionType() { 115 if (this.transactionType != null) { 116 return this.transactionType; 117 } 118 else { 119 return (this.jtaDataSource != null ? 120 PersistenceUnitTransactionType.JTA : PersistenceUnitTransactionType.RESOURCE_LOCAL); 121 } 122 } 123 124 public void setJtaDataSource(@Nullable DataSource jtaDataSource) { 125 this.jtaDataSource = jtaDataSource; 126 } 127 128 @Override 129 @Nullable 130 public DataSource getJtaDataSource() { 131 return this.jtaDataSource; 132 } 133 134 public void setNonJtaDataSource(@Nullable DataSource nonJtaDataSource) { 135 this.nonJtaDataSource = nonJtaDataSource; 136 } 137 138 @Override 139 @Nullable 140 public DataSource getNonJtaDataSource() { 141 return this.nonJtaDataSource; 142 } 143 144 public void addMappingFileName(String mappingFileName) { 145 this.mappingFileNames.add(mappingFileName); 146 } 147 148 @Override 149 public List<String> getMappingFileNames() { 150 return this.mappingFileNames; 151 } 152 153 public void addJarFileUrl(URL jarFileUrl) { 154 this.jarFileUrls.add(jarFileUrl); 155 } 156 157 @Override 158 public List<URL> getJarFileUrls() { 159 return this.jarFileUrls; 160 } 161 162 public void setPersistenceUnitRootUrl(@Nullable URL persistenceUnitRootUrl) { 163 this.persistenceUnitRootUrl = persistenceUnitRootUrl; 164 } 165 166 @Override 167 @Nullable 168 public URL getPersistenceUnitRootUrl() { 169 return this.persistenceUnitRootUrl; 170 } 171 172 /** 173 * Add a managed class name to the persistence provider's metadata. 174 * @see javax.persistence.spi.PersistenceUnitInfo#getManagedClassNames() 175 * @see #addManagedPackage 176 */ 177 public void addManagedClassName(String managedClassName) { 178 this.managedClassNames.add(managedClassName); 179 } 180 181 @Override 182 public List<String> getManagedClassNames() { 183 return this.managedClassNames; 184 } 185 186 /** 187 * Add a managed package to the persistence provider's metadata. 188 * <p>Note: This refers to annotated {@code package-info.java} files. It does 189 * <i>not</i> trigger entity scanning in the specified package; this is 190 * rather the job of {@link DefaultPersistenceUnitManager#setPackagesToScan}. 191 * @since 4.1 192 * @see SmartPersistenceUnitInfo#getManagedPackages() 193 * @see #addManagedClassName 194 */ 195 public void addManagedPackage(String packageName) { 196 this.managedPackages.add(packageName); 197 } 198 199 @Override 200 public List<String> getManagedPackages() { 201 return this.managedPackages; 202 } 203 204 public void setExcludeUnlistedClasses(boolean excludeUnlistedClasses) { 205 this.excludeUnlistedClasses = excludeUnlistedClasses; 206 } 207 208 @Override 209 public boolean excludeUnlistedClasses() { 210 return this.excludeUnlistedClasses; 211 } 212 213 public void setSharedCacheMode(SharedCacheMode sharedCacheMode) { 214 this.sharedCacheMode = sharedCacheMode; 215 } 216 217 @Override 218 public SharedCacheMode getSharedCacheMode() { 219 return this.sharedCacheMode; 220 } 221 222 public void setValidationMode(ValidationMode validationMode) { 223 this.validationMode = validationMode; 224 } 225 226 @Override 227 public ValidationMode getValidationMode() { 228 return this.validationMode; 229 } 230 231 public void addProperty(String name, String value) { 232 this.properties.setProperty(name, value); 233 } 234 235 public void setProperties(Properties properties) { 236 Assert.notNull(properties, "Properties must not be null"); 237 this.properties = properties; 238 } 239 240 @Override 241 public Properties getProperties() { 242 return this.properties; 243 } 244 245 public void setPersistenceXMLSchemaVersion(String persistenceXMLSchemaVersion) { 246 this.persistenceXMLSchemaVersion = persistenceXMLSchemaVersion; 247 } 248 249 @Override 250 public String getPersistenceXMLSchemaVersion() { 251 return this.persistenceXMLSchemaVersion; 252 } 253 254 @Override 255 public void setPersistenceProviderPackageName(@Nullable String persistenceProviderPackageName) { 256 this.persistenceProviderPackageName = persistenceProviderPackageName; 257 } 258 259 @Nullable 260 public String getPersistenceProviderPackageName() { 261 return this.persistenceProviderPackageName; 262 } 263 264 265 /** 266 * This implementation returns the default ClassLoader. 267 * @see org.springframework.util.ClassUtils#getDefaultClassLoader() 268 */ 269 @Override 270 @Nullable 271 public ClassLoader getClassLoader() { 272 return ClassUtils.getDefaultClassLoader(); 273 } 274 275 /** 276 * This implementation throws an UnsupportedOperationException. 277 */ 278 @Override 279 public void addTransformer(ClassTransformer classTransformer) { 280 throw new UnsupportedOperationException("addTransformer not supported"); 281 } 282 283 /** 284 * This implementation throws an UnsupportedOperationException. 285 */ 286 @Override 287 public ClassLoader getNewTempClassLoader() { 288 throw new UnsupportedOperationException("getNewTempClassLoader not supported"); 289 } 290 291 292 @Override 293 public String toString() { 294 return "PersistenceUnitInfo: name '" + this.persistenceUnitName + 295 "', root URL [" + this.persistenceUnitRootUrl + "]"; 296 } 297 298}