KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > util > DeploymentHelper


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

17
18 package org.apache.geronimo.deployment.util;
19
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25
26 import org.apache.geronimo.common.DeploymentException;
27 import org.w3c.dom.Document JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * Helper class that handles locating DDs in directories (mainly WEB-INF, META-INF),
32  * building class space and dealing with deployments
33  *
34  * @version $Rev: 109898 $ $Date: 2004-12-05 10:21:47 -0800 (Sun, 05 Dec 2004) $
35  */

36 public class DeploymentHelper {
37     protected final URL JavaDoc url;
38     protected final URLType urlType;
39     protected URL JavaDoc j2eeURL;
40     protected URL JavaDoc geronimoURL;
41
42     /**
43      * Create an helper related to the specified deployment URL with META-INF
44      * as the directory with the given files
45      *
46      * @see #DeploymentHelper(URLInfo, String, String, String)
47      */

48     public DeploymentHelper(URLInfo urlInfo, String JavaDoc j2eeDDName,
49             String JavaDoc geronimoDDName) throws DeploymentException {
50         this(urlInfo, j2eeDDName, geronimoDDName, "META-INF");
51     }
52
53     /**
54      * Creates an helper related to the specified deployment URL.
55      *
56      * @param urlInfo Deployment URLInfo.
57      * @param j2eeDDName name of the J2EE deployment descriptor file
58      * @param geronimoDDName name of the Geronimo deployment descriptor file
59      * @param infDir the directory where deployment descriptors are to be looked up
60      * @throws DeploymentException when the deployment doesn't exist
61      */

62     public DeploymentHelper(URLInfo urlInfo, String JavaDoc j2eeDDName,
63             String JavaDoc geronimoDDName, String JavaDoc infDir) throws DeploymentException {
64         this.url = urlInfo.getUrl();
65         this.urlType = urlInfo.getType();
66         try {
67             if (URLType.RESOURCE == urlType) {
68                 j2eeURL = null;
69                 geronimoURL = url;
70             } else if (URLType.PACKED_ARCHIVE == urlType) {
71                 j2eeURL = new URL JavaDoc("jar:" + this.url.toExternalForm() + "!/" + infDir + "/" + j2eeDDName);
72                 geronimoURL = new URL JavaDoc("jar:" + this.url.toExternalForm() + "!/" + infDir + "/" + geronimoDDName);
73             } else if (URLType.UNPACKED_ARCHIVE == urlType) {
74                 j2eeURL = new URL JavaDoc(this.url, infDir + "/" + j2eeDDName);
75                 geronimoURL = new URL JavaDoc(this.url, infDir + "/" + geronimoDDName);
76             } else {
77                 j2eeURL = null;
78                 geronimoURL = null;
79                 return;
80             }
81         } catch (MalformedURLException JavaDoc e1) {
82             throw new DeploymentException("Should never occur", e1);
83         }
84     }
85     /**
86      * Locates J2EE deployment descriptor.
87      *
88      * @return URL referencing the J2EE DD or null if no deployment descriptor
89      * is found.
90      */

91     public URL JavaDoc locateJ2EEDD() {
92         return j2eeURL;
93     }
94
95     public Document JavaDoc getJ2EEDoc(DocumentBuilder JavaDoc parser) {
96         return getDoc(parser, j2eeURL);
97     }
98
99     public Document JavaDoc getGeronimoDoc(DocumentBuilder JavaDoc parser) {
100         return getDoc(parser, geronimoURL);
101     }
102
103     private Document JavaDoc getDoc(DocumentBuilder JavaDoc parser, URL JavaDoc url) {
104         if (url == null) {
105             return null;
106         }
107         try {
108             return parser.parse(url.openStream());
109         } catch (SAXException JavaDoc e) {
110             return null;
111         } catch (IOException JavaDoc e) {
112             return null;
113         }
114     }
115
116     /**
117      * Locates Geronimo's deployment descriptor
118      *
119      * @return URL referencing Geronimo's DD or null if no deployment
120      * descriptor is found
121      */

122     public URL JavaDoc locateGeronimoDD() {
123         return geronimoURL;
124     }
125 }
126
Popular Tags