KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > srp > SRPVerifierStoreService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software 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 software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.security.srp;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.Name JavaDoc;
29
30 import org.jboss.naming.NonSerializableFactory;
31 import org.jboss.security.srp.SerialObjectStore;
32 import org.jboss.system.ServiceMBeanSupport;
33
34 /** The JMX mbean interface for the SRP password verifier store. This
35 implementation uses the SerialObjectStore as a simple and yet secure
36 source of usernames and their password verifiers and verifier salts. It
37 also provides a simple interface for adding and deleting users from the
38 SerialObjectStore. The mbean stores a non-serializable reference to the
39 SRPVerifierStore interface in JNDI under the property.
40
41 @see org.jboss.security.srp.SerialObjectStore
42
43 @author Scott.Stark@jboss.org
44 @version $Revision: 40096 $
45 */

46 public class SRPVerifierStoreService extends ServiceMBeanSupport
47    implements SRPVerifierStoreServiceMBean
48 {
49     private SerialObjectStore store;
50     private String JavaDoc fileName = "SRPVerifierStore.ser";
51     private String JavaDoc jndiName = "srp/DefaultVerifierSource";
52
53 // --- Begin SRPVerifierStoreServiceMBean interface methods
54
/** Get the jndi name for the SRPVerifierSource implementation binding.
55     */

56     public String JavaDoc getJndiName()
57     {
58         return jndiName;
59     }
60    /** set the jndi name for the SRPVerifierSource implementation binding.
61     */

62     public void setJndiName(String JavaDoc jndiName)
63     {
64         this.jndiName = jndiName;
65     }
66     public void setStoreFile(String JavaDoc fileName) throws IOException JavaDoc
67     {
68         this.fileName = fileName;
69         if( store != null )
70         {
71             File JavaDoc storeFile = new File JavaDoc(fileName);
72             store.save(storeFile);
73         }
74     }
75
76     public void addUser(String JavaDoc username,String JavaDoc password) throws IOException JavaDoc
77     {
78         try
79         {
80             store.addUser(username, password);
81             save();
82             log.debug("Added username: "+username);
83         }
84         catch(Exception JavaDoc e)
85         {
86             log.warn("Failed to addUser, username="+username, e);
87         }
88     }
89
90     public void delUser(String JavaDoc username) throws IOException JavaDoc
91     {
92         store.delUser(username);
93         log.debug("Added username: "+username);
94         save();
95     }
96 // --- End SRPVerifierStoreServiceMBean interface methods
97

98     public String JavaDoc getName()
99     {
100         return "SRPVerifierStoreService";
101     }
102
103     public void initService() throws Exception JavaDoc
104     {
105     }
106     
107     public void startService() throws Exception JavaDoc
108     {
109         File JavaDoc storeFile = new File JavaDoc(fileName);
110         store = new SerialObjectStore(storeFile);
111         log.info("Created SerialObjectStore at: "+storeFile.getAbsolutePath());
112         // Bind a reference to store using NonSerializableFactory as the ObjectFactory
113
InitialContext JavaDoc ctx = new InitialContext JavaDoc();
114         Name JavaDoc name = ctx.getNameParser("").parse(jndiName);
115         NonSerializableFactory.rebind(name, store, true);
116     }
117
118     private void save() throws IOException JavaDoc
119     {
120         if( store != null )
121         { // Try to locate the file on the classpath
122
File JavaDoc storeFile = new File JavaDoc(fileName);
123             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
124             URL JavaDoc url = loader.getResource(fileName);
125             if( url == null )
126             { // Try to locate the file's parent on the classpath
127
String JavaDoc parent = storeFile.getParent();
128                 if( parent != null )
129                 {
130                     url = loader.getResource(parent);
131                     if( url != null )
132                     {
133                         storeFile = new File JavaDoc(url.getFile(), storeFile.getName());
134                     }
135                     // else, just go with storeFile as a system file path
136
}
137             }
138             else
139             {
140                 storeFile = new File JavaDoc(url.getFile());
141             }
142             store.save(storeFile);
143         }
144     }
145 }
146
Popular Tags