You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by vt...@apache.org on 2005/03/15 06:21:19 UTC
svn commit: r157518 - in directory/authx/trunk:
core/src/java/org/apache/authx/authorization/
core/src/java/org/apache/authx/authorization/condition/ example/
example/src/java/org/apache/authx/example/web/ example/src/webapp/
example/src/webapp/WEB-INF/ script/ script/src/java/org/apache/authx/script/
script/src/java/org/apache/authx/script/common/
script/src/java/org/apache/authx/script/groovy/
script/src/java/org/apache/authx/script/xml/builder/
script/src/test/org/apache/authx/script/common/
script/src/test/org/apache/authx/script/groovy/
Author: vtence
Date: Mon Mar 14 21:21:11 2005
New Revision: 157518
URL: http://svn.apache.org/viewcvs?view=rev&rev=157518
Log:
Added support for authorization rules written in groovy. Demonstrated in example application. Grrovy rules can also support dependency injection (PicoContainer implemented).
Added:
directory/authx/trunk/core/src/java/org/apache/authx/authorization/condition/Conditions.java (with props)
directory/authx/trunk/example/src/java/org/apache/authx/example/web/Treasure.java (with props)
directory/authx/trunk/example/src/webapp/WEB-INF/groups.xml (with props)
directory/authx/trunk/example/src/webapp/WEB-INF/treasure.groovy (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/BeanFactory.java (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/Factory.java (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/common/
directory/authx/trunk/script/src/java/org/apache/authx/script/common/DynamicRuleProxy.java (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/common/PicoContainerFactory.java (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleLoader.java (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleSetBuilder.java (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/common/SmartCachingScriptClassLoader.java (with props)
directory/authx/trunk/script/src/java/org/apache/authx/script/groovy/GroovyRuleLoader.java (with props)
directory/authx/trunk/script/src/test/org/apache/authx/script/common/
directory/authx/trunk/script/src/test/org/apache/authx/script/common/DynamicRuleProxyTest.java (with props)
directory/authx/trunk/script/src/test/org/apache/authx/script/common/PicoContainerFactoryTest.java (with props)
directory/authx/trunk/script/src/test/org/apache/authx/script/common/ScriptedRuleSetBuilderTest.java (with props)
directory/authx/trunk/script/src/test/org/apache/authx/script/common/SmartCachingScriptClassLoaderTest.java (with props)
directory/authx/trunk/script/src/test/org/apache/authx/script/common/canadians.groovy (with props)
directory/authx/trunk/script/src/test/org/apache/authx/script/groovy/GroovyRuleLoaderTest.java (with props)
Removed:
directory/authx/trunk/core/src/java/org/apache/authx/authorization/condition/Predicates.java
directory/authx/trunk/script/src/java/org/apache/authx/script/groovy/SmartCachingScriptClassLoader.java
directory/authx/trunk/script/src/test/org/apache/authx/script/groovy/SmartCachingScriptClassLoaderTest.java
Modified:
directory/authx/trunk/core/src/java/org/apache/authx/authorization/DefaultRule.java
directory/authx/trunk/example/project.xml
directory/authx/trunk/example/src/java/org/apache/authx/example/web/CanPurchaseConditionBuilder.java
directory/authx/trunk/example/src/webapp/WEB-INF/nanocontainer.groovy
directory/authx/trunk/example/src/webapp/WEB-INF/users.xml
directory/authx/trunk/example/src/webapp/shop.groovy
directory/authx/trunk/example/src/webapp/try_again.vm
directory/authx/trunk/script/project.xml
directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/AndConditionBuilder.java
directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/FalseConditionBuilder.java
directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/OrConditionBuilder.java
directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/TrueConditionBuilder.java
Modified: directory/authx/trunk/core/src/java/org/apache/authx/authorization/DefaultRule.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/core/src/java/org/apache/authx/authorization/DefaultRule.java?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/core/src/java/org/apache/authx/authorization/DefaultRule.java (original)
+++ directory/authx/trunk/core/src/java/org/apache/authx/authorization/DefaultRule.java Mon Mar 14 21:21:11 2005
@@ -18,7 +18,7 @@
import org.apache.authx.authorization.effect.Effects;
import org.apache.authx.authorization.condition.OrCondition;
-import org.apache.authx.authorization.condition.Predicates;
+import org.apache.authx.authorization.condition.Conditions;
/**
* @author <a href="mailto:vtence@apache.org">Vincent Tence</a>
@@ -37,8 +37,8 @@
public DefaultRule( Effect effect )
{
m_effect = effect;
- m_subjectCondition = Predicates.FALSE;
- m_permissionCondition = Predicates.FALSE;
+ m_subjectCondition = Conditions.FALSE;
+ m_permissionCondition = Conditions.FALSE;
}
public void setEffect( Effect effect )
Added: directory/authx/trunk/core/src/java/org/apache/authx/authorization/condition/Conditions.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/core/src/java/org/apache/authx/authorization/condition/Conditions.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/core/src/java/org/apache/authx/authorization/condition/Conditions.java (added)
+++ directory/authx/trunk/core/src/java/org/apache/authx/authorization/condition/Conditions.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.authorization.condition;
+
+import org.apache.authx.authorization.Permission;
+import org.apache.authx.authorization.Condition;
+
+import java.security.Principal;
+
+public final class Conditions
+{
+ public static final Condition TRUE = new TrueCondition();
+ public static final Condition FALSE = new FalseCondition();
+
+ private Conditions()
+ {
+ }
+
+ public static Condition impliedBy( Permission p )
+ {
+ return new ImpliedPermissionCondition( p );
+ }
+
+ public static Condition dependedUponBy( Permission p )
+ {
+ return new DependedUponPermissionCondition( p );
+ }
+
+ public static Condition is( Object o )
+ {
+ return new EqualCondition( o );
+ }
+
+ public static Condition hasPrincipal( Principal p )
+ {
+ return new HasPrincipalCondition( p );
+ }
+
+ public static Condition and( Condition p1, Condition p2 )
+ {
+ return new AndCondition( p1, p2 );
+ }
+}
Propchange: directory/authx/trunk/core/src/java/org/apache/authx/authorization/condition/Conditions.java
------------------------------------------------------------------------------
svn:executable = *
Modified: directory/authx/trunk/example/project.xml
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/project.xml?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/example/project.xml (original)
+++ directory/authx/trunk/example/project.xml Mon Mar 14 21:21:11 2005
@@ -89,7 +89,7 @@
<dependency>
<groupId>groovy</groupId>
<artifactId>groovy</artifactId>
- <version>1.0-beta-8</version>
+ <version>1.0-beta-9</version>
<properties>
<war.bundle>true</war.bundle>
</properties>
Modified: directory/authx/trunk/example/src/java/org/apache/authx/example/web/CanPurchaseConditionBuilder.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/java/org/apache/authx/example/web/CanPurchaseConditionBuilder.java?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/example/src/java/org/apache/authx/example/web/CanPurchaseConditionBuilder.java (original)
+++ directory/authx/trunk/example/src/java/org/apache/authx/example/web/CanPurchaseConditionBuilder.java Mon Mar 14 21:21:11 2005
@@ -16,8 +16,8 @@
*/
package org.apache.authx.example.web;
+import org.apache.authx.authorization.condition.Conditions;
import org.apache.authx.script.xml.builder.AbstractElementBuilder;
-import org.apache.authx.authorization.condition.ImpliedPermissionCondition;
import org.dom4j.Element;
public class CanPurchaseConditionBuilder extends AbstractElementBuilder
@@ -30,6 +30,6 @@
public Object buildFrom( Element e )
{
float limit = Float.parseFloat( e.attributeValue( "limit" ) );
- return new ImpliedPermissionCondition( new PurchasePermission( limit ) );
+ return Conditions.impliedBy( new PurchasePermission( limit ) );
}
}
Added: directory/authx/trunk/example/src/java/org/apache/authx/example/web/Treasure.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/java/org/apache/authx/example/web/Treasure.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/example/src/java/org/apache/authx/example/web/Treasure.java (added)
+++ directory/authx/trunk/example/src/java/org/apache/authx/example/web/Treasure.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.example.web;
+
+public class Treasure
+{
+ private int amount;
+
+ public Treasure( int amount )
+ {
+ this.amount = amount;
+ }
+
+ public int getAmount()
+ {
+ return amount;
+ }
+}
Propchange: directory/authx/trunk/example/src/java/org/apache/authx/example/web/Treasure.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/example/src/webapp/WEB-INF/groups.xml
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/webapp/WEB-INF/groups.xml?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/example/src/webapp/WEB-INF/groups.xml (added)
+++ directory/authx/trunk/example/src/webapp/WEB-INF/groups.xml Mon Mar 14 21:21:11 2005
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<groups>
+ <user username="bill" group="rich"/>
+</groups>
\ No newline at end of file
Propchange: directory/authx/trunk/example/src/webapp/WEB-INF/groups.xml
------------------------------------------------------------------------------
svn:executable = *
Modified: directory/authx/trunk/example/src/webapp/WEB-INF/nanocontainer.groovy
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/webapp/WEB-INF/nanocontainer.groovy?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/example/src/webapp/WEB-INF/nanocontainer.groovy (original)
+++ directory/authx/trunk/example/src/webapp/WEB-INF/nanocontainer.groovy Mon Mar 14 21:21:11 2005
@@ -2,6 +2,7 @@
if ( assemblyScope instanceof javax.servlet.ServletContext )
{
+ // Let's define users of the system from an XML file containing usernames/passwords
realm = new org.apache.authx.authentication.realm.MemoryRealm()
// A nasty groovy bug prevents us from using servlet context to get the file real path
// See http://jira.codehaus.org/browse/GROOVY-210
@@ -9,22 +10,45 @@
realmBuilder = org.apache.authx.script.xml.Dom4JRealmBuilder.fromReader( users )
realmBuilder.buildRealm( realm )
+ // Our authenticator will use the realm we've just defined
authenticator = new org.apache.authx.authentication.DefaultAuthenticator( realm )
+ // Users in our system can have groups and roles
+ // Let's build the respective providers from their XML definitions
roleProvider = new org.apache.authx.authentication.attribute.AttributeProvider()
roles = new java.io.FileReader( "c:/tools/jakarta-tomcat-5.0.28/webapps/authx-example/WEB-INF/roles.xml" )
roleBuilder = org.apache.authx.script.xml.Dom4JRoleBuilder.fromReader( roles )
roleBuilder.buildProvider( roleProvider )
+ groupProvider = new org.apache.authx.authentication.attribute.AttributeProvider()
+ groups = new java.io.FileReader( "c:/tools/jakarta-tomcat-5.0.28/webapps/authx-example/WEB-INF/groups.xml" )
+ groupBuilder = org.apache.authx.script.xml.Dom4JGroupBuilder.fromReader( groups )
+ groupBuilder.buildProvider( groupProvider )
+ // We can now add roles and groups providers to our authenticator
authenticator.addInformationProvider( roleProvider )
+ authenticator.addInformationProvider( groupProvider )
pico.registerComponentInstance( org.apache.authx.authentication.Authenticator, authenticator )
+ // Next we have to define the system policy
+ // Let's first read some rules from an XML file - notice the custom condition builder used
policy = new org.apache.authx.authorization.Policy( new org.apache.authx.authorization.effect.PermitOverridesEffect() )
permissions = new java.io.FileReader( "c:/tools/jakarta-tomcat-5.0.28/webapps/authx-example/WEB-INF/permissions.xml" )
policyBuilder = org.apache.authx.script.xml.Dom4JRuleSetBuilder.fromReader( permissions )
policyBuilder.registerBuilder( new org.apache.authx.example.web.CanPurchaseConditionBuilder() )
policyBuilder.buildRuleSet( policy )
+ // Rules can also be defined in groovy
+ specials = new java.io.File( "c:/tools/jakarta-tomcat-5.0.28/webapps/authx-example/WEB-INF/treasure.groovy" )
+ // Rule classes can have dependencies that will be pulled out from the pico container
+ pico.registerComponentInstance( new org.apache.authx.example.web.Treasure( 500 ) )
+
+ picoFactory = new org.apache.authx.script.common.PicoContainerFactory( pico )
+ groovyLoader = new org.apache.authx.script.groovy.GroovyRuleLoader( picoFactory )
+ scriptedBuilder = new org.apache.authx.script.common.ScriptedRuleSetBuilder( groovyLoader )
+ scriptedBuilder.addScript( specials.toURL() )
+ scriptedBuilder.buildRuleSet( policy )
+
+ // Now that the policy is ready, let's define and register the authorizer
authorizer = new org.apache.authx.authorization.DefaultAuthorizer( policy );
authorizer.denyOn( org.apache.authx.authorization.effect.Effects.NOT_APPLICABLE );
pico.registerComponentInstance( org.apache.authx.authorization.Authorizer, authorizer )
Added: directory/authx/trunk/example/src/webapp/WEB-INF/treasure.groovy
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/webapp/WEB-INF/treasure.groovy?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/example/src/webapp/WEB-INF/treasure.groovy (added)
+++ directory/authx/trunk/example/src/webapp/WEB-INF/treasure.groovy Mon Mar 14 21:21:11 2005
@@ -0,0 +1,27 @@
+class TreasureLimit {
+
+ cash
+
+ TreasureLimit( org.apache.authx.example.web.Treasure treasure )
+ {
+ cash = treasure
+ }
+
+ evaluate( request )
+ {
+ if ( !rich( request.getSubject() ) ) return
+ if ( hasEnoughCash( request.getPermission() ) ) request.grant()
+ }
+
+ rich( subject )
+ {
+ rich = new org.apache.authx.authentication.attribute.GroupPrincipal( "rich" )
+ return subject.getPrincipals().contains( rich )
+ }
+
+ hasEnoughCash( purchase )
+ {
+ limit = new org.apache.authx.example.web.PurchasePermission( cash.getAmount() )
+ return limit.implies( purchase )
+ }
+}
\ No newline at end of file
Propchange: directory/authx/trunk/example/src/webapp/WEB-INF/treasure.groovy
------------------------------------------------------------------------------
svn:executable = *
Modified: directory/authx/trunk/example/src/webapp/WEB-INF/users.xml
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/webapp/WEB-INF/users.xml?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/example/src/webapp/WEB-INF/users.xml (original)
+++ directory/authx/trunk/example/src/webapp/WEB-INF/users.xml Mon Mar 14 21:21:11 2005
@@ -3,4 +3,5 @@
<user username="anonymous" password=""/>
<user username="vincent" password="foo"/>
<user username="alex" password="bar"/>
+ <user username="bill" password="bill"/>
</realm>
Modified: directory/authx/trunk/example/src/webapp/shop.groovy
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/webapp/shop.groovy?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/example/src/webapp/shop.groovy (original)
+++ directory/authx/trunk/example/src/webapp/shop.groovy Mon Mar 14 21:21:11 2005
@@ -16,6 +16,7 @@
this.authorizer = authorizer
this.context = context
catalog = new java.util.HashMap()
+ catalog.put( "monitor", new Item("17 inches LCD Monitor", 350 ) )
catalog.put( "book", new Item("The Pickaxe book, Second Edition", 50 ) )
catalog.put( "mug", new Item( "Purple Java Mug", 10 ) )
}
Modified: directory/authx/trunk/example/src/webapp/try_again.vm
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/example/src/webapp/try_again.vm?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/example/src/webapp/try_again.vm (original)
+++ directory/authx/trunk/example/src/webapp/try_again.vm Mon Mar 14 21:21:11 2005
@@ -3,6 +3,6 @@
<title>Apache Authentication and Authorization Framework Demo Application</title>
</head>
<body>
- Are you sure you're $action.username? <a href="authenticate.groovy">Try again</a>
+ Are you sure you're $action.username? <a href="login.groovy">Try again</a>
</body>
</html>
Modified: directory/authx/trunk/script/project.xml
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/project.xml?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/script/project.xml (original)
+++ directory/authx/trunk/script/project.xml Mon Mar 14 21:21:11 2005
@@ -42,7 +42,7 @@
<dependency>
<groupId>groovy</groupId>
<artifactId>groovy</artifactId>
- <version>1.0-beta-8</version>
+ <version>1.0-beta-9</version>
</dependency>
<dependency>
<groupId>asm</groupId>
@@ -57,6 +57,11 @@
<gump.project>asm</gump.project>
<gump.id>asm-util</gump.id>
</properties>
+ </dependency>
+ <dependency>
+ <groupId>picocontainer</groupId>
+ <artifactId>picocontainer</artifactId>
+ <version>1.1</version>
</dependency>
</dependencies>
</project>
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/BeanFactory.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/BeanFactory.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/BeanFactory.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/BeanFactory.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script;
+
+public class BeanFactory implements Factory
+{
+ public Object newInstance( Class clazz ) throws Exception
+ {
+ return clazz.newInstance();
+ }
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/BeanFactory.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/Factory.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/Factory.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/Factory.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/Factory.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script;
+
+public interface Factory
+{
+ Object newInstance( Class clazz ) throws Exception;
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/Factory.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/common/DynamicRuleProxy.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/common/DynamicRuleProxy.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/common/DynamicRuleProxy.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/common/DynamicRuleProxy.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import org.apache.authx.authorization.AuthorizationRequest;
+import org.apache.authx.authorization.Rule;
+import org.apache.authx.script.ScriptingException;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class DynamicRuleProxy implements Rule
+{
+ private final Object m_delegate;
+
+ public DynamicRuleProxy( Object delegate )
+ {
+ m_delegate = delegate;
+ }
+
+ public void evaluate( AuthorizationRequest request )
+ {
+ Method method = lookupEvaluateMethod();
+ invoke( method, request );
+ }
+
+ private void invoke( Method m, AuthorizationRequest request )
+ {
+ try
+ {
+ m.invoke( m_delegate, new Object[] { request } );
+ }
+ catch ( InvocationTargetException e )
+ {
+ throw new ScriptingException( "Rule execution failure", e.getTargetException() );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new ScriptingException( e );
+ }
+ }
+
+ private Method lookupEvaluateMethod()
+ {
+ Method evaluate = null;
+ try
+ {
+ evaluate = m_delegate.getClass().getMethod( "evaluate", new Class[] { Object.class } );
+ }
+ catch ( NoSuchMethodException e )
+ {
+ throw new ScriptingException( "Rule has no suitable evaluate method", e );
+ }
+ return evaluate;
+ }
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/common/DynamicRuleProxy.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/common/PicoContainerFactory.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/common/PicoContainerFactory.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/common/PicoContainerFactory.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/common/PicoContainerFactory.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import org.apache.authx.script.Factory;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+
+public class PicoContainerFactory implements Factory
+{
+ private final PicoContainer m_parent;
+
+ public PicoContainerFactory( PicoContainer container )
+ {
+ m_parent = container;
+ }
+
+ public Object newInstance( Class clazz ) throws Exception
+ {
+ MutablePicoContainer container = new DefaultPicoContainer( m_parent );
+ container.registerComponentImplementation( clazz );
+ Object instance = container.getComponentInstance( clazz );
+ container.dispose();
+
+ return instance;
+ }
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/common/PicoContainerFactory.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleLoader.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleLoader.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleLoader.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleLoader.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import org.apache.authx.authorization.Rule;
+import org.apache.authx.script.ScriptingException;
+
+import java.net.URL;
+import java.io.IOException;
+
+public interface ScriptedRuleLoader
+{
+ Rule load( URL scriptURL ) throws IOException, ScriptingException;
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleLoader.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleSetBuilder.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleSetBuilder.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleSetBuilder.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleSetBuilder.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import org.apache.authx.authorization.Rule;
+import org.apache.authx.authorization.RuleSet;
+import org.apache.authx.script.RuleSetBuilder;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+public class ScriptedRuleSetBuilder implements RuleSetBuilder
+{
+ private final ScriptedRuleLoader m_loader;
+ private final Collection m_rules;
+
+ public ScriptedRuleSetBuilder( ScriptedRuleLoader loader )
+ {
+ m_loader = loader;
+ m_rules = new ArrayList();
+ }
+
+ public void buildRuleSet( RuleSet ruleSet )
+ {
+ for ( Iterator it = m_rules.iterator(); it.hasNext(); )
+ {
+ Rule rule = (Rule) it.next();
+ ruleSet.addRule( rule );
+ }
+ }
+
+ public void addScript( URL scriptURL ) throws IOException
+ {
+ m_rules.add( m_loader.load( scriptURL ) );
+ }
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/common/ScriptedRuleSetBuilder.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/common/SmartCachingScriptClassLoader.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/common/SmartCachingScriptClassLoader.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/common/SmartCachingScriptClassLoader.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/common/SmartCachingScriptClassLoader.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import org.apache.authx.script.ScriptClassLoader;
+import org.apache.authx.script.ScriptingException;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.HashMap;
+import java.util.Map;
+
+public class SmartCachingScriptClassLoader implements ScriptClassLoader
+{
+ private final ScriptClassLoader m_delegate;
+ private final Map m_classCache;
+
+ public SmartCachingScriptClassLoader( ScriptClassLoader delegate )
+ {
+ this.m_delegate = delegate;
+ m_classCache = new HashMap();
+ }
+
+ public Class loadClass( URL scriptUrl ) throws IOException, ScriptingException
+ {
+ return cacheHit( scriptUrl )
+ ? loadScriptFromCache( scriptUrl )
+ : loadAndCacheScript( scriptUrl );
+ }
+
+ private boolean cacheHit( URL scriptURL ) throws IOException
+ {
+ if ( !foundCacheEntry( scriptURL ) )
+ {
+ return false;
+ }
+
+ CacheEntry entry = getCacheEntry( scriptURL );
+ return !entry.isOutOfDate( lastModificationTime( scriptURL ) );
+ }
+
+ private boolean foundCacheEntry( URL scriptURL )
+ {
+ return m_classCache.containsKey( key( scriptURL ) );
+ }
+
+ private CacheEntry getCacheEntry( URL scriptURL )
+ {
+ return ( CacheEntry ) m_classCache.get( key( scriptURL ) );
+ }
+
+ private Object key( URL scriptURL )
+ {
+ return scriptURL.toExternalForm();
+ }
+
+ private long lastModificationTime( URL scriptURL ) throws IOException
+ {
+ URLConnection urlConnection = scriptURL.openConnection();
+ return urlConnection.getLastModified();
+ }
+
+ private Class loadAndCacheScript( URL scriptURL ) throws IOException,
+ ScriptingException
+ {
+ Class clazz = m_delegate.loadClass( scriptURL );
+ CacheEntry entry = new CacheEntry( clazz,
+ lastModificationTime( scriptURL ) );
+ m_classCache.put( key( scriptURL ), entry );
+ return clazz;
+ }
+
+ private Class loadScriptFromCache( URL scriptURL )
+ {
+ CacheEntry entry = getCacheEntry( scriptURL );
+ return entry.getScriptClass();
+ }
+
+ private static class CacheEntry
+ {
+ private final Class scriptClass;
+ private final long loadTime;
+
+ public CacheEntry( Class scriptClass, long loadTime )
+ {
+ this.scriptClass = scriptClass;
+ this.loadTime = loadTime;
+ }
+
+ public Class getScriptClass()
+ {
+ return scriptClass;
+ }
+
+ public boolean isOutOfDate( long lastModified )
+ {
+ return loadTime < lastModified;
+ }
+ }
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/common/SmartCachingScriptClassLoader.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/java/org/apache/authx/script/groovy/GroovyRuleLoader.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/groovy/GroovyRuleLoader.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/groovy/GroovyRuleLoader.java (added)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/groovy/GroovyRuleLoader.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.groovy;
+
+import org.apache.authx.authorization.Rule;
+import org.apache.authx.script.Factory;
+import org.apache.authx.script.ScriptClassLoader;
+import org.apache.authx.script.ScriptingException;
+import org.apache.authx.script.BeanFactory;
+import org.apache.authx.script.common.DynamicRuleProxy;
+import org.apache.authx.script.common.ScriptedRuleLoader;
+import org.apache.authx.script.common.SmartCachingScriptClassLoader;
+
+import java.io.IOException;
+import java.net.URL;
+
+public class GroovyRuleLoader implements ScriptedRuleLoader
+{
+ private final ScriptClassLoader m_scriptClassLoader;
+ private final Factory m_factory;
+
+ public GroovyRuleLoader()
+ {
+ this( new BeanFactory() );
+ }
+
+ public GroovyRuleLoader( ScriptClassLoader scriptClassLoader )
+ {
+ this( scriptClassLoader, new BeanFactory() );
+ }
+
+ public GroovyRuleLoader( Factory factory )
+ {
+ this( new SmartCachingScriptClassLoader( new GroovyScriptClassLoader() ), factory );
+ }
+
+ public GroovyRuleLoader( ScriptClassLoader scriptClassLoader, Factory factory )
+ {
+ m_scriptClassLoader = scriptClassLoader;
+ m_factory = factory;
+ }
+
+ public Rule load( URL scriptURL ) throws IOException, ScriptingException
+ {
+ Class clazz = m_scriptClassLoader.loadClass( scriptURL );
+ Rule rule = new DynamicRuleProxy( instanciate( clazz ) );
+
+ return rule;
+ }
+
+ private Object instanciate( Class clazz )
+ {
+ Object instance;
+ try
+ {
+ instance = m_factory.newInstance( clazz );
+ }
+ catch ( Exception e )
+ {
+ throw new ScriptingException( "Failed to instanciate rule from groovy script", e );
+ }
+ return instance;
+ }
+
+}
Propchange: directory/authx/trunk/script/src/java/org/apache/authx/script/groovy/GroovyRuleLoader.java
------------------------------------------------------------------------------
svn:executable = *
Modified: directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/AndConditionBuilder.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/AndConditionBuilder.java?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/AndConditionBuilder.java (original)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/AndConditionBuilder.java Mon Mar 14 21:21:11 2005
@@ -18,7 +18,7 @@
import org.apache.authx.authorization.Condition;
import org.apache.authx.authorization.condition.AndCondition;
-import org.apache.authx.authorization.condition.Predicates;
+import org.apache.authx.authorization.condition.Conditions;
public class AndConditionBuilder
extends LogicalConditionBuilder
@@ -35,7 +35,7 @@
protected Condition getSeed()
{
- return Predicates.TRUE;
+ return Conditions.TRUE;
}
public Condition compute( Condition left, Condition right )
Modified: directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/FalseConditionBuilder.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/FalseConditionBuilder.java?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/FalseConditionBuilder.java (original)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/FalseConditionBuilder.java Mon Mar 14 21:21:11 2005
@@ -16,7 +16,7 @@
*/
package org.apache.authx.script.xml.builder;
-import org.apache.authx.authorization.condition.Predicates;
+import org.apache.authx.authorization.condition.Conditions;
import org.dom4j.Element;
public class FalseConditionBuilder extends AbstractElementBuilder
@@ -40,6 +40,6 @@
public Object buildFrom( Element e )
{
- return Predicates.FALSE;
+ return Conditions.FALSE;
}
}
Modified: directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/OrConditionBuilder.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/OrConditionBuilder.java?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/OrConditionBuilder.java (original)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/OrConditionBuilder.java Mon Mar 14 21:21:11 2005
@@ -18,7 +18,7 @@
import org.apache.authx.authorization.Condition;
import org.apache.authx.authorization.condition.OrCondition;
-import org.apache.authx.authorization.condition.Predicates;
+import org.apache.authx.authorization.condition.Conditions;
public class OrConditionBuilder extends LogicalConditionBuilder
{
@@ -34,7 +34,7 @@
protected Condition getSeed()
{
- return Predicates.FALSE;
+ return Conditions.FALSE;
}
public Condition compute( Condition left, Condition right )
Modified: directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/TrueConditionBuilder.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/TrueConditionBuilder.java?view=diff&r1=157517&r2=157518
==============================================================================
--- directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/TrueConditionBuilder.java (original)
+++ directory/authx/trunk/script/src/java/org/apache/authx/script/xml/builder/TrueConditionBuilder.java Mon Mar 14 21:21:11 2005
@@ -16,7 +16,7 @@
*/
package org.apache.authx.script.xml.builder;
-import org.apache.authx.authorization.condition.Predicates;
+import org.apache.authx.authorization.condition.Conditions;
import org.dom4j.Element;
public class TrueConditionBuilder extends AbstractElementBuilder
@@ -40,6 +40,6 @@
public Object buildFrom( Element e )
{
- return Predicates.TRUE;
+ return Conditions.TRUE;
}
}
Added: directory/authx/trunk/script/src/test/org/apache/authx/script/common/DynamicRuleProxyTest.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/test/org/apache/authx/script/common/DynamicRuleProxyTest.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/test/org/apache/authx/script/common/DynamicRuleProxyTest.java (added)
+++ directory/authx/trunk/script/src/test/org/apache/authx/script/common/DynamicRuleProxyTest.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import junit.framework.TestCase;
+import org.apache.authx.authorization.AuthorizationRequest;
+import org.apache.authx.authorization.effect.Effects;
+import org.apache.authx.script.ScriptClassLoader;
+import org.apache.authx.script.groovy.GroovyScriptClassLoader;
+import org.apache.authx.script.testmodel.Groups;
+import org.apache.authx.script.testmodel.Permissions;
+import org.apache.authx.script.testmodel.Subjects;
+
+import java.io.IOException;
+import java.net.URL;
+
+public class DynamicRuleProxyTest extends TestCase
+{
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( DynamicRuleProxyTest.class );
+ }
+
+ public void testWrapsRuleScriptIntoRuleProxy() throws Exception
+ {
+ URL scriptURL = getClass().getResource( "canadians.groovy" );
+ Class groovyClass = loadGroovyClass( scriptURL );
+ DynamicRuleProxy rule = new DynamicRuleProxy( groovyClass.newInstance() );
+
+ AuthorizationRequest request = new AuthorizationRequest( Subjects.with( Groups.canadians() ), Permissions.anything() );
+ rule.evaluate( request );
+
+ assertEquals( Effects.DENY, request.outcome() );
+ }
+
+ private Class loadGroovyClass( URL scriptURL ) throws IOException
+ {
+ ScriptClassLoader loader = new GroovyScriptClassLoader();
+ Class groovyClass = loader.loadClass( scriptURL );
+ return groovyClass;
+ }
+}
Propchange: directory/authx/trunk/script/src/test/org/apache/authx/script/common/DynamicRuleProxyTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/test/org/apache/authx/script/common/PicoContainerFactoryTest.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/test/org/apache/authx/script/common/PicoContainerFactoryTest.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/test/org/apache/authx/script/common/PicoContainerFactoryTest.java (added)
+++ directory/authx/trunk/script/src/test/org/apache/authx/script/common/PicoContainerFactoryTest.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import junit.framework.TestCase;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+
+public class PicoContainerFactoryTest extends TestCase
+{
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( PicoContainerFactoryTest.class );
+ }
+
+ public void testUsesPicoContainerToInstanciateClassesAndResolveDependencies() throws Exception
+ {
+ MutablePicoContainer container = new DefaultPicoContainer();
+ container.registerComponentInstance( String.class, "Hello" );
+
+ PicoContainerFactory factory = new PicoContainerFactory( container );
+ Greeter greeter = (Greeter) factory.newInstance( Greeter.class );
+
+ assertEquals( "Hello", greeter.greet() );
+ }
+
+ public void testSameClassCanBeInstanciatedMoreThanOnce() throws Exception
+ {
+ MutablePicoContainer container = new DefaultPicoContainer();
+ container.registerComponentInstance( String.class, "Hello" );
+
+ PicoContainerFactory factory = new PicoContainerFactory( container );
+ factory.newInstance( Greeter.class );
+ factory.newInstance( Greeter.class );
+
+ assertTrue( true );
+ }
+
+ public static class Greeter
+ {
+ private final String m_msg;
+
+ public Greeter( String msg )
+ {
+ m_msg = msg;
+ }
+ public String greet()
+ {
+ return m_msg;
+ }
+ }
+
+}
Propchange: directory/authx/trunk/script/src/test/org/apache/authx/script/common/PicoContainerFactoryTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/test/org/apache/authx/script/common/ScriptedRuleSetBuilderTest.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/test/org/apache/authx/script/common/ScriptedRuleSetBuilderTest.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/test/org/apache/authx/script/common/ScriptedRuleSetBuilderTest.java (added)
+++ directory/authx/trunk/script/src/test/org/apache/authx/script/common/ScriptedRuleSetBuilderTest.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import org.apache.authx.authorization.PrimitiveRule;
+import org.apache.authx.authorization.Rule;
+import org.apache.authx.authorization.RuleSet;
+import org.apache.authx.authorization.effect.Effects;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+import java.io.IOException;
+import java.net.URL;
+
+public class ScriptedRuleSetBuilderTest extends MockObjectTestCase
+{
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( ScriptedRuleSetBuilderTest.class );
+ }
+
+ public void testLoadsRulesAndBuildsRuleSet() throws IOException
+ {
+ URL scriptURL = getClass().getResource( "canadians.groovy" );
+ Mock mockLoader = new Mock( ScriptedRuleLoader.class );
+ Rule rule = new PrimitiveRule( Effects.GRANT );
+ mockLoader.stubs().method( "load" ).with( eq( scriptURL ) ).will( returnValue( rule ) );
+
+ ScriptedRuleSetBuilder builder = new ScriptedRuleSetBuilder( loader( mockLoader ) );
+ builder.addScript( scriptURL );
+ Mock mockPolicy = new Mock( RuleSet.class );
+ mockPolicy.expects( once() ).method( "addRule" ).with( eq( rule ) );
+
+ builder.buildRuleSet( policy( mockPolicy ) );
+
+ mockPolicy.verify();
+ }
+
+ private RuleSet policy( Mock mockPolicy )
+ {
+ return (RuleSet) mockPolicy.proxy();
+ }
+
+ private ScriptedRuleLoader loader( Mock mockLoader )
+ {
+ return (ScriptedRuleLoader) mockLoader.proxy();
+ }
+
+}
Propchange: directory/authx/trunk/script/src/test/org/apache/authx/script/common/ScriptedRuleSetBuilderTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/test/org/apache/authx/script/common/SmartCachingScriptClassLoaderTest.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/test/org/apache/authx/script/common/SmartCachingScriptClassLoaderTest.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/test/org/apache/authx/script/common/SmartCachingScriptClassLoaderTest.java (added)
+++ directory/authx/trunk/script/src/test/org/apache/authx/script/common/SmartCachingScriptClassLoaderTest.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.common;
+
+import junit.framework.TestCase;
+import org.apache.authx.script.groovy.GroovyScriptClassLoader;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.net.URL;
+
+public class SmartCachingScriptClassLoaderTest extends TestCase
+{
+ private SmartCachingScriptClassLoader scriptClassLoader;
+
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( SmartCachingScriptClassLoaderTest.class );
+ }
+
+ protected void setUp() throws Exception
+ {
+ scriptClassLoader = new SmartCachingScriptClassLoader( new GroovyScriptClassLoader() );
+ }
+
+ public void testClassesAreRetrievedFromCacheIfScriptHasNotChanged()
+ throws Exception
+ {
+ URL scriptURL = getClass().getResource( "canadians.groovy" );
+ Class firstHit = scriptClassLoader.loadClass( scriptURL );
+ Class secondHit = scriptClassLoader.loadClass( scriptURL );
+ assertSame( firstHit, secondHit );
+ }
+
+ public void testClassesAreReloadedIfScriptWasChanged() throws Exception
+ {
+ URL scriptURL = getClass().getResource( "canadians.groovy" );
+ Class firstHit = scriptClassLoader.loadClass( scriptURL );
+ touch( scriptURL );
+ Class secondHit = scriptClassLoader.loadClass( scriptURL );
+ assertNotSame( firstHit, secondHit );
+ }
+
+ /**
+ * There has to be a better way to do a touch. The thing is
+ * File.setLastModified( long time ) does not seem to work on my windoze
+ * box. That would have been perfect.
+ */
+ private void touch( URL scriptURL ) throws IOException
+ {
+ File script = new File( scriptURL.getFile() );
+ Writer writer = new FileWriter( script, true );
+ writer.write( " " );
+ writer.close();
+ }
+
+}
Propchange: directory/authx/trunk/script/src/test/org/apache/authx/script/common/SmartCachingScriptClassLoaderTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/test/org/apache/authx/script/common/canadians.groovy
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/test/org/apache/authx/script/common/canadians.groovy?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/test/org/apache/authx/script/common/canadians.groovy (added)
+++ directory/authx/trunk/script/src/test/org/apache/authx/script/common/canadians.groovy Mon Mar 14 21:21:11 2005
@@ -0,0 +1,9 @@
+class Canadians {
+
+ evaluate( request )
+ {
+ canadians = new org.apache.authx.authentication.attribute.GroupPrincipal( "canadians" );
+ if ( request.getSubject().getPrincipals().contains( canadians ) ) request.deny();
+ }
+
+}
\ No newline at end of file
Propchange: directory/authx/trunk/script/src/test/org/apache/authx/script/common/canadians.groovy
------------------------------------------------------------------------------
svn:executable = *
Added: directory/authx/trunk/script/src/test/org/apache/authx/script/groovy/GroovyRuleLoaderTest.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/script/src/test/org/apache/authx/script/groovy/GroovyRuleLoaderTest.java?view=auto&rev=157518
==============================================================================
--- directory/authx/trunk/script/src/test/org/apache/authx/script/groovy/GroovyRuleLoaderTest.java (added)
+++ directory/authx/trunk/script/src/test/org/apache/authx/script/groovy/GroovyRuleLoaderTest.java Mon Mar 14 21:21:11 2005
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.authx.script.groovy;
+
+import junit.framework.TestCase;
+import org.apache.authx.authorization.AuthorizationRequest;
+import org.apache.authx.authorization.Rule;
+import org.apache.authx.authorization.effect.Effects;
+import org.apache.authx.script.testmodel.Groups;
+import org.apache.authx.script.testmodel.Permissions;
+import org.apache.authx.script.testmodel.Subjects;
+
+import java.io.IOException;
+import java.net.URL;
+
+public class GroovyRuleLoaderTest extends TestCase
+{
+ public static void main( String[] args )
+ {
+ junit.textui.TestRunner.run( GroovyRuleLoaderTest.class );
+ }
+
+ public void testLoadsRulesFromGroovyScripts() throws IOException
+ {
+ URL scriptURL = getClass().getResource( "geeks.groovy" );
+ GroovyRuleLoader loader = new GroovyRuleLoader();
+ Rule rule = loader.load( scriptURL );
+
+ AuthorizationRequest request = new AuthorizationRequest( Subjects.with( Groups.geeks() ), Permissions.anything() );
+ rule.evaluate( request );
+
+ assertEquals( Effects.GRANT, request.outcome() );
+ }
+
+}
Propchange: directory/authx/trunk/script/src/test/org/apache/authx/script/groovy/GroovyRuleLoaderTest.java
------------------------------------------------------------------------------
svn:executable = *