Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4b1d8d18c990c306b97ed63f3e4bdab7648795ec (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
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.core.client;

import java.io.*;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.util.SyncFileWriter;

/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class TemplateHandler extends ResponseHandler {

	@Override
	public String getResponseID() {
		return "Template"; //$NON-NLS-1$
	}

	@Override
	public void handle(Session session, String localDir, IProgressMonitor monitor) throws CVSException {
		session.readLine(); /* read the remote dir which is not needed */
		// Only read the template file if the container exists.
		// This is OK as we only use the template from the project folder which must exist
		ICVSFolder localFolder = session.getLocalRoot().getFolder(localDir);
		IContainer container = (IContainer)localFolder.getIResource();
		ICVSStorage templateFile = null;
		if (container != null && container.exists()) {
			try {
				templateFile = CVSWorkspaceRoot.getCVSFileFor(SyncFileWriter.getTemplateFile(container));
			} catch (CVSException e) {
				// Log the inability to create the template file
				CVSProviderPlugin.log(new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, "Could not write template file in " + container.getFullPath() + ": " + e.getMessage(), e, session.getLocalRoot())); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
		if (container == null || templateFile == null) {
			// Create a dummy storage handle to recieve the contents from the server
			templateFile = new ICVSStorage() {
				public String getName() {
					return "Template"; //$NON-NLS-1$
				}
				public void setContents(
					InputStream stream,
					int responseType,
					boolean keepLocalHistory,
					IProgressMonitor monitor)
					throws CVSException {

					try ( // Transfer the contents
							OutputStream out = new ByteArrayOutputStream()) {
						byte[] buffer = new byte[1024];
						int read;
						while ((read = stream.read(buffer)) >= 0) {
							Policy.checkCanceled(monitor);
							out.write(buffer, 0, read);
						}
					} catch (IOException e) {
						throw CVSException.wrapException(e); 
					} finally {
						try {
							stream.close();
						} catch (IOException e1) {
							// Ignore close errors
						}
					}
				}
				public long getSize() {
					return 0;
				}
				public InputStream getContents() throws CVSException {
					return new ByteArrayInputStream(new byte[0]);
				}
			};
		}
		try {
			session.receiveFile(templateFile, false, UpdatedHandler.HANDLE_UPDATED, monitor);
		} catch (CVSException e) {
			if (!(templateFile instanceof ICVSFile && handleInvalidResourceName(session, (ICVSFile)templateFile, e))) {
				throw e;
			}
		}
	}

}

Back to the top