001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with 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,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.naming.reference;
022    
023    import javax.naming.NameNotFoundException;
024    
025    import org.apache.geronimo.gbean.AbstractName;
026    import org.apache.geronimo.gbean.AbstractNameQuery;
027    import org.apache.geronimo.kernel.GBeanNotFoundException;
028    import org.apache.geronimo.kernel.Kernel;
029    import org.apache.geronimo.kernel.repository.Artifact;
030    
031    /**
032     * @version $Rev: 471685 $ $Date: 2006-11-06 02:35:00 -0800 (Mon, 06 Nov 2006) $
033     */
034    public class EntityManagerFactoryReference extends ConfigurationAwareReference {
035    
036    
037        public EntityManagerFactoryReference(Artifact configId, AbstractNameQuery abstractNameQuery) {
038            super(configId, abstractNameQuery);
039        }
040    
041        public String getClassName() {
042            return "javax.persistence.EntityManagerFactory";
043        }
044    
045        public Object getContent() throws NameNotFoundException {
046        Kernel kernel = getKernel();
047    
048        AbstractName target;
049        try {
050            target = resolveTargetName();
051        } catch (GBeanNotFoundException e) {
052            throw (NameNotFoundException) new NameNotFoundException("Could not resolve name query: " + abstractNameQueries).initCause(e);
053        }
054    
055        Object entityManagerFactory;
056        try {
057            entityManagerFactory = kernel.invoke(target, "getEntityManagerFactory");
058        } catch (Exception e) {
059            throw (IllegalStateException) new IllegalStateException("Could not get EntityManagerFactory").initCause(e);
060        }
061        if (entityManagerFactory == null) {
062            throw new IllegalStateException("entity manager not returned. Target " + target + " not started");
063        }
064        return entityManagerFactory;
065    }
066    }