001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test.interceptors2;
023:
024: import javax.annotation.Resource;
025: import javax.ejb.EJBContext;
026: import javax.interceptor.InvocationContext;
027: import javax.ejb.PostActivate;
028: import javax.annotation.PostConstruct;
029: import javax.annotation.PreDestroy;
030: import javax.ejb.PrePassivate;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033:
034: /**
035: *
036: * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
037: * @version $Revision: 60233 $
038: */
039: public class XMLMethodInterceptor {
040: @Resource
041: EJBContext ejbCtx;
042:
043: public Object intercept(InvocationContext ctx) throws Exception {
044: System.out.println("XMLMethodInterceptor intercepting!");
045: StatusRemote status = findStatusRemote();
046: status.addInterception(new Interception(this , "intercept"));
047: return ctx.proceed();
048: }
049:
050: private StatusRemote findStatusRemote() {
051: try {
052: InitialContext ctx = new InitialContext();
053: StatusRemote status = (StatusRemote) ctx
054: .lookup("StatusBean/remote");
055: return status;
056: } catch (NamingException e) {
057: throw new RuntimeException(e);
058: }
059: }
060:
061: public void ignored(InvocationContext ctx) {
062: throw new RuntimeException("Should be ignored!!!");
063: }
064:
065: public void postConstruct(InvocationContext ctx) {
066: StatusRemote status = findStatusRemote();
067: status.addLifecycle(PostConstruct.class, new Interception(this ,
068: "postConstruct"));
069: try {
070: ctx.proceed();
071: } catch (Exception e) {
072: throw new RuntimeException(e);
073: }
074: }
075:
076: public void postActivate(InvocationContext ctx) {
077: StatusRemote status = findStatusRemote();
078: status.addLifecycle(PostActivate.class, new Interception(this ,
079: "postActivate"));
080: try {
081: ctx.proceed();
082: } catch (Exception e) {
083: throw new RuntimeException(e);
084: }
085: }
086:
087: public void prePassivate(InvocationContext ctx) {
088: StatusRemote status = findStatusRemote();
089: status.addLifecycle(PrePassivate.class, new Interception(this ,
090: "prePassivate"));
091: try {
092: ctx.proceed();
093: } catch (Exception e) {
094: throw new RuntimeException(e);
095: }
096: }
097:
098: public void preDestroy(InvocationContext ctx) {
099: StatusRemote status = findStatusRemote();
100: status.addLifecycle(PreDestroy.class, new Interception(this ,
101: "preDestroy"));
102: try {
103: ctx.proceed();
104: } catch (Exception e) {
105: throw new RuntimeException(e);
106: }
107: }
108:
109: }
|