KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > store > ServiceStore


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.store;
20
21 import org.lucane.server.*;
22 import org.lucane.common.concepts.*;
23 import java.util.*;
24
25 /**
26  * ServiceManager abstraction
27  */

28 public abstract class ServiceStore
29 {
30     /**
31      * Store a service
32      *
33      * @param service the ServiceConcept to store
34      */

35     public abstract void storeService(ServiceConcept service) throws Exception JavaDoc;
36     
37     /**
38      * Update a service
39      *
40      * @param service the ServiceConcept to update
41      */

42     public abstract void updateService(ServiceConcept service) throws Exception JavaDoc;
43         
44     /**
45      * Remove a service
46      *
47      * @param service the ServiceConcept to remove
48      */

49     public abstract void removeService(ServiceConcept service) throws Exception JavaDoc;
50
51     /**
52      * Get a service by its name
53      *
54      * @param name the service to fetch
55      * @return the corresponding ServiceConcept or null
56      */

57     public abstract ServiceConcept getService(String JavaDoc name) throws Exception JavaDoc;
58
59     /**
60      * Get all services
61      *
62      * @return an iterator listing all services
63      */

64     public abstract Iterator getAllServices() throws Exception JavaDoc;
65     
66     /**
67      * Get the services that a user can use
68      *
69      * @param user the UserConcept
70      * @return the list of services this user can access to.
71      */

72     public Iterator getAuthorizedServices(UserConcept user)
73     throws Exception JavaDoc
74     {
75         ArrayList authorizedServices = new ArrayList();
76     
77         UserStore um = Server.getInstance().getStore().getUserStore();
78         Iterator groups = um.getAllUserGroups(user);
79         while(groups.hasNext())
80         {
81             GroupConcept group = (GroupConcept)groups.next();
82             Iterator services = group.getServices();
83             while(services.hasNext())
84             {
85                 ServiceConcept service = (ServiceConcept)services.next();
86                 if(! authorizedServices.contains(service))
87                     authorizedServices.add(service);
88             }
89         }
90                     
91         return authorizedServices.iterator();
92     }
93     
94     /**
95      * Check if a user can use a service
96      *
97      * @param user the UserConcept
98      * @param service the ServiceConcept
99      * @return true if the user has access to the service, false instead.
100      */

101     public boolean isAuthorizedService(UserConcept user, ServiceConcept service)
102     throws Exception JavaDoc
103     {
104         Iterator i = getAuthorizedServices(user);
105         while(i.hasNext())
106         {
107             Object JavaDoc o = i.next();
108                 
109             if(service.equals(o))
110                 return true;
111         }
112         
113         return false;
114     }
115     
116     /**
117      * Get all users that can use a service
118      *
119      * @param service the ServiceConcept
120      * @return the list of users that have access to this service
121      */

122     public Iterator getUsersFor(ServiceConcept service)
123     throws Exception JavaDoc
124     {
125         ArrayList users = new ArrayList();
126
127         UserStore us = Server.getInstance().getStore().getUserStore();
128         Iterator i = us.getAllUsers();
129         while(i.hasNext())
130         {
131             UserConcept user = (UserConcept)i.next();
132             if(isAuthorizedService(user, service))
133                     users.add(user);
134         }
135         
136         return users.iterator();
137     }
138 }
139
Popular Tags