001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.resources;
019:
020: import java.io.File;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.io.IOException;
024:
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.types.Resource;
028: import org.apache.tools.ant.util.FileUtils;
029: import org.apache.tools.tar.TarEntry;
030: import org.apache.tools.tar.TarInputStream;
031:
032: /**
033: * A Resource representation of an entry in a tar archive.
034: * @since Ant 1.7
035: */
036: public class TarResource extends ArchiveResource {
037:
038: private String userName = "";
039: private String groupName = "";
040: private int uid;
041: private int gid;
042:
043: /**
044: * Default constructor.
045: */
046: public TarResource() {
047: }
048:
049: /**
050: * Construct a TarResource representing the specified
051: * entry in the specified archive.
052: * @param a the archive as File.
053: * @param e the TarEntry.
054: */
055: public TarResource(File a, TarEntry e) {
056: super (a, true);
057: setEntry(e);
058: }
059:
060: /**
061: * Construct a TarResource representing the specified
062: * entry in the specified archive.
063: * @param a the archive as Resource.
064: * @param e the TarEntry.
065: */
066: public TarResource(Resource a, TarEntry e) {
067: super (a, true);
068: setEntry(e);
069: }
070:
071: /**
072: * Return an InputStream for reading the contents of this Resource.
073: * @return an InputStream object.
074: * @throws IOException if the tar file cannot be opened,
075: * or the entry cannot be read.
076: */
077: public InputStream getInputStream() throws IOException {
078: if (isReference()) {
079: return ((Resource) getCheckedRef()).getInputStream();
080: }
081: Resource archive = getArchive();
082: final TarInputStream i = new TarInputStream(archive
083: .getInputStream());
084: TarEntry te = null;
085: while ((te = i.getNextEntry()) != null) {
086: if (te.getName().equals(getName())) {
087: return i;
088: }
089: }
090:
091: FileUtils.close(i);
092: throw new BuildException("no entry " + getName() + " in "
093: + getArchive());
094: }
095:
096: /**
097: * Get an OutputStream for the Resource.
098: * @return an OutputStream to which content can be written.
099: * @throws IOException if unable to provide the content of this
100: * Resource as a stream.
101: * @throws UnsupportedOperationException if OutputStreams are not
102: * supported for this Resource type.
103: */
104: public OutputStream getOutputStream() throws IOException {
105: if (isReference()) {
106: return ((Resource) getCheckedRef()).getOutputStream();
107: }
108: throw new UnsupportedOperationException(
109: "Use the tar task for tar output.");
110: }
111:
112: /**
113: * @return the user name for the tar entry
114: */
115: public String getUserName() {
116: if (isReference()) {
117: return ((TarResource) getCheckedRef()).getUserName();
118: }
119: return userName;
120: }
121:
122: /**
123: * @return the group name for the tar entry
124: */
125: public String getGroup() {
126: if (isReference()) {
127: return ((TarResource) getCheckedRef()).getGroup();
128: }
129: return groupName;
130: }
131:
132: /**
133: * @return the uid for the tar entry
134: */
135: public int getUid() {
136: if (isReference()) {
137: return ((TarResource) getCheckedRef()).getUid();
138: }
139: return uid;
140: }
141:
142: /**
143: * @return the uid for the tar entry
144: */
145: public int getGid() {
146: if (isReference()) {
147: return ((TarResource) getCheckedRef()).getGid();
148: }
149: return uid;
150: }
151:
152: /**
153: * fetches information from the named entry inside the archive.
154: */
155: protected void fetchEntry() {
156: Resource archive = getArchive();
157: TarInputStream i = null;
158: try {
159: i = new TarInputStream(archive.getInputStream());
160: TarEntry te = null;
161: while ((te = i.getNextEntry()) != null) {
162: if (te.getName().equals(getName())) {
163: setEntry(te);
164: return;
165: }
166: }
167: } catch (IOException e) {
168: log(e.getMessage(), Project.MSG_DEBUG);
169: throw new BuildException(e);
170: } finally {
171: if (i != null) {
172: FileUtils.close(i);
173: }
174: }
175: setEntry(null);
176: }
177:
178: private void setEntry(TarEntry e) {
179: if (e == null) {
180: setExists(false);
181: return;
182: }
183: setName(e.getName());
184: setExists(true);
185: setLastModified(e.getModTime().getTime());
186: setDirectory(e.isDirectory());
187: setSize(e.getSize());
188: setMode(e.getMode());
189: userName = e.getUserName();
190: groupName = e.getGroupName();
191: uid = e.getUserId();
192: gid = e.getGroupId();
193: }
194:
195: }
|