1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
65 }
66
67 protected void startService()
68 throws Exception
69 {
70
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
83 InitialContext initialCtx = new InitialContext();
84 DataSource ds = (DataSource) initialCtx.lookup(_dsName);
85
86
87 DatabaseSecurityModule dbsm = new DatabaseSecurityModule(ds);
88
89
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
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
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
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
160 public static String JNDI_NAME_PREFIX = "java:/security";
161
162 }