其实在shark中创建用户很简单,所有函数已经写好了,只要知道怎么调用就可以了。下面就是我写的一个创建用户的类。
/*
* Created Date 2005-1-7
*
*/
package hk.com.csl.business;
/**
* <p>Title: Shark Test</p>
* <p>Description: Shark Test System</p>
* <p>Copyright: Copyright (c) 2005</p>
* @author Jason
* @version 1.0
*/
import java.util.*;
import org.enhydra.shark.api.client.wfmodel.*;
import org.enhydra.shark.api.client.wfservice.*;
import java.io.FileInputStream;
import org.enhydra.shark.Shark;
import org.enhydra.shark.SharkConstants;
import org.enhydra.shark.api.client.wfbase.BaseException;
import org.enhydra.shark.api.TransactionException;
import org.enhydra.shark.api.SharkTransaction;
import org.enhydra.shark.api.RootException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import hk.com.csl.business.interfaces.IRegService;
import hk.com.csl.logger.*;
import hk.com.csl.web.form.RegForm;
public class RegServiceImp implements IRegService {
private static boolean _debug_ = false;
private static boolean sharkConfigured = false;
private static String engineName = "SharkExampleJSP";
private static IRegService instance;
public static IRegService getInstance(){
if (null == instance) instance = new RegServiceImp();
return instance;
}
//取得配置文件并初始化
private static void init(String realPath){
if (_debug_)
System.err.println("#_init_#");
if (!sharkConfigured){
Properties p = new Properties();
try {
p.load(new FileInputStream(realPath +"/conf/Shark.conf"));
realPath = replaceAll(realPath, "\\", "/");
System.out.println("realpath=" +realPath);
for (Iterator it = p.keySet().iterator(); it.hasNext();) {
String key = (String)it.next();
String value = p.getProperty(key);
System.out.println("value=" +value);
if (0 <= value.indexOf("@@")) {
if (_debug_)
System.err.print("key is "+key+", old value is"+value);
value = replaceAll(value, "@@/", realPath);
p.setProperty(key, value);
if (_debug_)
System.err.println(", new value is"+value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
p.setProperty("enginename", engineName);
Shark.configure(p);
sharkConfigured = true;
}
}
/**
*
* @param form
* @param request
* @param response
* @return boolean doesCreateUserSuccess
*/
//创建用户并判断是否创建成功。
public boolean createUser(ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
boolean doesCreateUserSuccess = false;
String realPath = request.getRealPath("/");
Logger.getBusinessLogger().debug("realpath =" +realPath);
RegForm regform = (RegForm) form;
String userName = regform.getUsername();
Logger.getBusinessLogger().debug("Username="+userName);
String pwd = regform.getPasswd();
Logger.getBusinessLogger().debug("password="+pwd);
String firstname = regform.getFirstname();
Logger.getBusinessLogger().debug("firstname="+firstname);
String lastname = regform.getLastname();
Logger.getBusinessLogger().debug("lastname="+lastname);
String email = regform.getEmail();
Logger.getBusinessLogger().debug("email="+email);
String groupName = regform.getGroupname();
Logger.getBusinessLogger().debug("groupName="+groupName);
try {
//初始化
init(realPath);
//判断组是否存在,如果没有就创建组
UserGroupAdministration uga= Shark.getInstance()
.getAdminInterface()
.getUserGroupAdministration();
//判断用户是否存在,如果不存在则创建新的用户。
if (!uga.doesGroupExist(groupName)) {
uga.createGroup(groupName,"test group");
}
if (!uga.doesUserExist(userName)) {
uga.createUser(groupName, userName, pwd,firstname, lastname,email);
}
doesCreateUserSuccess = true;
} catch (Throwable t) {doesCreateUserSuccess = false;}
return doesCreateUserSuccess;
}
/**
* Replace all occurence of forReplace with replaceWith in input string.
* @param input represents input string
* @param forReplace represents substring for replace
* @param replaceWith represents replaced string value
* @return new string with replaced values
*/
private static String replaceAll(String input,
String forReplace,
String replaceWith) {
if( input == null )
return null;
StringBuffer result = new StringBuffer();
boolean hasMore = true;
while (hasMore) {
int start = input.indexOf(forReplace);
int end = start + forReplace.length();
if (start != -1) {
result.append(input.substring(0, start) + replaceWith);
input = input.substring(end);
}
else {
hasMore = false;
result.append(input);
}
}
if (result.toString().equals(""))
return input; //nothing is changed
else
return result.toString();
}
}
本文地址:http://com.8s8s.com/it/it11965.htm