Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9fee0c8bfaf8784579084d7c23c9292fefacac32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.commonarchivecore.internal.strategy;



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
import org.eclipse.jst.j2ee.commonarchivecore.internal.File;
import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.SaveFailureException;
import org.eclipse.jst.j2ee.internal.J2EEConstants;
import org.eclipse.wst.common.internal.emf.resource.CompatibilityURIConverter;
import org.eclipse.wst.common.internal.emf.utilities.Revisit;


/**
 * This is a concrete implentation of SaveStrategy. All the contents of the archive, including xmi
 * resources, will be output to a ZipOutputStream. The output stream should be passed in by the
 * client.
 */
public class ZipStreamSaveStrategyImpl extends SaveStrategyImpl implements SaveStrategy {
	protected OutputStream destinationStream;
	/** Used internally */
	protected ZipOutputStream zipOutputStream;

	/**
	 * Wraps a new zip output stream around the parameter
	 */
	public ZipStreamSaveStrategyImpl(OutputStream out) {
		setDestinationStream(out);
		setZipOutputStream(new ZipOutputStream(out));
	}

	public void close() throws IOException {
		getDestinationStream().close();
	}

	protected SaveStrategy createNestedSaveStrategy(Archive anArchive) {
		return new ZipStreamSaveStrategyImpl(getZipOutputStream());
	}

	public void finish() throws IOException {
		getZipOutputStream().finish();
		//If this is not nested, close the stream to free up the resource
		//otherwise, don't close it because the parent may not be done
		if (!(getDestinationStream() instanceof ZipOutputStream))
			getDestinationStream().close();
	}

	/**
	 * @return java.io.OutputStream
	 */
	public java.io.OutputStream getDestinationStream() {
		return destinationStream;
	}

	/**
	 * @see com.ibm.etools.archive.impl.SaveStrategyImpl
	 */
	protected java.io.OutputStream getOutputStreamForResource(Resource aResource) throws java.io.IOException {
		return getZipOutputStream();
	}

	/**
	 * @see com.ibm.etools.archive.SaveStrategy#saveMofResource(Resource)
	 */
	public void saveMofResource(Resource aResource, OutputStream out) throws IOException {
		Revisit.revisit();
		URI uri = aResource.getURI();
		//Ensure container relative URI
		URIConverter conv = getArchive().getResourceSet().getURIConverter();
		if (conv instanceof CompatibilityURIConverter)
			uri = ((CompatibilityURIConverter) conv).deNormalize(uri);
		ZipEntry entry = new ZipEntry(uri.toString());
		zipOutputStream.putNextEntry(entry);
		super.saveMofResource(aResource, out);
		zipOutputStream.closeEntry();
	}

	/**
	 * @return java.util.zip.ZipOutputStream
	 */
	protected java.util.zip.ZipOutputStream getZipOutputStream() {
		return zipOutputStream;
	}

	/**
	 * @see com.ibm.etools.archive.SaveStrategy
	 */
	public void save(org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest aManifest) throws SaveFailureException {
		try {
			ZipEntry entry = new ZipEntry(J2EEConstants.MANIFEST_URI);
			getZipOutputStream().putNextEntry(entry);
			aManifest.write(getZipOutputStream());
			getZipOutputStream().closeEntry();
		} catch (IOException iox) {
			throw new SaveFailureException(J2EEConstants.MANIFEST_URI, iox);
		}
	}

	public void save(Archive anArchive) throws SaveFailureException {
		try {
			ZipEntry entry = new ZipEntry(anArchive.getURI());
			getZipOutputStream().putNextEntry(entry);
			super.save(anArchive);
			getZipOutputStream().closeEntry();
		} catch (IOException iox) {
			throw new SaveFailureException(anArchive.getURI(), iox);
		}
	}

	public void save(File aFile, InputStream in) throws SaveFailureException {
		try {
			String entryName = aFile.getURI();
			if (aFile.isDirectoryEntry() && !entryName.endsWith("/"))//$NON-NLS-1$
				entryName = entryName + '/';
			ZipEntry entry = new ZipEntry(entryName);
			if (aFile.getLastModified() > 0)
				entry.setTime(aFile.getLastModified());
			getZipOutputStream().putNextEntry(entry);
			if (!aFile.isDirectoryEntry()) {
				org.eclipse.jst.j2ee.commonarchivecore.internal.util.ArchiveUtil.copy(in, getZipOutputStream());
			}
			getZipOutputStream().closeEntry();
		} catch (IOException iox) {
			throw new SaveFailureException(aFile.getURI(), iox);
		}
	}

	/**
	 * @param newDestinationStream
	 *            java.io.OutputStream
	 */
	public void setDestinationStream(java.io.OutputStream newDestinationStream) {
		destinationStream = newDestinationStream;
	}

	/**
	 * @param newZipOutputStream
	 *            java.util.zip.ZipOutputStream
	 */
	protected void setZipOutputStream(java.util.zip.ZipOutputStream newZipOutputStream) {
		zipOutputStream = newZipOutputStream;
	}
}

Back to the top