KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > microcontainer > aspects > jndi > JndiIntroduction


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.aop.microcontainer.aspects.jndi;
23
24 import java.util.Properties JavaDoc;
25
26 import javax.naming.InitialContext JavaDoc;
27
28 import org.jboss.aop.advice.Interceptor;
29 import org.jboss.aop.joinpoint.Invocation;
30 import org.jboss.aop.joinpoint.MethodInvocation;
31 import org.jboss.kernel.spi.dependency.KernelControllerContext;
32 import org.jboss.logging.Logger;
33 import org.jboss.util.naming.Util;
34
35 /**
36  * A JNDI binding aspect that creates bindings on interception of the kernel setKernelControllerContext
37  * callback, and removes them on any other method. The expectation is that this is applied to the
38  * org.jboss.kernel.spi.dependency.KernelControllerContextAware interface so that unbinding occurs on
39  * the unsetKernelControllerContext method.
40  *
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 46386 $
43  */

44 public class JndiIntroduction implements Interceptor
45 {
46    private static final Logger log = Logger.getLogger(JndiIntroduction.class);
47    private Properties JavaDoc env;
48
49    public String JavaDoc getName()
50    {
51       return getClass().getName();
52    }
53  
54    /**
55     * Get the InitialContext properties to use for binding/unbinding
56     * @return the InitialContext ctor env
57     */

58    public Properties JavaDoc getEnv()
59    {
60       return env;
61    }
62
63    /**
64     * Set the InitialContext properties to use for binding/unbinding
65     * @param env - the InitialContext ctor env
66     */

67    public void setEnv(Properties JavaDoc env)
68    {
69       this.env = env;
70    }
71
72    /**
73     * Bind the target on setKernelControllerContext, unbind on any other method provided that
74     * the invocation has a JndiBinding annotation.
75     */

76    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
77    {
78       MethodInvocation mi = (MethodInvocation) invocation;
79       KernelControllerContext context = (KernelControllerContext) mi.getArguments()[0];
80
81       boolean trace = log.isTraceEnabled();
82       JndiBinding bindingInfo = (JndiBinding) invocation.resolveClassAnnotation(JndiBinding.class);
83       if( trace )
84          log.trace("Checking method: "+mi.getMethod()+", bindingInfo: "+bindingInfo);
85       // If this is the setKernelControllerContext callback, bind the target into jndi
86
if ("setKernelControllerContext".equals(mi.getMethod().getName()) && bindingInfo != null)
87       {
88          InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
89          Object JavaDoc target = context.getTarget();
90          Util.bind(ctx, bindingInfo.name(), target);
91          if( trace )
92             log.trace("Bound to: "+bindingInfo.name());
93          String JavaDoc[] aliases = bindingInfo.aliases();
94          if( aliases != null )
95          {
96             for(String JavaDoc name : aliases)
97             {
98                Util.bind(ctx, name, target);
99                if( trace )
100                   log.trace("Bound to alias: "+bindingInfo.name());
101             }
102          }
103       }
104       // If this is the unsetKernelControllerContext callback, unbind the target
105
else if( bindingInfo != null )
106       {
107          InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
108          Util.unbind(ctx, bindingInfo.name());
109          if( trace )
110             log.trace("Unbound: "+bindingInfo.name());
111          String JavaDoc[] aliases = bindingInfo.aliases();
112          if( aliases != null )
113          {
114             for(String JavaDoc name : aliases)
115             {
116                Util.unbind(ctx, name);
117                if( trace )
118                   log.trace("Unbound alias: "+bindingInfo.name());
119             }
120          }
121       }
122       else if ( trace )
123       {
124          log.trace("Ignoring null binding info");
125       }
126
127       return null;
128    }
129
130 }
131
Popular Tags