KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > am > PermissionGuardFactory


1 /*
2  * Created on 26.07.2004
3  *
4  */

5 package org.jresearch.gossip.am;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.jresearch.gossip.am.constants.Permissions;
10 import org.jresearch.gossip.am.model.IPermissionGuard;
11 import org.jresearch.gossip.am.model.IPermissionGuardFactory;
12 import org.jresearch.gossip.beans.user.User;
13 import org.jresearch.gossip.constants.UserStatus;
14 import org.jresearch.gossip.dao.ForumDAO;
15 import org.jresearch.gossip.dao.UserDAO;
16 import org.jresearch.gossip.exception.SystemException;
17
18 /**
19  * @author dbelov
20  *
21  */

22 public class PermissionGuardFactory implements IPermissionGuardFactory {
23
24     public final static int GUEST_ID = 0;
25
26     private static PermissionGuardFactory instance;
27
28     private ForumDAO forumDAO;
29
30     private UserDAO userDAO;
31
32     private static Object JavaDoc lock = new Object JavaDoc();
33
34     private PermissionGuardFactory() {
35         this.forumDAO = ForumDAO.getInstance();
36         this.userDAO = UserDAO.getInstance();
37     }
38
39     public static PermissionGuardFactory getInstance() {
40         if (instance == null) {
41             synchronized (lock) {
42                 if (instance == null) {
43                     instance = new PermissionGuardFactory();
44                 }
45             }
46         }
47         return instance;
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see org.jresearch.gossip.am.model.IPermissionGuardFactory#createGuard(int)
54      */

55     public IPermissionGuard createGuard(int userId) throws SystemException {
56         try {
57             int status = UserStatus.GUEST;
58             if (userId != GUEST_ID) {
59                 User user = userDAO.getUserInfoShort(userId);
60                 status = user.getStatus();
61             }
62             PermissionGuard pg = new PermissionGuard(Permissions.getInstance()
63                     .getPermissions(status));
64             return pg;
65         } catch (SQLException JavaDoc e) {
66             throw new SystemException(e);
67         }
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see org.jresearch.gossip.am.model.IPermissionGuardFactory#createGuard(java.lang.String)
74      */

75     public IPermissionGuard createGuard(String JavaDoc login) throws SystemException {
76         User user;
77         try {
78             int id = GUEST_ID;
79             if (login != null) {
80                 user = userDAO.getUserInfo(login);
81
82                 if (user != null) {
83                     id = user.getId();
84                 }
85             }
86             return createGuard(id);
87         } catch (SQLException JavaDoc e) {
88             throw new SystemException(e);
89         }
90     }
91
92     public IPermissionGuard getGuardForStatus(int status)
93             throws SystemException {
94
95         PermissionGuard pg = new PermissionGuard(Permissions.getInstance()
96                 .getPermissions(status));
97         return pg;
98
99     }
100
101 }
Popular Tags