001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.naming.reference;
020    
021    import java.util.Map;
022    
023    import javax.naming.NameNotFoundException;
024    
025    import org.apache.geronimo.kernel.repository.Artifact;
026    import org.apache.geronimo.kernel.Kernel;
027    import org.apache.geronimo.kernel.GBeanNotFoundException;
028    import org.apache.geronimo.gbean.AbstractNameQuery;
029    import org.apache.geronimo.gbean.AbstractName;
030    
031    /**
032     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
033     */
034    public class PersistenceContextReference extends ConfigurationAwareReference {
035    
036        private boolean transactionScoped;
037        private Map properties;
038    
039        public PersistenceContextReference(Artifact configId, AbstractNameQuery abstractNameQuery, boolean transactionScoped, Map properties) {
040            super(configId, abstractNameQuery);
041            this.transactionScoped = transactionScoped;
042            this.properties = properties;
043        }
044    
045        public String getClassName() {
046            return "javax.persistence.EntityManager";
047        }
048        
049        public Object getContent() throws NameNotFoundException {
050        Kernel kernel = getKernel();
051    
052        AbstractName target;
053        try {
054            target = resolveTargetName();
055        } catch (GBeanNotFoundException e) {
056            throw (NameNotFoundException) new NameNotFoundException("Could not resolve name query: " + abstractNameQueries).initCause(e);
057        }
058    
059        Object entityManager;
060        try {
061            entityManager = kernel.invoke(target, "getEntityManager", new Object[] {Boolean.valueOf(transactionScoped), properties}, new String[] {boolean.class.getName(), Map.class.getName()});
062        } catch (Exception e) {
063            throw (IllegalStateException) new IllegalStateException("Could not get entityManager").initCause(e);
064        }
065        if (entityManager == null) {
066            throw new IllegalStateException("entity manager not returned. Target " + target + " not started");
067        }
068        return entityManager;
069    }
070    }