View Javadoc

1   /*
2    * $Id: JndiContainerContext.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.impl.container;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.umo.lifecycle.InitialisationException;
15  import org.mule.umo.manager.ContainerException;
16  import org.mule.umo.manager.ObjectNotFoundException;
17  import org.mule.util.ObjectUtils;
18  
19  import java.io.Reader;
20  import java.util.Map;
21  
22  import javax.naming.Context;
23  import javax.naming.InitialContext;
24  import javax.naming.Name;
25  import javax.naming.NamingException;
26  
27  /**
28   * <code>JndiContainerContext</code> is a container implementaiton that exposes a
29   * jndi context. Whatever properties are set on the container in configuration will
30   * be passed to the initial context.
31   */
32  public class JndiContainerContext extends AbstractContainerContext
33  {
34      protected volatile Context context;
35      private volatile Map environment;
36  
37      public JndiContainerContext()
38      {
39          super("jndi");
40      }
41  
42      protected JndiContainerContext(String name)
43      {
44          super(name);
45      }
46  
47      public Map getEnvironment()
48      {
49          return environment;
50      }
51  
52      public void setEnvironment(Map environment)
53      {
54          this.environment = environment;
55      }
56  
57      public Context getContext()
58      {
59          return context;
60      }
61  
62      public void setContext(InitialContext context)
63      {
64          this.context = context;
65      }
66  
67      public Object getComponent(Object key) throws ObjectNotFoundException
68      {
69          try
70          {
71              if (key == null)
72              {
73                  throw new ObjectNotFoundException("null");
74              }
75              if (key instanceof Name)
76              {
77                  return context.lookup((Name) key);
78              }
79              else if (key instanceof Class)
80              {
81                  return context.lookup(((Class) key).getName());
82              }
83              else
84              {
85                  return context.lookup(key.toString());
86              }
87          }
88          catch (NamingException e)
89          {
90              throw new ObjectNotFoundException(ObjectUtils.toString(key, "null"), e);
91          }
92      }
93  
94      public void configure(Reader configuration) throws ContainerException
95      {
96          throw new UnsupportedOperationException("configure(Reader)");
97      }
98  
99      public void initialise() throws InitialisationException
100     {
101         try
102         {
103             if (context == null)
104             {
105                 context = JndiContextHelper.initialise(getEnvironment());
106             }
107         }
108         catch (NamingException e)
109         {
110             throw new InitialisationException(CoreMessages.failedToCreate("Jndi context"), e, this);
111         }
112     }
113 }