Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Proposal of basic AOP DSL #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/scala/org/springframework/scala/aop/AopSupport.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2011-2013 the original author or authors.
*
* 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.springframework.scala.aop

import org.springframework.aop.Pointcut
import org.aopalliance.aop.Advice
import org.springframework.aop.framework.ProxyFactoryBean
import org.springframework.aop.support.DefaultPointcutAdvisor
import org.springframework.scala.context.function.FunctionalConfiguration

trait AopSupport {

self: FunctionalConfiguration =>

def advice(beanName: String) = new AdviceDefinition(beanName)

def advice = new AdviceDefinition("")

class AdviceDefinition(beanName: String) {

def targetRef(targetName: String) = new TargetDefinition(targetName = Some(targetName))

def target(target: Any) = new TargetDefinition(target = Some(target))

class TargetDefinition(targetName: Option[String] = None, target: Option[Any] = None) {

val proxyFactory = new ProxyFactoryBean
(targetName, target) match {
case (None, Some(t)) => proxyFactory.setTarget(t)
case (Some(tn), None) => proxyFactory.setTargetName(tn)
case _ => throw new IllegalStateException("Either bean reference or embedded bean need to be passed to the AOP proxy builder.")
}
proxyFactory.setProxyTargetClass(true)
bean(name = beanName)(proxyFactory)

def on(pointcut: Pointcut) = new AdvicePointcutDefinition(pointcut)

class AdvicePointcutDefinition(pointcut: Pointcut) {

def using(advice: Advice): TargetDefinition = {
proxyFactory.addAdvisor(new DefaultPointcutAdvisor(pointcut, advice))
TargetDefinition.this
}

}

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2011-2013 the original author or authors.
*
* 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.springframework.scala.aop

import org.springframework.aop.support.StaticMethodMatcherPointcut
import java.lang.reflect.Method
import org.springframework.aop.Pointcut

object PointcutConversions {

implicit def asPointcut(matcher: (Method, Class[_]) => Boolean): Pointcut = new StaticMethodMatcherPointcut {
def matches(method: Method, targetClass: Class[_]) = matcher(method, targetClass)
}

}
96 changes: 96 additions & 0 deletions src/test/scala/org/springframework/scala/aop/AopSupportTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2011-2013 the original author or authors.
*
* 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.springframework.scala.aop

import org.springframework.context.support.GenericApplicationContext
import org.scalatest.{BeforeAndAfter, FunSuite}
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.springframework.scala.context.function.{FunctionalConfiguration, FunctionalConfigApplicationContext}
import PointcutConversions._
import AdviceConversions._
import java.lang.reflect.Method
import scala.Array
import java.util.{Calendar, Date}
import org.scalatest.matchers.ShouldMatchers
import org.springframework.aop.support.AopUtils

@RunWith(classOf[JUnitRunner])
class AopSupportTests extends FunSuite with BeforeAndAfter with ShouldMatchers {

val applicationContext: GenericApplicationContext = new FunctionalConfigApplicationContext(classOf[TestConfiguration])

before {
val counter = applicationContext.getBean(classOf[Counter])
counter.reset
}

test("Should create proxy by bean reference.") {
val dateProxy = applicationContext.getBean("dateRefProxy", classOf[Date])
AopUtils.isCglibProxy(dateProxy) should equal (true)
}

test("Should create proxy from embedded bean.") {
val calendarProxy = applicationContext.getBean(classOf[Calendar])
AopUtils.isCglibProxy(calendarProxy) should equal (true)
}

test("Should generate name for the anoumous bean.") {
val calendars = applicationContext.getBeansOfType(classOf[Calendar])
calendars.size should equal (1)
calendars.keySet.iterator.next should not be ('empty)
}

test("Should call advice.") {
val dateProxy = applicationContext.getBean("dateRefProxy", classOf[Date])
dateProxy.getTime
val counter = applicationContext.getBean(classOf[Counter])
counter.count should equal (1)
}

test("Should call multiple advices.") {
val advisedTwiceDate = applicationContext.getBean("advisedTwiceDate", classOf[Date])
advisedTwiceDate.getTime
val counter = applicationContext.getBean(classOf[Counter])
counter.count should equal (2)
}

}

class TestConfiguration extends FunctionalConfiguration with AopSupport {

bean("counter") {
Counter()
}

val getTimePointcut = (m: Method, c: Class[_]) => m.getName == "getTime"
val beforeAdvice = (m: Method, args: Array[AnyRef], target: Any) => {getBean[Counter]("counter").inc()}

bean("date")(new Date)
advice(beanName = "dateRefProxy") targetRef "date" on getTimePointcut using beforeAdvice

advice target Calendar.getInstance() on getTimePointcut using beforeAdvice

advice(beanName = "advisedTwiceDate").target(new Date).
on(getTimePointcut).using(beforeAdvice).
on(getTimePointcut).using(beforeAdvice)

}

case class Counter(var count: Int = 0) {
def inc() {count = count + 1}
def reset() {count = 0}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2011-2013 the original author or authors.
*
* 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.springframework.scala.aop

import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.springframework.aop.Pointcut
import java.lang.reflect.Method
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class PointcutConversionsTests extends FunSuite with ShouldMatchers {

test("Should convert function to static matcher.") {
import PointcutConversions.asPointcut
val toStringPointcut : Pointcut = (method: Method, targetClass: Class[_]) => method.getName == "toString"
val matches = toStringPointcut.getMethodMatcher.matches(getClass.getMethod("toString"), getClass)
matches should equal (true)
}

}