KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > reference > PersistenceContextReference


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.naming.reference;
19
20 import java.util.Map JavaDoc;
21
22 import javax.naming.NameNotFoundException JavaDoc;
23
24 import org.apache.geronimo.kernel.repository.Artifact;
25 import org.apache.geronimo.kernel.Kernel;
26 import org.apache.geronimo.kernel.GBeanNotFoundException;
27 import org.apache.geronimo.gbean.AbstractNameQuery;
28 import org.apache.geronimo.gbean.AbstractName;
29
30 /**
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  */

33 public class PersistenceContextReference extends ConfigurationAwareReference {
34
35     private boolean transactionScoped;
36     private Map JavaDoc properties;
37
38     public PersistenceContextReference(Artifact configId, AbstractNameQuery abstractNameQuery, boolean transactionScoped, Map JavaDoc properties) {
39         super(configId, abstractNameQuery);
40         this.transactionScoped = transactionScoped;
41         this.properties = properties;
42     }
43
44     public String JavaDoc getClassName() {
45         return "javax.persistence.EntityManager";
46     }
47     
48     public Object JavaDoc getContent() throws NameNotFoundException JavaDoc {
49     Kernel kernel = getKernel();
50
51     AbstractName target;
52     try {
53         target = resolveTargetName();
54     } catch (GBeanNotFoundException e) {
55         throw (NameNotFoundException JavaDoc) new NameNotFoundException JavaDoc("Could not resolve name query: " + abstractNameQueries).initCause(e);
56     }
57
58     Object JavaDoc entityManager;
59     try {
60         entityManager = kernel.invoke(target, "getEntityManager", new Object JavaDoc[] {Boolean.valueOf(transactionScoped), properties}, new String JavaDoc[] {boolean.class.getName(), Map JavaDoc.class.getName()});
61     } catch (Exception JavaDoc e) {
62         throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("Could not get entityManager").initCause(e);
63     }
64     if (entityManager == null) {
65         throw new IllegalStateException JavaDoc("entity manager not returned. Target " + target + " not started");
66     }
67     return entityManager;
68 }
69 }
70
Popular Tags