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.interceptor.AroundInvoke;
025: import javax.interceptor.InvocationContext;
026: import javax.ejb.PostActivate;
027: import javax.annotation.PostConstruct;
028: import javax.annotation.PreDestroy;
029: import javax.ejb.PrePassivate;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032:
033: /**
034: *
035: * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
036: * @version $Revision: 60233 $
037: */
038: public class AnnotatedClassInterceptor extends
039: AnnotatedClassInterceptor2 {
040: @AroundInvoke
041: public Object intercept(InvocationContext inv) throws Exception {
042: System.out.println("AnnotatedClassInterceptor intercepting!");
043: InitialContext ctx = new InitialContext();
044: StatusRemote status = (StatusRemote) ctx
045: .lookup("StatusBean/remote");
046: status.addInterception(new Interception(this , "intercept",
047: instance));
048: return inv.proceed();
049: }
050:
051: @PostConstruct
052: public void postConstruct(InvocationContext inv) {
053: StatusRemote status = null;
054: status = findStatusRemote();
055: status.addLifecycle(PostConstruct.class, new Interception(this ,
056: "postConstruct", instance));
057: try {
058: inv.proceed();
059: } catch (Exception e) {
060: throw new RuntimeException(e);
061: }
062: }
063:
064: @PostActivate
065: public void postActivate(InvocationContext inv) {
066: StatusRemote status = null;
067: status = findStatusRemote();
068: status.addLifecycle(PostActivate.class, new Interception(this ,
069: "postActivate", instance));
070: try {
071: inv.proceed();
072: } catch (Exception e) {
073: throw new RuntimeException(e);
074: }
075: }
076:
077: private StatusRemote findStatusRemote() {
078: StatusRemote status;
079: try {
080: InitialContext ctx = new InitialContext();
081: status = (StatusRemote) ctx.lookup("StatusBean/remote");
082: } catch (NamingException e) {
083: throw new RuntimeException(e);
084: }
085: return status;
086: }
087:
088: @PrePassivate
089: public void prePassivate(InvocationContext ctx) {
090: StatusRemote status = findStatusRemote();
091: status.addLifecycle(PrePassivate.class, new Interception(this ,
092: "prePassivate", instance));
093: try {
094: ctx.proceed();
095: } catch (Exception e) {
096: throw new RuntimeException(e);
097: }
098: }
099:
100: @PreDestroy
101: public void preDestroy(InvocationContext ctx) {
102: StatusRemote status = findStatusRemote();
103: status.addLifecycle(PreDestroy.class, new Interception(this ,
104: "preDestroy", instance));
105: try {
106: ctx.proceed();
107: } catch (Exception e) {
108: throw new RuntimeException(e);
109: }
110: }
111:
112: public Object intercept2(InvocationContext ctx) throws Exception {
113: throw new RuntimeException("Should not be called");
114: }
115:
116: public void postConstruct2(InvocationContext ctx) {
117: throw new RuntimeException("Should not be called");
118: }
119:
120: public void postActivate2(InvocationContext ctx) {
121: throw new RuntimeException("Should not be called");
122: }
123:
124: public void prePassivate2(InvocationContext ctx) {
125: throw new RuntimeException("Should not be called");
126: }
127:
128: public void preDestroy2(InvocationContext ctx) {
129: throw new RuntimeException("Should not be called");
130: }
131:
132: }
|