PropertyUserStore: Code Format
This commit is contained in:
parent
129a66f579
commit
df0aab1f49
|
@ -31,108 +31,104 @@ import org.eclipse.jetty.util.log.Logger;
|
|||
|
||||
/**
|
||||
* PropertyFileLoginModule
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class PropertyFileLoginModule extends AbstractLoginModule
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(PropertyFileLoginModule.class);
|
||||
|
||||
public static final String DEFAULT_FILENAME = "realm.properties";
|
||||
public static final Map<String, Map<String, UserInfo>> fileMap = new HashMap<String, Map<String, UserInfo>>();
|
||||
|
||||
private String propertyFileName;
|
||||
|
||||
|
||||
public static final Map<String, Map<String, UserInfo>> fileMap = new HashMap<String, Map<String, UserInfo>>();
|
||||
|
||||
/**
|
||||
private String propertyFileName;
|
||||
|
||||
/**
|
||||
* Read contents of the configured property file.
|
||||
*
|
||||
* @see javax.security.auth.spi.LoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, java.util.Map, java.util.Map)
|
||||
* @see javax.security.auth.spi.LoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, java.util.Map,
|
||||
* java.util.Map)
|
||||
* @param subject
|
||||
* @param callbackHandler
|
||||
* @param sharedState
|
||||
* @param options
|
||||
*/
|
||||
public void initialize(Subject subject, CallbackHandler callbackHandler,
|
||||
Map<String,?> sharedState, Map<String,?> options)
|
||||
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options)
|
||||
{
|
||||
super.initialize(subject, callbackHandler, sharedState, options);
|
||||
super.initialize(subject,callbackHandler,sharedState,options);
|
||||
loadProperties((String)options.get("file"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void loadProperties (String filename)
|
||||
|
||||
public void loadProperties(String filename)
|
||||
{
|
||||
File propsFile;
|
||||
|
||||
|
||||
if (filename == null)
|
||||
{
|
||||
propsFile = new File(System.getProperty("user.dir"), DEFAULT_FILENAME);
|
||||
//look for a file called realm.properties in the current directory
|
||||
//if that fails, look for a file called realm.properties in $jetty.home/etc
|
||||
propsFile = new File(System.getProperty("user.dir"),DEFAULT_FILENAME);
|
||||
// look for a file called realm.properties in the current directory
|
||||
// if that fails, look for a file called realm.properties in $jetty.home/etc
|
||||
if (!propsFile.exists())
|
||||
propsFile = new File(System.getProperty("jetty.home"), DEFAULT_FILENAME);
|
||||
propsFile = new File(System.getProperty("jetty.home"),DEFAULT_FILENAME);
|
||||
}
|
||||
else
|
||||
{
|
||||
propsFile = new File(filename);
|
||||
}
|
||||
|
||||
//give up, can't find a property file to load
|
||||
|
||||
// give up, can't find a property file to load
|
||||
if (!propsFile.exists())
|
||||
{
|
||||
LOG.warn("No property file found");
|
||||
throw new IllegalStateException ("No property file specified in login module configuration file");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
this.propertyFileName = propsFile.getCanonicalPath();
|
||||
if (fileMap.get(propertyFileName) != null)
|
||||
{
|
||||
if (LOG.isDebugEnabled()) {LOG.debug("Properties file "+propertyFileName+" already in cache, skipping load");}
|
||||
if (Log.isDebugEnabled())
|
||||
{
|
||||
Log.debug("Properties file " + propertyFileName + " already in cache, skipping load");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Map<String, UserInfo> userInfoMap = new HashMap<String, UserInfo>();
|
||||
Properties props = new Properties();
|
||||
props.load(new FileInputStream(propsFile));
|
||||
Iterator<Map.Entry<Object,Object>> iter = props.entrySet().iterator();
|
||||
while(iter.hasNext())
|
||||
Iterator<Map.Entry<Object, Object>> iter = props.entrySet().iterator();
|
||||
while (iter.hasNext())
|
||||
{
|
||||
|
||||
Map.Entry<Object,Object> entry = iter.next();
|
||||
String username=entry.getKey().toString().trim();
|
||||
String credentials=entry.getValue().toString().trim();
|
||||
String roles=null;
|
||||
int c=credentials.indexOf(',');
|
||||
if (c>0)
|
||||
|
||||
Map.Entry<Object, Object> entry = iter.next();
|
||||
String username = entry.getKey().toString().trim();
|
||||
String credentials = entry.getValue().toString().trim();
|
||||
String roles = null;
|
||||
int c = credentials.indexOf(',');
|
||||
if (c > 0)
|
||||
{
|
||||
roles=credentials.substring(c+1).trim();
|
||||
credentials=credentials.substring(0,c).trim();
|
||||
roles = credentials.substring(c + 1).trim();
|
||||
credentials = credentials.substring(0,c).trim();
|
||||
}
|
||||
|
||||
if (username!=null && username.length()>0 &&
|
||||
credentials!=null && credentials.length()>0)
|
||||
if (username != null && username.length() > 0 && credentials != null && credentials.length() > 0)
|
||||
{
|
||||
ArrayList<String> roleList = new ArrayList<String>();
|
||||
if(roles!=null && roles.length()>0)
|
||||
if (roles != null && roles.length() > 0)
|
||||
{
|
||||
StringTokenizer tok = new StringTokenizer(roles,", ");
|
||||
|
||||
|
||||
while (tok.hasMoreTokens())
|
||||
roleList.add(tok.nextToken());
|
||||
}
|
||||
|
||||
userInfoMap.put(username, (new UserInfo(username, Credential.getCredential(credentials.toString()), roleList)));
|
||||
|
||||
userInfoMap.put(username,(new UserInfo(username,Credential.getCredential(credentials.toString()),roleList)));
|
||||
}
|
||||
}
|
||||
|
||||
fileMap.put(propertyFileName, userInfoMap);
|
||||
|
||||
fileMap.put(propertyFileName,userInfoMap);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -141,13 +137,13 @@ public class PropertyFileLoginModule extends AbstractLoginModule
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't implement this as we want to pre-fetch all of the
|
||||
* users.
|
||||
/**
|
||||
* Don't implement this as we want to pre-fetch all of the users.
|
||||
*
|
||||
* @param username
|
||||
* @throws Exception
|
||||
*/
|
||||
public UserInfo getUserInfo (String username) throws Exception
|
||||
public UserInfo getUserInfo(String username) throws Exception
|
||||
{
|
||||
Map<?, ?> userInfoMap = (Map<?, ?>)fileMap.get(propertyFileName);
|
||||
if (userInfoMap == null)
|
||||
|
|
Loading…
Reference in New Issue