ClearCase查看变更集类

分享给大家自己写的一个ClearCase查看变更集的东西。
我写的ClearCase查看变更集的类,可以查看某个时间起你到底修改了cc某个流的哪些文件,输出的格式可以自己修改,我设置的输出格式是was更新的代码清单,对我们开发者来说是很方便的。因为使用lib包太大了发不了,可以在C:\Program Files\IBM\IMShared下找到。

package com.chruan.clearcase.cm.client;
/**
 * clearcase 变更集查看9月流
 * 自己可以修改doPrint()定制自己输出的格式
 * 
 * @author chruan(阮春辉)
 * @date 2012-8-8
 * 
 */
public class RifmsChangeSet {
	public static void main(String[] args) {
		ChangeSetView changeSetView = new ChangeSetView(
				"http://ccweb.chruan.com.cn/TeamWeb/services/Team", "ex_username",
				"ex_password");
		try {
			// 时间过滤(null为不限制)
			changeSetView.setStartDate(DateOp.strToDate("2012/05/01 00:00:00"));
			// 文件后缀过滤(必需是这些后缀的文件)
			changeSetView
					.setFileExtFilter(new String[] { ".java", ".jsp", ".xml" });
			// 文件名过滤(不包含的文件)
			changeSetView.setFileNoFilter(new String[] { "CardReaderAction.java",
					"Host8888.java", "EsbHostService.java",
					"TranService777015.java" });
			
			//设置视图的过滤 最好不要太多的视图,否则很慢。null或不设置为所有视图,
			//视图的名称可以在cc中看到,或视图建立的文件夹中看到。
//			changeSetView.setViewNamesFilter(new String[]{"ex_ifms7_Dev_RIFMS_2012_9月月度版本_开发"});
			
			//你的源码放在cc的位置 如lc08在D:\CCWORK\chruan_Dev_CHRUAN_2012_9月月度版本\vobs\CHRUAN_VOB\CHRUAN\sys\src
			changeSetView.setVobRoot("vobs/CHRUAN_VOB/sys/src/");
			//项目的名称
			changeSetView.setWebNames(new String[]{"tika","lucene","hbase"});
			
			//通过流查看变更集 流的名称可以在ClearCase导航器中选中想看的流,
			//在右边ClearCase详细信息窗口中地址栏看到。
			changeSetView
					.printChangeSet("stream:Dev_CHRUAN_2012_9月月度版本@/vobs/PV_OPS");
			
		}finally{
			changeSetView.terminate();
		}
	}
}



package com.chruan.clearcase.cm.client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.wvcm.ProviderFactory;
import javax.wvcm.ResourceList;
import javax.wvcm.Version;
import javax.wvcm.WvcmException;
import javax.wvcm.PropertyRequestItem.PropertyRequest;
import javax.wvcm.ProviderFactory.Callback;

import com.ibm.rational.wvcm.stp.StpLocation;
import com.ibm.rational.wvcm.stp.StpProvider;
import com.ibm.rational.wvcm.stp.cc.CcActivity;
import com.ibm.rational.wvcm.stp.cc.CcProvider;
import com.ibm.rational.wvcm.stp.cc.CcStream;
import com.ibm.rational.wvcm.stp.cc.CcVersion;
import com.ibm.rational.wvcm.stp.cc.CcView;

/**
 * clearcase 变更集查看
 * 
 * 要用到cc的包在 C:\Program Files\IBM\IMShared\plugins\com.ibm.rational.teamapi_7.1.1.v201006120047
 * @author chruan(阮春辉)
 * @version v1.0
 * @date 2012-7-6
 * 
 */
public class ChangeSetView {
	private String password;
	private String userName;
	private String serverUrl;
	private MyAuthorCallback callback;
	private StpProvider stpProvider;
	//自己的视图
	private Map ownViewList = new HashMap();
	//自己建立的活动
	private Map ownActivityList = new HashMap();
	//变更集
	private Map changeSet = new HashMap();
	//该日期后修改的
	private Date startDate = null;
	//文件后缀过滤(必需是这些后缀的文件)
	private String[] fileExt=null;
	//文件名过滤(不包含的文件)
	private String[] fileNo=null;
	private String[] filterViewNames=null;
	//你的源码放在cc的位置 如lc08在D:\CCWORK\ex_ruanch_Dev_IFMS_2012_9月月度版本\vobs\IFMS_VOB\IFMS\sys\src
	private String vobRoot=null;
	//项目的名称
	private String[] webNames;
	
	/**
	 * 
	 * @param serverUrl cc服务器地址
	 * @param userName cc用户名
	 * @param password cc密码
	 */
	public ChangeSetView(String serverUrl, String userName, String password) {
		// TODO Auto-generated constructor stub
		this.serverUrl = serverUrl;
		this.userName = userName;
		this.password = password;
		initProvider();
	}

