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 com.google.inject.Key;
23  import com.google.inject.TypeLiteral;
24  
25  import javax.inject.Provider;
26  import javax.persistence.EntityManagerFactory;
27  import javax.transaction.UserTransaction;
28  import java.lang.annotation.Annotation;
29  import java.util.Properties;
30  
31  /**
32   * Class holding the configuration for a single persistence unit.
33   */
34  class PersistenceUnitModuleConfiguration
35      implements UnannotatedPersistenceUnitBuilder, AnnotatedPersistenceUnitBuilder, UnconfiguredPersistenceUnitBuilder
36  {
37  
38      private Class<? extends Annotation> annotation;
39  
40      private boolean isJta = false;
41  
42      private UserTransaction userTransaction;
43  
44      private String utJndiName;
45  
46      private Provider<UserTransaction> utProvider;
47  
48      private Key<? extends Provider<UserTransaction>> utProviderKey;
49  
50      private Properties properties;
51  
52      private String puName;
53  
54      private EntityManagerFactory emf;
55  
56      private String emfJndiName;
57  
58      private Provider<EntityManagerFactory> emfProvider;
59  
60      private Key<? extends Provider<EntityManagerFactory>> emfProviderKey;
61  
62      /**
63       * {@inheritDoc}
64       */
65      public AnnotatedPersistenceUnitBuilder annotatedWith( Class<? extends Annotation> annotation )
66      {
67          this.annotation = annotation;
68          return this;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public UnconfiguredPersistenceUnitBuilder useLocalTransaction()
75      {
76          isJta = false;
77          return this;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      public UnconfiguredPersistenceUnitBuilder useGlobalTransaction( UserTransaction userTransaction )
84      {
85          this.isJta = true;
86          this.userTransaction = userTransaction;
87          return this;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public UnconfiguredPersistenceUnitBuilder useGlobalTransactionWithJndiName( String utJndiName )
94      {
95          this.isJta = true;
96          this.utJndiName = utJndiName;
97          return this;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     public UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy( Provider<UserTransaction> utProvider )
104     {
105         this.isJta = true;
106         this.utProvider = utProvider;
107         return this;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy(
114         Class<? extends Provider<UserTransaction>> utProviderClass )
115     {
116         return useGlobalTransactionProvidedBy( Key.get( utProviderClass ) );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     public UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy(
123         TypeLiteral<? extends Provider<UserTransaction>> utProviderType )
124     {
125         return useGlobalTransactionProvidedBy( Key.get( utProviderType ) );
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     public UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy(
132         Key<? extends Provider<UserTransaction>> utProviderKey )
133     {
134         this.isJta = true;
135         this.utProviderKey = utProviderKey;
136         return this;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public void setProperties( Properties properties )
143     {
144         this.properties = properties;
145     }
146 
147     void setPuName( String puName )
148     {
149         this.puName = puName;
150     }
151 
152     void setEmf( EntityManagerFactory emf )
153     {
154         this.emf = emf;
155     }
156 
157     void setEmfJndiName( String emfJndiName )
158     {
159         this.emfJndiName = emfJndiName;
160     }
161 
162     void setEmfProvider( Provider<EntityManagerFactory> emfProvider )
163     {
164         this.emfProvider = emfProvider;
165     }
166 
167     void setEmfProviderClass( Class<? extends Provider<EntityManagerFactory>> emfProviderClass )
168     {
169         this.emfProviderKey = Key.get( emfProviderClass );
170     }
171 
172     void setEmfProviderType( TypeLiteral<? extends Provider<EntityManagerFactory>> emfProviderType )
173     {
174         this.emfProviderKey = Key.get( emfProviderType );
175     }
176 
177     void setEmfProviderKey( Key<? extends Provider<EntityManagerFactory>> emfProviderKey )
178     {
179         this.emfProviderKey = emfProviderKey;
180     }
181 
182     boolean isApplicationManagedPersistenceUnit()
183     {
184         return puName != null;
185     }
186 
187 
188     UserTransaction getUserTransaction()
189     {
190         return userTransaction;
191     }
192 
193     String getUtJndiName()
194     {
195         return utJndiName;
196     }
197 
198     Provider<UserTransaction> getUtProvider()
199     {
200         return utProvider;
201     }
202 
203     Key<? extends Provider<UserTransaction>> getUtProviderKey()
204     {
205         return utProviderKey;
206     }
207 
208     Properties getProperties()
209     {
210         return properties;
211     }
212 
213     String getPuName()
214     {
215         return puName;
216     }
217 
218     EntityManagerFactory getEmf()
219     {
220         return emf;
221     }
222 
223     String getEmfJndiName()
224     {
225         return emfJndiName;
226     }
227 
228     Provider<EntityManagerFactory> getEmfProvider()
229     {
230         return emfProvider;
231     }
232 
233     Key<? extends Provider<EntityManagerFactory>> getEmfProviderKey()
234     {
235         return emfProviderKey;
236     }
237 
238     boolean isEmfProvidedByJndiLookup()
239     {
240         return emfJndiName != null;
241     }
242 
243     boolean isEmfProvidedByInstance()
244     {
245         return emf != null;
246     }
247 
248     boolean isEmfProvidedByProvider()
249     {
250         return emfProvider != null;
251     }
252 
253     boolean isEmfProvidedByProviderKey()
254     {
255         return emfProviderKey != null;
256     }
257 
258     boolean isJta()
259     {
260         return isJta;
261     }
262 
263     boolean isUserTransactionProvidedByJndiLookup()
264     {
265         return utJndiName != null;
266     }
267 
268 
269     boolean isUserTransactionProvidedByInstance()
270     {
271         return userTransaction != null;
272     }
273 
274     boolean isUserTransactionProvidedByProvider()
275     {
276         return utProvider != null;
277     }
278 
279     boolean isUserTransactionProvidedByProviderKey()
280     {
281         return utProviderKey != null;
282     }
283 
284     boolean isAnnotated()
285     {
286         return annotation != null;
287     }
288 
289     AnnotationHolder getAnnotationHolder()
290     {
291         return new AnnotationHolder( annotation );
292     }
293 
294     Class<? extends Annotation> getAnnotation()
295     {
296         return annotation;
297     }
298 
299 }