KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > AbstractPUMatchingEMandEMFRefTest


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;
26
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.tools.verifier.Result;
29
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * For every entity manager reference and entity manager factory reference
36  * in a component (ejb/servlet/app-client etc), there must be a matching
37  * persistence unit defined in the scope of that component.
38  *
39  * @author Sanjeeb.Sahoo@Sun.COM
40  */

41 public abstract class AbstractPUMatchingEMandEMFRefTest extends VerifierTest
42         implements VerifierCheck {
43     final static String JavaDoc className = AbstractPUMatchingEMandEMFRefTest.class.getName();
44     public Result check(Descriptor descriptor) {
45         // initialize the result object
46
Result result = getInitializedResult();
47         addErrorDetails(result,
48                 getVerifierContext().getComponentNameConstructor());
49         result.setStatus(Result.PASSED); //default status is PASSED
50

51         BundleDescriptor bundleDescriptor = getBundleDescriptor(descriptor);
52         
53         for (EntityManagerReferenceDescriptor emRefDesc : getEntityManagerReferenceDescriptors(descriptor)) {
54             String JavaDoc referringUnitName = emRefDesc.getUnitName();
55             PersistenceUnitDescriptor pu = bundleDescriptor.findReferencedPU(referringUnitName);
56             if (pu == null) {
57                 result.failed(smh.getLocalString(
58                         className + "failed",
59                         "There is no unique persistence unit found by name " +
60                         "[ {0} ] in the scope of this component.",
61                         new Object JavaDoc[]{referringUnitName}));
62             } else {
63                 result.passed(smh.getLocalString(
64                         className + "passed",
65                         "Found a persistence unit by name [ {0} ] in the scope of this component",
66                         new Object JavaDoc[]{referringUnitName}));
67             }
68         }
69         for (EntityManagerFactoryReferenceDescriptor emfRefDesc : getEntityManagerFactoryReferenceDescriptors(descriptor)) {
70             String JavaDoc referringUnitName = emfRefDesc.getUnitName();
71             PersistenceUnitDescriptor pu = bundleDescriptor.findReferencedPU(referringUnitName);
72             if (pu == null) {
73                 result.failed(smh.getLocalString(
74                         className + "failed",
75                         "There is no unique persistence unit found by name " +
76                         "[ {0} ] in the scope of this component.",
77                         new Object JavaDoc[]{referringUnitName}));
78             } else {
79                 result.passed(smh.getLocalString(
80                         className + "passed",
81                         "Found a persistence unit by name [ {0} ] in the scope of this component",
82                         new Object JavaDoc[]{referringUnitName}));
83             }
84         }
85         
86         StringBuilder JavaDoc visiblePUNames = new StringBuilder JavaDoc();
87         final Map JavaDoc<String JavaDoc, PersistenceUnitDescriptor> visiblePUs =
88                 bundleDescriptor.getVisiblePUs();
89         int count = 0;
90         for(String JavaDoc puName : visiblePUs.keySet()) {
91             visiblePUNames.append(puName);
92             if(visiblePUs.size() != ++count) { // end not yet reached
93
visiblePUNames.append(", ");
94             }
95         }
96         String JavaDoc message = smh.getLocalString(className + ".puList",
97                 "PUs that are visible to this component are: [ {0} ]",
98                 new Object JavaDoc[]{visiblePUNames});
99         result.addErrorDetails(message);
100         result.addGoodDetails(message);
101         
102         return result;
103     }
104     
105     protected abstract BundleDescriptor getBundleDescriptor(
106             Descriptor descriptor);
107     
108     protected abstract Collection JavaDoc<EntityManagerReferenceDescriptor>
109             getEntityManagerReferenceDescriptors(Descriptor descriptor);
110     
111     protected abstract Collection JavaDoc<EntityManagerFactoryReferenceDescriptor>
112             getEntityManagerFactoryReferenceDescriptors(Descriptor descriptor);
113     
114 }
115
Popular Tags