View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  
21  package org.apache.geronimo.naming.reference;
22  
23  import javax.naming.NameNotFoundException;
24  
25  import org.apache.geronimo.gbean.AbstractName;
26  import org.apache.geronimo.gbean.AbstractNameQuery;
27  import org.apache.geronimo.kernel.GBeanNotFoundException;
28  import org.apache.geronimo.kernel.Kernel;
29  import org.apache.geronimo.kernel.repository.Artifact;
30  
31  /**
32   * @version $Rev: 471685 $ $Date: 2006-11-06 02:35:00 -0800 (Mon, 06 Nov 2006) $
33   */
34  public class EntityManagerFactoryReference extends ConfigurationAwareReference {
35  
36  
37      public EntityManagerFactoryReference(Artifact configId, AbstractNameQuery abstractNameQuery) {
38          super(configId, abstractNameQuery);
39      }
40  
41      public String getClassName() {
42          return "javax.persistence.EntityManagerFactory";
43      }
44  
45      public Object getContent() throws NameNotFoundException {
46      Kernel kernel = getKernel();
47  
48      AbstractName target;
49      try {
50          target = resolveTargetName();
51      } catch (GBeanNotFoundException e) {
52          throw (NameNotFoundException) new NameNotFoundException("Could not resolve name query: " + abstractNameQueries).initCause(e);
53      }
54  
55      Object entityManagerFactory;
56      try {
57          entityManagerFactory = kernel.invoke(target, "getEntityManagerFactory");
58      } catch (Exception e) {
59          throw (IllegalStateException) new IllegalStateException("Could not get EntityManagerFactory").initCause(e);
60      }
61      if (entityManagerFactory == null) {
62          throw new IllegalStateException("entity manager not returned. Target " + target + " not started");
63      }
64      return entityManagerFactory;
65  }
66  }