View Javadoc
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  
26  import static org.apache.onami.persist.Preconditions.checkNotNull;
27  
28  /**
29   * Source for {@link javax.persistence.EntityManagerFactory}.
30   * The sourced instance is looked up via a JNDI call.
31   */
32  @Singleton
33  class EntityManagerFactorySourceByJndiLookup
34      implements EntityManagerFactorySource
35  {
36  
37      /**
38       * The JNDI name of the persistence unit.
39       */
40      private final String jndiName;
41  
42      /**
43       * Helper for JNDI lookup.
44       */
45      private final JndiLookupHelper jndiLookupHelper;
46  
47      /**
48       * Constructor.
49       *
50       * @param jndiName         jndi name of the entity manager factory. Must not be {@code null}.
51       * @param jndiLookupHelper the lookup helper. Must not be {@code null}.
52       */
53      @Inject
54      EntityManagerFactorySourceByJndiLookup( @ForContainerManaged String jndiName, JndiLookupHelper jndiLookupHelper )
55      {
56          this.jndiName = checkNotNull( jndiName, "jndiName is mandatory!" );
57          this.jndiLookupHelper = checkNotNull( jndiLookupHelper, "jndiLookupHelper is mandatory!" );
58      }
59  
60      /**
61       * Gets a {@link javax.persistence.EntityManagerFactory} by looking it up in the JNDI context.
62       *
63       * @return the found entity manager factory
64       * @throws RuntimeException when no entity manager factory was found.
65       */
66      //@Override
67      public EntityManagerFactory getEntityManagerFactory()
68      {
69          return jndiLookupHelper.doJndiLookup( EntityManagerFactory.class, jndiName );
70      }
71  
72  }