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: fd4450abbc4a9dae9af73d24e462cb9787f28b60 (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
/*******************************************************************************
 * Copyright (c) 2003, 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.internal.jca.archive.operations;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jst.j2ee.commonarchivecore.internal.File;
import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.ArchiveRuntimeException;
import org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.LoadStrategyImpl;


public class ConnectorComponentNestedJARLoadStrategyImpl extends LoadStrategyImpl {

	private List files;
	private int sourceSegmentCount;
	private int outputSegmentCount;
	private Map urisToIFiles;

	/**
	 * Constructor for NestedJARLoadStrategyImpl.
	 */
	public ConnectorComponentNestedJARLoadStrategyImpl(List files, IContainer sourceContainer, IFolder javaOutputFolder) {
		super();
		this.files = files;
		sourceSegmentCount = sourceContainer.getProjectRelativePath().segmentCount();
		outputSegmentCount = javaOutputFolder.getProjectRelativePath().segmentCount();
	}

	/**
	 * @see com.ibm.etools.archive.impl.LoadStrategyImpl#primContains(String)
	 */
	protected boolean primContains(String uri) {
		// Should only be used by discriminators, and we don't discriminate these archives
		return true;
	}

	/**
	 * @see com.ibm.etools.archive.impl.LoadStrategyImpl#getFiles()
	 */
	public List getFiles() {
		urisToIFiles = new HashMap();
		List result = new ArrayList();
		int size = files.size();
		for (int i = 0; i < size; i++) {
			IFile iFile = (IFile) files.get(i);
			IPath relPath;
			if(ConnectorComponentLoadStrategyImpl.isClass(iFile)){
				relPath = getRelativePath(iFile, outputSegmentCount);
			} else {
				relPath = getRelativePath(iFile, sourceSegmentCount);
			}
			addFile(iFile, relPath, result);
		}
		return result;
	}

	protected void addFile(IFile iFile, IPath relPath, List result) {
		File cFile = createFile(iFile, relPath);
		result.add(cFile);
		urisToIFiles.put(cFile.getURI(), iFile);
	}

	protected long getLastModified(IResource aResource) {
		return aResource.getLocation().toFile().lastModified();
	}

	private File createFile(IFile iFile, IPath relPath) {
		File cFile = createFile(relPath.toString());
		cFile.setLastModified(getLastModified(iFile));
		return cFile;
	}

	private IPath getRelativePath(IFile file, int parentSegmentCount) {
		return file.getProjectRelativePath().removeFirstSegments(parentSegmentCount);
	}

	/**
	 * @see com.ibm.etools.archive.LoadStrategy#getInputStream(String)
	 */
	public InputStream getInputStream(String uri) throws IOException, FileNotFoundException {
		if (null == urisToIFiles) {
			getFiles();
		}
		IFile file = (IFile) urisToIFiles.get(uri);
		if (file != null) {
			try {
				return file.getContents();
			} catch (CoreException core) {
				throw new ArchiveRuntimeException(uri, core);
			}
		}
		throw new FileNotFoundException(uri);
	}

}

Back to the top