1. Marcin Grzejszczak
  2. Too Much Coding

Commits

Marcin Grzejszczak  committed 0efd3d1

[AOP] Fixed project structure and added missing maven plugin

  • Participants
  • Parent commits 6509e14
  • Branches default

Comments (0)

Files changed (35)

File AOP/pom.xml

View file
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 		<maven.compiler.source>1.6</maven.compiler.source>
 		<maven.compiler.target>1.6</maven.compiler.target>
-
 		<spring.version>3.1.0.RELEASE</spring.version>
-		<aspectj.version>1.6.11</aspectj.version>
+		<aspectj.version>1.6.7</aspectj.version>
 
 		<!-- Plugin versions -->
-		<aspectj-maven-plugin.version>1.4</aspectj-maven-plugin.version>
+		<aspectj-maven-plugin.version>1.3</aspectj-maven-plugin.version>
 
 	</properties>
 	<repositories>
 			<id>spring-release</id>
 			<url>http://maven.springframework.org/release</url>
 		</repository>
-		<repository>
-            <id>maven-us-nuxeo</id>
-            <url>https://maven-us.nuxeo.org/nexus/content/groups/public</url>
-        </repository>
 	</repositories>
 
 	<dependencies>
 			<artifactId>junit</artifactId>
 			<version>4.10</version>
 		</dependency>
+		<dependency>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-all</artifactId>
+			<version>1.9.0</version>
+		</dependency>
 		<!-- Spring -->
 		<dependency>
 			<groupId>org.springframework</groupId>
 		<dependency>
 			<groupId>org.aspectj</groupId>
 			<artifactId>aspectjweaver</artifactId>
-			<version>1.6.11</version>
-		</dependency>
-		<dependency><!-- java bytecode processor -->
-			<groupId>javassist</groupId>
-			<artifactId>javassist</artifactId>
-			<version>3.4.GA</version>
+			<version>${aspectj.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.slf4j</groupId>
 			<version>2.6</version>
 		</dependency>
 	</dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>aspectj-maven-plugin</artifactId>
+                <version>${aspectj-maven-plugin.version}</version>
+                <configuration>
+                    <source>${maven.compiler.source}</source>
+                    <target>${maven.compiler.target}</target>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>compile</goal>
+                            <goal>test-compile</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 </project>

File AOP/src/main/java/pl/grzejszczak/marcin/aop/AopIntegrationTest.java

View file
+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));
+
+	}
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/annotation/SecurityAnnotation.java

View file
+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();
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/interceptor/SecurityInterceptor.java

View file
+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 atrAccessSecurityAnnotationPresent = (annotation != null);
+
+		if (atrAccessSecurityAnnotationPresent) {
+			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<Role> requiredRolesList = Arrays.asList(annotationRule.allowedRole());
+		Role userRole = userService.getUserRole();
+		boolean userHasRole = requiredRolesList.contains(userRole);
+		return userHasRole;
+	}
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/service/UserService.java

View file
+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();
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/service/UserServiceImpl.java

View file
+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();
+	}
+
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/type/Role.java

View file
+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;
+	}
+
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdmin.java

View file
+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();
+	}
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdminAndGuest.java

View file
+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();
+	}
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/ui/SomeComponentForGuest.java

View file
+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();
+	}
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/ui/SomeComponentForWriter.java

View file
+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();
+	}
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/ui/UIComponent.java

View file
+package pl.grzejszczak.marcin.aop.ui;
+
+public abstract class UIComponent {
+	protected String componentName;
+
+	protected String getComponentName() {
+		return componentName;
+	}
+
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/ui/UIFactory.java

View file
+package pl.grzejszczak.marcin.aop.ui;
+
+public interface UIFactory {
+	UIComponent createComponent(Class<? extends UIComponent> componentClass) throws Exception;
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/ui/UIFactoryImpl.java

View file
+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<? extends UIComponent> componentClass) throws Exception {
+		if (componentClass == null) {
+			throw new NullArgumentException("Provide class for the component");
+		}
+		return (UIComponent) Class.forName(componentClass.getName()).newInstance();
+	}
+
+}

File AOP/src/main/java/pl/grzejszczak/marcin/aop/user/UserHolder.java

View file
+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;
+	}
+
+}

File AOP/src/main/pl/grzejszczak/marcin/aop/AopIntegrationTest.java

-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));
-
-	}
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/AopTest.java

-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));
-	}
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/annotation/SecurityAnnotation.java

-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();
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/interceptor/SecurityInterceptor.java

-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<Role> requiredRolesList = Arrays.asList(annotationRule.allowedRole());
-		Role userRole = userService.getUserRole();
-		boolean userHasRole = requiredRolesList.contains(userRole);
-		return userHasRole;
-	}
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/service/UserService.java

-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();
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/service/UserServiceImpl.java

-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();
-	}
-
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/type/Role.java

-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;
-	}
-
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdmin.java

-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();
-	}
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForAdminAndGuest.java

-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();
-	}
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForGuest.java

