Skip to main content
summaryrefslogtreecommitdiffstats
blob: c32c9590c741fe57e028838c4729c92dff7d6b17 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 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.jee.archive;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;

/**
 * Abstract implementation of {@link IArchiveSaveAdapter} intended for
 * subclassing by clients. See {@link IArchiveSaveAdapter} for details.
 */
public abstract class AbstractArchiveSaveAdapter extends AbstractArchiveAdapter implements IArchiveSaveAdapter {

	public void close() throws IOException {
	}

	public void finish() throws IOException {
	}

	protected IArchiveResource manifest = null; 
	protected IArchiveResource metaInf = null; 
	
	public void save(IProgressMonitor monitor) throws ArchiveSaveFailureException {
		final int GATHER_RESOURCES_TICKS = 1000;
		final int SAVE_RESOURCES_TICKS = 1000;
		final int FINISH_TICKS = 10;
		final int TOTAL_TICKS = GATHER_RESOURCES_TICKS + SAVE_RESOURCES_TICKS + FINISH_TICKS;
		Exception caughtException = null;
		try {
			monitor.beginTask("Saving resources", TOTAL_TICKS);
			
			List<IArchiveResource> files = getArchiveResourcesForSave();
			IPath manifestPath = new Path("META-INF/MANIFEST.MF");
			if(getArchive().containsArchiveResource(manifestPath)){
				manifest = getArchive().getArchiveResource(manifestPath);
			}
			IPath metaInfPath = manifestPath.removeFirstSegments(1);
			if(getArchive().containsArchiveResource(metaInfPath)){
				metaInf = getArchive().getArchiveResource(metaInfPath);
			}
				
			monitor.worked(GATHER_RESOURCES_TICKS);
			IProgressMonitor saveSubMonitor = new SubProgressMonitor(monitor, SAVE_RESOURCES_TICKS);
			int SUB_SAVE_TICKS = 10;
			int SUB_TOTAL_TICKS = SUB_SAVE_TICKS * (files.size() + 2);
			try {
				saveSubMonitor.beginTask("Saving resources", SUB_TOTAL_TICKS);
				if(manifest != null){
					save(manifest);
				}
				saveSubMonitor.worked(SUB_SAVE_TICKS);
				if(metaInf != null){
					save(metaInf);
				}
				saveSubMonitor.worked(SUB_SAVE_TICKS);
				for (IArchiveResource file : files) {
					if (shouldSave(file)) {
						save(file);
					}
					saveSubMonitor.worked(SUB_SAVE_TICKS);
				}
			} finally {
				saveSubMonitor.done();
			}
		} catch(Exception e){
			caughtException = e;
		} finally {
			try {
				finish();
			} catch (IOException e) {
				throw new ArchiveSaveFailureException(e);
			} finally {
				monitor.done();
				if(caughtException != null){
					throw new ArchiveSaveFailureException(caughtException);
				}
			}
		}
	}

	protected List<IArchiveResource> getArchiveResourcesForSave() {
		List<IArchiveResource> files = getArchive().getArchiveResources();
		return files;
	}

	/**
	 * Subclasses should implement to perform the actual save on the specified
	 * {@link IArchiveResource}.
	 * 
	 * @param file
	 * @throws ArchiveSaveFailureException
	 */
	protected abstract void save(IArchiveResource file) throws ArchiveSaveFailureException;

	/**
	 * Subclassess should override to avoid saving the specified
	 * {@link IArchiveResource}. e.g. to avoid saving source files.
	 * 
	 * @param file
	 * @return
	 */
	protected boolean shouldSave(IArchiveResource file) {
		if(file == manifest || file == metaInf){
			return false;
		}
		return true;
	}

}

Back to the top