/*
 * JBoss, Home of Professional Open Source
 * Copyright (c) 2010, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.ejb3.context;

import javax.ejb.EJBLocalObject;
import javax.ejb.EJBObject;
import javax.ejb.SessionContext;
import javax.ejb.TimerService;
import javax.transaction.UserTransaction;
import javax.xml.rpc.handler.MessageContext;

import org.jboss.as.ee.component.ComponentView;
import org.jboss.as.ee.component.interceptors.DependencyInjectionCompleteMarker;
import org.jboss.as.ejb3.component.interceptors.CancellationFlag;
import org.jboss.as.ejb3.component.session.SessionBeanComponent;
import org.jboss.as.ejb3.component.session.SessionBeanComponentInstance;
import org.jboss.invocation.InterceptorContext;

/**
 * Implementation of the SessionContext interface.
 * <p/>
 *
 * @author <a href="cdewolf@redhat.com">Carlo de Wolf</a>
 */
public class SessionContextImpl extends EJBContextImpl implements SessionContext {

    private static final long serialVersionUID = 1L;

    public SessionContextImpl(SessionBeanComponentInstance instance) {
        super(instance);
    }

    public <T> T getBusinessObject(Class<T> businessInterface) throws IllegalStateException {
        // to allow override per invocation
        final InterceptorContext invocation = CurrentInvocationContext.get();
        return getComponent().getBusinessObject(businessInterface, invocation);
    }

    public EJBLocalObject getEJBLocalObject() throws IllegalStateException {
        // to allow override per invocation
        final InterceptorContext invocation = CurrentInvocationContext.get();
        return getComponent().getEJBLocalObject(invocation);
    }

    public EJBObject getEJBObject() throws IllegalStateException {
        // to allow override per invocation
        final InterceptorContext invocation = CurrentInvocationContext.get();
        return getComponent().getEJBObject(invocation);
    }

    public Class<?> getInvokedBusinessInterface() throws IllegalStateException {
        final InterceptorContext invocation = CurrentInvocationContext.get();
        final ComponentView view = invocation.getPrivateData(ComponentView.class);
        if(view.getViewClass().equals(getComponent().getEjbObjectType()) || view.getViewClass().equals(getComponent().getEjbLocalObjectType()) ) {
            throw new IllegalStateException("Cannot invoke getInvokedBusinessInterface when invocation occures through EjbObject or EJBLocalObject interfaces");
        }
        return view.getViewClass();
    }

    public SessionBeanComponent getComponent() {
        return (SessionBeanComponent) super.getComponent();
    }

    public MessageContext getMessageContext() throws IllegalStateException {
        final InterceptorContext invocation = CurrentInvocationContext.get();
        final MessageContext context =  invocation.getPrivateData(MessageContext.class);
        if(context == null) {
            throw new IllegalStateException("Cannot call getMessageContext(), no MessageContext is present for this invocation");

        }
        return context;
    }

    public boolean wasCancelCalled() throws IllegalStateException {
        final InterceptorContext invocation = CurrentInvocationContext.get();
        final CancellationFlag flag = invocation.getPrivateData(CancellationFlag.class);
        return flag.get();
    }

    @Override
    public TimerService getTimerService() throws IllegalStateException {
        final InterceptorContext invocation = CurrentInvocationContext.get();
        boolean lifecycleCallback = invocation.getMethod() == null;
        if (lifecycleCallback && !DependencyInjectionCompleteMarker.isDependencyInjectionComplete(invocation)) {
            throw new IllegalStateException("getTimerService() is not allowed while dependency injection is in progress");
        }
        return super.getTimerService();
    }

    @Override
    public UserTransaction getUserTransaction() throws IllegalStateException {
        final InterceptorContext invocation = CurrentInvocationContext.get();
        boolean lifecycleCallback = invocation.getMethod() == null;
        if (lifecycleCallback && !DependencyInjectionCompleteMarker.isDependencyInjectionComplete(invocation)) {
            throw new IllegalStateException("getTimerService() is not allowed while dependency injection is in progress");
        }
        return getComponent().getUserTransaction();
    }
}

@@ -1043,24 +1043,57 @@
 3.context;%0A%0A
+import java.security.Principal;%0A%0A
 import javax
@@ -5368,11 +5368,892 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public boolean isCallerInRole(final String roleName) %7B%0A        final InterceptorContext invocation = CurrentInvocationContext.get();%0A        final boolean lifecycleCallback = invocation.getMethod() == null;%0A        if (lifecycleCallback) %7B%0A            throw new IllegalStateException(%22isCallerInRole is not allowed in lifecycle methods of stateless session beans%22);%0A        %7D%0A        return super.isCallerInRole(roleName);%0A    %7D%0A%0A    @Override%0A    public Principal getCallerPrincipal() %7B%0A        final InterceptorContext invocation = CurrentInvocationContext.get();%0A        final boolean lifecycleCallback = invocation.getMethod() == null;%0A        if (lifecycleCallback) %7B%0A            throw new IllegalStateException(%22getCallerPrincipal is not allowed in lifecycle methods of stateless session beans%22);%0A        %7D%0A        return super.getCallerPrincipal();%0A    %7D
 %0A%7D%0A

Throw exception as required by the spec