本文主要是介绍实现VSS(Visual Source Safe)的Java API,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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(Visual Source Safe)的Java API的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!