Package org.apache.ws.util.jndi

Source Code of org.apache.ws.util.jndi.SpringJndiPopulator

/*=============================================================================*
*  Copyright 2004 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*=============================================================================*/
package org.apache.ws.util.jndi;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.resource.JndiConstants;
import org.apache.ws.resource.ResourceHome;
import org.apache.ws.util.spring.SpringBeanObjectFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameAlreadyBoundException;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;

/**
* Provides the ability to populate a JNDI context with the beans from a Spring listable bean factory.
*
* @author Ian Springer
*/
public abstract class SpringJndiPopulator
{
   private static Log LOG = LogFactory.getLog( SpringJndiPopulator.class.getName(  ) );

   /**
    * DOCUMENT_ME
    *
    * @param beanFactory DOCUMENT_ME
    */
   public static void populateJndi( ListableBeanFactory beanFactory )
   {
      Context  resourceContext = getResourceContext(  );
      String[] beanDefNames = beanFactory.getBeanNamesForType( ResourceHome.class );
      for ( int i = 0; i < beanDefNames.length; i++ )
      {
         String       beanDefName       = beanDefNames[i];
         ResourceHome resourceHome      = (ResourceHome) beanFactory.getBean( beanDefName );
         String       portComponentName = resourceHome.getPortComponentName(  );
         if ( portComponentName == null )
         {
            throw new IllegalStateException( "Bean definition for resource home "
                                             + resourceHome.getClass(  ).getName(  )
                                             + " does not initialize the 'portComponentName' property." );
         }

         LOG.info( "Binding " + resourceHome.getClass(  ).getName(  ) + " resource home instance to JNDI name '"
                   + JndiConstants.CONTEXT_NAME_RESOURCE + "/" + portComponentName + "'..." );
         Reference ref =
            new Reference( resourceHome.getClass(  ).getName(  ),
                           new StringRefAddr( SpringBeanObjectFactory.ADDR_TYPE_SPRING_BEAN_ID, beanDefName ),
                           SpringBeanObjectFactory.class.getName(  ), null );
         try
         {
            resourceContext.rebind( portComponentName, ref );
         }
         catch ( NamingException ne )
         {
            throw new RuntimeException( ne );
         }
      }
   }

   private static Context getInitialContext(  )
   {
      Context initialContext;
      try
      {
         initialContext = new InitialContext(  );
      }
      catch ( NamingException ne )
      {
         throw new RuntimeException( ne );
      }

      return initialContext;
   }

   private static Context getResourceContext(  )
   {
      Context wsrfContext = createContext( getInitialContext(  ),
                                           "wsrf" );
      return createContext( wsrfContext, "resource" );
   }

   private static Context createContext( Context targetContext,
                                         String  name )
   {
      try
      {
         Context context;
         try
         {
            context = targetContext.createSubcontext( name );
         }
         catch ( NameAlreadyBoundException nabe )
         {
            context = (Context) targetContext.lookup( name );
         }

         return context;
      }
      catch ( NamingException ne )
      {
         throw new RuntimeException( ne );
      }
   }
}
TOP

Related Classes of org.apache.ws.util.jndi.SpringJndiPopulator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.