KickJava   Java API By Example, From Geeks To Geeks.

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


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.tests.VerifierCheck;
31 import com.sun.enterprise.tools.verifier.tests.VerifierTest;
32 import com.sun.enterprise.util.io.FileUtils;
33
34 import java.io.File JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * jar files specified using <jar-file> element in persistence.xml should be
40  * present in the archive.
41  *
42  * @author Sanjeeb.Sahoo@Sun.COM
43  */

44 public class JarNotFound extends VerifierTest implements VerifierCheck {
45     public Result check(Descriptor descriptor) {
46         Result result = getInitializedResult();
47         result.setStatus(Result.PASSED);
48         addErrorDetails(result,
49                 getVerifierContext().getComponentNameConstructor());
50         PersistenceUnitDescriptor pu = PersistenceUnitDescriptor.class.cast(
51                 descriptor);
52         File JavaDoc absolutePURootFile = getAbsolutePuRootFile(pu);
53         logger.fine("Absolute PU Root: " + absolutePURootFile);
54         String JavaDoc absolutePuRoot = absolutePURootFile.getAbsolutePath();
55         List JavaDoc<String JavaDoc> jarFileNames = new ArrayList JavaDoc<String JavaDoc>(pu.getJarFiles());
56         for (String JavaDoc jarFileName : jarFileNames) {
57             // ASSUMPTION:
58
// Because of the way deployment changes names of directories etc.
59
// it is very difficult to back calculate path names. So,
60
// the following code assumes user is specifying valid URIs.
61

62             // in the xml, names always use '/'
63
String JavaDoc nativeJarFileName = jarFileName.replace('/',
64                     File.separatorChar);
65             final File JavaDoc parentFile = new File JavaDoc(absolutePuRoot).getParentFile();
66             // only components are exploded, hence first look for original archives.
67
File JavaDoc jarFile = new File JavaDoc(parentFile, nativeJarFileName);
68             if (!jarFile.exists()) {
69                 // if the referenced jar is itself a component, then
70
// it might have been exploded, hence let's see
71
// if that is the case.
72

73                 // let's calculate the name component and path component from this URI
74
// e.g. if URI is ../../foo_bar/my-ejb.jar,
75
// name component is foo_bar/my-ejb.jar and
76
// path component is ../../
77
// These are my own notions used here.
78
String JavaDoc pathComponent = "";
79                 String JavaDoc nameComponent = jarFileName;
80                 if(jarFileName.lastIndexOf("../") != -1) {
81                     final int separatorIndex = jarFileName.lastIndexOf("../")+3;
82                     pathComponent = jarFileName.substring(0,separatorIndex);
83                     nameComponent = jarFileName.substring(separatorIndex);
84                 }
85                 logger.fine("For jar-file="+ jarFileName+ ", " +
86                         "pathComponent=" +pathComponent +
87                         ", nameComponent=" + nameComponent);
88                 jarFile = new File JavaDoc(new File JavaDoc(parentFile, pathComponent),
89                         FileUtils.makeFriendlyFileName(nameComponent));
90                 if (!jarFile.exists()) {
91                     result.failed(smh.getLocalString(
92                             getClass().getName() + "failed",
93                             "[ {0} ] specified in persistence.xml does not exist in the application.",
94                             new Object JavaDoc[]{jarFileName}));
95                 }
96             }
97         }
98         return result;
99     }
100
101     private File JavaDoc getAbsolutePuRootFile(
102             PersistenceUnitDescriptor persistenceUnitDescriptor) {
103         final String JavaDoc applicationLocation =
104                 getVerifierContext().getAbstractArchive().getArchiveUri();
105         File JavaDoc absolutePuRootFile = new File JavaDoc(applicationLocation,
106                 persistenceUnitDescriptor.getAbsolutePuRoot().replace('/',
107                         File.separatorChar));
108         if (!absolutePuRootFile.exists()) {
109             throw new RuntimeException JavaDoc(
110                     absolutePuRootFile.getAbsolutePath() + " does not exist!");
111         }
112         return absolutePuRootFile;
113     }
114
115 }
116
Popular Tags