Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.spring.deployers;
 
 
 
 import java.util.List;

Deploys SpringMetaData.

Parameters:
<T> exact bean factory type
Author(s):
Ales Justin
See also:
ApplicationContextDeployer
BeanFactoryDeployer
 
 
     protected AbstractSpringMetaDataDeployer() {
     }

    
Create deployment visitor.

Returns:
the deployment visitor
 
     protected abstract DeploymentVisitor<SpringMetaDatacreateDeploymentVisitor();
 
     protected abstract class SpringDeploymentVisitor implements DeploymentVisitor<SpringMetaData> {
 
         private final String SUCCESSFUL_BINDINGS_KEY = AbstractSpringMetaDataDeployer.class.getName() + "_SUCCESSFUL_BINDINGS";
 
         public Class<SpringMetaDatagetVisitorType() {
             return SpringMetaData.class;
         }
 
         public void deploy(DeploymentUnit unitSpringMetaData springMetaDatathrows DeploymentException {
             ClassLoader classLoader = unit.getClassLoader();
             ClassLoader old = Thread.currentThread().getContextClassLoader();
             List<StringsuccessfulBindings = new ArrayList<String>();            
             try {
                 for (SpringContextDescriptor springContextDescriptor : springMetaData.getSpringContextDescriptors()) {
                     Thread.currentThread().setContextClassLoader(classLoader);
                     T beanFactory = doCreate(springContextDescriptor);
                     String name = ((NameablebeanFactory).getName();
                     springContextDescriptor.setName(name);
                     bindIfPossible(beanFactoryname);                    
                     successfulBindings.add(name);
                     if (.isTraceEnabled()) {
                         .trace("Bean factory [" + name + "] bound to local JNDI.");
                     }
                 }
             } 
             catch (DeploymentException e) {
                 // remove our bindings if deployment unsuccessful
                 for (String successfulBinding : successfulBindings) {
                     unbind(successfulBinding);                   
                 }     
                 successfulBindings.clear();
                 throw e;
             } finally {
                 Thread.currentThread().setContextClassLoader(old);
                 unit.addAttachment(successfulBindings);
             }
         }

        
Do create BeanFactory instance.

Parameters:
metaData the spring meta data
Returns:
new bean factory instance
        protected abstract T doCreate(SpringContextDescriptor metaData);
        public void undeploy(DeploymentUnit unitSpringMetaData springMetaData) {
            List<StringownBindings = unit.getAttachment(List.class);
            for (SpringContextDescriptor springContextDescriptor : springMetaData.getSpringContextDescriptors()) {
                String name = springContextDescriptor.getName();
                try {
                    if (ownBindings.contains(name)) {
                        T beanFactory = lookup(name);
                        if (beanFactory != null) {
                            doClose(beanFactory);
                            unbind(name);
                            if (.isTraceEnabled()) {
                                .trace("Bean factory [" + name + "] unbinded from local JNDI.");
                            }
                        }
                    }
                } catch (Exception e) {
                    if (.isTraceEnabled()) {
                        .trace("Exception finding BeanFactory instance named " + namee);
                    }
                }
            }
        }

        
Do close bean factory.

Parameters:
beanFactory the bean factory to close
        protected abstract void doClose(T beanFactory);
    }

    
Bind factory to non-serializable JNDI context.

Parameters:
beanFactory the bean factory
name the jndi name
Throws:
org.jboss.deployers.spi.DeploymentException for any error
    protected void bindIfPossible(T beanFactoryString namethrows DeploymentException {
        InitialContext ctx = null;
        try {
            ctx = new InitialContext();
            Object existingObject = NonSerializableFactory.lookup(name);
            if (existingObject != null) {
                throw new DeploymentException("Unable to bind BeanFactory into JNDI as "  + name + " : binding already"
                + " exists");
            }
            NonSerializableFactory.rebind(ctxnamebeanFactory);
        } catch (NamingException e) {
            throw new DeploymentException("Unable to bind BeanFactory into JNDI"e);
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Throwable ignored) {
                }
            }
        }
    }

    
Unind factory from non-serializable JNDI context.

Parameters:
name the jndi name
    protected void unbind(String name) {
        InitialContext ctx = null;
        try {
            ctx = new InitialContext();
            ctx.unbind(name);
            NonSerializableFactory.unbind(name);
        } catch (NamingException e) {
            .warn("Unable to unbind BeanFactory from JNDI"e);
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Throwable ignored) {
                }
            }
        }
    }

    
Do a jndi lookup for bean factory.

Parameters:
name the jndi name
Returns:
bean factory instance
Throws:
java.lang.Exception for any exception
    protected T lookup(String namethrows Exception {
        Class<T> beanFactoryClass = getExactBeanFactoryClass();
        T beanFactory = beanFactoryClass.cast(Util.lookup(namebeanFactoryClass));
        if (.isTraceEnabled()) {
            .trace("Found Spring bean factory [" + name + "]: " + beanFactory);
        }
        return beanFactory;
    }

    
Exact bean factory class.

Returns:
the bean factory class
    protected abstract Class<T> getExactBeanFactoryClass();
New to GrepCode? Check out our FAQ X