public class Role {
protected String name;
public String getName() throws AppSecurityException{
return this.name;
}
public void setName(String name)throws AppSecurityException{
this.name = name;
}
}
public class User {
protected String name;
protected boolean value;
public String getName()throws AppSecurityException{
return this.name;
}
public void setName(String name){
this.name = name;
}
public boolean isLocked() throws AppSecurityException{
return this.value;
}
public void setLocked(boolean value) throws AppSecurityException{
this.value = value;
}
}
Yukarida goruldugu gibi burada bizim icin gerekli olan degiskenler mevcut. Simdi ise sira bir tane abstract class oluturmak olacak, burada interface`de olabilir. ama biz abstract class yaptik. Cunku metodumuzun bitanesinin icini doldurmak istiyoruz.
public abstract class SecurityManager {
public static SecurityManager getInstance(String instanceName) throws AppSecurityException {
if(("jazn").equalsIgnoreCase(instanceName)){
return new SecurityManagerJAZN();
} else{
throw new AppSecurityException("NOT FOUND");
}
}
public abstract User createUser(String username, String password);
public abstract List
public abstract User getUser(String username) throws AppSecurityException;
public abstract void dropUser(User user) throws AppSecurityException;
public abstract void dropUser(String username) throws AppSecurityException;
public abstract Role createRole(String roleName) throws AppSecurityException;
public abstract void dropRole(Role role) throws AppSecurityException;
public abstract List
public abstract Role getRole(String roleName) throws AppSecurityException;
public abstract List
public abstract List
public abstract void grantRole(User user , Role role)throws AppSecurityException;
public abstract void grantRole(Role role , Role grantee )throws AppSecurityException;
public abstract void revokeRole(User user, Role role) throws AppSecurityException;
}
Evet birtanede SecurityManager abstract clasimiz oldu. Simdi sira geldi bunlarin icini doldurmaya.
Bunun icin birtane SecurtityManagerJAZN classimizi olusturalim
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import oracle.security.jazn.JAZNContext;
import oracle.security.jazn.JAZNException;
import oracle.security.jazn.realm.Realm;
import oracle.security.jazn.realm.RealmManager;
import oracle.security.jazn.realm.RealmRole;
import oracle.security.jazn.realm.RealmUser;
import oracle.security.jazn.realm.RoleManager;
import oracle.security.jazn.realm.UserManager;
import org.accr.model.exception.AppSecurityException;
import org.accr.model.security.Role;
import org.accr.model.security.SecurityManager;
import org.accr.model.security.User;
public class SecurityManagerJAZN extends SecurityManager{
protected String realmName = "jazn.com";
public User createUser(String username, String password) throws AppSecurityException { try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
UserManager userMgr = realm.getUserManager();
RealmUser realmUser = userMgr.createUser(username,password);
User user = new User();
user.setName(realmUser.getName());
return user;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public List
try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
UserManager userMgr = realm.getUserManager();
Set
Iterator iter = users.iterator();
ArrayList
while(iter.hasNext()){
RealmUser realmUser = (RealmUser)iter.next();
User user = new User();
user.setName(realmUser.getName());
userList.add(user);
}
return userList;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public User getUser(String username) throws AppSecurityException {
try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
UserManager userMgr = realm.getUserManager();
RealmUser realmUser = userMgr.getUser(username);
User user = new User();
user.setName(realmUser.getName());
return user;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public void dropUser(User user) throws AppSecurityException {
String username = user.getName();
this.dropUser(username);
}
public void dropUser(String username) throws AppSecurityException {
try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
UserManager userMgr = realm.getUserManager();
userMgr.dropUser(username);
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public Role createRole(String roleName) throws AppSecurityException{
try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
RoleManager realmMgr = realm.getRoleManager();
RealmRole realmRole = realmMgr.createRole(roleName);
Role role = new Role();
role.setName(realmRole.getName());
return role;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public void dropRole(Role role) throws AppSecurityException {
String roleName = role.getName();
this.dropUser(roleName);
}
public List
try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
RoleManager roleMgr = realm.getRoleManager();
Set
Iterator iter = rols.iterator();
ArrayList
while(iter.hasNext()){
RealmRole realmRole = (RealmRole)iter.next();
Role role = new Role();
role.setName(realmRole.getName());
roleList.add(role);
}
return roleList;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public Role getRole(String roleName) throws AppSecurityException {
try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
RoleManager roleMgr = realm.getRoleManager();
RealmRole realmRole = roleMgr.getRole(roleName);
Role role = new Role();
role.setName(realmRole.getName());
return role;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public void grantRole(User user, Role role) throws AppSecurityException {
try{
RealmManager realmMgr = JAZNContext.getRealmManager();
Realm realm = realmMgr.getRealm(realmName);
UserManager userMgr = realm.getUserManager();
String userName = user.getName();
RealmUser realmUser = userMgr.getUser(userName);
RoleManager roleMgr = realm.getRoleManager();
RealmRole realmRole = roleMgr.getRole(role.getName());
roleMgr.grantRole(realmUser,realmRole);
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public void grantRole(Role role, Role grantee) throws AppSecurityException {
try{
RealmManager realmMgr = JAZNContext.getRealmManager();
Realm realm = realmMgr.getRealm(realmName);
String roleName = role.getName();
RoleManager roleMgr = realm.getRoleManager();
RealmRole realmRole = roleMgr.getRole(roleName);
RealmRole realmGrantee = roleMgr.getRole(role.getName());
roleMgr.grantRole(realmRole, realmGrantee);
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public void revokeRole(User user, Role role) throws AppSecurityException {
try{
RealmManager realmMgr = JAZNContext.getRealmManager();
Realm realm = realmMgr.getRealm(realmName);
UserManager userMgr = realm.getUserManager();
String userName = user.getName();
RealmUser realmUser = userMgr.getUser(userName);
RoleManager roleMgr = realm.getRoleManager();
RealmRole realmRole = roleMgr.getRole(role.getName());
roleMgr.revokeRole(realmUser, realmRole);
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public List
try{
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
RoleManager roleMgr = realm.getRoleManager();
UserManager userMgr = realm.getUserManager();
String userName = user.getName();
RealmUser realmUser = userMgr.getUser(userName);
Set
(realmUser,true);
Iterator
ArrayList
while(iter.hasNext()){
Role role = new Role();
role.setName(iter.next().getName());
roleList.add(role);
} return roleList;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
public List
RealmManager reamMgr = JAZNContext.getRealmManager();
Realm realm = reamMgr.getRealm(realmName);
RoleManager roleMgr = realm.getRoleManager();
String roleName = role.getName();
RealmRole realmRole = roleMgr.getRole(roleName);
Set
Iterator
ArrayList
while(iter.hasNext()){
Role role1 = new Role();
role.setName(iter.next().getName());
roleList.add(role1);
}
return roleList;
}catch(JAZNException e){
throw new AppSecurityException(e);
}
}
}