Accessing Microsoft Visual SourceSafe from Java |
This example demonstrates how to access Microsoft Visual SourceSafe from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Microsoft Visual SourceSafe. It provides bi-directional access of Java objects and COM components.
This example shows how you can use the J-Integra® to talk from a Java client to Microsoft Visual SourceSafe using the COM API that Visual SourceSafe exposes, in order to check an item out of Visual SourceSafe. You can run the Java client on a Windows machine to access its local Visual SourceSafe, or run the Java client on a non-Windows machine (such as Linux) to access Visual SourceSafe installed on a remote Windows machine.
We assume that you are familiar with Java -- no COM knowledge should be required. We assume you have downloaded and expanded the J-Integra® kit from http://j-integra.intrinsyc.com/ and installed it correctly.
You can try this example on local Windows machine first to get a feel for how easy it is to use J-Integra® to access Microsoft Visual SourceSafe from Java. Once you make it working on local machine, you can then try to run the Java client on a non-Windows machine to remotely access Visual SourceSafe on another Windows machine.
Run J-Integra®'s com2java tool on the Windows machine, and select C:\Program Files\Microsoft Visual Studio\Common\VSS\win32\SSAPI.DLL as the type library, choose an empty directory named SSAPI, (e.g. C:\SSAPI) as output directly, and use SSAPI as Java package name. Click the Generate Proxies button to generate Java proxies from Visual SourceSafe type library.
Create an Excel document called "c:\MyFile.xls".
Create a .java file named VssClient.java. Then copy and paste the Java code below:
import SSAPI.*; public class VssClient { public static void main(String[] args) throws Exception { String srcSafeIni = args[0]; String username = args[1]; String password = args[2]; String spec = args[3]; String local = args[4]; try { // DCOM authentication: Make sure Nt Domain, Nt User, Nt Password are valid credentials. // Uncomment this line if VssClient.java remotely accesses Visual SourceSafe: // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD"); // Specify host name or IP address of Visual SourceSafe machine as parameter if // VssClient.java remotely accesses Visual SourceSafe. // VSSDatabase database = new VSSDatabase("123.456.489.0"); VSSDatabase database = new VSSDatabase(); database.open(srcSafeIni, username, password); System.out.println(database.getUsername()); IVSSItem item = database.getVSSItem(spec, false); item.checkout("This is a Test comment", local, 0); } finally { com.linar.jintegra.Cleaner.releaseAll(); } } } |
On the Java client machine, make sure your CLASSPATH includes jintegra.jar from the J-Integra® kit, update and then compile JavaSWBEM.java. If you get compilation errors then chances are your CLASSPATH is incorrect, or you have not copied the proxies across per step 2. Compile and run the example in J-Integra®'s native mode (you need to use DCOM mode if remotely accessing Visual SourceSafe):
javac VssClient.java
java -DJINTEGRA_NATIVE_MODE VssClient "C:\Program Files\Microsoft Visual
Studio\Common\VSS\srcsafe.ini" usr pwd "$/My Project/MyFile.xls"
c:\MyFile.xls
Your srcsafe.ini file may be in a different directory.
You can also run the Java client on a remote machine, such as Linux, Solaris, UNIX and AIX. For instance, if you run it on a Linux machine, then you must do the following.:
We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about Microsoft Visual SourceSafe programming, please refer to the Microsoft Visual SourceSafe Object Model. It's also easier to find a VB example first, and then covert the VB example to Java program using the reference Mapping VB Code to Java Code.