001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.guard;
014    
015    import java.lang.reflect.Constructor;
016    
017    import net.sf.oval.internal.Log;
018    import net.sf.oval.internal.util.Invocable;
019    
020    import org.aopalliance.intercept.ConstructorInterceptor;
021    import org.aopalliance.intercept.ConstructorInvocation;
022    import org.aopalliance.intercept.MethodInterceptor;
023    import org.aopalliance.intercept.MethodInvocation;
024    
025    /**
026     * AOP Alliance Interceptor implementation of the Guard aspect
027     * 
028     * @author Sebastian Thomschke
029     */
030    public class GuardInterceptor implements MethodInterceptor, ConstructorInterceptor
031    {
032            protected static final class MethodInvocable implements Invocable
033            {
034                    private final MethodInvocation methodInvocation;
035    
036                    protected MethodInvocable(final MethodInvocation methodInvocation)
037                    {
038                            this.methodInvocation = methodInvocation;
039                    }
040    
041                    /**
042                     * {@inheritDoc}
043                     */
044                    public Object invoke() throws Throwable
045                    {
046                            return methodInvocation.proceed();
047                    }
048            }
049    
050            private static final Log LOG = Log.getLog(GuardInterceptor.class);
051    
052            private Guard guard;
053    
054            public GuardInterceptor()
055            {
056                    this(new Guard());
057            }
058    
059            public GuardInterceptor(final Guard guard)
060            {
061                    LOG.info("Instantiated");
062    
063                    setGuard(guard);
064            }
065    
066            /**
067             * {@inheritDoc}
068             */
069            public Object construct(final ConstructorInvocation constructorInvocation) throws Throwable
070            {
071                    final Constructor< ? > ctor = constructorInvocation.getConstructor();
072                    final Object[] args = constructorInvocation.getArguments();
073                    final Object target = constructorInvocation.getThis();
074    
075                    // pre conditions
076                    {
077                            guard.guardConstructorPre(target, ctor, args);
078                    }
079    
080                    final Object result = constructorInvocation.proceed();
081    
082                    // post conditions
083                    {
084                            guard.guardConstructorPost(target, ctor, args);
085                    }
086    
087                    return result;
088            }
089    
090            /**
091             * @return the guard
092             */
093            public Guard getGuard()
094            {
095                    return guard;
096            }
097    
098            /**
099             * {@inheritDoc}
100             */
101            public Object invoke(final MethodInvocation methodInvocation) throws Throwable
102            {
103                    return guard.guardMethod(methodInvocation.getThis(), methodInvocation.getMethod(), methodInvocation
104                                    .getArguments(), new MethodInvocable(methodInvocation));
105            }
106    
107            public void setGuard(final Guard guard)
108            {
109                    this.guard = guard;
110            }
111    }