Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.spi.ee.openejb.jpa » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.spi.ee.openejb.jpa;
   15   
   16   import javax.enterprise.inject.CreationException;
   17   import javax.naming.Context;
   18   import javax.naming.NameClassPair;
   19   import javax.naming.NamingEnumeration;
   20   import javax.naming.NamingException;
   21   import javax.persistence.EntityManager;
   22   import javax.persistence.EntityManagerFactory;
   23   
   24   import org.apache.openejb.loader.SystemInstance;
   25   import org.apache.openejb.persistence.JtaEntityManager;
   26   import org.apache.openejb.persistence.JtaEntityManagerRegistry;
   27   
   28   public class JPAServiceOpenEJBImpl
   29   {
   30       private Context context;
   31       
   32       public JPAServiceOpenEJBImpl(Context context)
   33       {
   34           this.context = context;
   35           
   36       }
   37       
   38       public  EntityManager getPersistenceContext( String unitName) {
   39           // get JtaEntityManagerRegistry
   40           JtaEntityManagerRegistry jtaEntityManagerRegistry = SystemInstance.get().getComponent(JtaEntityManagerRegistry.class);
   41   
   42           EntityManagerFactory factory = getPersistenceUnit( unitName );
   43   
   44           JtaEntityManager jtaEntityManager = new JtaEntityManager(jtaEntityManagerRegistry, factory, null, false);
   45   
   46           return jtaEntityManager;
   47       }
   48   
   49       public  EntityManagerFactory getPersistenceUnit(String unitName) {
   50           EntityManagerFactory factory;
   51           try {
   52   
   53               NamingEnumeration<NameClassPair> persUnits = context.list("java:openejb/PersistenceUnit");
   54               
   55               if (persUnits == null)
   56               {
   57                   throw new CreationException( "No PersistenceUnit found in java:openejb/PersistenceUnit!" );
   58               }
   59   
   60               String shortestMatch = null;
   61               
   62               while (persUnits.hasMore())
   63               {
   64                   NameClassPair puNc = persUnits.next();
   65                   
   66                   if (puNc.getName().startsWith(unitName))
   67                   {
   68                       if (shortestMatch == null || shortestMatch.length() > puNc.getName().length())
   69                       {
   70                           shortestMatch = puNc.getName();
   71                       }
   72                   }
   73                   
   74               }
   75               
   76               if (shortestMatch == null)
   77               {
   78                   throw new CreationException("PersistenceUnit '" + unitName + "' not found");
   79               }
   80               
   81               factory = (EntityManagerFactory) context.lookup("java:openejb/PersistenceUnit/" + shortestMatch);
   82               
   83               
   84           } catch (NamingException e) {
   85               throw new CreationException("PersistenceUnit '" + unitName + "' not found", e );
   86           }
   87           return factory;
   88       }
   89   
   90   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.spi.ee.openejb.jpa » [javadoc | source]