1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import org.apache.beehive.netui.pageflow.DefaultServletContainerAdapter; 21 import org.apache.beehive.netui.pageflow.ServletContainerAdapter; 22 import org.apache.beehive.netui.pageflow.adapter.Adapter; 23 import org.apache.beehive.netui.pageflow.adapter.AdapterContext; 24 import org.apache.beehive.netui.util.internal.DiscoveryUtils; 25 import org.apache.beehive.netui.util.logging.Logger; 26 27 import javax.servlet.ServletContext ; 28 29 30 33 public class AdapterManager 34 { 35 private static final Logger _log = Logger.getInstance( AdapterManager.class ); 36 37 private static final String SERVLET_CONTAINER_ADAPTER_ATTR = InternalConstants.ATTR_PREFIX + "servletAdapter"; 38 private static final String SERVLET_CONTAINER_ADAPTER_PROP = "beehive.servletcontaineradapter"; 39 40 public static ServletContainerAdapter getServletContainerAdapter( ServletContext servletContext ) 41 { 42 ServletContainerAdapter adapter = 43 ( ServletContainerAdapter ) servletContext.getAttribute( SERVLET_CONTAINER_ADAPTER_ATTR ); 44 45 if ( adapter == null ) 46 { 47 if ( _log.isErrorEnabled() ) 48 { 49 _log.error( "ServletContainerAdapter manager not initialized correctly." ); 50 } 51 52 return initServletContext( servletContext ); 57 } 58 59 return adapter; 60 } 61 62 public static ServletContainerAdapter initServletContext( ServletContext servletContext ) 63 { 64 ServletContainerAdapter servletContainerAdapter = createServletContainerAdapter( servletContext ); 65 servletContext.setAttribute( SERVLET_CONTAINER_ADAPTER_ATTR, servletContainerAdapter ); 66 return servletContainerAdapter; 67 } 68 69 private static Adapter tryAdapter( Class adapterClass, Object externalContext ) 71 { 72 try 73 { 74 Adapter sa = ( Adapter ) adapterClass.newInstance(); 75 76 try 77 { 78 AdapterContext context = new AdapterContext( externalContext ); 79 80 if ( sa.accept( context ) ) 81 { 82 _log.info( "Adapter " + adapterClass.getName() + " accepted." ); 83 sa.setContext( context ); 84 return sa; 85 } 86 else 87 { 88 _log.info( "Adapter " + adapterClass.getName() + " is present but did not accept." ); 89 } 90 } 91 catch ( Exception e ) 92 { 93 _log.error( adapterClass.getName() + ".accept() threw an exception.", e ); 94 } 95 catch ( LinkageError e ) 96 { 97 _log.error( adapterClass.getName() + ".accept() caused a linkage error and may be out of date.", e ); 98 } 99 } 100 catch ( InstantiationException e ) 101 { 102 _log.error( "Could not create instance of Adapter class " + adapterClass.getName(), e ); 103 } 104 catch ( IllegalAccessException e ) 105 { 106 _log.error( "Could not create instance of Adapter class " + adapterClass.getName(), e ); 107 } 108 catch ( Exception e ) 109 { 110 _log.error( "Error creating instance of Adapter class " + adapterClass.getName(), e ); 111 } 112 113 return null; 114 } 115 116 135 136 private static ServletContainerAdapter createServletContainerAdapter( ServletContext servletContext ) 137 { 138 String adapterClassName = System.getProperty( SERVLET_CONTAINER_ADAPTER_PROP ); 139 140 if ( adapterClassName != null ) 141 { 142 Class adapterClass = 143 DiscoveryUtils.loadImplementorClass( adapterClassName, ServletContainerAdapter.class ); 144 145 if ( adapterClass != null ) 146 { 147 ServletContainerAdapter sa = 148 ( ServletContainerAdapter ) tryAdapter( adapterClass, servletContext ); 149 if ( sa != null ) return sa; 150 } 151 } 152 153 171 172 Class [] classes = DiscoveryUtils.getImplementorClasses( ServletContainerAdapter.class ); 173 174 for ( int i = 0; i < classes.length; i++ ) 175 { 176 ServletContainerAdapter sa = ( ServletContainerAdapter ) tryAdapter( classes[i], servletContext ); 177 if ( sa != null ) return sa; 178 } 179 180 _log.info( "No ServletContainerAdapter specified or discovered; using " + DefaultServletContainerAdapter.class ); 181 ServletContainerAdapter sa = 182 new DefaultServletContainerAdapter() 183 { 184 public boolean accept( AdapterContext context ) 185 { 186 return true; 187 } 188 }; 189 sa.setContext( new AdapterContext( servletContext ) ); 190 return sa; 191 } 192 } 193 | Popular Tags |