| 1 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  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  54 { 55 BasicAttributes attrs = new BasicAttributes(); 56 57 attrs.put(new BasicAttribute((String )mapping.get("name"), plugin.getName())); 59 attrs.put(new BasicAttribute((String )mapping.get("version"), plugin.getVersion())); 60 61 if(plugin.getDescription() != null && plugin.getDescription().length() > 0) 62 attrs.put(new BasicAttribute((String )mapping.get("description"), plugin.getDescription())); 63 64 Iterator keys = attributes.keySet().iterator(); 66 while(keys.hasNext()) 67 { 68 String key = (String )keys.next(); 69 attrs.put(new BasicAttribute(key, attributes.get(key))); 70 } 71 72 context.createSubcontext((String )mapping.get("name") + '=' + plugin.getName(), attrs); 73 } 74 75 public void updatePlugin(PluginConcept plugin) 76 throws Exception  77 { 78 BasicAttributes attrs = new BasicAttributes(); 79 80 attrs.put(new BasicAttribute((String )mapping.get("name"), plugin.getName())); 82 attrs.put(new BasicAttribute((String )mapping.get("version"), plugin.getVersion())); 83 84 if(plugin.getDescription() != null && plugin.getDescription().length() > 0) 85 attrs.put(new BasicAttribute((String )mapping.get("description"), plugin.getDescription())); 86 87 Iterator keys = attributes.keySet().iterator(); 89 while(keys.hasNext()) 90 { 91 String key = (String )keys.next(); 92 attrs.put(new BasicAttribute(key, attributes.get(key))); 93 } 94 95 context.modifyAttributes((String )mapping.get("name") + '=' + plugin.getName(), DirContext.REPLACE_ATTRIBUTE, attrs); 96 } 97 98 public void removePlugin(PluginConcept plugin) 99 throws Exception  100 { 101 LdapGroupStore groups = (LdapGroupStore)Server.getInstance().getStore().getGroupStore(); 102 groups.removePluginLinks(plugin.getName()); 103 104 context.destroySubcontext((String )mapping.get("name") + '=' + plugin.getName()); 105 } 106 107 public PluginConcept getPlugin(String pluginName) 108 throws Exception  109 { 110 String pluginKey = (String )mapping.get("name") + '=' + pluginName; 111 112 Attributes attrs = context.getAttributes(pluginKey); 113 114 String name = (String )attrs.get((String )mapping.get("name")).get(); 115 String version = (String )attrs.get((String )mapping.get("version")).get(); 116 PluginConcept plugin = new PluginConcept(name, version); 117 118 try { 119 String description = (String )attrs.get((String )mapping.get("description")).get(); 120 plugin.setDescription(description); 121 } catch(Exception e) { 122 } 124 125 return plugin; 126 } 127 128 public Iterator getAllPlugins() throws Exception  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 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 |