1 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 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 54 { 55 BasicAttributes attrs = new BasicAttributes(); 56 57 attrs.put(new BasicAttribute((String )mapping.get("name"), service.getName())); 59 attrs.put(new BasicAttribute((String )mapping.get("installed"), service.isInstalled() ? "1" : "0")); 60 61 if(service.getDescription() != null && service.getDescription().length() > 0) 62 attrs.put(new BasicAttribute((String )mapping.get("description"), service.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") + '=' + service.getName(), attrs); 73 } 74 75 public void updateService(ServiceConcept service) 76 throws Exception 77 { 78 BasicAttributes attrs = new BasicAttributes(); 79 80 attrs.put(new BasicAttribute((String )mapping.get("name"), service.getName())); 82 attrs.put(new BasicAttribute((String )mapping.get("installed"), service.isInstalled() ? "1" : "0")); 83 84 if(service.getDescription() != null && service.getDescription().length() > 0) 85 attrs.put(new BasicAttribute((String )mapping.get("description"), service.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") + '=' + service.getName(), DirContext.REPLACE_ATTRIBUTE, attrs); 96 } 97 98 public void removeService(ServiceConcept service) 99 throws Exception 100 { 101 LdapGroupStore groups = (LdapGroupStore)Server.getInstance().getStore().getGroupStore(); 102 groups.removeServiceLinks(service.getName()); 103 104 context.destroySubcontext((String )mapping.get("name") + '=' + service.getName()); 105 } 106 107 public ServiceConcept getService(String serviceName) 108 throws Exception 109 { 110 String serviceKey = (String )mapping.get("name") + '=' + serviceName; 111 Attributes attrs = context.getAttributes(serviceKey); 112 113 String name = (String )attrs.get((String )mapping.get("name")).get(); 114 boolean installed = attrs.get((String )mapping.get("installed")).get().equals("1"); 115 ServiceConcept service = new ServiceConcept(name, installed); 116 117 try { 118 String description = (String )attrs.get((String )mapping.get("description")).get(); 119 service.setDescription(description); 120 } catch(Exception e) { 121 } 123 124 return service; 125 } 126 127 public Iterator getAllServices() throws Exception 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 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 |