View Javadoc

1   /*
2    * $Id: DatabaseSecurityModuleService.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 javax.naming.*;
27  import javax.naming.spi.*;
28  import javax.management.*;
29  import javax.sql.*;
30  
31  import org.jboss.util.ServiceMBeanSupport;
32  
33  /***
34   *
35   * @author  Jose M. Palomar <josem@talika.org>
36   * @version $Revision: 20 $
37   */
38  public class DatabaseSecurityModuleService extends ServiceMBeanSupport implements DatabaseSecurityModuleServiceMBean, ObjectFactory
39  {
40      
41      public DatabaseSecurityModuleService() {
42      }
43      
44      public DatabaseSecurityModuleService(String name, String dsName) {
45          _name = name;
46          _dsName = dsName;
47      }
48      
49      public String getName()
50      {
51          return "Database Security Module";
52      }
53      
54      protected ObjectName getObjectName(MBeanServer server, ObjectName name)
55      throws javax.management.MalformedObjectNameException
56      {
57          this.server = server;
58          return new ObjectName(OBJECT_NAME);
59      }
60      
61      protected void initService()
62      throws Exception
63      {   
64          // Nothing todo
65      }
66      
67      protected void startService()
68      throws Exception
69      {
70          // Check required attributes
71          if(_name == null || _dsName == null) {
72              
73              if(_name == null) log.log("InstanceName attribute not set");
74              if(_dsName == null) log.log("DataSource attribute not set");
75             
76              log.log("Database Security Module not started");
77              
78              return;
79              
80          }        
81          
82          // Get datasource from JNDI
83          InitialContext initialCtx = new InitialContext();
84          DataSource ds = (DataSource) initialCtx.lookup(_dsName);
85  
86          // Create the security module
87          DatabaseSecurityModule dbsm = new DatabaseSecurityModule(ds);
88                  
89          // Bind reference to SM in JNDI
90          Reference ref = new Reference(dbsm.getClass().toString(), getClass().getName(), null);
91          Context ctx = (Context) new InitialContext();        
92          jndiName = JNDI_NAME_PREFIX + "/" + _name;
93          try {                                               
94              ctx.bind(jndiName, ref);                                    
95          }
96          catch(NameNotFoundException nabe) {
97              ctx.createSubcontext(JNDI_NAME_PREFIX);
98              ctx.bind(jndiName, ref);
99          }                
100         
101         // Store it in global Hashtable
102         dbsmTable.put(_name, dbsm);
103         
104         log.log("Database Security Module " + _name + " bound to " + jndiName);
105         log.log("DataSource: " + _dsName);        
106         
107     }    
108     
109     protected void stopService()
110     {
111         try
112         {
113             new InitialContext().unbind(jndiName);
114             dbsmTable.remove(_name);
115         }
116         catch (CommunicationException e) {
117         }
118         catch (Exception e)
119         {
120             log.exception(e);
121         }
122     }
123     
124     protected void destroyService()
125     {
126         // Nothing todo        
127     }
128         
129     public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
130     throws Exception
131     {
132         return dbsmTable.get(name.get(name.size()-1));
133     }
134     
135     public void setInstanceName(String name) {
136         _name = name;
137     }
138     
139     public String getInstanceName() {
140         return _name;
141     }
142     
143     public void setDataSource(String name) {
144         _dsName = name;
145     }
146     
147     public String getDataSource() {
148         return _dsName;
149     }
150     
151     // Atributes
152     private MBeanServer server = null;
153     private String _name = null;
154     private String _dsName = null;
155     private String jndiName = null;    
156         
157     private static Hashtable dbsmTable = new Hashtable();
158     
159     // Constants
160     public static String JNDI_NAME_PREFIX = "java:/security";
161     
162 }