Listing 1

    public void init(DeploymentInfo di) throws DeploymentException {
        try {
            if (di.watch == null) {
                // resolve the watch
                if (di.url.getProtocol().equals("file")) {
                    File file = new File(di.url.getFile());
                    // If not directory we watch the package
                    if (!file.isDirectory()) {
                        di.watch = di.url;
                    }
                    // If directory we watch the xml files
                    else {
                        di.watch = new URL(di.url, "META-INF/jboss-spring.xml");
                    }
                } else {
                    // We watch the top only, no directory support
                    di.watch = di.url;
                }
            }
        } catch (Exception e) {
            log.error("failed to parse Spring context document: ", e);
            throw new DeploymentException(e);
        }
        super.init(di);
    }
 
    private URL getDocUrl(DeploymentInfo di, URLClassLoader localCL)
	throws DeploymentException {
        URL docURL = di.localUrl;
        if (!di.isXML) {
            docURL = localCL.findResource("META-INF/jboss-spring.xml");
        }
        // Validate that the descriptor was found
        if (docURL == null) {
            throw new DeploymentException("Failed to find META-INF/jboss-spring.xml");
        }
        return docURL;
    }


Listing 2

    public void create(DeploymentInfo di) throws DeploymentException {
        try {
            beanFactoryLoader.create(di);
            Notification msg = new Notification("Spring Deploy", this,
            getNextNotificationSequenceNumber());
            sendNotification(msg);
            log.info("Deployed Spring: " + di.url);
        } catch (Exception e) {
            throw new DeploymentException(e);
        }
    }
 
 
    public void create(DeploymentInfo di) throws DeploymentException {
        URL docURL = getDocUrl(di);
        String defaultName = getDefaultName(di);
        BeanFactory beanFactory = null;
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(di.ucl);
            beanFactory = createBeanFactory(defaultName, new UrlResource(docURL));
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
        String name = ((Nameable)beanFactory).getName();
        bind(beanFactory, name);
        log.info("Bean factory [" + name + "] binded to local JNDI.");
        beanFactoryNamesMap.put(docURL.toString(), name);
    }


Listing 3

<beans>
        <description>BeanFactory=(myBeanFactory)
ParentBeanFactory=(parentBeanFactory)</description>
...
 
    protected void preProcessXml(Element root) throws BeanDefinitionStoreException {
        NodeList nl = root.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element) {
                Element ele = (Element) node;
                if (DESCRIPTION_ELEMENT.equals(node.getNodeName())) {
                    String nodeValue = ele.getFirstChild().getNodeValue();
                    log.info("Bean names [description tag]: " + nodeValue);
                    Matcher bfm = parse(nodeValue, BEAN_FACTORY_ELEMENT);
                    if (bfm.find()) {
                        beanFactoryName = bfm.group(1);
                    }
                    Matcher pbfm = parse(nodeValue, PARENT_BEAN_FACTORY_ELEMENT);
                    if (pbfm.find()) {
                        String parentBeanFactoryName = pbfm.group(1);
                        try {

beanFactory.setParentBeanFactory(BeanFactoryLoaderImpl.
lookup(parentBeanFactoryName));
                        } catch (Exception e) {
throw new BeanDefinitionStoreException
("Failure during parent bean factory JNDI lookup: " +
parentBeanFactoryName, e);
                        }
                    }
		}
            }
        }
    }


Listing 4

public void stop(DeploymentInfo di) throws DeploymentException {
        try {
            URL docURL = getDocUrl(di);
            String name = (String) beanFactoryNamesMap.remove(docURL.toString());
            BeanFactory beanFactory = lookup(name);
            doClose(beanFactory);
            unbind(name);
            log.info("Bean factory [" + name + "] unbinded from local JNDI.");
        } catch (Exception e) {
            throw new DeploymentException(e);
        }
    }


Listing 5

public class SpringInjectionInterceptor extends SpringInjectionSupport
   implements Interceptor {
 
   public String getName() {
      return "SpringInjectionInterceptor";
   }
 
   public Object invoke(Invocation invocation) throws Throwable {
      if (!(invocation instanceof ConstructorInvocation)) {
         throw new IllegalArgumentException(
                  "This interceptor is meant to be applied" +
                  " only on new instantiation of EJB objects");
      }
      Object target = invocation.invokeNext();
      inject(target);
      return target;
   }
}