KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > conf > UnitLoader


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

19
20 package org.apache.cayenne.jpa.conf;
21
22 import java.net.URL JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import org.apache.cayenne.jpa.JpaUnit;
27 import org.xml.sax.InputSource JavaDoc;
28
29 /**
30  * A class that locates persistent units in the environment and loads their definitions.
31  *
32  * @author Andrus Adamchik
33  */

34 public class UnitLoader {
35
36     static final String JavaDoc DESCRIPTOR_LOCATION = "META-INF/persistence.xml";
37
38     protected UnitDescriptorParser parser;
39
40     public UnitLoader(boolean validateDescriptors) {
41         try {
42             this.parser = new UnitDescriptorParser(validateDescriptors);
43         }
44         catch (Exception JavaDoc e) {
45             throw new RuntimeException JavaDoc("Error creating XML parser", e);
46         }
47     }
48
49     /**
50      * Loads PersistenceUnitInfo from standard locations. Returns null if no persistent
51      * unit with requested name is found.
52      * <p>
53      * <i>Implementation note: the loader performs no local caching of unit data. It will
54      * scan all available peristence unit descriptors every time this method is called.</i>
55      * </p>
56      */

57     public JpaUnit loadUnit(String JavaDoc persistenceUnitName) {
58
59         if (persistenceUnitName == null) {
60             throw new IllegalArgumentException JavaDoc("Null persistenceUnitName");
61         }
62
63         // load descriptors
64
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
65         try {
66             Enumeration JavaDoc<URL JavaDoc> descriptors = loader.getResources(DESCRIPTOR_LOCATION);
67             while (descriptors.hasMoreElements()) {
68
69                 String JavaDoc descriptorURL = descriptors.nextElement().toExternalForm();
70
71                 // determine descriptor "root"
72

73                 String JavaDoc descriptorRoot = descriptorURL.substring(0, descriptorURL.length()
74                         - DESCRIPTOR_LOCATION.length());
75
76                 Collection JavaDoc<JpaUnit> units = parser.getPersistenceUnits(new InputSource JavaDoc(
77                         descriptorURL), new URL JavaDoc(descriptorRoot));
78
79                 for (JpaUnit unit : units) {
80                     if (persistenceUnitName.equals(unit.getPersistenceUnitName())) {
81                         return unit;
82                     }
83                 }
84             }
85         }
86         catch (Exception JavaDoc e) {
87             // throw on bad unit
88
throw new RuntimeException JavaDoc("Error loading persistence descriptors", e);
89         }
90
91         return null;
92     }
93 }
94
Popular Tags