-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();
-	}
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/ui/SomeComponentForWriter.java

-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();
-	}
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIComponent.java

-package pl.grzejszczak.marcin.aop.ui;
-
-public abstract class UIComponent {
-	protected String componentName;
-
-	protected String getComponentName() {
-		return componentName;
-	}
-
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactory.java

-package pl.grzejszczak.marcin.aop.ui;
-
-public interface UIFactory {
-	UIComponent createComponent(Class<? extends UIComponent> componentClass) throws Exception;
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/ui/UIFactoryImpl.java

-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<? extends UIComponent> componentClass) throws Exception {
-		if (componentClass == null) {
-			throw new NullArgumentException("Provide class for the component");
-		}
-		return (UIComponent) Class.forName(componentClass.getName()).newInstance();
-	}
-
-}

File AOP/src/main/pl/grzejszczak/marcin/aop/user/UserHolder.java

-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;
-	}
-
-}

File AOP/src/main/resources/aopApplicationContext.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+	xmlns:context="http://www.springframework.org/schema/context"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xmlns:aop="http://www.springframework.org/schema/aop"
+	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
+        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
+        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
+        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
+
+	<context:annotation-config />
+	<context:component-scan base-package="pl.grzejszczak.marcin.aop">
+		<context:exclude-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
+	</context:component-scan>
+	<aop:aspectj-autoproxy/>
+	<bean class="pl.grzejszczak.marcin.aop.interceptor.SecurityInterceptor" factory-method="aspectOf"/>	
+
+</beans>

File AOP/src/main/resources/log4j.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+
+	<!-- Appenders -->
+	<appender name="console" class="org.apache.log4j.ConsoleAppender">
+		<param name="Target" value="System.out" />
+		<layout class="org.apache.log4j.PatternLayout">
+			<param name="ConversionPattern" value="%d %-5p [%t] %c:%L %m%n" />
+		</layout>
+	</appender>
+	
+	<!-- Application Loggers -->
+	<logger name="pl.grzejszczak.marcin">
+		<level value="DEBUG" />
+	</logger>
+	
+	<!-- 3rdparty Loggers -->
+	<logger name="org.springframework.core">
+		<level value="INFO" />
+	</logger>
+	
+	<logger name="org.springframework.beans">
+		<level value="INFO" />
+	</logger>
+	
+	<logger name="org.springframework.context">
+		<level value="INFO" />
+	</logger>
+
+	<logger name="org.springframework.web">
+		<level value="INFO" />
+	</logger>
+	
+	<logger name="org.springframework.security">
+		<level value="INFO" />
+	</logger>
+	
+	<logger name="com.juancavallotti.jdto">
+		<level value="WARN" />
+	</logger>
+
+
+	<!-- Root Logger -->
+	<root>
+		<priority value="INFO" />
+		<appender-ref ref="console" />
+	</root>
+	
+</log4j:configuration>

File AOP/src/resources/aopApplicationContext.xml

-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-	xmlns:context="http://www.springframework.org/schema/context"
-	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xmlns:aop="http://www.springframework.org/schema/aop"
-	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
-        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
-        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
-        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
-
-	<aop:aspectj-autoproxy />
-	<context:annotation-config />
-	<context:component-scan base-package="pl.grzejszczak.marcin.aop">
-		<context:exclude-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
-	</context:component-scan>
-	<bean class="pl.grzejszczak.marcin.aop.interceptor.SecurityInterceptor" factory-method="aspectOf"/>	
-
-</beans>

File AOP/src/resources/log4j.xml

-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
-<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
-
-	<!-- Appenders -->
-	<appender name="console" class="org.apache.log4j.ConsoleAppender">
-		<param name="Target" value="System.out" />
-		<layout class="org.apache.log4j.PatternLayout">
-			<param name="ConversionPattern" value="%d %-5p [%t] %c:%L %m%n" />
-		</layout>
-	</appender>
-	
-	<!-- Application Loggers -->
-	<logger name="pl.grzejszczak.marcin">
-		<level value="DEBUG" />
-	</logger>
-	
-	<!-- 3rdparty Loggers -->
-	<logger name="org.springframework.core">
-		<level value="INFO" />
-	</logger>
-	
-	<logger name="org.springframework.beans">
-		<level value="INFO" />
-	</logger>
-	
-	<logger name="org.springframework.context">
-		<level value="INFO" />
-	</logger>
-
-	<logger name="org.springframework.web">
-		<level value="INFO" />
-	</logger>
-	
-	<logger name="org.springframework.security">
-		<level value="INFO" />
-	</logger>
-	
-	<logger name="com.juancavallotti.jdto">
-		<level value="WARN" />
-	</logger>
-
-
-	<!-- Root Logger -->
-	<root>
-		<priority value="INFO" />
-		<appender-ref ref="console" />
-	</root>
-	
-</log4j:configuration>

File AOP/src/test/java/pl/grzejszczak/marcin/aop/AopTest.java

View file
+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));
+	}
+}