forked from jakartaee/persistence
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
365 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
320 changes: 320 additions & 0 deletions
320
api/src/main/java/jakarta/persistence/PersistenceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,320 @@ | ||
/* | ||
* Copyright (c) 2008, 2020 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
// Contributors: | ||
// Gavin King - 3.2 | ||
package jakarta.persistence; | ||
|
||
import javax.sql.DataSource; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents a configuration of a persistence unit, allowing programmatic | ||
* creation of an {@link EntityManagerFactory}. The configuration options | ||
* available via this API reflect the similarly-named elements of the | ||
* {@code persistence.xml} file. | ||
* | ||
* <p>This API may not be used to configure a container-managed persistence | ||
* unit. That is, the configured persistence unit should be considered a | ||
* Java SE persistence unit, even when this API is used within the Jakarta | ||
* EE environment. | ||
* | ||
* <p>If injection of the {@link EntityManagerFactory} is required, a CDI | ||
* {@code Producer} may be used to make the {@link EntityManagerFactory} | ||
* available as a CDI managed bean. | ||
* | ||
* <pre> | ||
* @Produces @ApplicationScoped @Documents | ||
* EntityManagerFactory configure() { | ||
* return new PersistenceConfiguration() | ||
* .name("DocumentData") | ||
* .nonJtaDataSource(datasource) | ||
* .managedClass(Document.class) | ||
* .createEntityManagerFactory(); | ||
* } | ||
* </pre> | ||
* | ||
* <p>Similarly, if injection of an {@link EntityManager} is required, | ||
* a CDI {@code Producer} method/{@code Disposer} method pair may be | ||
* used to make the {@link EntityManager} available as a CDI managed | ||
* bean. | ||
* | ||
* <pre> | ||
* @Produces @TransactionScoped @Documents | ||
* EntityManager create(@Documents EntityManagerFactory factory) { | ||
* return factory.createEntityManager(); | ||
* } | ||
* | ||
* void close(@Disposes @Documents EntityManager entityManager) { | ||
* entityManager.close(); | ||
* } | ||
* </pre> | ||
* | ||
* @see Persistence#createEntityManagerFactory(PersistenceConfiguration) | ||
* | ||
* @since 3.2 | ||
*/ | ||
public class PersistenceConfiguration { | ||
|
||
private String name; | ||
private String provider; | ||
private DataSource jtaDataSource; | ||
private DataSource nonJtaDataSource; | ||
// private URL persistenceUnitRootUrl; | ||
|
||
private SharedCacheMode sharedCacheMode = SharedCacheMode.UNSPECIFIED; | ||
private ValidationMode validationMode = ValidationMode.AUTO; | ||
private TransactionType transactionType = TransactionType.RESOURCE_LOCAL; | ||
// private boolean excludeUnlistedClasses = false; | ||
|
||
private List<URL> jarFileUrls = new ArrayList<>(); | ||
private List<Class<?>> managedClasses = new ArrayList<>(); | ||
private List<String> mappingFileNames = new ArrayList<>(); | ||
private Map<String,Object> properties = new HashMap<>(); | ||
|
||
public enum TransactionType { JTA, RESOURCE_LOCAL } | ||
|
||
/** | ||
* Create a new {@link EntityManagerFactory} based on this configuration. | ||
* @throws IllegalStateException if required configuration is missing | ||
*/ | ||
public EntityManagerFactory createEntityManagerFactory() { | ||
return Persistence.createEntityManagerFactory(this); | ||
} | ||
|
||
/** | ||
* Specify the name of the persistence unit. | ||
*/ | ||
public PersistenceConfiguration name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the name of the persistence unit. | ||
*/ | ||
public String name() { | ||
return name; | ||
} | ||
|
||
// /** | ||
// * Specify a root URL for the persistence unit. | ||
// * @param url the URL of a directory or JAR archive | ||
// * @return this configuration | ||
// */ | ||
// public PersistenceConfiguration persistenceUnitRootUrl(URL url) { | ||
// persistenceUnitRootUrl = url; | ||
// return this; | ||
// } | ||
// | ||
// /** | ||
// * @return the root URL for the persistence unit, if any | ||
// */ | ||
// public URL persistenceUnitRootUrl() { | ||
// return persistenceUnitRootUrl; | ||
// } | ||
|
||
/** | ||
* Specify the persistence provider. | ||
* @param providerClassName the qualified name of the persistence provider class | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration provider(String providerClassName) { | ||
this.provider = providerClassName; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the qualified name of the persistence provider class. | ||
*/ | ||
public String provider() { | ||
return provider; | ||
} | ||
|
||
/** | ||
* Specify a JTA {@link DataSource}. | ||
* @param dataSource the JTA datasource | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration jtaDataSource(DataSource dataSource) { | ||
this.jtaDataSource = dataSource; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the configured JTA datasource, if any | ||
*/ | ||
public DataSource jtaDataSource() { | ||
return jtaDataSource; | ||
} | ||
|
||
/** | ||
* Specify a non-JTA {@link DataSource}. | ||
* @param dataSource the non-JTA datasource | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration nonJtaDataSource(DataSource dataSource) { | ||
this.nonJtaDataSource = dataSource; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the configured non-JTA datasource, if any | ||
*/ | ||
public DataSource nonJtaDataSource() { | ||
return nonJtaDataSource; | ||
} | ||
|
||
/** | ||
* Add a managed class (an {@link Entity}, {@link Embeddable}, or | ||
* {@link MappedSuperclass}) to the configuration. | ||
* @param managedClass the managed class | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration managedClass(Class<?> managedClass) { | ||
managedClasses.add(managedClass); | ||
return this; | ||
} | ||
|
||
/** | ||
* @return all configured managed classes | ||
*/ | ||
public List<Class<?>> managedClasses() { | ||
return managedClasses; | ||
} | ||
|
||
/** | ||
* Add an {@code orm.xml} mapping file name to the configuration. | ||
* @param name the file path of the mapping file | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration mappingFile(String name) { | ||
mappingFileNames.add(name); | ||
return this; | ||
} | ||
|
||
/** | ||
* @return all configured mapping file names | ||
*/ | ||
public List<String> mappingFiles() { | ||
return mappingFileNames; | ||
} | ||
|
||
/** | ||
* Add a JAR archive to the configuration. | ||
* @param url the URL of the JAR archive | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration jarFileUrl(URL url) { | ||
jarFileUrls.add(url); | ||
return this; | ||
} | ||
|
||
/** | ||
* @return all configured JAR archives | ||
*/ | ||
public List<URL> jarFileUrls() { | ||
return jarFileUrls; | ||
} | ||
|
||
/** | ||
* Specify the transaction type for the persistence unit. | ||
* @param transactionType the transaction type | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration transactionType(TransactionType transactionType) { | ||
this.transactionType = transactionType; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the transaction type | ||
*/ | ||
public TransactionType transactionType() { | ||
return transactionType; | ||
} | ||
|
||
/** | ||
* Specify the shared cache mode for the persistence unit. | ||
* @param sharedCacheMode the shared cache mode | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration sharedCacheMode(SharedCacheMode sharedCacheMode) { | ||
this.sharedCacheMode = sharedCacheMode; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the shared cache mode | ||
*/ | ||
public SharedCacheMode sharedCacheMode() { | ||
return sharedCacheMode; | ||
} | ||
|
||
/** | ||
* Specify the validation mode for the persistence unit. | ||
* @param validationMode the shared cache mode | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration validationMode(ValidationMode validationMode) { | ||
this.validationMode = validationMode; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the validation mode | ||
*/ | ||
public ValidationMode validationMode() { | ||
return validationMode; | ||
} | ||
|
||
// public PersistenceConfiguration excludeUnlistedClasses(boolean excludeUnlistedClasses) { | ||
// this.excludeUnlistedClasses = excludeUnlistedClasses; | ||
// return this; | ||
// } | ||
// | ||
// public boolean excludeUnlistedClasses() { | ||
// return excludeUnlistedClasses; | ||
// } | ||
|
||
/** | ||
* Set a property of this persistence unit. | ||
* @param name the property name | ||
* @param value the property value | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration property(String name, Object value) { | ||
properties.put(name, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* Set multiple properties of this persistence unit. | ||
* @param properties the properties | ||
* @return this configuration | ||
*/ | ||
public PersistenceConfiguration properties(Map<String,?> properties) { | ||
this.properties.putAll(properties); | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the configured properties | ||
*/ | ||
public Map<String, Object> properties() { | ||
return properties; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters