Author: ajaquith
Date: Thu May 22 20:55:42 2008
New Revision: 659401
URL: http://svn.apache.org/viewvc?rev=659401&view=rev
Log:
Miscellaneous and varied Java 5 enhancements (such as for-loops).
Modified:
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/AuthorizationManager.java
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/PrincipalComparator.java
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SecurityVerifier.java
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SessionMonitor.java
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/UserManager.java
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/WikiPrincipal.java
Modified:
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/AuthorizationManager.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/AuthorizationManager.java?rev=659401&r1=659400&r2=659401&view=diff
==============================================================================
---
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/AuthorizationManager.java
(original)
+++
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/AuthorizationManager.java
Thu May 22 20:55:42 2008
@@ -103,7 +103,7 @@
private Authorizer m_authorizer = null;
/** Cache for storing ProtectionDomains used to evaluate the local policy.
*/
- private Map m_cachedPds = new
WeakHashMap();
+ private Map<Principal, ProtectionDomain>
m_cachedPds = new WeakHashMap<Principal, ProtectionDomain>();
private WikiEngine m_engine = null;
@@ -232,13 +232,11 @@
log.debug( "Checking ACL entries..." );
log.debug( "Acl for this page is: " + acl );
- log.debug( "Checking for principal: " + String.valueOf(aclPrincipals)
);
+ log.debug( "Checking for principal: " + String.valueOf( aclPrincipals
) );
log.debug( "Permission: " + permission );
- for( int i = 0; i < aclPrincipals.length; i++ )
+ for( Principal aclPrincipal : aclPrincipals )
{
- Principal aclPrincipal = aclPrincipals[i];
-
// If the ACL principal we're looking at is unresolved,
// try to resolve it here & correct the Acl
if ( aclPrincipal instanceof UnresolvedPrincipal )
@@ -368,9 +366,8 @@
{
String principalName = principal.getName();
Principal[] userPrincipals = session.getPrincipals();
- for( int i = 0; i < userPrincipals.length; i++ )
+ for( Principal userPrincipal : userPrincipals )
{
- Principal userPrincipal = userPrincipals[i];
if( userPrincipal.getName().equals( principalName ) )
{
return true;
@@ -388,6 +385,7 @@
* @param properties the set of properties used to initialize the wiki
engine
* @throws WikiException if the AuthorizationManager cannot be initialized
*/
+ @SuppressWarnings("deprecation")
public final void initialize( WikiEngine engine, Properties properties )
throws WikiException
{
m_engine = engine;
@@ -412,17 +410,17 @@
File policyFile = new File( policyURL.getPath() );
m_localPolicy = new LocalPolicy( policyFile,
engine.getContentEncoding() );
m_localPolicy.refresh();
- log.info("Initialized default security policy: " +
policyFile.getAbsolutePath());
+ log.info( "Initialized default security policy: " +
policyFile.getAbsolutePath() );
}
else
{
- StringBuffer sb = new StringBuffer("JSPWiki was unable to
initialize the ");
- sb.append("default security policy (WEB-INF/jspwiki.policy)
file. ");
- sb.append("Please ensure that the jspwiki.policy file exists
in the default location. ");
- sb.append("This file should exist regardless of the existance
of a global policy file. ");
- sb.append("The global policy file is identified by the
java.security.policy variable. ");
- WikiSecurityException wse = new
WikiSecurityException(sb.toString());
- log.fatal(sb.toString(), wse);
+ StringBuffer sb = new StringBuffer( "JSPWiki was unable to
initialize the " );
+ sb.append( "default security policy (WEB-INF/jspwiki.policy)
file. " );
+ sb.append( "Please ensure that the jspwiki.policy file exists
in the default location. " );
+ sb.append( "This file should exist regardless of the existance
of a global policy file. " );
+ sb.append( "The global policy file is identified by the
java.security.policy variable. " );
+ WikiSecurityException wse = new WikiSecurityException(
sb.toString() );
+ log.fatal( sb.toString(), wse );
throw wse;
}
}
@@ -464,7 +462,7 @@
{
try
{
- Class authClass = ClassUtil.findClass(
"com.ecyrd.jspwiki.auth.authorize", clazz );
+ Class<?> authClass = ClassUtil.findClass(
"com.ecyrd.jspwiki.auth.authorize", clazz );
Object impl = authClass.newInstance();
return impl;
}
@@ -499,16 +497,16 @@
*/
protected boolean allowedByLocalPolicy( Principal[] principals, Permission
permission )
{
- for ( int i = 0; i < principals.length; i++ )
+ for ( Principal principal : principals )
{
// Get ProtectionDomain for this Principal from cache, or create
new one
- ProtectionDomain pd = (ProtectionDomain)m_cachedPds.get(
principals[i] );
+ ProtectionDomain pd = m_cachedPds.get( principal );
if ( pd == null )
{
ClassLoader cl = this.getClass().getClassLoader();
CodeSource cs = new CodeSource( null, (Certificate[])null );
- pd = new ProtectionDomain( cs, null, cl, new Principal[]{
principals[i] } );
- m_cachedPds.put( principals[i], pd );
+ pd = new ProtectionDomain( cs, null, cl, new Principal[]{
principal } );
+ m_cachedPds.put( principal, pd );
}
// Consult the local policy and get the answer
@@ -542,9 +540,9 @@
{
if( !m_useJAAS ) return true;
- Boolean allowed = (Boolean)WikiSession.doPrivileged( session, new
PrivilegedAction()
+ Boolean allowed = (Boolean) WikiSession.doPrivileged( session, new
PrivilegedAction<Boolean>()
{
- public Object run()
+ public Boolean run()
{
try
{
Modified:
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/PrincipalComparator.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/PrincipalComparator.java?rev=659401&r1=659400&r2=659401&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/PrincipalComparator.java
(original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/PrincipalComparator.java
Thu May 22 20:55:42 2008
@@ -31,7 +31,7 @@
* @since 2.3
*/
public class PrincipalComparator
- implements Comparator, Serializable
+ implements Comparator<Principal>, Serializable
{
private static final long serialVersionUID = 1L;
@@ -42,14 +42,10 @@
* @return the result of the comparison
* @see java.util.Comparator#compare(Object, Object)
*/
- public int compare( Object o1, Object o2 )
+ public int compare( Principal o1, Principal o2 )
{
Collator collator = Collator.getInstance();
- if ( o1 instanceof Principal && o2 instanceof Principal )
- {
- return collator.compare( ((Principal)o1).getName(),
((Principal)o2).getName() );
- }
- throw new ClassCastException( "Objects must be of type Principal.");
+ return collator.compare( o1.getName(), o2.getName() );
}
}
Modified:
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SecurityVerifier.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SecurityVerifier.java?rev=659401&r1=659400&r2=659401&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SecurityVerifier.java
(original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SecurityVerifier.java
Thu May 22 20:55:42 2008
@@ -25,7 +25,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.security.*;
-import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -223,47 +222,45 @@
s.append( " <tr>\n" );
for( int i = 0; i < roles.length; i++ )
{
- for( int j = 0; j < pageActions.length; j++ )
+ for( String pageAction : pageActions )
{
- String action = pageActions[j].substring( 0, 1 );
- s.append( " <th title=\"" + pageActions[j] + "\">" + action
+ "</th>\n" );
+ String action = pageAction.substring( 0, 1 );
+ s.append( " <th title=\"" + pageAction + "\">" + action +
"</th>\n" );
}
}
s.append( " </tr>\n" );
// Write page permission tests first
- for( int i = 0; i < pages.length; i++ )
+ for( String page : pages )
{
- String page = pages[i];
s.append( " <tr>\n" );
s.append( " <td>PagePermission \"" + wiki + ":" + page +
"\"</td>\n" );
- for( int j = 0; j < roles.length; j++ )
+ for( Principal role : roles )
{
- for( int k = 0; k < pageActions.length; k++ )
+ for( String pageAction : pageActions )
{
- Permission permission =
PermissionFactory.getPagePermission( wiki + ":" + page, pageActions[k] );
- s.append( printPermissionTest( permission, roles[j], 1 ) );
+ Permission permission =
PermissionFactory.getPagePermission( wiki + ":" + page, pageAction );
+ s.append( printPermissionTest( permission, role, 1 ) );
}
}
s.append( " </tr>\n" );
}
// Now do the group tests
- for( int i = 0; i < groups.length; i++ )
+ for( String group : groups )
{
- String group = groups[i];
s.append( " <tr>\n" );
s.append( " <td>GroupPermission \"" + wiki + ":" + group +
"\"</td>\n" );
- for( int j = 0; j < roles.length; j++ )
+ for( Principal role : roles )
{
- for( int k = 0; k < groupActions.length; k++ )
+ for( String groupAction : groupActions )
{
Permission permission = null;
- if ( groupActions[k] != null)
+ if ( groupAction != null)
{
- permission = new GroupPermission( wiki + ":" + group,
groupActions[k] );
+ permission = new GroupPermission( wiki + ":" + group,
groupAction );
}
- s.append( printPermissionTest( permission, roles[j], 1 ) );
+ s.append( printPermissionTest( permission, role, 1 ) );
}
}
s.append( " </tr>\n" );
@@ -273,14 +270,14 @@
// Now check the wiki-wide permissions
String[] wikiPerms = new String[]
{ "createGroups", "createPages", "login", "editPreferences",
"editProfile" };
- for( int i = 0; i < wikiPerms.length; i++ )
+ for( String wikiPerm : wikiPerms )
{
s.append( " <tr>\n" );
- s.append( " <td>WikiPermission \"" + wiki + "\",\"" +
wikiPerms[i] + "\"</td>\n" );
- for( int j = 0; j < roles.length; j++ )
+ s.append( " <td>WikiPermission \"" + wiki + "\",\"" + wikiPerm
+ "\"</td>\n" );
+ for( Principal role : roles )
{
- Permission permission = new WikiPermission( wiki, wikiPerms[i]
);
- s.append( printPermissionTest( permission, roles[j],
pageActions.length ) );
+ Permission permission = new WikiPermission( wiki, wikiPerm );
+ s.append( printPermissionTest( permission, role,
pageActions.length ) );
}
s.append( " </tr>\n" );
}
@@ -288,10 +285,10 @@
// Lastly, check for AllPermission
s.append( " <tr>\n" );
s.append( " <td>AllPermission \"" + wiki + "\"</td>\n" );
- for( int j = 0; j < roles.length; j++ )
+ for( Principal role : roles )
{
Permission permission = new AllPermission( wiki );
- s.append( printPermissionTest( permission, roles[j],
pageActions.length ) );
+ s.append( printPermissionTest( permission, role,
pageActions.length ) );
}
s.append( " </tr>\n" );
@@ -374,9 +371,9 @@
s.append( " </tr>\n" );
s.append( " <tr>\n" );
s.append( " <th>Anonymous</th>\n" );
- for( int i = 0; i < roles.length; i++ )
+ for( Principal role : roles )
{
- s.append( " <th>" + roles[i].getName() + "</th>\n" );
+ s.append( " <th>" + role.getName() + "</th>\n" );
}
s.append( "</tr>\n" );
s.append( "</thead>\n" );
@@ -402,10 +399,9 @@
s.append( "\"" );
s.append( allowsAnonymous ? BG_GREEN + ">" : BG_RED + ">" );
s.append( " </td>\n" );
- for( int j = 0; j < roles.length; j++ )
+ for( Principal role : roles )
{
- Role role = (Role) roles[j];
- boolean allowed = allowsAnonymous || wca.isConstrained(
jsp, role );
+ boolean allowed = allowsAnonymous || wca.isConstrained(
jsp, (Role)role );
s.append( " <td title=\"" );
s.append( allowed ? "ALLOW: " : "DENY: " );
s.append( jsp );
@@ -470,9 +466,8 @@
Authorizer authorizer =
m_engine.getAuthorizationManager().getAuthorizer();
Principal[] containerRoles = authorizer.getRoles();
boolean missing = false;
- for( int i = 0; i < m_policyPrincipals.length; i++ )
+ for( Principal principal : m_policyPrincipals )
{
- Principal principal = m_policyPrincipals[i];
if ( principal instanceof Role )
{
Role role = (Role) principal;
@@ -713,12 +708,11 @@
// Verify the file
policy.read();
- List errors = policy.getMessages();
+ List<Exception> errors = (List<Exception>)policy.getMessages();
if ( errors.size() > 0 )
{
- for( Iterator it = errors.iterator(); it.hasNext(); )
+ for( Exception e : errors )
{
- Exception e = (Exception) it.next();
m_session.addMessage( ERROR_POLICY, e.getMessage() );
}
}
@@ -730,21 +724,20 @@
// Stash the unique principals mentioned in the file,
// plus our standard roles.
- Set principals = new LinkedHashSet();
+ Set<Principal> principals = new LinkedHashSet<Principal>();
principals.add( Role.ALL );
principals.add( Role.ANONYMOUS );
principals.add( Role.ASSERTED );
principals.add( Role.AUTHENTICATED );
ProtectionDomain[] domains = policy.getProtectionDomains();
- for ( int i = 0; i < domains.length; i++ )
+ for ( ProtectionDomain domain : domains )
{
- Principal[] domainPrincipals = domains[i].getPrincipals();
- for( int j = 0; j < domainPrincipals.length; j++ )
+ for( Principal principal : domain.getPrincipals() )
{
- principals.add( domainPrincipals[j] );
+ principals.add( principal );
}
}
- m_policyPrincipals = (Principal[]) principals.toArray( new
Principal[principals.size()] );
+ m_policyPrincipals = principals.toArray( new
Principal[principals.size()] );
}
catch( IOException e )
{
@@ -765,7 +758,7 @@
Subject subject = new Subject();
subject.getPrincipals().add( principal );
boolean allowedByGlobalPolicy = ((Boolean)
- Subject.doAsPrivileged( subject, new PrivilegedAction()
+ Subject.doAsPrivileged( subject, new PrivilegedAction<Object>()
{
public Object run()
{
@@ -837,11 +830,11 @@
String loginName = "TestUser" + String.valueOf(
System.currentTimeMillis() );
try
{
- UserProfile profile = new DefaultUserProfile();
- profile.setEmail("[EMAIL PROTECTED]");
+ UserProfile profile = db.newProfile();
+ profile.setEmail( "[EMAIL PROTECTED]" );
profile.setLoginName( loginName );
profile.setFullname( "FullName"+loginName );
- profile.setPassword("password");
+ profile.setPassword( "password" );
db.save(profile);
// Make sure the profile saved successfully
Modified: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SessionMonitor.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SessionMonitor.java?rev=659401&r1=659400&r2=659401&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SessionMonitor.java
(original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/SessionMonitor.java Thu
May 22 20:55:42 2008
@@ -49,10 +49,10 @@
private static Logger log = Logger.getLogger( SessionMonitor.class );
/** Map with WikiEngines as keys, and SessionMonitors as values. */
- private static Map c_monitors = new HashMap();
+ private static Map<WikiEngine, SessionMonitor> c_monitors = new
HashMap<WikiEngine, SessionMonitor>();
/** Weak hashmap with HttpSessions as keys, and WikiSessions as values. */
- private final Map m_sessions = new WeakHashMap();
+ private final Map<String, WikiSession> m_sessions = new
WeakHashMap<String, WikiSession>();
private WikiEngine m_engine;
@@ -74,7 +74,7 @@
synchronized( c_monitors )
{
- monitor = (SessionMonitor) c_monitors.get(engine);
+ monitor = c_monitors.get(engine);
if( monitor == null )
{
monitor = new SessionMonitor(engine);
@@ -109,7 +109,7 @@
{
WikiSession wikiSession = null;
String sid = ( session == null ) ? "(null)" : session.getId();
- WikiSession storedSession = (WikiSession)m_sessions.get( sid );
+ WikiSession storedSession = m_sessions.get( sid );
// If the weak reference returns a wiki session, return it
if( storedSession != null )
@@ -198,14 +198,12 @@
*/
public final Principal[] userPrincipals()
{
- Collection principals = new ArrayList();
- for ( Iterator it = m_sessions.values().iterator(); it.hasNext(); )
+ Collection<Principal> principals = new ArrayList<Principal>();
+ for ( WikiSession session : m_sessions.values() )
{
- WikiSession session = (WikiSession)it.next();
-
principals.add( session.getUserPrincipal() );
}
- Principal[] p = (Principal[])principals.toArray( new
Principal[principals.size()] );
+ Principal[] p = principals.toArray( new Principal[principals.size()] );
Arrays.sort( p, m_comparator );
return p;
}
@@ -265,10 +263,10 @@
public void sessionDestroyed( HttpSessionEvent se )
{
HttpSession session = se.getSession();
- Iterator it = c_monitors.values().iterator();
+ Iterator<SessionMonitor> it = c_monitors.values().iterator();
while( it.hasNext() )
{
- SessionMonitor monitor = (SessionMonitor)it.next();
+ SessionMonitor monitor = it.next();
WikiSession storedSession = monitor.findSession(session);
Modified: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/UserManager.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/UserManager.java?rev=659401&r1=659400&r2=659401&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/UserManager.java
(original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/UserManager.java Thu May
22 20:55:42 2008
@@ -103,6 +103,7 @@
* @param engine the current wiki engine
* @param props the wiki engine initialization properties
*/
+ @SuppressWarnings("deprecation")
public final void initialize( WikiEngine engine, Properties props )
{
m_engine = engine;
@@ -146,7 +147,7 @@
PROP_DATABASE );
log.info("Attempting to load user database class "+dbClassName);
- Class dbClass = ClassUtil.findClass( USERDATABASE_PACKAGE,
dbClassName );
+ Class<?> dbClass = ClassUtil.findClass( USERDATABASE_PACKAGE,
dbClassName );
m_database = (UserDatabase) dbClass.newInstance();
m_database.initialize( m_engine, m_engine.getWikiProperties() );
log.info("UserDatabase initialized.");
@@ -470,6 +471,7 @@
* @param context the current wiki context
* @param profile the supplied UserProfile
*/
+ @SuppressWarnings("unchecked")
public final void validateProfile( WikiContext context, UserProfile
profile )
{
boolean isNew = profile.isNew();
@@ -481,7 +483,7 @@
// Query the SpamFilter first
//
- List<PageFilter> ls =
(List<PageFilter>)m_engine.getFilterManager().getFilterList();
+ List<PageFilter> ls = m_engine.getFilterManager().getFilterList();
for( PageFilter pf : ls )
{
if( pf instanceof SpamFilter )
@@ -630,6 +632,16 @@
/**
* No-op; always throws <code>NoSuchPrincipalException</code>.
+ * @param uid the unique identifier to search for
+ * @return the user profile
+ * @throws NoSuchPrincipalException never...
+ */
+ public UserProfile findByUid( long uid ) throws
NoSuchPrincipalException
+ {
+ throw new NoSuchPrincipalException("No user profiles available");
+ }
+ /**
+ * No-op; always throws <code>NoSuchPrincipalException</code>.
* @param index the name to search for
* @return the user profile
* @throws NoSuchPrincipalException never...
@@ -660,15 +672,6 @@
}
/**
- * No-op.
- * @return <code>false</code>
- */
- public boolean isSharedWithContainer()
- {
- return false;
- }
-
- /**
* No-op; always throws <code>NoSuchPrincipalException</code>.
* @param loginName the login name
* @param newName the proposed new login name
Modified: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/WikiPrincipal.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/WikiPrincipal.java?rev=659401&r1=659400&r2=659401&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/WikiPrincipal.java
(original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/auth/WikiPrincipal.java Thu
May 22 20:55:42 2008
@@ -37,7 +37,7 @@
* @author Andrew Jaquith
* @since 2.2
*/
-public final class WikiPrincipal implements Principal, Comparable, Serializable
+public final class WikiPrincipal implements Principal, Comparable<Principal>,
Serializable
{
private static final long serialVersionUID = 1L;
@@ -61,7 +61,7 @@
public static final String UNSPECIFIED = "unspecified";
/** Static instance of Comparator that allows Principals to be sorted. */
- public static final Comparator COMPARATOR = new PrincipalComparator();
+ public static final Comparator<Principal> COMPARATOR = new
PrincipalComparator();
private static final String[] VALID_TYPES;
@@ -172,14 +172,9 @@
* @return [EMAIL PROTECTED]
* @since 2.7.0
*/
- public int compareTo(Object o)
+ public int compareTo(Principal o)
{
- if( o instanceof Principal )
- {
- return getName().compareTo( ((Principal)o).getName() );
- }
-
- throw new ClassCastException( "WikiPrincipal can only be compared to
other Principals" );
+ return getName().compareTo( o.getName() );
}