KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > filesystem > RemoteLogger


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.filesystem;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.team.core.TeamException;
17 import org.eclipse.team.internal.ccvs.core.*;
18 import org.eclipse.team.internal.ccvs.core.client.*;
19 import org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry;
20 import org.eclipse.team.internal.ccvs.core.client.listeners.LogListener;
21 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
22 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderTree;
23
24 public class RemoteLogger {
25
26     static final String JavaDoc DEAD_STATE = "dead"; //$NON-NLS-1$
27

28     private ICVSRemoteFolder remoteFolder;
29     private LogEntryCache cache;
30     
31     private RLogTreeBuilder treeBuilder;
32
33     public RemoteLogger(ICVSRemoteFolder folder) {
34         this.remoteFolder = folder;
35     }
36
37     private void getRemoteChildren(CVSTag tag, IProgressMonitor monitor) {
38         Session session = new Session(this.remoteFolder.getRepository(), this.remoteFolder, false /* output to console */);
39         try {
40             // Build the local options
41
this.cache = new LogEntryCache();
42             LogListener listener = new LogListener(cache);
43
44             Command.LocalOption[] localOptions = getLocalOptions(tag, null);
45             try {
46                 session.open(Policy.subMonitorFor(monitor, 10));
47                 RLog rlog = new RLog();
48                 rlog.execute(session, Command.NO_GLOBAL_OPTIONS, localOptions, new ICVSRemoteResource[] {this.remoteFolder}, listener, Policy.subMonitorFor(monitor, 90));
49             } catch (CVSException e) {
50             }
51         } finally {
52             session.close();
53         }
54     }
55
56     public ICVSResource[] fetchChildren(IProgressMonitor monitor) throws CVSException, TeamException {
57         return fetchTree(monitor).getChildren();
58     }
59
60     public HashMap JavaDoc getFolderMap() {
61         return treeBuilder.getFolderMap();
62     }
63
64     public RemoteFolderTree fetchTree(IProgressMonitor monitor) throws CVSException, TeamException {
65         try{
66         monitor.beginTask(null, 100);
67         CVSTag tag = this.remoteFolder.getTag();
68         if (tag == null)
69             tag = CVSTag.DEFAULT;
70
71         getRemoteChildren(tag, new SubProgressMonitor(monitor,70));
72
73         final ICVSRemoteFolder project = this.remoteFolder;
74         //Get the entry paths
75
String JavaDoc[] entry = this.cache.getCachedFilePaths();
76
77         treeBuilder = new RLogTreeBuilder(project.getRepository(), tag, cache);
78         for (int i = 0; i < entry.length; i++) {
79             ILogEntry[] logEntry = this.cache.getLogEntries(entry[i]);
80
81             //might not have state if this a branch entry
82
if (logEntry[0].getState() != null && logEntry[0].getState().equals(DEAD_STATE))
83                 continue;
84
85             ICVSRemoteFile remoteFile = logEntry[0].getRemoteFile();
86             //if the current folder tag is a branch tag, we need to take the extra step
87
//of making sure that the file's revision number has been set appropriately
88
if (tag.getType() == CVSTag.BRANCH && remoteFile.getRevision().equals(LogListener.BRANCH_REVISION))
89                 verifyRevision(tag, logEntry[0], remoteFile);
90
91             IPath logPath = new Path(null, remoteFile.getRepositoryRelativePath());
92             if (logPath.segmentCount() > 0) {
93                 //trim everything up to the project segment
94
String JavaDoc[] pathSegments = logPath.segments();
95                 int index;
96                 String JavaDoc projectName = project.getName();
97                 for (index = 0; index < pathSegments.length; index++) {
98                     if (pathSegments[index].equals(projectName))
99                         break;
100                 }
101                 logPath = logPath.removeFirstSegments(index + 1);
102             }
103             treeBuilder.newFile(logPath, remoteFile);
104         }
105
106         return treeBuilder.getTree();
107         }
108         finally{
109             monitor.done();
110         }
111     }
112
113     protected Command.LocalOption[] getLocalOptions(CVSTag tag1, CVSTag tag2) {
114         if (tag1 != null && tag2 != null) {
115             return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.makeTagOption(tag1, tag2)};
116         } else if (tag1 != null) {
117             if (tag1.getType() == CVSTag.HEAD || tag1.getType() == CVSTag.VERSION)
118                 return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.getCurrentTag(tag1)};
119
120             if (tag1.getType() == CVSTag.DATE)
121                 return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.REVISIONS_ON_DEFAULT_BRANCH, RLog.getCurrentTag(tag1)};
122             //branch tag
123
return new Command.LocalOption[] {RLog.getCurrentTag(tag1)};
124         } else {
125             return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES};
126         }
127     }
128
129     private void verifyRevision(CVSTag tag, ILogEntry entry, ICVSRemoteFile remoteFile) throws CVSException {
130         if (entry instanceof LogEntry) {
131             LogEntry logEntry = (LogEntry) entry;
132             String JavaDoc[] allBranchRevisions = logEntry.getBranchRevisions();
133             CVSTag[] allCVSTags = entry.getTags();
134             for (int i = 0; i < allCVSTags.length; i++) {
135                 if (allCVSTags[i].equals(tag)) {
136                     //get the revision number stored for this tag
137
((RemoteFile) remoteFile).setRevision(allBranchRevisions[i]);
138                     break;
139                 }
140             }
141         }
142     }
143
144     public HashMap JavaDoc getLogMap() {
145         return treeBuilder.getLogMap();
146     }
147
148 }
149
Popular Tags