View Javadoc

1   /*
2    * $Id: LDAPSecurityModuleService.java 20 2004-07-28 12:07:45Z josem $
3    *
4    * JBoss Security Modules
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.jsm;
24  
25  import java.util.Hashtable;
26  import java.util.Properties;
27  import javax.naming.*;
28  import javax.naming.directory.*;
29  import javax.naming.spi.*;
30  import javax.management.*;
31  
32  import org.jboss.util.ServiceMBeanSupport;
33  
34  /***
35   *
36   * @author  Jose M. Palomar <josem@talika.org>
37   * @version $Revision: 20 $
38   */
39  public class LDAPSecurityModuleService extends ServiceMBeanSupport implements LDAPSecurityModuleServiceMBean, ObjectFactory
40  {
41      
42      public LDAPSecurityModuleService() {
43      }
44  
45      public LDAPSecurityModuleService(String name, String url, String bindDN, String passwd, String searchBase) {
46          _name = name;
47          _url = url;
48          _bindDN = bindDN;
49          _passwd = passwd;
50          _searchBase = searchBase;
51      }
52      
53      public String getName()
54      {
55          return "LDAP Security Module";
56      }
57      
58      protected ObjectName getObjectName(MBeanServer server, ObjectName name)
59      throws javax.management.MalformedObjectNameException
60      {
61          this.server = server;
62          return new ObjectName(OBJECT_NAME);
63      }
64      
65      protected void initService()
66      throws Exception
67      {
68      }
69      
70      protected void startService()
71      throws Exception
72      {
73          
74          // Check required attributes
75          if(_name == null || _url == null || _searchBase == null) {
76              
77              if(_name == null) log.log("InstanceName attribute not set");
78              if(_url == null) log.log("Url attribute not set");
79              if(_searchBase == null) log.log("SearchBase attribute not set");
80              
81              log.log("LDAP Security Module not started");
82              
83              return;
84              
85          }
86          
87          // Get LDAP context
88          Properties p = new Properties();
89          p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
90          p.setProperty(Context.PROVIDER_URL, _url);
91          if(_bindDN != null) p.setProperty(Context.SECURITY_PRINCIPAL, _bindDN);
92          if(_passwd != null) p.setProperty(Context.SECURITY_CREDENTIALS, _passwd);        
93          DirContext dirCtx = new InitialDirContext(p);
94          
95          // Create a SecurityManager        
96          LDAPSecurityModule ldapsm = new LDAPSecurityModule(dirCtx, _searchBase);
97          
98          // Bind reference to SM in JNDI
99          Reference ref = new Reference(ldapsm.getClass().toString(), getClass().getName(), null);
100         Context ctx = (Context) new InitialContext();        
101         jndiName = JNDI_NAME_PREFIX + "/" + _name;
102         try {                                               
103             ctx.bind(jndiName, ref);                                    
104         }
105         catch(NameNotFoundException nabe) {
106             ctx.createSubcontext(JNDI_NAME_PREFIX);
107             ctx.bind(jndiName, ref);
108         }                
109         
110         // Store it in global Hashtable
111         ldapsmTable.put(_name, ldapsm);
112         
113         log.log("LDAP Security Module " + _name + " bound to " + jndiName);
114         log.log("Url:        " + _url);
115         log.log("Searh Base: " + _searchBase);
116         if(_bindDN != null) log.log("BindDN:     " + _bindDN);
117         if(_passwd != null) log.log("Password:   " + _passwd);
118         
119     }    
120     
121     protected void stopService()
122     {
123         try
124         {
125             new InitialContext().unbind(jndiName);
126             ldapsmTable.remove(_name);
127         }
128         catch (CommunicationException e) {
129         }
130         catch (Exception e)
131         {
132             log.exception(e);
133         }
134     }
135     
136     protected void destroyService()
137     {
138     }     
139     
140     public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
141     throws Exception
142     {
143         return ldapsmTable.get(name.get(name.size()-1));
144     }
145     
146     public void setInstanceName(String name) {
147         _name = name;
148     }
149 
150     public String getInstanceName() {
151         return _name;
152     }
153     
154     public void setUrl(String url) {
155         _url = url;
156     }
157     
158     public String getUrl() {
159         return _url;
160     }
161     
162     public void setBindDN(String bindDN) {
163         _bindDN = bindDN;
164     }
165     
166     public String getBindDN() {
167         return _bindDN;
168     }
169     
170     public void setPassword(String password) {
171         _passwd = password;
172     }
173     
174     public String getPassword() {
175         return _passwd;
176     }
177     
178     public void setSearchBase(String searchBase) {
179         _searchBase = searchBase;
180     }
181     
182     public String getSearchBase() {
183         return _searchBase;
184     }
185     
186     // Atributes
187     private MBeanServer server = null;        
188     private String _name = null;
189     private String _url = null;
190     private String _bindDN = null;
191     private String _passwd = null;
192     private String _searchBase = null;
193     private String jndiName = null;
194     
195     private static Hashtable ldapsmTable = new Hashtable();
196     
197     // Constants
198     public static String JNDI_NAME_PREFIX = "java:/security";
199     
200 }