Home » openejb-3.1.2-src » org.apache » openejb » persistence » [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
    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   package org.apache.openejb.persistence;
   18   
   19   import org.apache.openejb.util.Join;
   20   
   21   import java.lang.instrument.ClassFileTransformer;
   22   import java.lang.instrument.IllegalClassFormatException;
   23   import java.net.MalformedURLException;
   24   import java.net.URL;
   25   import java.net.URI;
   26   import java.net.URISyntaxException;
   27   import java.net.URLDecoder;
   28   import java.net.URLEncoder;
   29   import java.security.ProtectionDomain;
   30   import java.util.ArrayList;
   31   import java.util.List;
   32   import java.util.Properties;
   33   import java.io.File;
   34   import java.io.IOException;
   35   import javax.persistence.spi.ClassTransformer;
   36   import javax.persistence.spi.PersistenceUnitInfo;
   37   import javax.persistence.spi.PersistenceUnitTransactionType;
   38   import javax.sql.DataSource;
   39   
   40   public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
   41       /**
   42        * External handler which handles adding a runtime ClassTransformer to the classloader.
   43        */
   44       private final PersistenceClassLoaderHandler persistenceClassLoaderHandler;
   45   
   46       /**
   47        * The unique id of this persistence unit.
   48        */
   49       private String id;
   50   
   51       /**
   52        * Name of this persistence unit.  The JPA specification has restrictions on the
   53        * uniqueness of this name.
   54        */
   55       private String persistenceUnitName;
   56   
   57       /**
   58        * Name of the persistence provider implementation class.
   59        */
   60       private String persistenceProviderClassName;
   61   
   62       /**
   63        * Does this persistence unti participate in JTA transactions or does it manage
   64        * resource local tranactions using the JDBC APIs.
   65        */
   66       private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.JTA;
   67   
   68       /**
   69        * Data source used by jta persistence units for accessing transactional data.
   70        */
   71       private DataSource jtaDataSource;
   72   
   73       /**
   74        * Data source used by non-jta persistence units and by jta persistence units for
   75        * non-transactional operations such as accessing a primary key table or sequence.
   76        */
   77       private DataSource nonJtaDataSource;
   78   
   79       /**
   80        * Names if the entity-mapping.xml files relative to to the persistenceUnitRootUrl.
   81        */
   82       private final List<String> mappingFileNames = new ArrayList<String>();
   83   
   84       /**
   85        * The jar file locations that make up this persistence unit.
   86        */
   87       private final List<URL> jarFileUrls = new ArrayList<URL>();
   88   
   89       /**
   90        * Location of the root of the persistent unit.  The directory in which
   91        * META-INF/persistence.xml is located.
   92        */
   93       private URL persistenceUnitRootUrl;
   94   
   95       /**
   96        * List of the managed entity classes.
   97        */
   98       private final List<String> managedClassNames = new ArrayList<String>();
   99   
  100       /**
  101        * Should class not listed in the persistence unit be managed by the EntityManager?
  102        */
  103       private boolean excludeUnlistedClasses;
  104   
  105       /**
  106        * JPA provider properties for this persistence unit.
  107        */
  108       private Properties properties;
  109   
  110       /**
  111        * Class loader used by JPA to load Entity classes.
  112        */
  113       private ClassLoader classLoader;
  114   
  115       public PersistenceUnitInfoImpl() {
  116           this.persistenceClassLoaderHandler = null;
  117       }
  118   
  119       public PersistenceUnitInfoImpl(PersistenceClassLoaderHandler persistenceClassLoaderHandler) {
  120           this.persistenceClassLoaderHandler = persistenceClassLoaderHandler;
  121       }
  122   
  123       public String getId() {
  124           return id;
  125       }
  126   
  127       public void setId(String id) {
  128           this.id = id;
  129       }
  130   
  131       public String getPersistenceUnitName() {
  132           return persistenceUnitName;
  133       }
  134   
  135       public void setPersistenceUnitName(String persistenceUnitName) {
  136           this.persistenceUnitName = persistenceUnitName;
  137       }
  138   
  139       public String getPersistenceProviderClassName() {
  140           return persistenceProviderClassName;
  141       }
  142   
  143       public void setPersistenceProviderClassName(String persistenceProviderClassName) {
  144           this.persistenceProviderClassName = persistenceProviderClassName;
  145       }
  146   
  147       public PersistenceUnitTransactionType getTransactionType() {
  148           return transactionType;
  149       }
  150   
  151       public void setTransactionType(PersistenceUnitTransactionType transactionType) {
  152           this.transactionType = transactionType;
  153       }
  154   
  155       public DataSource getJtaDataSource() {
  156           return jtaDataSource;
  157       }
  158   
  159       public void setJtaDataSource(DataSource jtaDataSource) {
  160           this.jtaDataSource = jtaDataSource;
  161       }
  162   
  163       public DataSource getNonJtaDataSource() {
  164           return nonJtaDataSource;
  165       }
  166   
  167       public void setNonJtaDataSource(DataSource nonJtaDataSource) {
  168           this.nonJtaDataSource = nonJtaDataSource;
  169       }
  170   
  171       public List<String> getMappingFileNames() {
  172           return mappingFileNames;
  173       }
  174   
  175       public void setMappingFileNames(List<String> mappingFileNames) {
  176           if (mappingFileNames == null) {
  177               throw new NullPointerException("mappingFileNames is null");
  178           }
  179           this.mappingFileNames.clear();
  180           this.mappingFileNames.addAll(mappingFileNames);
  181       }
  182   
  183       public void addMappingFileName(String mappingFileName) {
  184           if (mappingFileName == null) {
  185               throw new NullPointerException("mappingFileName is null");
  186           }
  187           mappingFileNames.add(mappingFileName);
  188       }
  189   
  190       public List<URL> getJarFileUrls() {
  191           return jarFileUrls;
  192       }
  193   
  194       public URL getPersistenceUnitRootUrl() {
  195           return persistenceUnitRootUrl;
  196       }
  197   
  198       public void setRootUrlAndJarUrls(String persistenceUnitRootUrl, List<String> jarFiles) throws MalformedURLException {
  199           File root = new File(persistenceUnitRootUrl);
  200   
  201           this.persistenceUnitRootUrl = toUrl(root);
  202           try {
  203   
  204               for (String path : jarFiles) {
  205                   File file = new File(root, path);
  206                   file = file.getCanonicalFile();
  207                   jarFileUrls.add(toUrl(file));
  208               }
  209           } catch (IOException e) {
  210               throw new IllegalStateException(e);
  211           }
  212       }
  213   
  214       private URL toUrl(File root) throws MalformedURLException {
  215           URL url = root.toURL();
  216   
  217           try {
  218               url.toURI();
  219           } catch (URISyntaxException e) {
  220               // Likely has spaces in it.
  221               try {
  222                   String s = url.toExternalForm();
  223                   URL fixed = new URL(s.replaceAll(" ", "%20"));
  224                   fixed.toURI();
  225                   url = fixed;
  226               } catch (MalformedURLException e1) {
  227               } catch (URISyntaxException e1) {
  228                   // oh well, we tried.
  229               }
  230           }
  231   
  232           return url;
  233       }
  234   
  235       public List<String> getManagedClassNames() {
  236           return managedClassNames;
  237       }
  238   
  239       public void setManagedClassNames(List<String> managedClassNames) {
  240           if (managedClassNames == null) {
  241               throw new NullPointerException("managedClassNames is null");
  242           }
  243           this.managedClassNames.clear();
  244           this.managedClassNames.addAll(managedClassNames);
  245       }
  246   
  247       public void addManagedClassName(String className) {
  248           managedClassNames.add(className);
  249       }
  250   
  251       public boolean excludeUnlistedClasses() {
  252           return excludeUnlistedClasses;
  253       }
  254   
  255       public void setExcludeUnlistedClasses(boolean excludeUnlistedClasses) {
  256           this.excludeUnlistedClasses = excludeUnlistedClasses;
  257       }
  258   
  259       public Properties getProperties() {
  260           return properties;
  261       }
  262   
  263       public void setProperties(Properties properties) {
  264           this.properties = properties;
  265       }
  266   
  267       public ClassLoader getClassLoader() {
  268           return classLoader;
  269       }
  270   
  271       public void setClassLoader(ClassLoader classLoader) {
  272           this.classLoader = classLoader;
  273       }
  274   
  275       public void addTransformer(ClassTransformer classTransformer) {
  276           if (persistenceClassLoaderHandler != null) {
  277               PersistenceClassFileTransformer classFileTransformer = new PersistenceClassFileTransformer(classTransformer);
  278               persistenceClassLoaderHandler.addTransformer(id, classLoader, classFileTransformer);
  279           }
  280       }
  281   
  282       public ClassLoader getNewTempClassLoader() {
  283           if (persistenceClassLoaderHandler != null) {
  284               return persistenceClassLoaderHandler.getNewTempClassLoader(classLoader);
  285           } else {
  286               return null;
  287           }
  288       }
  289   
  290       public static class PersistenceClassFileTransformer implements ClassFileTransformer {
  291           private final ClassTransformer classTransformer;
  292   
  293           public PersistenceClassFileTransformer(ClassTransformer classTransformer) {
  294               this.classTransformer = classTransformer;
  295           }
  296   
  297           public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
  298               // Example code to easily debug transformation of a specific class
  299               // if ("org/apache/openejb/test/entity/cmp/BasicCmpBean".equals(className) ||
  300               //        "org/apache/openejb/test/entity/cmp/BasicCmp2Bean_BasicCmp2Bean".equals(className)) {
  301               //    System.err.println("Loading " + className);
  302               // }
  303               byte[] bytes = classTransformer.transform(classLoader, className.replace('/', '.'), classBeingRedefined, protectionDomain, classfileBuffer);
  304               return bytes;
  305           }
  306       }
  307   }

Home » openejb-3.1.2-src » org.apache » openejb » persistence » [javadoc | source]