KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > Descriptor


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive;
25
26 // ToolBox
27
import org.enhydra.tool.archive.xml.DescriptorHandler;
28 import org.enhydra.tool.archive.xml.EjbDescriptorHandler;
29 import org.enhydra.tool.common.PathHandle;
30
31 // JDK
32
import java.io.InputStream JavaDoc;
33 import java.io.BufferedInputStream JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36
37 //
38
public class Descriptor {
39     public static final String JavaDoc EJB = "ejb-jar";
40     public static final String JavaDoc JONAS_EJB = "jonas-ejb-jar";
41     public static final String JavaDoc WEB = "web";
42     public static final String JavaDoc ENHYDRA_WEB = "enhydra-web";
43     public static final String JavaDoc ENHYDRA_SERVICE = "enhydra-service";
44
45     //
46
private boolean required = false;
47     private String JavaDoc type = new String JavaDoc();
48     private String JavaDoc path = new String JavaDoc();
49
50     public static String JavaDoc getArchivePath(String JavaDoc type) {
51         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
52
53         if (type.endsWith("web")) {
54             buf.append("WEB-INF/");
55         } else {
56             buf.append("META-INF/");
57         }
58         buf.append(type);
59         buf.append(".xml");
60         return buf.toString();
61     }
62
63     public Descriptor(boolean r, String JavaDoc t) {
64         required = r;
65         type = t;
66     }
67
68     public Descriptor(String JavaDoc p) {
69       PathHandle ph = null;
70       setPath(p);
71       required = false;
72       ph = PathHandle.createPathHandle(getPath());
73       type = ph.getFile().getName();
74       type = type.substring(0, type.indexOf('.'));
75     }
76
77     public boolean isRequired() {
78         return required;
79     }
80
81     public String JavaDoc getType() {
82         return type;
83     }
84
85     public String JavaDoc getPath() {
86         return path;
87     }
88
89     public void setPath(String JavaDoc p) {
90         if (p == null) {
91             path = new String JavaDoc();
92         } else {
93             PathHandle ph = PathHandle.createPathHandle(p);
94
95             path = ph.getPath();
96         }
97     }
98
99     public InputStream JavaDoc getInputStream() throws ArchiveException {
100         DescriptorHandler dh = null;
101         InputStream JavaDoc stream = null;
102
103         if (getType().equals(Descriptor.EJB)) {
104             dh = new EjbDescriptorHandler();
105         }
106         if (dh != null) {
107             dh.setSource(getPath());
108             dh.prep();
109             stream = dh.getInputStream();
110         } else {
111             FileInputStream JavaDoc fileStream = null;
112             BufferedInputStream JavaDoc bufStream = null;
113
114             try {
115                 fileStream = new FileInputStream JavaDoc(getPath());
116                 bufStream = new BufferedInputStream JavaDoc(fileStream);
117                 stream = bufStream;
118             } catch (FileNotFoundException JavaDoc e) {
119                 throw new ArchiveException(e,
120                                            "Invalid descriptor: "
121                                            + getPath());
122             }
123         }
124         return stream;
125     }
126
127     protected String JavaDoc getArchivePath() {
128         return Descriptor.getArchivePath(getType());
129     }
130
131     protected boolean isValid() {
132         boolean valid = true;
133
134         try {
135             validate();
136         } catch (ArchiveException e) {
137             valid = false;
138         }
139         return valid;
140     }
141
142     protected void validate() throws ArchiveException {
143         PathHandle ph = PathHandle.createPathHandle(getPath());
144
145         if (!ph.isFile()) {
146             if (isRequired()) {
147                 throw new ArchiveException("Required descriptor not found: "
148                                            + ph.getPath());
149             } else if (!ph.isEmpty()) {
150                 throw new ArchiveException("Invalid descriptor path: "
151                                            + ph.getPath());
152             }
153         }
154     }
155
156     public boolean equals(Object JavaDoc comp) {
157         Descriptor d = null;
158         boolean equal = false;
159
160         if (comp instanceof Descriptor) {
161             d = (Descriptor) comp;
162             equal = d.isRequired() == isRequired();
163             if (equal) {
164                 equal = d.getType().equals(getType());
165             }
166             if (equal) {
167                 equal = d.getPath().equals(getPath());
168             }
169         }
170         return equal;
171     }
172
173 }
174
Popular Tags