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
AfxMessageBox("Get DB failed",MB_OK,0);
#endif
return NULL;
}
}
/*
*Get current project in vss
*/
JNIEXPORT jstring JNICALL Java_org_colimas_scm_vss_VSSOperator_getCurrentProject
(JNIEnv *env, jobject)
{
if(pVdb==NULL)
return NULL;
CComBSTR bstrProject;
//调用IVSSDatabase方法
if(S_OK==pVdb->get_CurrentProject(&bstrProject))
{
return env->NewString(bstrProject,bstrProject.Length());
}else
return NULL;
}
3.2.4 getFile实现
JNIEXPORT jboolean JNICALL Java_org_colimas_scm_vss_VSSOperator_getFile
(JNIEnv *env, jobject, jstring itemName, jstring localPath, jint flag)
{
jboolean result=JNI_FALSE;
if(pVdb==NULL)
return result;
IVSSItemPtr vssRootItem;
const unsigned short *lpath=env->GetStringChars(itemName,0);
const unsigned short *llocalPath=env->GetStringChars(localPath,0);
//获得Item实例
if(pVdb->get_VSSItem(CComBSTR(lpath), FALSE, &vssRootItem)==S_OK)
{
//获得文件,并将文件下载到本地目录。
if(vssRootItem->Get(&CComBSTR(llocalPath),flag)==S_OK)
{
result=JNI_TRUE;
}
}
return result;
}
3.2.5 getItemNames实现
JNIEXPORT jobjectArray JNICALL Java_org_colimas_scm_vss_VSSOperator_getItemNames
(JNIEnv *env, jobject, jstring path)
{
if(pVdb==NULL)
return NULL;
jobjectArray nameArray;
IVSSItemPtr vssRootItem;
const unsigned short *lpath=env->GetStringChars(path,0);
//获得父Item。
if(pVdb->get_VSSItem(CComBSTR(lpath), FALSE, &vssRootItem)==S_OK)
{
//获得所有子Item集合
IVSSItemsPtr vss_items;
if(vssRootItem->get_Items(_variant_t(false), &vss_items)==S_OK)
{
long l_count;
CComBSTR name;
//获得子Item个数
vss_items->get_Count(&l_count);
nameArray=env->NewObjectArray(l_count,env->FindClass("java/lang/String"),env->NewStringUTF(""));
//遍历
for (long i = 0; i < l_count; i++)
{
IVSSItemPtr vss_ChildItem;
//获得子Item
if(vss_items->get_Item(_variant_t(i+1L), &vss_ChildItem)==S_OK)
{
//获得子Item名称
vss_ChildItem->get_Name(&name);
jstring jname=env->NewString(name,name.Length());
env->SetObjectArrayElement(nameArray,i,jname);
}else
break;
}
}
}
return nameArray;
}
3.2.6 getVersion实现
JNIEXPORT jboolean JNICALL Java_org_colimas_scm_vss_VSSOperator_getVersion
(JNIEnv *env, jobject, jstring itemName, jstring localPath, jint version,jint flag)
{
jboolean result=JNI_FALSE;
if(pVdb==NULL)
return result;
IVSSItemPtr vssRootItem;
const unsigned short *lpath=env->GetStringChars(itemName,0);
const unsigned short *llocalPath=env->GetStringChars(localPath,0);
//获得Item实例
if(pVdb->get_VSSItem(CComBSTR(lpath), FALSE, &vssRootItem)==S_OK)
{
IVSSItemPtr vss_oldItem;
//long l_version=version;
//获得该Item某个版本实例
if(vssRootItem->get_Version(_variant_t(version), &vss_oldItem)==S_OK){
//下载到本地。
if(vss_oldItem->Get(&CComBSTR(llocalPath),flag)==S_OK)
{
result=JNI_TRUE;
}
}
}
return result;
}
3.2.7 checkout实现
JNIEXPORT jboolean JNICALL Java_org_colimas_scm_vss_VSSOperator_checkout
(JNIEnv *env, jobject , jstring itemName, jstring localPath, jstring comment,jint flag)
{
jboolean result=JNI_FALSE;
if(pVdb==NULL)
return result;
IVSSItemPtr vssRootItem;
const unsigned short *lpath=env->GetStringChars(itemName,0);
const unsigned short *l_localPath=env->GetStringChars(localPath,0);
const unsigned short *l_comment=env->GetStringChars(comment,0);
//获得Item实例
if(pVdb->get_VSSItem(CComBSTR(lpath), FALSE, &vssRootItem)==S_OK)
{
//checkout到本地目录
if(vssRootItem->Checkout(CComBSTR(l_comment),CComBSTR(l_localPath),flag)==S_OK)
{
result=JNI_TRUE;
}else
AfxMessageBox("checked it out fail",MB_OK);
}
return result;
}
3.2.8 checkin实现
JNIEXPORT jboolean JNICALL Java_org_colimas_scm_vss_VSSOperator_checkin
(JNIEnv *env, jobject, jstring itemName, jstring localPath, jstring comment, jint flag)
{
jboolean result=JNI_FALSE;
if(pVdb==NULL)
return result;
IVSSItemPtr vssRootItem;
const unsigned short *lpath=env->GetStringChars(itemName,0);
const unsigned short *l_localPath=env->GetStringChars(localPath,0);
const unsigned short *l_comment=env->GetStringChars(comment,0);
//获得Item实例
if(pVdb->get_VSSItem(CComBSTR(lpath), FALSE, &vssRootItem)==S_OK)
{
//checkin
if(vssRootItem->Checkin(CComBSTR(l_comment),CComBSTR(l_localPath),flag)==S_OK)
{
result=JNI_TRUE;
}else
{
AfxMessageBox("checked it in fail",MB_OK);
}
}
return result;
}
3.2.9 undoCheckout实现
JNIEXPORT jboolean JNICALL Java_org_colimas_scm_vss_VSSOperator_undoCheckout
(JNIEnv *env, jobject, jstring itemName, jstring localPath, jint flag)
{
jboolean result=JNI_FALSE;
if(pVdb==NULL)
return result;
IVSSItemPtr vssRootItem;
const unsigned short *lpath=env->GetStringChars(itemName,0);
const unsigned short *l_localPath=env->GetStringChars(localPath,0);
//获得Item实例
if(pVdb->get_VSSItem(CComBSTR(lpath), FALSE, &vssRootItem)==S_OK)
{
//undo
if(vssRootItem->UndoCheckout(CComBSTR(l_localPath),flag)==S_OK)
{
result=JNI_TRUE;
}else
AfxMessageBox("undochecked it out fail",MB_OK);
}
return result;
}
4.测试
VSS API用到一些Flag。我将他们定义到Java接口文件内。Flag的具体作为不做叙述。
package
org.colimas.scm.vss;
/**
*
@author
tyrone
*
*/
public
interface
VSSFlag {
public
int
VSSFLAG_USERRONO
= 1;
public
int
VSSFLAG_USERROYES
= 2;
public
int
VSSFLAG_TIMENOW
= 4;
public
int
VSSFLAG_TIMEMOD
= 8;
public
int
VSSFLAG_TIMEUPD
= 12;
public
int
VSSFLAG_EOLCR
= 16;
public
int
VSSFLAG_EOLLF
= 32;
public
int
VSSFLAG_EOLCRLF
= 48;
public
int
VSSFLAG_REPASK
= 64;
public
int
VSSFLAG_REPREPLACE
= 128;
public
int
VSSFLAG_REPSKIP
= 192;
public
int
VSSFLAG_REPMERGE
= 256;
/**
*
Specifies
a
full
text
comparison.
*
Use
this
flag
with
the
Checkin,
Get,
or
UndoCheckout
methods
to
compare
the
text
contents
of
the
local
copy
and
the
SourceSafe
master
copy.
If
the
local
copy
is
up
-
to
-
date,
SourceSafe
does
not
replace
it
with
the
master
copy.
*/
public
int
VSSFLAG_CMPFULL
= 512;
/**
*
Specifies
a
timestamp
comparison.
*
Use
this
flag
with
the
Checkin,
Get,
or
UndoCheckout
methods
to
compare
date/time
stamp
of
the
local
copy
and
the
SourceSafe
master
copy.
If
the
local
copy
is
up
-
to
-
date,
SourceSafe
does
not
replace
it
with
the
master
copy.
*/
public
int
VSSFLAG_CMPTIME
= 1024;
public
int
VSSFLAG_CMPCHKSUM
= 1536;
/**
*
Specifies
no
comparison
mechanism.
*
Use
this
flag
with
the
Checkin,
Get,
*
or
UndoCheckout
methods
to
always
replace
the
local
copy
with
the
SourceSafe
master
copy.
*/
public
int
VSSFLAG_CMPFAIL
= 2048;
/**
*
Indicates
that
a
command
should
not
be
recursive
(act
on
an
entire
project
tree).
This
is
the
default.
*/
public
int
VSSFLAG_RECURSNO
= 4096;
/**
*
Indicates
that
a
command
should
be
recursive
(act
on
an
entire
project
tree).
*
Use
this
flag
with
the
Checkin,
Checkout,
Get,
or
Share
methods.
*/
public
int
VSSFLAG_RECURSYES
= 8192;
/**
*
Used
to
override
the
working
folder
specifications.
*/
public
int
VSSFLAG_FORCEDIRNO
= 16384;
/**
*
Used
to
maintain
the
working
folder
settings.
This
is
the
default.
*/
public
int
VSSFLAG_FORCEDIRYES
= 32768;
public
int
VSSFLAG_KEEPNO
= 65536;
public
int
VSSFLAG_KEEPYES
= 131072;
/**
*
Indicates
whether
the
local
copy
should
be
deleted
*
after
an
Add,
Checkin,
or
UndoCheckout.
*/
public
int
VSSFLAG_DELNO
= 262144;
/**
*
Indicates
that
the
local
copy
of
a
file
is
deleted
when
it
is
checked
in.
*/
public
int
VSSFLAG_DELYES
= 524288;
/**
*
Indicates
that
the
local
copy
of
a
file
is
not
replaced
by
an
UndoCheckout
operation.
*
When
this
flag
is
set,
the
local
copy
is
not
replaced
with
the
SourceSafe
master
copy
and
its
read
-
only
flag
is
set
to
true.
*/
public
int
VSSFLAG_DELNOREPLACE
= 786432;
/**
*
Indicates
that
Visual
SourceSafe
auto
-
detects
the
file
to
determine
whether
it
is
text
or
binary.
*/
public
int
VSSFLAG_BINTEST
= 1048576;
/**
*
Indicates
that
a
file
is
binary.
*
When
this
flag
is
set,
SourceSafe
sets
the
added
file
type
to
binary.
*/
public
int
VSSFLAG_BINBINARY
= 2097152;
/**
*
Indicates
that
a
file
is
text.
*/
public
int
VSSFLAG_BINTEXT
= 3145728;
/**
*
Indicates
that
an
added
file
retains
its
historical
versions.
*/
public
int
VSSFLAG_DELTAYES
= 4194304;
/**
*
Indicates
that
an
added
file
does
not
retain
its
historical
versions.
*/
public
int
VSSFLAG_DELTANO
= 8388608;
public
int
VSSFLAG_UPDASK
= 16777216;
public
int
VSSFLAG_UPDUPDATE
= 33554432;
public
int
VSSFLAG_UPDUNCH
= 50331648;
/**
*
Indicates
that
the
local
copy
should
be
replaced
with
the
SourceSafe
master
copy
on
commands
that
do
an
automatic
Get.
*/
public
int
VSSFLAG_GETYES
= 67108864;
/**
*
Indicates
that
the
local
copy
should
not
be
replaced
with
the
SourceSafe
master
copy
on
commands
that
do
an
automatic
Get.
*/
public
int
VSSFLAG_GETNO
= 134217728;
/**
*
Indicates
that
a
checkout
is
exclusive.
*
SourceSafe
allows
the
files
to
be
checked
out
by
more
than
one
user.
*/
本文介绍如何使用Java通过JNI访问微软的Visual SourceSafe (VSS),包括连接VSS、获取项目和文件信息、文件操作等功能。
1859

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



