1 package org.apache.onami.persist;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
24 import javax.persistence.EntityManagerFactory;
25 import javax.persistence.Persistence;
26 import java.util.Properties;
27
28 import static org.apache.onami.persist.Preconditions.checkNotNull;
29
30 /**
31 * Factory for {@link EntityManagerFactory}.
32 */
33 @Singleton
34 class EntityManagerFactoryFactory
35 {
36
37 /**
38 * Name of the persistence unit as defined in the persistence.xml.
39 */
40 private final String puName;
41
42 /**
43 * Additional properties. Theses override the ones defined in the persistence.xml.
44 */
45 private final Properties properties;
46
47 /**
48 * Constructor.
49 *
50 * @param puName the name of the persistence unit as defined in the persistence.xml. Must not be {@code null}.
51 * @param properties the additional properties. Theses override the ones defined in the persistence.xml.
52 * Must not be {@code null}.
53 */
54 @Inject
55 EntityManagerFactoryFactory( @ForApplicationManaged String puName,
56 @Nullable @ForApplicationManaged Properties properties )
57 {
58 this.puName = checkNotNull( puName, "puName is mandatory!" );
59 this.properties = properties;
60 }
61
62 /**
63 * Creates a new {@link EntityManagerFactory}.
64 *
65 * @return the newly created entity manager factory.
66 */
67 EntityManagerFactory createApplicationManagedEntityManagerFactory()
68 {
69 return Persistence.createEntityManagerFactory( puName, properties );
70 }
71
72 }