	private void initProvider() {
		// TODO Auto-generated method stub
		this.callback = new MyAuthorCallback(userName, password);
		try {
			stpProvider = (StpProvider) ProviderFactory.createProvider(
					CcProvider.CC_ONLY_PROVIDER_CLASS, callback);
			stpProvider.ccProvider().setServerUrl(serverUrl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 打印变更集
	 * @param stream
	 */
	public void printChangeSet(String stream) {
		try {
			// 取得流
			CcStream ccstream = getccStream(stream);
			// 取得自己的视图
			doViewList(ccstream);
			// 取得自己的活动
			doActivityList(ccstream);
			// 取得变更集
			doChangeSet();
			// 格式输出
			doPrint();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	private void doPrint() {
		// TODO Auto-generated method stub
		List changeList = sort(changeSet);
		changeList = doFilter(changeList);
		//所有更新
		System.out.println("所有更新:");
		for (int idx = 0, size = changeList.size(); idx < size; idx++)
			System.out.println(changeList.get(idx));
		
		for (int idx=0,size=webNames.length;idx<size;idx++)
			doFilter2(changeList,webNames[idx]);
	}

	/**
	 * 后缀过滤 文件名过滤
	 * @param list
	 * @return
	 */
	private List doFilter(List list) {
		// TODO Auto-generated method stub
		List result = new ArrayList();
		for (int idx = 0, size = list.size(); idx < size; idx++) {
			String item = (String) list.get(idx);
			// filter file extends
			boolean find = false;
			for (int i = 0; i < fileExt.length; i++) {
				if (item.endsWith(fileExt[i])) {
//					System.out.println(item+" "+fileExt[i]);
					find = true;
					break;
				}
			}
			for (int i = 0; i < fileNo.length; i++) {
				if (item.endsWith(fileNo[i])) {
					find = false;
					break;
				}
			}
			if (find)
				result.add(item);
		}
		return result;
	}

	/**
	 * 通过web项目过滤
	 * @param list
	 * @param web
	 */
	private void doFilter2(List list,String web) {
		// TODO Auto-generated method stub
		System.out.println("============ " + web + " ===========");

		String root = vobRoot + web + "/";
		String webroot = root + "WebRoot/";
		int wlen = webroot.length();
		String srcroot = root + "src/";
		int slen = srcroot.length();

		List rl = new ArrayList();
		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			String item = (String) iter.next();
			// filter root
			if (item.startsWith(root)) {
				if (item.startsWith(webroot))
					item = item.substring(wlen);
				else if (item.startsWith(srcroot))
					item = "WEB-INF/classes/" + item.substring(slen);

				if (item.endsWith(".java"))
					item = item.substring(0, item.length() - 4) + "class";
				// System.out.println(item);
				rl.add(item);
			}
		}
		
		Collections.sort(rl);
		for (int idx = 0, size = rl.size(); idx < size; idx++)
			System.out.println(rl.get(idx));
		
		System.out.println("\n\n");
	}

	/**
	 * 排序
	 * @param changeSet2
	 * @return
	 */
	private List sort(Map changeSet2) {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		Iterator iter = changeSet2.keySet().iterator();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			list.add(key);
		}
		Collections.sort(list);
		return list;
	}

	/**
	 * 取得所有视图的所有活动的变更
	 */
	private void doChangeSet() {
		// TODO Auto-generated method stub
		Iterator viter = ownViewList.keySet().iterator();

		while (viter.hasNext()) {
			CcView view = (CcView) viter.next();
			Iterator aiter = ownActivityList.keySet().iterator();
			while (aiter.hasNext()) {
				CcActivity act = (CcActivity) aiter.next();
				doChangeSet(view, act);
			}
		}
	}

	/**
	 * 取得一个视图的所有活动的变更
	 * @param view
	 * @param act
	 */
	private void doChangeSet(CcView view, CcActivity act) {
		// TODO Auto-generated method stub
		try {
			PropertyRequest feedback = new PropertyRequest(
				CcActivity.LATEST_VERSION_LIST);
			act = (CcActivity) act.doReadProperties(view, feedback);
			ResourceList<Version> versionList = act.getLatestVersionList();
			for (int idx = 0, size = versionList.size(); idx < size; idx++) {
				CcVersion version = (CcVersion) versionList.get(idx);
				doVersion(view, version);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 取得变更的文件名
	 * @param view
	 * @param version
	 */
	private void doVersion(CcView view, CcVersion version) {
		// TODO Auto-generated method stub
		try {
			PropertyRequest feedback = new PropertyRequest(
					CcVersion.DISPLAY_NAME, CcVersion.VIEW_RELATIVE_PATH,
					CcVersion.CREATION_DATE);
			version = (CcVersion) version.doReadProperties(view, feedback);
			Date cdate = version.getCreationDate();
			if ((startDate == null)
					|| (startDate != null && cdate.after(startDate)))
				changeSet.put(version.getViewRelativePath(), null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 通过流取得所有的活动
	 * @param ccstream
	 */
	private void doActivityList(CcStream ccstream) {
		// TODO Auto-generated method stub
		try {

			PropertyRequest feedback = new PropertyRequest(
					CcStream.ACTIVITY_LIST);
			ccstream = (CcStream) ccstream.doReadProperties(feedback);
			ResourceList<CcActivity> actList = ccstream.getActivityList();
			// System.out.println(actList);
			for (int idx = 0, size = actList.size(); idx < size; idx++) {
				CcActivity act = actList.get(idx);
				doActivity(act);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 检查是不是自己的活动
	 * @param act
	 */
	private void doActivity(CcActivity act) {
		// TODO Auto-generated method stub
		try {
			PropertyRequest feedback = new PropertyRequest(
					CcActivity.CREATOR_LOGIN_NAME);
			act = (CcActivity) act.doReadProperties(feedback);
			String login = act.getCreatorLoginName();
			// System.out.println(login);
			// 如果是自己建立的活动
			if (userName.equals(login))
				ownActivityList.put(act, null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	/**
	 * 通过流取得所有视图
	 * @param ccstream
	 */
	private void doViewList(CcStream ccstream) {
		// TODO Auto-generated method stub
		try {
			PropertyRequest feedback = new PropertyRequest(CcStream.VIEW_LIST);
			ccstream = (CcStream) ccstream.doReadProperties(feedback);
			ResourceList<CcView> viewList = ccstream.getViewList();
			// System.out.println(viewList);
			for (int idx = 0, size = viewList.size(); idx < size; idx++) {
				CcView view = viewList.get(idx);
				doView(view);
				// break;
			}

			// System.out.println(ownViewList.size());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 检查你是否有权限的视图,如果有权限就可以看作是你的视图
	 * @param view
	 */
	private void doView(CcView view) {
		// TODO Auto-generated method stub
		try {
			PropertyRequest feedback = new PropertyRequest(CcView.DISPLAY_NAME,
					CcView.CREATOR_LOGIN_NAME);
			view = (CcView) view.doReadProperties(feedback);
			view.getCreatorLoginName();
			String name = view.getDisplayName();
			
			if (filterViewNames !=null && filterViewNames.length>0){
				for (int idx=0;idx< filterViewNames.length;idx++){
					if (name.equals(filterViewNames[idx])){
						ownViewList.put(view, null);
						System.out.println("视图:"+name);
						break;
					}
				}
			}else{
				ownViewList.put(view, null);
				System.out.println("视图:"+name);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}
	}

	/**
	 * 关闭provider
	 */
	public void terminate() {
		if (stpProvider != null)
			try {
				stpProvider.terminate();
			} catch (WvcmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	/**
	 * 取得流
	 * @param location
	 * @return
	 */
	private CcStream getccStream(String location) {
		// TODO Auto-generated method stub
		try {
			StpLocation loc = stpProvider.stpLocation(location);
			return (CcStream) stpProvider.ccProvider().ccStream(loc);
		} catch (WvcmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	private static class MyAuthorCallback implements Callback {
		private final String loginName;
		private final String password;

		public MyAuthorCallback(String loginName, String password) {
			// TODO Auto-generated constructor stub
			this.loginName = loginName;
			this.password = password;
		}

		public Authentication getAuthentication(String realm, int retryCount)
				throws WvcmException {
			// TODO Auto-generated method stub
			return new Authentication() {
				public String loginName() {
					// TODO Auto-generated method stub
					return loginName;
				}

				public String password() {
					// TODO Auto-generated method stub
					return password;
				}
			};
		}
	}

	/**
	 * 文件后缀过滤(必需是这些后缀的文件)
	 * @param fileExt
	 */
	public void setFileExtFilter(String[] fileExt) {
		// TODO Auto-generated method stub
		this.fileExt = fileExt;
	}

	/**
	 * 文件名过滤(不包含的文件)
	 * @param fileNo
	 */
	public void setFileNoFilter(String[] fileNo) {
		// TODO Auto-generated method stub
		this.fileNo = fileNo;
	}

	/**
	 * 时间过滤(null为不限制)
	 * @param startDate
	 */
	public void setStartDate(Date startDate) {
		// TODO Auto-generated method stub
		this.startDate = startDate;
	}
	public void setWebNames(String[] webNames) {
		// TODO Auto-generated method stub
		this.webNames = webNames;
	}

	public String[] getFilterViewNames() {
		return filterViewNames;
	}

	public void setViewNamesFilter(String[] filterViewNames) {
		this.filterViewNames = filterViewNames;
	}

	public String getVobRoot() {
		return vobRoot;
	}

	public void setVobRoot(String vobRoot) {
		this.vobRoot = vobRoot;
	}
	
}

你可能感兴趣的:(exception,list,String,HashMap,null,iterator)