001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.tool.archive;
024:
025: // ToolBox
026: import org.enhydra.tool.archive.xml.DescriptorHandler; //Zeljko Kovacevic 30.10 2003 comment this
027: //import org.enhydra.tool.archive.xml.EjbDescriptorHandler;
028: import org.enhydra.tool.common.PathHandle;
029:
030: // JDK
031: import java.io.InputStream;
032: import java.io.BufferedInputStream;
033: import java.io.FileInputStream;
034: import java.io.FileNotFoundException;
035:
036: //
037: public class Descriptor {
038: public static final String EJB = "ejb-jar";
039: public static final String JONAS_EJB = "jonas-ejb-jar";
040: public static final String WEB = "web";
041: public static final String ENHYDRA_WEB = "enhydra-web";
042: public static final String ENHYDRA_SERVICE = "enhydra-service";
043:
044: //
045: private boolean required = false;
046: private String type = new String();
047: private String path = new String();
048:
049: public static String getArchivePath(String type) {
050: StringBuffer buf = new StringBuffer();
051:
052: if (type.endsWith("web")) {
053: buf.append("WEB-INF/");
054: } else {
055: buf.append("META-INF/");
056: }
057: buf.append(type);
058: buf.append(".xml");
059: return buf.toString();
060: }
061:
062: public Descriptor(boolean r, String t) {
063: required = r;
064: type = t;
065: }
066:
067: public Descriptor(String p) {
068: PathHandle ph = null;
069: setPath(p);
070: required = false;
071: ph = PathHandle.createPathHandle(getPath());
072: type = ph.getFile().getName();
073: type = type.substring(0, type.indexOf('.'));
074: }
075:
076: public boolean isRequired() {
077: return required;
078: }
079:
080: public String getType() {
081: return type;
082: }
083:
084: public String getPath() {
085: return path;
086: }
087:
088: public void setPath(String p) {
089: if (p == null) {
090: path = new String();
091: } else {
092: PathHandle ph = PathHandle.createPathHandle(p);
093:
094: path = ph.getPath();
095: }
096: }
097:
098: public InputStream getInputStream() throws ArchiveException {
099: DescriptorHandler dh = null;
100: InputStream stream = null;
101: //Zeljko Kovacevic 30.10 2003 comment this
102: if (getType().equals(Descriptor.EJB)) {
103: // dh = new EjbDescriptorHandler();
104: }
105: if (dh != null) {
106: dh.setSource(getPath());
107: dh.prep();
108: stream = dh.getInputStream();
109: } else {
110: FileInputStream fileStream = null;
111: BufferedInputStream bufStream = null;
112:
113: try {
114: fileStream = new FileInputStream(getPath());
115: bufStream = new BufferedInputStream(fileStream);
116: stream = bufStream;
117: } catch (FileNotFoundException e) {
118: throw new ArchiveException(e, "Invalid descriptor: "
119: + getPath());
120: }
121: }
122: return stream;
123: }
124:
125: protected String getArchivePath() {
126: return Descriptor.getArchivePath(getType());
127: }
128:
129: protected boolean isValid() {
130: boolean valid = true;
131:
132: try {
133: validate();
134: } catch (ArchiveException e) {
135: valid = false;
136: }
137: return valid;
138: }
139:
140: protected void validate() throws ArchiveException {
141: PathHandle ph = PathHandle.createPathHandle(getPath());
142:
143: if (!ph.isFile()) {
144: if (isRequired()) {
145: throw new ArchiveException(
146: "Required descriptor not found: "
147: + ph.getPath());
148: } else if (!ph.isEmpty()) {
149: throw new ArchiveException("Invalid descriptor path: "
150: + ph.getPath());
151: }
152: }
153: }
154:
155: public boolean equals(Object comp) {
156: Descriptor d = null;
157: boolean equal = false;
158:
159: if (comp instanceof Descriptor) {
160: d = (Descriptor) comp;
161: equal = d.isRequired() == isRequired();
162: if (equal) {
163: equal = d.getType().equals(getType());
164: }
165: if (equal) {
166: equal = d.getPath().equals(getPath());
167: }
168: }
169: return equal;
170: }
171:
172: }
|