From aee70af55db3abb488685f71717ac114efcbac9f Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 16 Sep 2013 21:10:17 +0200 Subject: [PATCH] [AOP] Fixed project structure and added missing maven plugin --- AOP/pom.xml | 42 ++++++--- .../marcin/aop/AopIntegrationTest.java | 28 ------ .../pl/grzejszczak/marcin/aop/AopTest.java | 37 -------- .../aop/annotation/SecurityAnnotation.java | 11 --- .../aop/interceptor/SecurityInterceptor.java | 90 ------------------- .../marcin/aop/service/UserService.java | 12 --- .../marcin/aop/service/UserServiceImpl.java | 30 ------- .../pl/grzejszczak/marcin/aop/type/Role.java | 34 ------- .../marcin/aop/ui/SomeComponentForAdmin.java | 16 ---- .../aop/ui/SomeComponentForAdminAndGuest.java | 16 ---- .../marcin/aop/ui/SomeComponentForGuest.java | 16 ---- .../marcin/aop/ui/SomeComponentForWriter.java | 16 ---- .../marcin/aop/ui/UIComponent.java | 10 --- .../grzejszczak/marcin/aop/ui/UIFactory.java | 5 -- .../marcin/aop/ui/UIFactoryImpl.java | 17 ---- .../marcin/aop/user/UserHolder.java | 20 ----- AOP/src/resources/aopApplicationContext.xml | 19 ---- AOP/src/resources/log4j.xml | 50 ----------- 18 files changed, 29 insertions(+), 440 deletions(-) delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/AopIntegrationTest.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/AopTest.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/annotation/SecurityAnnotation.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/interceptor/SecurityInterceptor.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/service/UserService.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/service/UserServiceImpl.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/type/Role.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdmin.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdminAndGuest.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForGuest.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForWriter.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIComponent.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactory.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactoryImpl.java delete mode 100644 AOP/src/main/pl/grzejszczak/marcin/aop/user/UserHolder.java delete mode 100644 AOP/src/resources/aopApplicationContext.xml delete mode 100644 AOP/src/resources/log4j.xml diff --git a/AOP/pom.xml b/AOP/pom.xml index 007265e..f347ea2 100644 --- a/AOP/pom.xml +++ b/AOP/pom.xml @@ -9,12 +9,11 @@ UTF-8 1.6 1.6 - 3.1.0.RELEASE - 1.6.11 + 1.6.7 - 1.4 + 1.3 @@ -22,10 +21,6 @@ spring-release http://maven.springframework.org/release - - maven-us-nuxeo - https://maven-us.nuxeo.org/nexus/content/groups/public - @@ -34,6 +29,11 @@ junit 4.10 + + org.mockito + mockito-all + 1.9.0 + org.springframework @@ -69,12 +69,7 @@ org.aspectj aspectjweaver - 1.6.11 - - - javassist - javassist - 3.4.GA + ${aspectj.version} org.slf4j @@ -87,4 +82,25 @@ 2.6 + + + + org.codehaus.mojo + aspectj-maven-plugin + ${aspectj-maven-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + compile + test-compile + + + + + + diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/AopIntegrationTest.java b/AOP/src/main/pl/grzejszczak/marcin/aop/AopIntegrationTest.java deleted file mode 100644 index 2e92a2b..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/AopIntegrationTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package pl.grzejszczak.marcin.aop; - -import org.junit.Assert; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import pl.grzejszczak.marcin.aop.service.UserService; -import pl.grzejszczak.marcin.aop.type.Role; -import pl.grzejszczak.marcin.aop.ui.SomeComponentForAdmin; -import pl.grzejszczak.marcin.aop.ui.SomeComponentForGuest; -import pl.grzejszczak.marcin.aop.ui.SomeComponentForWriter; -import pl.grzejszczak.marcin.aop.ui.UIFactory; -import pl.grzejszczak.marcin.aop.user.UserHolder; - -public class AopIntegrationTest { - public static void main(String[] args) throws Exception { - ApplicationContext context = new ClassPathXmlApplicationContext("aopApplicationContext.xml"); - - UIFactory uiFactory = context.getBean(UIFactory.class); - UserService userService = context.getBean(UserService.class); - - userService.setCurrentUser(new UserHolder(Role.ADMIN)); - Assert.assertNotNull(uiFactory.createComponent(SomeComponentForAdmin.class)); - Assert.assertNull(uiFactory.createComponent(SomeComponentForGuest.class)); - Assert.assertNull(uiFactory.createComponent(SomeComponentForWriter.class)); - - } -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/AopTest.java b/AOP/src/main/pl/grzejszczak/marcin/aop/AopTest.java deleted file mode 100644 index 99c3280..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/AopTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package pl.grzejszczak.marcin.aop; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import pl.grzejszczak.marcin.aop.service.UserService; -import pl.grzejszczak.marcin.aop.type.Role; -import pl.grzejszczak.marcin.aop.ui.SomeComponentForAdmin; -import pl.grzejszczak.marcin.aop.ui.SomeComponentForAdminAndGuest; -import pl.grzejszczak.marcin.aop.ui.SomeComponentForGuest; -import pl.grzejszczak.marcin.aop.ui.SomeComponentForWriter; -import pl.grzejszczak.marcin.aop.ui.UIFactory; -import pl.grzejszczak.marcin.aop.user.UserHolder; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = { "classpath:aopApplicationContext.xml" }) -public class AopTest { - - @Autowired - private UIFactory uiFactory; - - @Autowired - private UserService userService; - - @Test - public void adminTest() throws Exception { - userService.setCurrentUser(new UserHolder(Role.ADMIN)); - Assert.assertNotNull(uiFactory.createComponent(SomeComponentForAdmin.class)); - Assert.assertNotNull(uiFactory.createComponent(SomeComponentForAdminAndGuest.class)); - Assert.assertNull(uiFactory.createComponent(SomeComponentForGuest.class)); - Assert.assertNull(uiFactory.createComponent(SomeComponentForWriter.class)); - } -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/annotation/SecurityAnnotation.java b/AOP/src/main/pl/grzejszczak/marcin/aop/annotation/SecurityAnnotation.java deleted file mode 100644 index 8672e83..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/annotation/SecurityAnnotation.java +++ /dev/null @@ -1,11 +0,0 @@ -package pl.grzejszczak.marcin.aop.annotation; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -import pl.grzejszczak.marcin.aop.type.Role; - -@Retention(RetentionPolicy.RUNTIME) -public @interface SecurityAnnotation { - Role[] allowedRole(); -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/interceptor/SecurityInterceptor.java b/AOP/src/main/pl/grzejszczak/marcin/aop/interceptor/SecurityInterceptor.java deleted file mode 100644 index 24e1b52..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/interceptor/SecurityInterceptor.java +++ /dev/null @@ -1,90 +0,0 @@ -package pl.grzejszczak.marcin.aop.interceptor; - -import java.lang.annotation.Annotation; -import java.lang.reflect.AnnotatedElement; -import java.util.Arrays; -import java.util.List; - -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -import pl.grzejszczak.marcin.aop.annotation.SecurityAnnotation; -import pl.grzejszczak.marcin.aop.service.UserService; -import pl.grzejszczak.marcin.aop.type.Role; -import pl.grzejszczak.marcin.aop.ui.UIComponent; - -@Aspect -public class SecurityInterceptor { - private static final Logger LOGGER = LoggerFactory.getLogger(SecurityInterceptor.class); - - public SecurityInterceptor() { - LOGGER.debug("Security Interceptor created"); - } - - @Autowired - private UserService userService; - - @Pointcut("execution(pl.grzejszczak.marcin.aop.ui.UIComponent pl.grzejszczak.marcin.aop.ui.UIFactory.createComponent(..))") - private void getComponent(ProceedingJoinPoint thisJoinPoint) { - } - - @Around("getComponent(thisJoinPoint)") - public UIComponent checkSecurity(ProceedingJoinPoint thisJoinPoint) throws Throwable { - LOGGER.info("Intercepting creation of a component"); - - Object[] arguments = thisJoinPoint.getArgs(); - if (arguments.length == 0) { - return null; - } - - Annotation annotation = checkTheAnnotation(arguments); - boolean securityAnnotationPresent = (annotation != null); - - if (securityAnnotationPresent) { - boolean userHasRole = verifyRole(annotation); - if (!userHasRole) { - LOGGER.info("Current user doesn't have permission to have this component created"); - return null; - } - } - LOGGER.info("Current user has required permissions for creating a component"); - return (UIComponent) thisJoinPoint.proceed(); - } - - /** - * Basing on the method's argument check if the class is annotataed with - * {@link SecurityAnnotation} - * - * @param arguments - * @return - */ - private Annotation checkTheAnnotation(Object[] arguments) { - Object concreteClass = arguments[0]; - LOGGER.info("Argument's class - [{}]", new Object[] { arguments }); - AnnotatedElement annotatedElement = (AnnotatedElement) concreteClass; - Annotation annotation = annotatedElement.getAnnotation(SecurityAnnotation.class); - LOGGER.info("Annotation present - [{}]", new Object[] { annotation }); - return annotation; - } - - /** - * The function verifies if the current user has sufficient privilages to - * have the component built - * - * @param annotation - * @return - */ - private boolean verifyRole(Annotation annotation) { - LOGGER.info("Security annotation is present so checking if the user can use it"); - SecurityAnnotation annotationRule = (SecurityAnnotation) annotation; - List requiredRolesList = Arrays.asList(annotationRule.allowedRole()); - Role userRole = userService.getUserRole(); - boolean userHasRole = requiredRolesList.contains(userRole); - return userHasRole; - } -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/service/UserService.java b/AOP/src/main/pl/grzejszczak/marcin/aop/service/UserService.java deleted file mode 100644 index fa4018b..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/service/UserService.java +++ /dev/null @@ -1,12 +0,0 @@ -package pl.grzejszczak.marcin.aop.service; - -import pl.grzejszczak.marcin.aop.type.Role; -import pl.grzejszczak.marcin.aop.user.UserHolder; - -public interface UserService { - UserHolder getCurrentUser(); - - void setCurrentUser(UserHolder userHolder); - - Role getUserRole(); -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/service/UserServiceImpl.java b/AOP/src/main/pl/grzejszczak/marcin/aop/service/UserServiceImpl.java deleted file mode 100644 index 6ecd270..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/service/UserServiceImpl.java +++ /dev/null @@ -1,30 +0,0 @@ -package pl.grzejszczak.marcin.aop.service; - -import org.springframework.stereotype.Service; - -import pl.grzejszczak.marcin.aop.type.Role; -import pl.grzejszczak.marcin.aop.user.UserHolder; - -@Service -public class UserServiceImpl implements UserService { - private UserHolder userHolder; - - @Override - public UserHolder getCurrentUser() { - return userHolder; - } - - @Override - public void setCurrentUser(UserHolder userHolder) { - this.userHolder = userHolder; - } - - @Override - public Role getUserRole() { - if (userHolder == null) { - return null; - } - return userHolder.getUserRole(); - } - -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/type/Role.java b/AOP/src/main/pl/grzejszczak/marcin/aop/type/Role.java deleted file mode 100644 index 71e7068..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/type/Role.java +++ /dev/null @@ -1,34 +0,0 @@ -package pl.grzejszczak.marcin.aop.type; - - -public enum Role { - ADMIN("ADM"), WRITER("WRT"), GUEST("GST"); - - private String name; - - private Role(String name) { - this.name = name; - } - - public static Role getRoleByName(String name) { - - for (Role role : Role.values()) { - - if (role.name.equals(name)) { - return role; - } - } - - throw new IllegalArgumentException("No such role exists [" + name + "]"); - } - - public String getName() { - return this.name; - } - - @Override - public String toString() { - return name; - } - -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdmin.java b/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdmin.java deleted file mode 100644 index 92e0b96..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdmin.java +++ /dev/null @@ -1,16 +0,0 @@ -package pl.grzejszczak.marcin.aop.ui; - -import pl.grzejszczak.marcin.aop.annotation.SecurityAnnotation; -import pl.grzejszczak.marcin.aop.type.Role; - -@SecurityAnnotation(allowedRole = { Role.ADMIN }) -public class SomeComponentForAdmin extends UIComponent { - - public SomeComponentForAdmin() { - this.componentName = "SomeComponentForAdmin"; - } - - public static UIComponent getComponent() { - return new SomeComponentForAdmin(); - } -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdminAndGuest.java b/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdminAndGuest.java deleted file mode 100644 index 51a9e16..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdminAndGuest.java +++ /dev/null @@ -1,16 +0,0 @@ -package pl.grzejszczak.marcin.aop.ui; - -import pl.grzejszczak.marcin.aop.annotation.SecurityAnnotation; -import pl.grzejszczak.marcin.aop.type.Role; - -@SecurityAnnotation(allowedRole = { Role.ADMIN, Role.GUEST }) -public class SomeComponentForAdminAndGuest extends UIComponent { - - public SomeComponentForAdminAndGuest() { - this.componentName = "SomeComponentForAdmin"; - } - - public static UIComponent getComponent() { - return new SomeComponentForAdminAndGuest(); - } -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForGuest.java b/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForGuest.java deleted file mode 100644 index 91b898f..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForGuest.java +++ /dev/null @@ -1,16 +0,0 @@ -package pl.grzejszczak.marcin.aop.ui; - -import pl.grzejszczak.marcin.aop.annotation.SecurityAnnotation; -import pl.grzejszczak.marcin.aop.type.Role; - -@SecurityAnnotation(allowedRole = { Role.GUEST }) -public class SomeComponentForGuest extends UIComponent { - - public SomeComponentForGuest() { - this.componentName = "SomeComponentForGuest"; - } - - public static UIComponent getComponent() { - return new SomeComponentForGuest(); - } -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForWriter.java b/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForWriter.java deleted file mode 100644 index ab217e0..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForWriter.java +++ /dev/null @@ -1,16 +0,0 @@ -package pl.grzejszczak.marcin.aop.ui; - -import pl.grzejszczak.marcin.aop.annotation.SecurityAnnotation; -import pl.grzejszczak.marcin.aop.type.Role; - -@SecurityAnnotation(allowedRole = { Role.WRITER }) -public class SomeComponentForWriter extends UIComponent { - - public SomeComponentForWriter() { - this.componentName = "SomeComponentForWriter"; - } - - public static UIComponent getComponent() { - return new SomeComponentForWriter(); - } -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIComponent.java b/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIComponent.java deleted file mode 100644 index 630e7ef..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIComponent.java +++ /dev/null @@ -1,10 +0,0 @@ -package pl.grzejszczak.marcin.aop.ui; - -public abstract class UIComponent { - protected String componentName; - - protected String getComponentName() { - return componentName; - } - -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactory.java b/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactory.java deleted file mode 100644 index 4478dc9..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactory.java +++ /dev/null @@ -1,5 +0,0 @@ -package pl.grzejszczak.marcin.aop.ui; - -public interface UIFactory { - UIComponent createComponent(Class componentClass) throws Exception; -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactoryImpl.java b/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactoryImpl.java deleted file mode 100644 index 6936d18..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactoryImpl.java +++ /dev/null @@ -1,17 +0,0 @@ -package pl.grzejszczak.marcin.aop.ui; - -import org.apache.commons.lang.NullArgumentException; -import org.springframework.stereotype.Component; - -@Component -public class UIFactoryImpl implements UIFactory { - - @Override - public UIComponent createComponent(Class componentClass) throws Exception { - if (componentClass == null) { - throw new NullArgumentException("Provide class for the component"); - } - return (UIComponent) Class.forName(componentClass.getName()).newInstance(); - } - -} diff --git a/AOP/src/main/pl/grzejszczak/marcin/aop/user/UserHolder.java b/AOP/src/main/pl/grzejszczak/marcin/aop/user/UserHolder.java deleted file mode 100644 index 1564ca6..0000000 --- a/AOP/src/main/pl/grzejszczak/marcin/aop/user/UserHolder.java +++ /dev/null @@ -1,20 +0,0 @@ -package pl.grzejszczak.marcin.aop.user; - -import pl.grzejszczak.marcin.aop.type.Role; - -public class UserHolder { - private Role userRole; - - public UserHolder(Role userRole) { - this.userRole = userRole; - } - - public Role getUserRole() { - return userRole; - } - - public void setUserRole(Role userRole) { - this.userRole = userRole; - } - -} diff --git a/AOP/src/resources/aopApplicationContext.xml b/AOP/src/resources/aopApplicationContext.xml deleted file mode 100644 index 8087673..0000000 --- a/AOP/src/resources/aopApplicationContext.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - diff --git a/AOP/src/resources/log4j.xml b/AOP/src/resources/log4j.xml deleted file mode 100644 index b8f2ed2..0000000 --- a/AOP/src/resources/log4j.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -