KickJava   Java API By Example, From Geeks To Geeks.

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


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.PluginConcept;
24 import org.lucane.server.store.PluginStore;
25 import org.lucane.server.Server;
26
27 import javax.naming.*;
28 import javax.naming.directory.*;
29
30 public class LdapPluginStore extends PluginStore
31 {
32     private DirContext context;
33     private HashMap mapping;
34     private HashMap attributes;
35     
36     public LdapPluginStore(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.getPluginsDn());
45         this.context = new InitialDirContext(ht);
46             
47         this.attributes = config.getPluginsAttributes();
48         this.mapping = config.getPluginsMapping();
49     }
50
51
52     public void storePlugin(PluginConcept plugin)
53     throws Exception JavaDoc
54     {
55         BasicAttributes attrs = new BasicAttributes();
56         
57         //mapped plugin attributes
58
attrs.put(new BasicAttribute((String JavaDoc)mapping.get("name"), plugin.getName()));
59         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("version"), plugin.getVersion()));
60
61         if(plugin.getDescription() != null && plugin.getDescription().length() > 0)
62             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("description"), plugin.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") + '=' + plugin.getName(), attrs);
73     }
74
75     public void updatePlugin(PluginConcept plugin)
76     throws Exception JavaDoc
77     {
78         BasicAttributes attrs = new BasicAttributes();
79         
80         //mapped plugin attributes
81
attrs.put(new BasicAttribute((String JavaDoc)mapping.get("name"), plugin.getName()));
82         attrs.put(new BasicAttribute((String JavaDoc)mapping.get("version"), plugin.getVersion()));
83
84         if(plugin.getDescription() != null && plugin.getDescription().length() > 0)
85             attrs.put(new BasicAttribute((String JavaDoc)mapping.get("description"), plugin.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") + '=' + plugin.getName(), DirContext.REPLACE_ATTRIBUTE, attrs);
96     }
97
98     public void removePlugin(PluginConcept plugin)
99     throws Exception JavaDoc
100     {
101         LdapGroupStore groups = (LdapGroupStore)Server.getInstance().getStore().getGroupStore();
102         groups.removePluginLinks(plugin.getName());
103
104         context.destroySubcontext((String JavaDoc)mapping.get("name") + '=' + plugin.getName());
105     }
106
107     public PluginConcept getPlugin(String JavaDoc pluginName)
108     throws Exception JavaDoc
109     {
110         String JavaDoc pluginKey = (String JavaDoc)mapping.get("name") + '=' + pluginName;
111         
112         Attributes attrs = context.getAttributes(pluginKey);
113         
114         String JavaDoc name = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("name")).get();
115         String JavaDoc version = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("version")).get();
116         PluginConcept plugin = new PluginConcept(name, version);
117                 
118         try {
119             String JavaDoc description = (String JavaDoc)attrs.get((String JavaDoc)mapping.get("description")).get();
120             plugin.setDescription(description);
121         } catch(Exception JavaDoc e) {
122             //no description
123
}
124
125         return plugin;
126     }
127
128     public Iterator getAllPlugins() throws Exception JavaDoc
129     {
130         ArrayList plugins = new ArrayList();
131         
132         NamingEnumeration list = context.list("");
133         while(list.hasMore())
134         {
135             NameClassPair pair = (NameClassPair)list.next();
136             String JavaDoc name = pair.getName();
137             name = name.substring(name.indexOf('=')+1);
138             
139             plugins.add(getPlugin(name));
140         }
141         
142         return plugins.iterator();
143     }
144 }
Popular Tags