You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2020/02/27 15:10:38 UTC

[jspwiki] 13/20: move static UserProfile newProfile( UserDatabase db ) from DefaultUserProfile into AbstractUserDatabase to break a cycle between those two classes

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit a35b48eaed05996b7031283f711ef43ad275ea34
Author: juanpablo <ju...@apache.org>
AuthorDate: Wed Feb 26 19:48:33 2020 +0100

    move static UserProfile newProfile( UserDatabase db ) from DefaultUserProfile into AbstractUserDatabase to break a cycle between those two classes
---
 .../wiki/auth/user/AbstractUserDatabase.java       |  79 ++++-----
 .../apache/wiki/auth/user/DefaultUserProfile.java  | 176 +++++++++------------
 .../org/apache/wiki/auth/user/UserDatabase.java    |   1 +
 3 files changed, 117 insertions(+), 139 deletions(-)

diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
index 78b030d..e4d79da 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
@@ -48,15 +48,15 @@ public abstract class AbstractUserDatabase implements UserDatabase {
     protected static final String SSHA_PREFIX = "{SSHA}";
 
     /**
-     * Looks up and returns the first {@link UserProfile}in the user database
-     * that whose login name, full name, or wiki name matches the supplied
-     * string. This method provides a "forgiving" search algorithm for resolving
-     * principal names when the exact profile attribute that supplied the name
-     * is unknown.
+     * Looks up and returns the first {@link UserProfile} in the user database that whose login name, full name, or wiki name matches the
+     * supplied string. This method provides a "forgiving" search algorithm for resolving principal names when the exact profile attribute
+     * that supplied the name is unknown.
+     *
      * @param index the login name, full name, or wiki name
      * @see org.apache.wiki.auth.user.UserDatabase#find(java.lang.String)
      */
-    @Override public UserProfile find( final String index ) throws NoSuchPrincipalException {
+    @Override
+    public UserProfile find( final String index ) throws NoSuchPrincipalException {
         UserProfile profile = null;
 
         // Try finding by full name
@@ -93,25 +93,29 @@ public abstract class AbstractUserDatabase implements UserDatabase {
      * {@inheritDoc}
      * @see org.apache.wiki.auth.user.UserDatabase#findByEmail(java.lang.String)
      */
-    @Override public abstract UserProfile findByEmail( String index ) throws NoSuchPrincipalException;
+    @Override
+    public abstract UserProfile findByEmail( String index ) throws NoSuchPrincipalException;
 
     /**
      * {@inheritDoc}
      * @see org.apache.wiki.auth.user.UserDatabase#findByFullName(java.lang.String)
      */
-    @Override public abstract UserProfile findByFullName( String index ) throws NoSuchPrincipalException;
+    @Override
+    public abstract UserProfile findByFullName( String index ) throws NoSuchPrincipalException;
 
     /**
      * {@inheritDoc}
      * @see org.apache.wiki.auth.user.UserDatabase#findByLoginName(java.lang.String)
      */
-    @Override public abstract UserProfile findByLoginName( String index ) throws NoSuchPrincipalException;
+    @Override
+    public abstract UserProfile findByLoginName( String index ) throws NoSuchPrincipalException;
 
     /**
      * {@inheritDoc}
      * @see org.apache.wiki.auth.user.UserDatabase#findByWikiName(java.lang.String)
      */
-    @Override public abstract UserProfile findByWikiName( String index ) throws NoSuchPrincipalException;
+    @Override
+    public abstract UserProfile findByWikiName( String index ) throws NoSuchPrincipalException;
 
     /**
      * <p>Looks up the Principals representing a user from the user database. These
@@ -126,50 +130,50 @@ public abstract class AbstractUserDatabase implements UserDatabase {
      *            {@link UserProfile#getLoginName()}method.
      * @return the array of Principals representing the user
      * @see org.apache.wiki.auth.user.UserDatabase#getPrincipals(java.lang.String)
-     * @throws NoSuchPrincipalException {@inheritDoc}
+     * @throws NoSuchPrincipalException If the user database does not contain user with the supplied identifier
      */
-    @Override public Principal[] getPrincipals( final String identifier ) throws NoSuchPrincipalException
-    {
-        try {
-            final UserProfile profile = findByLoginName( identifier );
-            final ArrayList< Principal > principals = new ArrayList<>();
-            if( profile.getLoginName() != null && profile.getLoginName().length() > 0 ) {
-                principals.add( new WikiPrincipal( profile.getLoginName(), WikiPrincipal.LOGIN_NAME ) );
-            }
-            if( profile.getFullname() != null && profile.getFullname().length() > 0 ) {
-                principals.add( new WikiPrincipal( profile.getFullname(), WikiPrincipal.FULL_NAME ) );
-            }
-            if( profile.getWikiName() != null && profile.getWikiName().length() > 0 ) {
-                principals.add( new WikiPrincipal( profile.getWikiName(), WikiPrincipal.WIKI_NAME ) );
-            }
-            return principals.toArray( new Principal[ principals.size() ] );
-        } catch( final NoSuchPrincipalException e ) {
-            throw e;
+    @Override
+    public Principal[] getPrincipals( final String identifier ) throws NoSuchPrincipalException {
+        final UserProfile profile = findByLoginName( identifier );
+        final ArrayList< Principal > principals = new ArrayList<>();
+        if( profile.getLoginName() != null && profile.getLoginName().length() > 0 ) {
+            principals.add( new WikiPrincipal( profile.getLoginName(), WikiPrincipal.LOGIN_NAME ) );
+        }
+        if( profile.getFullname() != null && profile.getFullname().length() > 0 ) {
+            principals.add( new WikiPrincipal( profile.getFullname(), WikiPrincipal.FULL_NAME ) );
         }
+        if( profile.getWikiName() != null && profile.getWikiName().length() > 0 ) {
+            principals.add( new WikiPrincipal( profile.getWikiName(), WikiPrincipal.WIKI_NAME ) );
+        }
+        return principals.toArray( new Principal[ principals.size() ] );
     }
 
     /**
      * {@inheritDoc}
+     *
      * @see org.apache.wiki.auth.user.UserDatabase#initialize(org.apache.wiki.api.core.Engine, java.util.Properties)
      */
-    @Override public abstract void initialize( Engine engine, Properties props ) throws NoRequiredPropertyException, WikiSecurityException;
+    @Override
+    public abstract void initialize( Engine engine, Properties props ) throws NoRequiredPropertyException, WikiSecurityException;
 
     /**
-     * Factory method that instantiates a new DefaultUserProfile with a new, distinct
-     * unique identifier.
+     * Factory method that instantiates a new DefaultUserProfile with a new, distinct unique identifier.
      * 
      * @return A new, empty profile.
      */
-    @Override public UserProfile newProfile()
-    {
-        return DefaultUserProfile.newProfile( this );
+    @Override
+    public UserProfile newProfile() {
+        final UserProfile profile = new DefaultUserProfile();
+        profile.setUid( AbstractUserDatabase.generateUid( this ) );
+        return profile;
     }
 
     /**
      * {@inheritDoc}
      * @see org.apache.wiki.auth.user.UserDatabase#save(org.apache.wiki.auth.user.UserProfile)
      */
-    @Override public abstract void save( UserProfile profile ) throws WikiSecurityException;
+    @Override
+    public abstract void save( UserProfile profile ) throws WikiSecurityException;
 
     /**
      * Validates the password for a given user. If the user does not exist in the user database, this method always returns
@@ -181,7 +185,8 @@ public abstract class AbstractUserDatabase implements UserDatabase {
      * @return <code>true</code> if the supplied user password matches the stored password
      * @see org.apache.wiki.auth.user.UserDatabase#validatePassword(java.lang.String, java.lang.String)
      */
-    @Override public boolean validatePassword( final String loginName, final String password ) {
+    @Override
+    public boolean validatePassword( final String loginName, final String password ) {
         final String hashedPassword;
         try {
             final UserProfile profile = findByLoginName( loginName );
@@ -286,7 +291,7 @@ public abstract class AbstractUserDatabase implements UserDatabase {
      */
     protected long parseLong( final String value ) {
         if( NumberUtils.isParsable( value ) ) {
-            return Long.valueOf( value );
+            return Long.parseLong( value );
         } else {
             return 0L;
         }
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/DefaultUserProfile.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/DefaultUserProfile.java
index ae2df73..416cca4 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/DefaultUserProfile.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/DefaultUserProfile.java
@@ -27,88 +27,63 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
+
 /**
- * Default implementation for representing wiki user information, such as the
- * login name, full name, wiki name, and e-mail address.
+ * Default implementation for representing wiki user information, such as the login name, full name, wiki name, and e-mail address.
+ *
  * @since 2.3
  */
+public final class DefaultUserProfile implements UserProfile {
 
-public final class DefaultUserProfile implements UserProfile
-{
     private static final long serialVersionUID = -5600466893735300647L;
-
     private static final String EMPTY_STRING = "";
-
     private static final String WHITESPACE = "\\s";
-    
-    private Map<String,Serializable> attributes = new HashMap<>();
 
-    private Date     created   = null;
-
-    private String   email     = null;
-
-    private String   fullname  = null;
-    
+    private Map< String, Serializable > attributes = new HashMap<>();
+    private Date created = null;
+    private String email = null;
+    private String fullname = null;
     private Date lockExpiry = null;
-
-    private String   loginName = null;
-
-    private Date     modified  = null;
-
-    private String   password  = null;
-    
+    private String loginName = null;
+    private Date modified = null;
+    private String password = null;
     private String uid = null;
-
-    private String   wikiname  = null;
+    private String wikiname = null;
 
     /**
-     * Private constructor to prevent direct instantiation.
+     * Package constructor to allow direct instantiation only from package related classes (i.e., AbstractUserDatabase).
      */
-    private DefaultUserProfile() {}
-
-    /**
-     * Static factory method that creates a new DefaultUserProfile
-     * and sets a unique identifier (uid) for the supplied UserDatabase.
-     * @param db the UserDatabase for which the uid should be
-     * created
-     * @return the new profile
-     */
-    protected static UserProfile newProfile( UserDatabase db )
-    {
-        UserProfile profile = new DefaultUserProfile();
-        profile.setUid( AbstractUserDatabase.generateUid( db ) );
-        return profile;
-    }
+    DefaultUserProfile() {}
 
     /**
      * {@inheritDoc}
      */
     @Override
-    public boolean equals( Object o )
-    {
-        if ( ( o != null ) && ( o instanceof UserProfile ) )
-        {
-            DefaultUserProfile u = (DefaultUserProfile) o;
-            return  same( fullname, u.fullname ) && same( password, u.password )
-                    && same( loginName, u.loginName ) && same(StringUtils.lowerCase( email ), StringUtils.lowerCase( u.email ) ) && same( wikiname,
-                    u.wikiname );
+    public boolean equals( final Object o ) {
+        if ( o instanceof UserProfile ) {
+            final DefaultUserProfile u = ( DefaultUserProfile )o;
+            return  same( fullname, u.fullname ) &&
+                    same( password, u.password ) &&
+                    same( loginName, u.loginName ) &&
+                    same( StringUtils.lowerCase( email ), StringUtils.lowerCase( u.email ) ) &&
+                    same( wikiname, u.wikiname );
         }
 
         return false;
     }
 
     @Override
-    public int hashCode()
-    {
-        return (fullname  != null ? fullname.hashCode()  : 0) ^
-               (password  != null ? password.hashCode()  : 0) ^
-               (loginName != null ? loginName.hashCode() : 0) ^
-               (wikiname  != null ? wikiname.hashCode()  : 0) ^
-               (email     != null ? StringUtils.lowerCase( email ).hashCode()     : 0);
+    public int hashCode() {
+        return ( fullname  != null ? fullname.hashCode()  : 0 ) ^
+               ( password  != null ? password.hashCode()  : 0 ) ^
+               ( loginName != null ? loginName.hashCode() : 0 ) ^
+               ( wikiname  != null ? wikiname.hashCode()  : 0 ) ^
+               ( email     != null ? StringUtils.lowerCase( email ).hashCode() : 0 );
     }
 
     /**
      * Returns the creation date
+     *
      * @return the creation date
      * @see org.apache.wiki.auth.user.UserProfile#getCreated()
      */
@@ -120,6 +95,7 @@ public final class DefaultUserProfile implements UserProfile
 
     /**
      * Returns the user's e-mail address.
+     *
      * @return the e-mail address
      */
     @Override
@@ -130,6 +106,7 @@ public final class DefaultUserProfile implements UserProfile
 
     /**
      * Returns the user's full name.
+     *
      * @return the full name
      */
     @Override
@@ -140,6 +117,7 @@ public final class DefaultUserProfile implements UserProfile
 
     /**
      * Returns the last-modified date.
+     *
      * @return the last-modified date
      * @see org.apache.wiki.auth.user.UserProfile#getLastModified()
      */
@@ -160,12 +138,10 @@ public final class DefaultUserProfile implements UserProfile
     }
 
     /**
-     * Returns the user password for use with custom authentication. Note that
-     * the password field is not meaningful for container authentication; the
-     * user's private credentials are generally stored elsewhere. While it
-     * depends on the {@link UserDatabase}implementation, in most cases the
-     * value returned by this method will be a password hash, not the password
-     * itself.
+     * Returns the user password for use with custom authentication. Note that the password field is not meaningful for container
+     * authentication; the user's private credentials are generally stored elsewhere. While it depends on the {@link UserDatabase}
+     * implementation, in most cases the value returned by this method will be a password hash, not the password itself.
+     *
      * @return the password
      */
     @Override
@@ -176,6 +152,7 @@ public final class DefaultUserProfile implements UserProfile
 
     /**
      * Returns the user's wiki name.
+     *
      * @return the wiki name.
      */
     @Override
@@ -185,10 +162,9 @@ public final class DefaultUserProfile implements UserProfile
     }
 
     /**
-     * Returns <code>true</code> if the user profile is
-     * new. This implementation checks whether
-     * {@link #getLastModified()} returns <code>null</code>
-     * to determine the status.
+     * Returns <code>true</code> if the user profile is new. This implementation checks whether {@link #getLastModified()} returns
+     * <code>null</code> to determine the status.
+     *
      * @see org.apache.wiki.auth.user.UserProfile#isNew()
      */
     @Override
@@ -202,80 +178,80 @@ public final class DefaultUserProfile implements UserProfile
      * @see org.apache.wiki.auth.user.UserProfile#setCreated(java.util.Date)
      */
     @Override
-    public void setCreated(Date date)
+    public void setCreated( final Date date )
     {
         created = date;
     }
 
     /**
      * Sets the user's e-mail address.
+     *
      * @param email the e-mail address
      */
     @Override
-    public void setEmail( String email )
+    public void setEmail( final String email )
     {
     	this.email = email;
     }
 
     /**
      * Sets the user's full name. For example, "Janne Jalkanen."
+     *
      * @param arg the full name
      */
     @Override
-    public void setFullname( String arg )
-    {
+    public void setFullname( final String arg ) {
         fullname = arg;
 
         // Compute wiki name
-        if ( fullname != null )
-        {
-            wikiname = fullname.replaceAll(WHITESPACE, EMPTY_STRING);
+        if ( fullname != null ) {
+            wikiname = fullname.replaceAll( WHITESPACE, EMPTY_STRING );
         }
     }
 
     /**
      * Sets the last-modified date.
+     *
      * @param date the last-modified date
      * @see org.apache.wiki.auth.user.UserProfile#setLastModified(java.util.Date)
      */
     @Override
-    public void setLastModified( Date date )
+    public void setLastModified( final Date date )
     {
         modified = date;
     }
 
     /**
-     * Sets the name by which the user logs in. The login name is used as the
-     * username for custom authentication (see
+     * Sets the name by which the user logs in. The login name is used as the username for custom authentication (see
      * {@link org.apache.wiki.auth.AuthenticationManager#login(WikiSession,HttpServletRequest, String, String)}).
-     * The login name is typically a short name ("jannej"). In contrast, the
-     * wiki name is typically of type FirstnameLastName ("JanneJalkanen").
+     * The login name is typically a short name ("jannej"). In contrast, the wiki name is typically of type
+     * FirstnameLastName ("JanneJalkanen").
+     *
      * @param name the login name
      */
     @Override
-    public void setLoginName( String name )
+    public void setLoginName( final String name )
     {
         loginName = name;
     }
 
     /**
-     * Sets the user's password for use with custom authentication. It is
-     * <em>not</em> the responsibility of implementing classes to hash the
-     * password; that responsibility is borne by the UserDatabase implementation
-     * during save operations (see {@link UserDatabase#save(UserProfile)}).
-     * Note that the password field is not meaningful for container
-     * authentication; the user's private credentials are generally stored
-     * elsewhere.
+     * Sets the user's password for use with custom authentication. It is <em>not</em> the responsibility of implementing classes to hash
+     * the password; that responsibility is borne by the UserDatabase implementation during save operations (see
+     * {@link UserDatabase#save(UserProfile)}). Note that the password field is not meaningful for container authentication; the user's
+     * private credentials are generally stored elsewhere.
+     *
      * @param arg the password
      */
     @Override
-    public void setPassword( String arg )
+    public void setPassword( final String arg )
     {
         password = arg;
     }
 
     /**
      * Returns a string representation of this user profile.
+     *
      * @return the string
      */
     @Override
@@ -285,20 +261,17 @@ public final class DefaultUserProfile implements UserProfile
     }
 
     /**
-     * Private method that compares two objects and determines whether they are
-     * equal. Two nulls are considered equal.
+     * Private method that compares two objects and determines whether they are equal. Two nulls are considered equal.
+     *
      * @param arg1 the first object
      * @param arg2 the second object
      * @return the result of the comparison
      */
-    private boolean same( Object arg1, Object arg2 )
-    {
-        if ( arg1 == null && arg2 == null )
-        {
+    private boolean same( final Object arg1, final Object arg2 ) {
+        if( arg1 == null && arg2 == null ) {
             return true;
         }
-        if ( arg1 == null || arg2 == null )
-        {
+        if( arg1 == null || arg2 == null ) {
             return false;
         }
         return arg1.equals( arg2 );
@@ -310,7 +283,7 @@ public final class DefaultUserProfile implements UserProfile
      * {@inheritDoc}
      */
     @Override
-    public Map<String,Serializable> getAttributes()
+    public Map< String, Serializable > getAttributes()
     {
         return attributes;
     }
@@ -337,23 +310,21 @@ public final class DefaultUserProfile implements UserProfile
      * {@inheritDoc}
      */
     @Override
-    public boolean isLocked()
-    {
-        boolean locked =  lockExpiry != null && System.currentTimeMillis() < lockExpiry.getTime();
-
+    public boolean isLocked() {
+        final boolean locked = lockExpiry != null && System.currentTimeMillis() < lockExpiry.getTime();
         // Clear the lock if it's expired already
-        if ( !locked && lockExpiry != null )
-        {
+        if( !locked && lockExpiry != null ) {
             lockExpiry = null;
         }
         return locked;
+
     }
 
     /**
      * {@inheritDoc}
      */
     @Override
-    public void setLockExpiry( Date expiry )
+    public void setLockExpiry( final Date expiry )
     {
     	this.lockExpiry = expiry;
     }
@@ -362,8 +333,9 @@ public final class DefaultUserProfile implements UserProfile
      * {@inheritDoc}
      */
     @Override
-    public void setUid( String uid )
+    public void setUid( final String uid )
     {
         this.uid = uid;
     }
+
 }
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/UserDatabase.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/UserDatabase.java
index c8928cc..906c54b 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/UserDatabase.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/UserDatabase.java
@@ -59,6 +59,7 @@ public interface UserDatabase {
      *
      * @param identifier the name of the user to retrieve; this corresponds to value returned by the user profile's {@link UserProfile#getLoginName()} method.
      * @return the array of Principals representing the user's identities
+     * @throws NoSuchPrincipalException If the user database does not contain user with the supplied identifier
      */
     Principal[] getPrincipals( String identifier ) throws NoSuchPrincipalException;