001 /* 002 * file Example.java 003 * 004 * Licensed Materials - Property of IBM 005 * Restricted Materials of IBM - you are allowed to copy, modify and 006 * redistribute this file as part of any program that interfaces with 007 * IBM Rational CM API. 008 * 009 * com.ibm.rational.stp.client.samples.Example 010 * 011 * © Copyright IBM Corporation 2005, 2008. All Rights Reserved. 012 * Note to U.S. Government Users Restricted Rights: Use, duplication or 013 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 014 */ 015 016 package com.ibm.rational.stp.client.samples; 017 018 import java.io.File; 019 020 import javax.wvcm.ProviderFactory; 021 import javax.wvcm.Version; 022 import javax.wvcm.WvcmException; 023 import javax.wvcm.PropertyRequestItem.PropertyRequest; 024 import javax.wvcm.ProviderFactory.Callback; 025 026 import com.ibm.rational.wvcm.stp.StpLocation; 027 import com.ibm.rational.wvcm.stp.StpProvider; 028 import com.ibm.rational.wvcm.stp.StpProvider.Domain; 029 import com.ibm.rational.wvcm.stp.cc.CcDirectory; 030 import com.ibm.rational.wvcm.stp.cc.CcFile; 031 import com.ibm.rational.wvcm.stp.cc.CcProvider; 032 import com.ibm.rational.wvcm.stp.cc.CcVersion; 033 034 /** 035 * This sample application uses the ClearCase CM API to 036 * put a file under source control. 037 */ 038 public class Example { 039 040 private static boolean tracing = true; 041 042 /** 043 * CM API Authentication callback. This callback is invoked when the 044 * ClearCase CM API provider logs in to the CM Server. 045 */ 046 private static class MyAuthCallback implements Callback { 047 048 private final String serverUrl; 049 private final String login; 050 private final String password; 051 052 /** 053 * Construct a new authentication callback with the correct credentials 054 * for the specified CM server. 055 */ 056 public MyAuthCallback(String serverUrl, String login, String password) { 057 this.serverUrl = serverUrl; 058 this.login = login; 059 this.password = password; 060 } 061 062 /* (non-Javadoc) 063 * @see javax.wvcm.ProviderFactory$Callback#getAuthentication(java.lang.String, int) 064 */ 065 public Authentication getAuthentication(String realm, int retries) { 066 trace("Logging in to CM Server: " + serverUrl); 067 trace("Login: " + login); 068 trace("Password: " + password); 069 070 // Retry count > 0 means authentication failed. 071 // Perhaps password was wrong or login domain was left out? 072 myAssert(retries == 0); 073 074 return new Authentication() { 075 public String loginName() { 076 // On Windows servers, this should return domain and 077 // username separated by a backslash 078 return login; 079 } 080 public String password() { 081 return password; 082 } 083 }; 084 } 085 } 086 087 static void printUsageMsg() { 088 System.out.println("This program expects the following arguments:"); 089 System.out.println("1) URL of a ClearCase CM Server"); 090 System.out.println("2) CM Server login"); 091 System.out.println(" If the CM Server is a Windows system,"); 092 System.out.println(" specify both the domain and login"); 093 System.out.println(" separated by a backslash: domain\\login."); 094 System.out.println("3) CM Server password"); 095 System.out.println("4) Test file to add to source control"); 096 System.out.println(" This file must reside in a loaded, source-"); 097 System.out.println(" controlled directory in a ClearCase"); 098 System.out.println(" web view on the local machine."); 099 System.out.println(); 100 System.out.println("For example:"); 101 System.out.println(); 102 System.out.println(" http://my_cm_server:9080/TeamWeb/services/Team my_domain\\my_login"); 103 System.out.println(" my_password C:/webviews/a/b/c.txt"); 104 } 105 106 /** 107 * Get the ClearCase CM API provider for this example. 108 * Establish authentication callback for logging in to the CM Server. 109 * @return new ClearCase provider 110 * @throws Exception if provider cannot be instantiated 111 */ 112 static CcProvider getProvider( 113 String serverUrl, String login, String password) throws Exception 114 { 115 Callback callback = new MyAuthCallback(serverUrl, login, password); 116 StpProvider provider = (StpProvider) 117 ProviderFactory.createProvider(CcProvider.CC_ONLY_PROVIDER_CLASS, callback); 118 119 provider.setServerUrl(serverUrl); 120 121 return provider.ccProvider(); 122 } 123 124 /** 125 * Construct a CcFile proxy for the specified file. 126 */ 127 static CcFile buildProxy(File f, CcProvider provider) 128 throws WvcmException 129 { 130 StpLocation fileLoc = provider.filePathLocation(Domain.CLEAR_CASE, f); 131 CcFile file = provider.ccFile(fileLoc); 132 133 // Resolve the file to the correct resource type - 134 // CcFile, CcDirectory, etc. 135 return (CcFile) file.doResolve(); 136 } 137 138 /** 139 * Print some interesting properties of the specified file. 140 */ 141 static void printProps(CcFile file) throws WvcmException { 142 143 PropertyRequest wantedProps = new PropertyRequest( 144 CcFile.IS_VERSION_CONTROLLED, 145 CcFile.IS_CHECKED_OUT, 146 CcFile.VERSION.nest( 147 Version.CREATION_DATE, 148 Version.VERSION_NAME)); 149 150 file = (CcFile) file.doReadProperties(wantedProps); 151 152 trace("File is version controlled: " + file.getIsVersionControlled()); 153 trace("File is checked out: " + file.getIsCheckedOut()); 154 155 CcVersion version = file.getVersion(); 156 157 trace("File's checked in version: " + version.getVersionName()); 158 trace("Checked in version created on: " + version.getCreationDate()); 159 } 160 161 /** 162 * Put the specified file under ClearCase source control. The file must 163 * reside in a ClearCase web view in a source-controlled directory, 164 * but must not already be under source control itself. 165 */ 166 static void addToSourceControl(CcFile res, CcDirectory parent) 167 throws Exception 168 { 169 PropertyRequest wantedProps = new PropertyRequest( 170 CcFile.IS_CHECKED_OUT, 171 CcFile.IS_VERSION_CONTROLLED); 172 173 trace("Verifying that file is NOT already source controlled..."); 174 res = (CcFile) res.doReadProperties(wantedProps); 175 myAssert(res.getIsVersionControlled() == false); 176 177 trace("Verifying that the parent folder IS source controlled..."); 178 parent = (CcDirectory) parent.doReadProperties(wantedProps); 179 myAssert(parent.getIsVersionControlled() == true); 180 181 if (parent.getIsCheckedOut()) { 182 trace("Parent folder is already checked out"); 183 } else { 184 // In order to add the test file to source control, 185 // the parent directory must first be checked out. 186 trace("Checking out parent folder..."); 187 parent.doCheckout(null, null); 188 } 189 190 trace("Adding file to source control..."); 191 res.doVersionControl(null); 192 193 trace("Checking in parent folder..."); 194 parent.doCheckin(null, null); 195 } 196 197 static void myAssert(boolean expr) { 198 if ( ! expr) { 199 throw new AssertionError(); 200 } 201 } 202 203 static void trace(String string) { 204 if (tracing) { 205 System.out.println("##### " + string); 206 } 207 } 208 209 public static void main(String[] args) { 210 211 trace("Processing program arguments"); 212 if (args.length != 4) { 213 printUsageMsg(); 214 return; 215 } 216 String CMServerUrl = args[0]; 217 String login = args[1]; 218 String password = args[2]; 219 File testFile = new File(args[3]); 220 221 try { 222 trace("Instantiating ClearCase CM API provider"); 223 CcProvider provider = getProvider(CMServerUrl, login, password); 224 225 trace("Constructing proxy for test file"); 226 CcFile testRes = buildProxy(testFile, provider); 227 228 trace("Constructing proxy for test file's parent directory"); 229 File parentDir = testFile.getParentFile(); 230 CcDirectory parent = (CcDirectory) buildProxy(parentDir, provider); 231 232 trace("Adding test file to source control: " + testFile); 233 addToSourceControl(testRes, parent); 234 235 trace("Printing test file's properties"); 236 printProps(testRes); 237 238 trace("Done"); 239 240 } catch (Exception e) { 241 trace("ClearCase CM API example failed. Printing stack trace."); 242 e.printStackTrace(); 243 } 244 } 245 }