View Javadoc

1   /*
2    * $Id: EjbContainerContext.java 7963 2007-08-21 08:53:15Z 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.providers.ejb.i18n.EjbMessages;
14  import org.mule.umo.manager.ObjectNotFoundException;
15  import org.mule.util.ClassUtils;
16  
17  import java.lang.reflect.Method;
18  
19  import javax.ejb.EJBHome;
20  import javax.naming.NamingException;
21  
22  /**
23   * <code>EjbContainerContext</code> is a container implementaiton that allows EJB
24   * Session beans to be referenced as Mule managed UMOs
25   */
26  public class EjbContainerContext extends RmiContainerContext
27  {
28      public EjbContainerContext()
29      {
30          super("ejb");
31      }
32  
33      protected EjbContainerContext(String name)
34      {
35          super(name);
36      }
37  
38      public Object getComponent(Object key) throws ObjectNotFoundException
39      {
40          Object homeObject = null;
41          if (key == null)
42          {
43              throw new ObjectNotFoundException("null");
44          }
45          try
46          {
47              homeObject = context.lookup(key.toString());
48          }
49          catch (NamingException e)
50          {
51              throw new ObjectNotFoundException(key.toString(), e);
52          }
53  
54          if (homeObject == null)
55          {
56              throw new ObjectNotFoundException(key.toString());
57          }
58          else if (homeObject instanceof EJBHome)
59          {
60  
61              Method method = ClassUtils.getMethod(homeObject.getClass(), "create", null);
62              if (method == null)
63              {
64                  throw new ObjectNotFoundException(key.toString(), new IllegalArgumentException(
65                      EjbMessages.ejbObjectMissingCreate(key).toString()));
66              }
67              try
68              {
69                  return method.invoke(homeObject, ClassUtils.NO_ARGS);
70              }
71              catch (Exception e)
72              {
73                  throw new ObjectNotFoundException(key.toString(), e);
74              }
75          }
76          else
77          {
78              throw new ObjectNotFoundException(key.toString(), new IllegalArgumentException(
79                  EjbMessages.ejbKeyRefNotValid(key).toString()));
80          }
81      }
82  }