KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > persistence > DuplicatePUNameTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.enterprise.tools.verifier.tests.persistence;
26
27 import com.sun.enterprise.deployment.Descriptor;
28 import com.sun.enterprise.deployment.PersistenceUnitDescriptor;
29 import com.sun.enterprise.deployment.PersistenceUnitsDescriptor;
30 import com.sun.enterprise.tools.verifier.Result;
31 import com.sun.enterprise.tools.verifier.tests.VerifierCheck;
32 import com.sun.enterprise.tools.verifier.tests.VerifierTest;
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 /**
37  * A persistence unit must have a name.
38  * Only one persistence unit of any given name may be defined
39  * within a single EJB-JAR file, within a single WAR file,
40  * within a single application client jar, or within
41  * an EAR (in the EAR root or lib directory).
42  * See section #6.2 of EJB 3.0 Persistence API spec
43  *
44  * @author Sanjeeb.Sahoo@Sun.COM
45  */

46 public class DuplicatePUNameTest extends VerifierTest implements VerifierCheck {
47     public Result check(Descriptor descriptor) {
48         PersistenceUnitDescriptor pu = PersistenceUnitDescriptor.class.cast(
49                 descriptor);
50         Result result = getInitializedResult();
51         addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
52         result.setStatus(Result.PASSED); // default status is PASSED
53
int count = 0;
54         for(PersistenceUnitDescriptor nextPU : getPUsInSameScope(pu)) {
55             result.addErrorDetails(smh.getLocalString(getClass().getName() + "puName",
56                     "Found a persistence unit by name [ {0} ] in persistence unit root [ {1} ]",
57                     new Object JavaDoc[]{nextPU.getName(), nextPU.getPuRoot()}));
58                     if (nextPU.getName().equals(pu.getName())) count++;
59         }
60         if (count != 1) {
61             result.failed(smh.getLocalString(getClass().getName() + "failed",
62                     "There are [ {0} ] number of persistence units by name [ {1} ]",
63                     new Object JavaDoc[]{count, pu.getName()}));
64         }
65         return result;
66     }
67     
68     /**
69      * @return the list of PersistenceUnits which will be tested to see if they contain duplicate PU name.
70      */

71     private List JavaDoc<PersistenceUnitDescriptor> getPUsInSameScope(PersistenceUnitDescriptor pu) {
72         List JavaDoc<PersistenceUnitDescriptor> result;
73         if(pu.getParent().getParent().isApplication()) {
74             // for ear, the PU name has to be unique only within a jar file.
75
result = pu.getParent().getPersistenceUnitDescriptors();
76         } else {
77             // for war/ejb-jar/appclient-jar, PU name has to be unique within the whole BundleDescriptor.
78
result = new ArrayList JavaDoc<PersistenceUnitDescriptor>();
79             for (PersistenceUnitsDescriptor pus : pu.getParent().getParent().getPersistenceUnitsDescriptors()) {
80                 for(PersistenceUnitDescriptor nextPU : pus.getPersistenceUnitDescriptors()) {
81                     result.add(nextPU);
82                 }
83             }
84         }
85         return result;
86     }
87     
88 }
89
Popular Tags