VSS,即Visual Source Safe,是微软公司开发的Windows平台上优秀的Source版本控制器。不同于CVS的是,它不是开源,因此支持VSS的只有Windows平台。但微软发布了VSS的API。任何开发者都可以通过ssapi.dll文件,开发自定义的VSS插件或其他工具。Ssapi.dll是ActiveX组件,自然能支持多种开发语言。但想要支持Java,就得使用JNI了。
本文使用JNI实现访问VSS的Java api。
1.VSS Java API
定义VSS Java API。
package org.colimas.scm.vss;
/**
* VSS Operator class that will call native method.
* @author tyrone
*
*/
public class VSSOperator {
private static String LIBPATH="vssperator.dll";
public static String ROOT="$/";
/**
* load dll
*
*/
public VSSOperator(){
String path=Thread.currentThread().getContextClassLoader().getResource("./").getPath();
path=path+LIBPATH;
System.load(path);
}
/**
* load dll
*
*/
public VSSOperator(String libPath){
System.load(libPath);
}
/**
* Connect to VSS
* @param path srcsafe.ini file path.
* @param id user id
* @param pass password
* @return
*/
public native boolean connection(String path,String id,String pass);
/**
* disconnect from VSS
*
*/
public native void disconnection();
/**
* get current project.
* @return
*/
public native String getCurrentProject();
/**
* get database name
* @return
*/
public native String getDatabaseName();
/**
* get item names from specified path.
* @param path
* @return
*/
public native String[] getItemNames(String path);
/**
* get specified file(s) from VSS to local.
* @param itemName
* @param localPath
* @param flag
* @return
*/
public native boolean getFile(String itemName,String localPath,int flag);
/**
* get specified version of one file from VSS to local.
* @param itemName
* @param localPath
* @param version
* @param flag
* @return
*/
public native boolean getVersion(String itemName,String localPath,int version,int flag);
/**
* checkout specified file(s) from vss to local.
* @param itemName
* @param localPath
* @param comment
* @param flag
* @return
*/
public native boolean checkout(String itemName,String localPath,String comment,int flag);
/**
* check file(s) in VSS
* @param itemName
* @param localPath
* @param comment
* @param flag
* @return
*/
public native boolean checkin(String itemName,String localPath,String comment,int flag);
/**
* undo check out action
* @param itemName
* @param localPath
* @param flag
* @return
*/
public native boolean undoCheckout(String itemName,String localPath,int flag);
}
每个native方法都由VC++实现。
2.VSS的API
CodeProject网站上有一些VSS API的相关文章,本文借鉴了下面link的内容。
http://www.codeproject.com/samples/SSBrowser.asp。
从该网站上下载source,内有一个头文件ssauto.h是访问VSS API的关键。该文件定义了VSSAPI涉及到的所有接口,本文使用的接口有:
IVSSDatabase:VSS数据库接口。该接口负责创建连接和获得IVSSItem。
IVSSItem:VSS内的某个文件或工程,该接口负责返回文件或项目信息,Checkout,Checkin,获得文件等。
IVSSItems:VSS内某些文件或工程,IVSSItem集合。
3.Native方法实现
3.1 搭建开发环境
3.1.1.使用javah命令生成JNI的头文件。
D:/ >javah -classpath ./;D:/MyProject/colima
s/ra/SCMResources/bin/classes org.colimas.scm.vss.VSSOperator
生成一个叫org_colimas_scm_vss_VSSOperator.h的文件。
3.1.2.创建VC++的Dynamic Link Library支持MFC的工程。将org_colimas_scm_vss_VSSOperator.h文件和ssauto.h文件添加到工程内。
3.1.3.在StdAfx.h头文件加入:
#include "ssauto.h"
#include <atlbase.h>
#include <comdef.h>
extern "C" const GUID __declspec(selectany) LIBID_SourceSafeTypeLib =
{0x783cd4e0,0x9d54,0x11cf,{0xb8,0xee,0x00,0x60,0x8c,0xc9,0xa7,0x1f}};
extern "C" const GUID __declspec(selectany) IID_IVSSItem =
{0x783cd4e1,0x9d54,0x11cf,{0xb8,0xee,0x00,0x60,0x8c,0xc9,0xa7,0x1f}};
extern "C" const GUID __declspec(selectany) IID_IVSSItems =
{0x783cd4e5,0x9d54,0x11cf,{0xb8,0xee,0x00,0x60,0x8c,0xc9,0xa7,0x1f}};
extern "C" const GUID __declspec(selectany) IID_IVSSDatabase =
{0x783cd4e2,0x9d54,0x11cf,{0xb8,0xee,0x00,0x60,0x8c,0xc9,0xa7,0x1f}};
extern "C" const GUID __declspec(selectany) CLSID_VSSItem =
{0x783cd4e3,0x9d54,0x11cf,{0xb8,0xee,0x00,0x60,0x8c,0xc9,0xa7,0x1f}};
extern "C" const GUID __declspec(selectany) CLSID_VSSDatabase =
{0x783cd4e4,0x9d54,0x11cf,{0xb8,0xee,0x00,0x60,0x8c,0xc9,0xa7,0x1f}};
extern "C" const GUID __declspec(selectany) IID_IVSS =
{0x783cd4eb,0x9d54,0x11cf,{0xb8,0xee,0x00,0x60,0x8c,0xc9,0xa7,0x1f}};
//并为一些接口定义智能指针
_COM_SMARTPTR_TYPEDEF(IVSSItem, IID_IVSSItem);
_COM_SMARTPTR_TYPEDEF(IVSSItems, IID_IVSSItems);
3.1.4.在Option的Directory内加入JDK include和include/win32路径。
3.2 方法实现
在VC++源程序内加入
#include "org_colimas_scm_vss_VSSOperator.h"
并定义全局变量。
IVSSDatabase *pVdb;
IClassFactory *pClf;
3.2.1 connection方法实现
JNI定义为
JNIEXPORT jboolean JNICALL Java_org_colimas_scm_vss_VSSOperator_connection
(JNIEnv *env, jobject, jstring VSSini, jstring User, jstring Password)
在注册表中查找VSS API组件
CLSIDFromProgID(L"SourceSafe", &clsid )
//实例化类工厂。
CoGetClassObject( clsid, CLSCTX_ALL, NULL,
IID_IClassFactory, (void**)&pClf )
//实例化IVSSDatabase
pClf->CreateInstance( NULL, IID_IVSSDatabase,
(void **) &pVdb )
//创建一个VSS连接。
pVdb->Open(bstrPath, bstrUName, bstrUPass)
代码如下:
JNIEXPORT jboolean JNICALL Java_org_colimas_scm_vss_VSSOperator_connection
(JNIEnv *env, jobject, jstring VSSini, jstring User, jstring Password)
{
//将参数转换为C++基本类型。
const unsigned short * psz_VSSini=env->GetStringChars(VSSini, 0);
const unsigned short * psz_User=env->GetStringChars(User, 0);
const unsigned short * psz_Password=env->GetStringChars(Password, 0);
//再将这些变量转化为COM能识别的类型。
BSTR bstrPath = SysAllocString(psz_VSSini);
BSTR bstrUName = SysAllocString(psz_User);
BSTR bstrUPass = SysAllocString(psz_Password);
CLSID clsid;
jboolean result=JNI_FALSE;
CoInitialize(0);
if(S_OK == CLSIDFromProgID(L"SourceSafe", &clsid ))
{
if(S_OK == CoGetClassObject( clsid, CLSCTX_ALL, NULL,
IID_IClassFactory, (void**)&pClf ))
{
if(S_OK == pClf->CreateInstance( NULL, IID_IVSSDatabase,
(void **) &pVdb ))
{
if(S_OK == pVdb->Open(bstrPath, bstrUName, bstrUPass))
{
#ifdef _DEBUG
AfxMessageBox("Open successfully",MB_OK,0);
#endif
result=JNI_TRUE;
}else{
#ifdef _DEBUG
AfxMessageBox("Open failed",MB_OK,0);
#endif
}
}else
{
AfxMessageBox("Init failed",MB_OK,0);
}
}
}else
{
AfxMessageBox("You didnot install VSS yet",MB_OK,0);
}
//CoUninitialize();
SysFreeString(bstrPath);
SysFreeString(bstrUName);
SysFreeString(bstrUPass);
return result;
}
3.2.2 disconnectin实现
JNIEXPORT void JNICALL Java_org_colimas_scm_vss_VSSOperator_disconnection
(JNIEnv *, jobject)
{
pVdb->Release();
pClf->Release();
CoUninitialize();
#ifdef _DEBUG
AfxMessageBox("Close successfully",MB_OK,0);
#endif
}
3.2.3 getDatabaseName和getCurrentProject实现
JNIEXPORT jstring JNICALL Java_org_colimas_scm_vss_VSSOperator_getDatabaseName
(JNIEnv * env, jobject)
{
if(pVdb==NULL)
return NULL;
CComBSTR bstrDatabase;
//调用IVSSDatabase方法
if(S_OK==pVdb->get_DatabaseName(&bstrDatabase))
{
return env->NewString(bstrDatabase,bstrDatabase.Length());
}else{
#ifdef _DEBUG
&nbs

本文介绍如何使用Java Native Interface (JNI) 实现对Visual Source Safe (VSS) 的API调用,从而在Java中进行VSS的操作,如连接、断开、获取项目、数据库名、文件等。通过VC++编写动态链接库,实现Java中的native方法,调用VSS的COM组件进行交互。
的Java API&spm=1001.2101.3001.5002&articleId=1487903&d=1&t=3&u=0caf1c8c27fd4326b1e7aa6be58a9b48)
1万+

被折叠的 条评论
为什么被折叠?



