1 22 package org.jboss.security.srp; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.URL ; 27 import javax.naming.InitialContext ; 28 import javax.naming.Name ; 29 30 import org.jboss.naming.NonSerializableFactory; 31 import org.jboss.security.srp.SerialObjectStore; 32 import org.jboss.system.ServiceMBeanSupport; 33 34 46 public class SRPVerifierStoreService extends ServiceMBeanSupport 47 implements SRPVerifierStoreServiceMBean 48 { 49 private SerialObjectStore store; 50 private String fileName = "SRPVerifierStore.ser"; 51 private String jndiName = "srp/DefaultVerifierSource"; 52 53 56 public String getJndiName() 57 { 58 return jndiName; 59 } 60 62 public void setJndiName(String jndiName) 63 { 64 this.jndiName = jndiName; 65 } 66 public void setStoreFile(String fileName) throws IOException 67 { 68 this.fileName = fileName; 69 if( store != null ) 70 { 71 File storeFile = new File (fileName); 72 store.save(storeFile); 73 } 74 } 75 76 public void addUser(String username,String password) throws IOException 77 { 78 try 79 { 80 store.addUser(username, password); 81 save(); 82 log.debug("Added username: "+username); 83 } 84 catch(Exception e) 85 { 86 log.warn("Failed to addUser, username="+username, e); 87 } 88 } 89 90 public void delUser(String username) throws IOException 91 { 92 store.delUser(username); 93 log.debug("Added username: "+username); 94 save(); 95 } 96 98 public String getName() 99 { 100 return "SRPVerifierStoreService"; 101 } 102 103 public void initService() throws Exception 104 { 105 } 106 107 public void startService() throws Exception 108 { 109 File storeFile = new File (fileName); 110 store = new SerialObjectStore(storeFile); 111 log.info("Created SerialObjectStore at: "+storeFile.getAbsolutePath()); 112 InitialContext ctx = new InitialContext (); 114 Name name = ctx.getNameParser("").parse(jndiName); 115 NonSerializableFactory.rebind(name, store, true); 116 } 117 118 private void save() throws IOException 119 { 120 if( store != null ) 121 { File storeFile = new File (fileName); 123 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 124 URL url = loader.getResource(fileName); 125 if( url == null ) 126 { String parent = storeFile.getParent(); 128 if( parent != null ) 129 { 130 url = loader.getResource(parent); 131 if( url != null ) 132 { 133 storeFile = new File (url.getFile(), storeFile.getName()); 134 } 135 } 137 } 138 else 139 { 140 storeFile = new File (url.getFile()); 141 } 142 store.save(storeFile); 143 } 144 } 145 } 146 | Popular Tags |