KickJava   Java API By Example, From Geeks To Geeks.

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


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.tools.verifier.Result;
30 import com.sun.enterprise.tools.verifier.persistence.AVKPersistenceUnitInfoImpl;
31 import com.sun.enterprise.tools.verifier.tests.VerifierCheck;
32 import com.sun.enterprise.tools.verifier.tests.VerifierTest;
33
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.PersistenceException;
36
37 import com.sun.enterprise.loader.InstrumentableClassLoader;
38 import javax.persistence.spi.PersistenceProvider;
39 import javax.persistence.spi.PersistenceUnitInfo;
40 import java.util.logging.Level JavaDoc;
41 import java.util.Properties JavaDoc;
42
43 import oracle.toplink.essentials.exceptions.IntegrityException;
44 import oracle.toplink.essentials.exceptions.ValidationException;
45 import oracle.toplink.essentials.exceptions.DatabaseException;
46 import oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider;
47
48 /**
49  * This test uses TopLink Essential to do the validation.
50  *
51  * @author Sanjeeb.Sahoo@Sun.COM
52  */

53 public class DefaultProviderVerification extends VerifierTest
54         implements VerifierCheck {
55     public Result check(Descriptor descriptor) {
56         PersistenceUnitDescriptor pu =
57                 PersistenceUnitDescriptor.class.cast(descriptor);
58         Result result = getInitializedResult();
59         result.setStatus(Result.PASSED);
60         PersistenceProvider provider;
61         final String JavaDoc appLocation =
62                 getVerifierContext().getAbstractArchive().getArchiveUri();
63         final InstrumentableClassLoader cl =
64                 InstrumentableClassLoader.class.cast(pu.getClassLoader());
65         PersistenceUnitInfo pi = new AVKPersistenceUnitInfoImpl(pu, appLocation, cl);
66         logger.fine("PersistenceInfo for PU is :\n" + pi);
67         Properties JavaDoc props = new Properties JavaDoc();
68         // This property is set to indicate that TopLink should only
69
// validate the descriptors etc. and not try to login to database.
70
props.put(EntityManagerFactoryProvider.TOPLINK_VALIDATION_ONLY_PROPERTY,
71                 "TRUE"); // NOI18N
72
// This property is used so that TopLink throws validation exceptions
73
// as opposed to printing CONFIG level messages to console.
74
// e.g. if mapping file does not exist, we will get an exception.
75
props.put(EntityManagerFactoryProvider.TOPLINK_ORM_THROW_EXCEPTIONS,
76                 "TRUE"); // NOI18N
77
provider = new EntityManagerFactoryProvider();
78         EntityManagerFactory emf = null;
79         try {
80             emf = provider.createContainerEntityManagerFactory(pi, props);
81             logger.logp(Level.FINE, "DefaultProviderVerification", "check",
82                     "emf = {0}", emf);
83         } catch(IntegrityException ie){
84             result.setStatus(Result.FAILED);
85             addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
86             for(Object JavaDoc o: ie.getIntegrityChecker().getCaughtExceptions()){
87                 Exception JavaDoc e = Exception JavaDoc.class.cast(o);
88                 result.addErrorDetails(e.getMessage());
89             }
90         } catch (ValidationException ve) {
91             addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
92             result.failed(ve.getMessage());
93         } catch(DatabaseException de) {
94             addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
95             result.failed(de.getMessage());
96         } catch(PersistenceException pe) {
97             addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
98             result.failed(pe.getMessage());
99         } finally {
100             if(emf != null) {
101                 emf.close();
102             }
103         }
104         return result;
105     }
106
107 }
108
Popular Tags