KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > store > ldap > LdapServiceStore


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 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.ldap;
20
21 import java.util.*;
22
23 import org.lucane.common.concepts.ServiceConcept;
24 import org.lucane.server.store.ServiceStore;
25 import org.lucane.server.Server;
26
27 import javax.naming.*;
28 import javax.naming.directory.*;
29
30 public class LdapServiceStore extends ServiceStore
31 {
32     private DirContext context;
33     private HashMap mapping;
34     private HashMap attributes;
35     
36     public LdapServiceStore(LdapConfig config)
37     throws Exception JavaDoc
38     {
39         Hashtable ht = new Hashtable();
40         ht.put(DirContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
41         ht.put(DirContext.SECURITY_AUTHENTICATION, config.getAuthType());
42         ht.put(DirContext.SECURITY_PRINCIPAL, config.getAuthBindDn());
43         ht.put(DirContext.SECURITY_CREDENTIALS, config.getAuthPassword());
44         ht.put(DirContext.PROVIDER_URL, config.getLdapUrl() + config.getServicesDn());
45         this.context = new InitialDirContext(ht);
46         
47         this.attributes = config.getServicesAttributes();
48         this.mapping = config.getServicesMapping();
49     }
50
51
52     public void storeService(ServiceConcept service)
53     throws Exception JavaDoc
54     {
55         BasicAttributes attrs = new BasicAttributes();
56         
57         //mapped service attributes
58
attrs.put(new BasicAttribute((String JavaDoc)mapping.get("name"), service.getName()));
59         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("installed"), service.isInstalled() ? "1" : "0"));
60
61         if(service.getDescription() != null && service.getDescription().length() > 0)
62             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("description"), service.getDescription()));
63
64         //other attributes
65
Iterator keys = attributes.keySet().iterator();
66         while(keys.hasNext())
67         {
68             String JavaDoc key = (String JavaDoc)keys.next();
69             attrs.put(new BasicAttribute(key, attributes.get(key)));
70         }
71
72         context.createSubcontext((String JavaDoc)mapping.get("name") + '=' + service.getName(), attrs);
73     }
74
75     public void updateService(ServiceConcept service)
76     throws Exception JavaDoc
77     {
78         BasicAttributes attrs = new BasicAttributes();
79         
80         //mapped service attributes
81
attrs.put(new BasicAttribute((String JavaDoc)mapping.get("name"), service.getName()));
82         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("installed"), service.isInstalled() ? "1" : "0"));
83
84         if(service.getDescription() != null && service.getDescription().length() > 0)
85             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("description"), service.getDescription()));
86
87         //other attributes
88
Iterator keys = attributes.keySet().iterator();
89         while(keys.hasNext())
90         {
91             String JavaDoc key = (String JavaDoc)keys.next();
92             attrs.put(new BasicAttribute(key, attributes.get(key)));
93         }
94
95         context.modifyAttributes((String JavaDoc)mapping.get("name") + '=' + service.getName(), DirContext.REPLACE_ATTRIBUTE, attrs);
96     }
97
98     public void removeService(ServiceConcept service)
99     throws Exception JavaDoc
100     {
101         LdapGroupStore groups = (LdapGroupStore)Server.getInstance().getStore().getGroupStore();
102         groups.removeServiceLinks(service.getName());
103
104         context.destroySubcontext((String JavaDoc)mapping.get("name") + '=' + service.getName());
105     }
106
107     public ServiceConcept getService(String JavaDoc serviceName)
108     throws Exception JavaDoc
109     {
110         String JavaDoc serviceKey = (String JavaDoc)mapping.get("name") + '=' + serviceName;
111         Attributes attrs = context.getAttributes(serviceKey);
112         
113         String JavaDoc name = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("name")).get();
114         boolean installed = attrs.get((String JavaDoc)mapping.get("installed")).get().equals("1");
115         ServiceConcept service = new ServiceConcept(name, installed);
116                 
117         try {
118             String JavaDoc description = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("description")).get();
119             service.setDescription(description);
120         } catch(Exception JavaDoc e) {
121             //no description
122
}
123
124         return service;
125     }
126
127     public Iterator getAllServices() throws Exception JavaDoc
128     {
129         ArrayList services = new ArrayList();
130         
131         NamingEnumeration list = context.list("");
132         while(list.hasMore())
133         {
134             NameClassPair pair = (NameClassPair)list.next();
135             String JavaDoc name = pair.getName();
136             name = name.substring(name.indexOf('=')+1);
137             
138             services.add(getService(name));
139         }
140         
141         return services.iterator();
142     }
143 }
Popular Tags