You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ra...@apache.org on 2002/03/06 15:25:41 UTC
cvs commit: jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager AbstractUserManager.java DbUserManager.java DbUserManager.xinfo DefaultUserManager.java DefaultUserManager.xinfo LdapUserManager.java LdapUserManager.xinfo PropertiesUserManager.java PropertiesUserManager.xinfo User.java UserManagerInterface.java
rana_b 02/03/06 06:25:41
Added: ftpserver/src/java/org/apache/avalon/ftpserver/usermanager
AbstractUserManager.java DbUserManager.java
DbUserManager.xinfo DefaultUserManager.java
DefaultUserManager.xinfo LdapUserManager.java
LdapUserManager.xinfo PropertiesUserManager.java
PropertiesUserManager.xinfo User.java
UserManagerInterface.java
Log:
second stage of refactoring
Revision Changes Path
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/AbstractUserManager.java
Index: AbstractUserManager.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.ftpserver.usermanager;
import org.apache.avalon.phoenix.Block;
import org.apache.avalon.phoenix.BlockContext;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
/**
* Abstract user manager class.
*
* @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
*/
public
abstract class AbstractUserManager extends AbstractLogEnabled
implements UserManagerInterface,
Block,
Contextualizable,
Composable,
Configurable,
Initializable {
protected Configuration mConfig;
protected BlockContext mBlockContext;
protected ComponentManager mComponentManager;
/**
* Set context object - first step.
*/
public void contextualize(Context context) throws ContextException {
mBlockContext = (BlockContext) context;
}
/**
* Get block context.
*/
public BlockContext getContext() {
return mBlockContext;
}
/**
* Set component manager - second step.
*/
public void compose(ComponentManager compManager) throws ComponentException {
mComponentManager = compManager;
}
/**
* Get component manager.
*/
public ComponentManager getComponentManager() {
return mComponentManager;
}
/**
* Configure user manager - third step.
*/
public void configure(Configuration config) throws ConfigurationException {
mConfig = config;
}
/**
* Get config object.
*/
public Configuration getConfig() {
return mConfig;
}
/**
* Initialize - fourth step.
*/
public void initialize() throws Exception {
}
/**
* Reload user data - dummy implementation.
*/
public void reload() throws Exception {
}
/**
* Close user manager - dummy implementation.
*/
public void close() {
}
}
1.6 +164 -65 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/DbUserManager.java
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/DbUserManager.xinfo
Index: DbUserManager.xinfo
===================================================================
<?xml version="1.0"?>
<blockinfo>
<!-- section to describe user manager block -->
<block>
<version>1.0</version>
</block>
<!-- services that are offered by this block -->
<services>
<service name="org.apache.avalon.ftpserver.usermanager.UserManagerInterface" version="1.0" />
</services>
</blockinfo>
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/DefaultUserManager.java
Index: DefaultUserManager.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.ftpserver.usermanager;
import java.util.List;
import java.util.Iterator;
import java.util.Collections;
import java.util.ArrayList;
import org.apache.avalon.cornerstone.services.store.ObjectRepository;
import org.apache.avalon.cornerstone.services.store.Store;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
/**
* Default file object store based user manager.
*
* @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
*/
public class DefaultUserManager extends AbstractUserManager {
protected Configuration mStoreConfig;
protected Store mStore;
protected ObjectRepository mObjectRepository;
/**
* Configure user manager
*/
public void configure(Configuration conf) throws ConfigurationException {
super.configure(conf);
mStoreConfig = conf.getChild("repository");
}
/**
* Get store manager.
*/
public void compose(ComponentManager compManager) throws ComponentException {
super.compose(compManager);
mStore = (Store) compManager.lookup(Store.class.getName());
}
/**
* Initialize object repository.
*/
public void initialize() throws Exception {
super.initialize();
mObjectRepository = (ObjectRepository) mStore.select(mStoreConfig);
}
/**
* Save user object.
*/
public synchronized void save(User usr) {
if (usr.getName() == null) {
throw new NullPointerException("User name is null.");
}
usr.setPassword(getPassword(usr));
mObjectRepository.put(usr.getName(), usr);
}
/**
* Delete an user. Removes all this user entries from the properties.
* After removing the corresponding from the properties, save the data.
*/
public synchronized void delete(String usrName) {
mObjectRepository.remove(usrName);
}
/**
* Get all user names.
*/
public synchronized List getAllUserNames() {
ArrayList usrList = new ArrayList();
Iterator nameIt = mObjectRepository.list();
while(nameIt.hasNext()) {
usrList.add(nameIt.next());
}
Collections.sort(usrList);
return usrList;
}
/**
* Load user data.
*/
public synchronized User getUserByName(String userName) {
User user = null;
if(doesExist(userName)) {
user = (User)mObjectRepository.get(userName);
}
return user;
}
/**
* User existance check
*/
public synchronized boolean doesExist(String name) {
return mObjectRepository.containsKey(name);
}
/**
* Get user password. Returns the encrypted value.
* If the password value is not null
* password = new password
* else
* if user does exist
* password = old password
* else
* password = ""
*/
private String getPassword(User usr) {
String password = usr.getPassword();
if ( (password == null) && doesExist(usr.getName()) ) {
usr = getUserByName(usr.getName());
password = usr.getPassword();
}
if (password == null) {
password = "";
}
return password;
}
/**
* User authenticate method
*/
public boolean authenticate(String userName, String password) {
User user = getUserByName(userName);
if(user == null) {
return false;
}
if(user.getIsAnonymous()) {
return true;
}
return password.equals(user.getPassword());
}
}
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/DefaultUserManager.xinfo
Index: DefaultUserManager.xinfo
===================================================================
<?xml version="1.0"?>
<blockinfo>
<!-- section to describe block -->
<block>
<version>1.0</version>
</block>
<!-- services that are offered by this block -->
<services>
<service name="org.apache.avalon.ftpserver.usermanager.UserManagerInterface" version="1.0" />
</services>
<!-- service dependency -->
<dependencies>
<dependency>
<service name="org.apache.avalon.cornerstone.services.store.Store" version="1.0"/>
</dependency>
</dependencies>
</blockinfo>
1.5 +103 -79 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/LdapUserManager.java
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/LdapUserManager.xinfo
Index: LdapUserManager.xinfo
===================================================================
<?xml version="1.0"?>
<blockinfo>
<!-- section to describe user manager block -->
<block>
<version>1.0</version>
</block>
<!-- services that are offered by this block -->
<services>
<service name="org.apache.avalon.ftpserver.usermanager.UserManagerInterface" version="1.0" />
</services>
</blockinfo>
1.5 +124 -67 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/PropertiesUserManager.java
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/PropertiesUserManager.xinfo
Index: PropertiesUserManager.xinfo
===================================================================
<?xml version="1.0"?>
<blockinfo>
<!-- section to describe user manager block -->
<block>
<version>1.0</version>
</block>
<!-- services that are offered by this block -->
<services>
<service name="org.apache.avalon.ftpserver.usermanager.UserManagerInterface" version="1.0" />
</services>
</blockinfo>
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/User.java
Index: User.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.ftpserver.usermanager;
import java.io.Serializable;
import java.net.InetAddress;
import java.rmi.server.UID;
import org.apache.avalon.ftpserver.util.VirtualDirectory;
/**
* Generic user class. All the application specific user classes will
* be derived from this.
*
* @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
*/
public
class User implements Serializable {
public final static String ANONYMOUS = "anonymous";
public final static String ADMIN = "admin";
private String mstUserName = null;
private String mstPassword = null;
private long mlIdleTime = 0; // no limit
private int miUploadRateLimit = 0; // no limit
private int miDownloadRateLimit = 0; // no limit
private long mlLoginTime = 0;
private long mlLastAccessTime = 0;
private boolean mbEnabled = true;
private VirtualDirectory mUserDirectory = null;
private String mstSessionId = null;
private InetAddress mClientAddress = null;
/**
* Constructor, set session id and default virtual directory object.
*/
public User() {
mUserDirectory = new VirtualDirectory();
mstSessionId = new UID().toString();
}
/**
* Get the user name.
*/
public String getName() {
return mstUserName;
}
/**
* Set user name.
*/
public void setName(String name) {
mstUserName = name;
}
/**
* Get the user password.
*/
public String getPassword() {
return mstPassword;
}
/**
* Set user password
*/
public void setPassword(String pass) {
mstPassword = pass;
}
/**
* Get the maximum idle time in second.
*/
public int getMaxIdleTime() {
return (int)(mlIdleTime/1000);
}
/**
* Set the maximum idle time in second.
*/
public void setMaxIdleTime(int idleSec) {
if(idleSec < 0L) {
mlIdleTime = 0L;
}
mlIdleTime = idleSec * 1000L;
}
/**
* Get the user enable status.
*/
public boolean getEnabled() {
return mbEnabled;
}
/**
* Set the user enable status
*/
public void setEnabled(boolean enb) {
mbEnabled = enb;
}
/**
* Get maximum user upload rate in bytes/sec.
*/
public int getMaxUploadRate() {
return miUploadRateLimit;
}
/**
* Set user maximum upload rate limit.
* Less than or equal to zero means no limit.
*/
public void setMaxUploadRate(int rate) {
miUploadRateLimit = rate;
}
/**
* Get maximum user download rate in bytes/sec
*/
public int getMaxDownloadRate() {
return miDownloadRateLimit;
}
/**
* Set user maximum download rate limit.
* Less than or equal to zero means no limit.
*/
public void setMaxDownloadRate(int rate) {
miDownloadRateLimit = rate;
}
/**
* Get client address
*/
public InetAddress getClientAddress() {
return mClientAddress;
}
/**
* Set client address
*/
public void setClientAddress(InetAddress clientAddress) {
mClientAddress = clientAddress;
}
/**
* get user filesystem view
*/
public VirtualDirectory getVirtualDirectory() {
return mUserDirectory;
}
/**
* Is an anonymous user?
*/
public boolean getIsAnonymous() {
return ANONYMOUS.equals(getName());
}
/**
* Is an admin user
*/
public boolean getIsAdmin() {
return ADMIN.equals(getName());
}
/**
* Get session id.
*/
public String getSessionId() {
return mstSessionId;
}
/**
* Get user loglin time.
*/
public long getLoginTime() {
return mlLoginTime;
}
/**
* Get last access time
*/
public long getLastAccessTime() {
return mlLastAccessTime;
}
/**
* Check the user login status.
*/
public boolean hasLoggedIn() {
return mlLoginTime != 0;
}
/**
* User login.
*/
public void login() {
mlLoginTime = System.currentTimeMillis();
mlLastAccessTime = mlLoginTime;
}
/**
* User logout
*/
public void logout() {
mlLoginTime = 0;
}
/**
* Is an active user (is removable)?
* Compares the last access time with the specified time.
*/
public boolean isActive(long currTime) {
boolean bActive = true;
long maxIdleTime = getMaxIdleTime() * 1000; // milliseconds
if(maxIdleTime != 0L) {
long idleTime = currTime - mlLastAccessTime;
bActive = maxIdleTime > idleTime;
}
return bActive;
}
/**
* Is still active. Compares the last access time with the
* current time.
*/
public boolean isActive() {
return isActive(System.currentTimeMillis());
}
/**
* Hit user - update last access time
*/
public void hitUser() {
mlLastAccessTime = System.currentTimeMillis();
}
/**
* Equality check.
*/
public boolean equals(Object obj) {
if (obj instanceof User) {
return ((User)obj).mstSessionId.equals(mstSessionId);
}
return false;
}
/**
* String representation
*/
public String toString() {
return mstUserName;
}
}
1.1 jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/usermanager/UserManagerInterface.java
Index: UserManagerInterface.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.ftpserver.usermanager;
import java.util.List;
/**
* This is user manager interface. All the user manager classes
* implement this interface. If we want to add a new user manager,
* we have to implement this class.
*
* @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
*/
public
interface UserManagerInterface {
public final static String ROLE = "org.apache.avalon.ftpserver.usermanager.UserManagerInterface";
/**
* Save the user. If a new user, create it else update the
* existing user.
*/
void save(User user) throws Exception;
/**
* Delete the user from the system.
*
* @param name name of the user to be deleted.
*/
void delete(String userName) throws Exception;
/**
* Get user by name.
*/
User getUserByName(String name);
/**
* Get all user names in the system.
*/
List getAllUserNames();
/**
* User existance check.
*
* @param name user name
*/
boolean doesExist(String name);
/**
* Authenticate user
*/
boolean authenticate(String login, String password);
/**
* Load the user data again
*/
void reload() throws Exception;
/**
* Close the user manager
*/
void close();
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>