Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: b404f5d374342340f633aafd9a0f9d87b6ab218c (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
 * 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.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.exception.SaveFailureException;
import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveURIConverterImpl;
import org.eclipse.jst.j2ee.commonarchivecore.internal.util.ArchiveUtil;
import org.eclipse.jst.j2ee.internal.J2EEConstants;


/**
 * Implementer that knows how to save an archives contents to a directory on the local file system
 */
public class DirectorySaveStrategyImpl extends SaveStrategyImpl implements SaveStrategy {
	protected String directoryName;
	protected URIConverter uriConverter;
	/** Indicates which type of archives should be expanded */
	protected int expansionFlags;

	/**
	 * DirectoryDumpStrategy constructor comment.
	 */
	public DirectorySaveStrategyImpl() {
		super();
	}

	/**
	 * DirectoryDumpStrategy constructor comment.
	 */
	public DirectorySaveStrategyImpl(String dirName, int flags) {
		super();
		setDirectoryName(dirName);
		setExpansionFlags(flags);
		initialize();
	}

	/**
	 * @see com.ibm.etools.archive.impl.SaveStrategyImpl
	 */
	protected SaveStrategy createNestedDirectoryStrategy(Archive anArchive) {
		String uri = org.eclipse.jst.j2ee.commonarchivecore.internal.util.ArchiveUtil.getOSUri(getDirectoryName(), anArchive.getURI());
		return new DirectorySaveStrategyImpl(uri, getExpansionFlags());
	}

	/**
	 * @see com.ibm.etools.archive.impl.SaveStrategyImpl
	 */
	protected SaveStrategy createNestedSaveStrategy(Archive anArchive) throws IOException {
		if (shouldExpand(anArchive))
			return createNestedDirectoryStrategy(anArchive);
		return createNestedZipStrategy(anArchive);
	}

	/**
	 * @see com.ibm.etools.archive.impl.SaveStrategyImpl
	 */
	protected SaveStrategy createNestedZipStrategy(Archive anArchive) throws IOException {
		OutputStream out = getUriConverter().createOutputStream(URI.createURI(anArchive.getURI()));
		return new ZipStreamSaveStrategyImpl(out);
	}

	/**
	 * Insert the method's description here. Creation date: (11/15/00 2:26:37 PM)
	 * 
	 * @return java.lang.String
	 */
	public java.lang.String getDirectoryName() {
		return directoryName;
	}

	/**
	 * Insert the method's description here. Creation date: (12/19/00 10:18:21 AM)
	 * 
	 * @return int
	 */
	public int getExpansionFlags() {
		return expansionFlags;
	}

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

	/**
	 * Insert the method's description here. Creation date: (12/08/00 4:50:32 PM)
	 * 
	 * @return org.eclipse.emf.ecore.resource.URIConverter
	 */
	public org.eclipse.emf.ecore.resource.URIConverter getUriConverter() {
		return uriConverter;
	}

	public void initialize() {
		ArchiveURIConverterImpl converter = new ArchiveURIConverterImpl(getArchive(), getDirectoryName());
		converter.setOutputFilepath(getDirectoryName());
		setUriConverter(converter);
	}

	/**
	 * @see com.ibm.etools.archive.SaveStrategy
	 */
	public boolean isDirectory() {
		return true;
	}

	protected void mkdirs(String directoryPath) {
		File aDirectory = new File(getDirectoryName() + File.separatorChar + directoryPath);
		aDirectory.mkdirs();
	}

	/**
	 * @see com.ibm.etools.archive.SaveStrategy
	 */
	public void save(org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest aManifest) throws SaveFailureException {
		try {
			OutputStream out = getUriConverter().createOutputStream(URI.createURI(J2EEConstants.MANIFEST_URI));
			aManifest.write(out);
			out.close();
			if (getArchive().getOptions().isReadOnly())
				setTimestampAfterSaving(J2EEConstants.MANIFEST_URI);
		} catch (IOException iox) {
			throw new SaveFailureException(J2EEConstants.MANIFEST_URI, iox);
		}
	}

	/**
	 * @see com.ibm.etools.archive.impl.SaveStrategyImpl
	 */
	public void save(org.eclipse.jst.j2ee.commonarchivecore.internal.File aFile, InputStream in) throws SaveFailureException {
		String entryName = aFile.getURI();
		if (aFile.isDirectoryEntry())
			mkdirs(entryName);
		else {
			try {
				OutputStream out = getUriConverter().createOutputStream(URI.createURI(entryName));
				ArchiveUtil.copy(in, out);
				setTimestampAfterSaving(aFile);
			} catch (IOException iox) {
				throw new SaveFailureException(aFile.getURI(), iox);
			}
		}
	}

	protected void saveMofResource(org.eclipse.emf.ecore.resource.Resource aResource, OutputStream os) throws IOException {
		super.saveMofResource(aResource, os);
		os.close();
	}

	/**
	 * Insert the method's description here. Creation date: (11/15/00 2:26:37 PM)
	 * 
	 * @param newDirectoryName
	 *            java.lang.String
	 */
	public void setDirectoryName(java.lang.String newDirectoryName) {
		directoryName = newDirectoryName;
	}

	/**
	 * Insert the method's description here. Creation date: (12/19/00 10:18:21 AM)
	 * 
	 * @param newExpansionFlags
	 *            int
	 */
	public void setExpansionFlags(int newExpansionFlags) {
		expansionFlags = newExpansionFlags;
	}

	protected void setTimestampAfterSaving(org.eclipse.jst.j2ee.commonarchivecore.internal.File aFile) {
		long lastMod = aFile.getLastModified();
		if (lastMod == 0)
			return;
		String absPath = ArchiveUtil.getOSUri(getDirectoryName(), aFile.getURI());
		File ioFile = new File(absPath);
		ioFile.setLastModified(lastMod);
	}

	protected void setTimestampAfterSaving(String uri) {
		org.eclipse.jst.j2ee.commonarchivecore.internal.File aFile = null;
		try {
			aFile = getArchive().getFile(uri);
		} catch (FileNotFoundException mustBeANewEntry) {
			return;
		}
		setTimestampAfterSaving(aFile);
	}

	/**
	 * Insert the method's description here. Creation date: (12/08/00 4:50:32 PM)
	 * 
	 * @param newUriConverter
	 *            org.eclipse.emf.ecore.resource.URIConverter
	 */
	public void setUriConverter(org.eclipse.emf.ecore.resource.URIConverter newUriConverter) {
		uriConverter = newUriConverter;
	}

	protected boolean shouldExpand(Archive anArchive) {

		int flag = 0;

		if (anArchive.isWARFile())
			flag = Archive.EXPAND_WAR_FILES;
		else if (anArchive.isEARFile())
			flag = Archive.EXPAND_EAR_FILES;
		else if (anArchive.isRARFile())
			flag = Archive.EXPAND_RAR_FILES;
		else if (anArchive.isEJBJarFile())
			flag = Archive.EXPAND_EJBJAR_FILES;
		else if (anArchive.isApplicationClientFile())
			flag = Archive.EXPAND_APPCLIENT_FILES;
		else
			flag = Archive.EXPAND_ARCHIVES;

		return (getExpansionFlags() & flag) != 0;

	}

	protected boolean shouldIterateOver(Archive anArchive) {
		return super.shouldIterateOver(anArchive) || shouldExpand(anArchive);
	}
}

Back to the top