正在分析Authorization模块,好精彩!
单就这2个类来说,完全体现的认证的大致过程,而无需深入分析具体的实现,这可能也就是面向接口编程的一大好处吧
抽象工厂模式的完全体现
//类的认证
public interface Authorization{
public long getUserID();
public boolean isAnonymous();
}
//认证类抽象工厂
public abstract class AuthorizationFactory{
//有这个抽象工厂来生成具体的工厂
//具体工厂的加载由Class.forName来具体实现
//这部分先是Jive默认实现的认证工厂,如果自己来实现,只需要修改对应的配置文件
//而不需修改任何代码!
private static String className =
"com.jivesoftware.forum.database.DbAuthorizationFactory";
public static AuthorizationFactory factory = null;
public static Authorization getAuthrization(String username,String password)
throws UnauthorizationException{
loadAuthorizationFactory();
return factory.createAuthorization(username,password);
}
public static Authorization getAnonymousAuthorization(){
loadAuthorizationFactory();
return factory.createAnonymousAuthorization();
}
//具体的方法具体实现
public abstract static Authorization createAuthorization(String username,String password)
throws UnauthorizationException;
public abstract static Authorization createAnonymousAuthorization();
//实现同步加载具体工厂类代码
private static void loadAuthorizationFactory(){
if(factory == null){
synchronized(className){
if(factory == null){
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();
}
}
}
}
}
}
单就这2个类来说,完全体现的认证的大致过程,而无需深入分析具体的实现,这可能也就是面向接口编程的一大好处吧
抽象工厂模式的完全体现
//类的认证
public interface Authorization{
public long getUserID();
public boolean isAnonymous();
}
//认证类抽象工厂
public abstract class AuthorizationFactory{
//有这个抽象工厂来生成具体的工厂
//具体工厂的加载由Class.forName来具体实现
//这部分先是Jive默认实现的认证工厂,如果自己来实现,只需要修改对应的配置文件
//而不需修改任何代码!
private static String className =
"com.jivesoftware.forum.database.DbAuthorizationFactory";
public static AuthorizationFactory factory = null;
public static Authorization getAuthrization(String username,String password)
throws UnauthorizationException{
loadAuthorizationFactory();
return factory.createAuthorization(username,password);
}
public static Authorization getAnonymousAuthorization(){
loadAuthorizationFactory();
return factory.createAnonymousAuthorization();
}
//具体的方法具体实现
public abstract static Authorization createAuthorization(String username,String password)
throws UnauthorizationException;
public abstract static Authorization createAnonymousAuthorization();
//实现同步加载具体工厂类代码
private static void loadAuthorizationFactory(){
if(factory == null){
synchronized(className){
if(factory == null){
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();
}
}
}
}
}
}