Accessing Microsoft Visual SourceSafe from Java

Java/J2EE COM Interoperability Products Page

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.

Contents

  1. Introduction
  2. Run the Java Client on Local Windows Machine
    1. Generate Java Proxies
    2. Create the Example
    3. Compile and Run the example
  3. Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc
  4. More about Visual SourceSafe programming

1 Introduction

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.

2 Run the Java Client on Local Windows Machine

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.

2.1 Generate the Java proxies

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.

2.2 Create the example

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();
    }
  }
}

2.3 Compile and run the example

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.

3 Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

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.:

  1. Move the com2java tool to the Windows machine to generate the Java proxies from Visual SourceSafe, and then move the Java proxies from the Windows machine to the Linux machine.
  2. Install J-Integra® (the jintegra.jar file) on the Linux machine and include the jintegra.jar file and generated Java proxies in CLASSPATH.
  3. Use DCOMCNFG to configure WMI on the Windows machine.
  4. Pass the IP address or computer name of the Visual SourceSafe machine to the constructor of VSSDatabase object in VssClient.java:
    VSSDatabase database = new VSSDatabase("123.456.489.0");
  5. Pass correct login credentials to com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
    Refer to Configuring DCOM for Remote Access for more information about AuthInfo.setDefault.
  6. Move VssClient.java to the Linux machine. Compile and run it in DCOM mode without using DJINTEGRA_NATIVE_MODE property:
    java VssClient "C:\Program Files\Microsoft Visual Studio\Common\VSS\srcsafe.ini" usr pwd "$/My Project/MyFile.xls" c:\MyFile.xls

4 More about Visual SourceSafe programming

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.