Jive之抽象工厂模式
来源:互联网 发布:windows 进程监控 编辑:程序博客网 时间:2024/09/21 09:28
public abstract class AuthorizationFactory {
/**
* The default class to instantiate is database implementation.
*/
private static String className =
"com.jivesoftware.forum.database.DbAuthorizationFactory";
private static AuthorizationFactory factory = null;
/**
* Returns the Authorization token associated with the specified username
* and password. If the username and password do not match the record of
* any user in the system, the method throws an UnauthorizedException.<p>
*
* When using most implementations of this class, authorization tokens
* should be cached. A convenient place to store a token is often in the
* HttpSession.
*
* @param username the username to create an Authorization with.
* @param password the password to create an Authorization with.
* @return an Authorization token if the username and password are correct.
* @throws UnauthorizedException if the username and password do not match
* any existing user.
*/
public static Authorization getAuthorization(String username,
String password) throws UnauthorizedException
{
loadAuthorizationFactory();
return factory.createAuthorization(username, password);
}
/**
* Returns the anonymous user Authorization.
*
* @return an anonymous Authorization token.
*/
public static Authorization getAnonymousAuthorization() {
loadAuthorizationFactory();
return factory.createAnonymousAuthorization();
}
/**
* Creates Authorization tokens for users. This method is implemented by
* concrete subclasses of AuthorizationFactory.
*
* @param username the username to create an Authorization with.
* @param password the password to create an Authorization with.
* @return an Authorization token if the username and password are correct.
* @throws UnauthorizedException if the username and password do not match
* any existing user.
*/
protected abstract Authorization createAuthorization(String username,
String password) throws UnauthorizedException;
/**
* Creates anonymous Authorization tokens. This method is implemented by
* concrete subclasses AuthorizationFactory.
*
* @return an anonymous Authorization token.
*/
protected abstract Authorization createAnonymousAuthorization();
/**
* Loads a concrete AuthorizationFactory that can be used generate
* Authorization tokens for authorized users.<p>
*
* By default, the implementation used will be an instance of
* DbAuthorizationFactory -- the standard database implementation that uses
* the Jive user table. A different factory can be specified by setting the
* Jive property "AuthorizationFactory.className". However, you must
* restart Jive for any change to take effect.
*/
private static void loadAuthorizationFactory() {
if (factory == null) {
//Use className as a convenient object to get a lock on.
synchronized(className) {
if (factory == null) {
//See if the classname has been set as a Jive property.
String classNameProp = JiveGlobals.getJiveProperty(
"AuthorizationFactory.className");
if (classNameProp != null) {
className = classNameProp;
}
try {
Class c = Class.forName(className);
factory = (AuthorizationFactory)c.newInstance();
}
catch (Exception e) {
System.err.println("Exception loading class: " + e);
e.printStackTrace();
}
}
}
}
}
}
public class DbAuthorizationFactory extends AuthorizationFactory {
/** DATABASE QUERIES **/
private static final String AUTHORIZE =
"SELECT userID FROM jiveUser WHERE username=? AND passwordHash=?";
/**
* The same token can be used for all anonymous users, so cache it.
*/
private static final Authorization anonymousAuth = new DbAuthorization(-1);
/**
* Creates Authorization tokens for users. This method is implemented by
* concrete subclasses of AuthorizationFactory.
*
* @param username the username to create an Authorization with.
* @param password the password to create an Authorization with.
* @return an Authorization token if the username and password are correct.
* @throws UnauthorizedException if the username and password do not match
* any existing user.
*/
public Authorization createAuthorization(String username, String password)
throws UnauthorizedException
{
if (username == null || password == null) {
throw new UnauthorizedException();
}
//Jive stores all passwords in hashed form. So, hash the plain text
//password for comparison.
password = StringUtils.hash(password);
long userID = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(AUTHORIZE);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
//If the query had no results, the username and password
//did not match a user record. Therefore, throw an exception.
if (!rs.next()) {
throw new UnauthorizedException();
}
userID = rs.getLong(1);
}
catch( SQLException sqle ) {
System.err.println("Exception in DbAuthorizationFactory:" + sqle);
sqle.printStackTrace();
throw new UnauthorizedException();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Got this far, so the user must be authorized.
return new DbAuthorization(userID);
}
/**
* Creates anonymous Authorization tokens.
*
* @return an anonymous Authorization token.
*/
public Authorization createAnonymousAuthorization() {
return anonymousAuth;
}
}
- Jive之抽象工厂模式
- Jive之代理模式
- 工厂模式之抽象工厂模式
- 工厂模式之抽象工厂模式
- 工厂模式之抽象工厂模式
- 抽象工厂模式之摩托车厂
- 设计模式之抽象工厂
- 设计模式 之 抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之-抽象工厂
- 设计模式之抽象工厂
- YesKey 产品介绍
- 这是我们公司总结的一些关于中文乱码问题的一些解决方案和经验和大家分享!
- 成功需要走十步的话,三五步之间最难走……[
- android java代码的启动:app_process
- 一个简单的2选1数据选择器的测试平台(Testbench)
- Jive之抽象工厂模式
- 登录mysql出现/var/lib/mysql/mysql.sock不存在以及mysql dead but subsys locked的问题
- 字符串處理(按指定的關鍵字在字段中出現的次數排序)
- 案例分析准备资料收集
- 如何使用来自 Rich Edit(CRichEditCtrl) 控件的内置打印功能
- 用Crosstool 制作交叉编译工具链
- Step-by-step plan For a career Change
- Base64编码解码与实现
- vsftp配置大全