KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > AdapterManager


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
28
29
30 /**
31  * @exclude
32  */

33 public class AdapterManager
34 {
35     private static final Logger _log = Logger.getInstance( AdapterManager.class );
36     
37     private static final String JavaDoc SERVLET_CONTAINER_ADAPTER_ATTR = InternalConstants.ATTR_PREFIX + "servletAdapter";
38     private static final String JavaDoc SERVLET_CONTAINER_ADAPTER_PROP = "beehive.servletcontaineradapter";
39
40     public static ServletContainerAdapter getServletContainerAdapter( ServletContext JavaDoc 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             //
53
// We can initialize it now, but it's not good because many requests could conceivably be in this
54
// code at the same time.
55
//
56
return initServletContext( servletContext );
57         }
58         
59         return adapter;
60     }
61     
62     public static ServletContainerAdapter initServletContext( ServletContext JavaDoc servletContext )
63     {
64         ServletContainerAdapter servletContainerAdapter = createServletContainerAdapter( servletContext );
65         servletContext.setAttribute( SERVLET_CONTAINER_ADAPTER_ATTR, servletContainerAdapter );
66         return servletContainerAdapter;
67     }
68
69     // TODO: this method could move to a more general place.
70
private static Adapter tryAdapter( Class JavaDoc adapterClass, Object JavaDoc 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 JavaDoc e )
92             {
93                 _log.error( adapterClass.getName() + ".accept() threw an exception.", e );
94             }
95             catch ( LinkageError JavaDoc e )
96             {
97                 _log.error( adapterClass.getName() + ".accept() caused a linkage error and may be out of date.", e );
98             }
99         }
100         catch ( InstantiationException JavaDoc e )
101         {
102             _log.error( "Could not create instance of Adapter class " + adapterClass.getName(), e );
103         }
104         catch ( IllegalAccessException JavaDoc e )
105         {
106             _log.error( "Could not create instance of Adapter class " + adapterClass.getName(), e );
107         }
108         catch ( Exception JavaDoc e )
109         {
110             _log.error( "Error creating instance of Adapter class " + adapterClass.getName(), e );
111         }
112
113         return null;
114     }
115
116     /*
117     private static Class loadClass( ClassLoaders classLoaders, String className, Class spiClass )
118     {
119         for ( int i = 0; i < classLoaders.size(); ++i )
120         {
121             try
122             {
123                 return classLoaders.get( i ).loadClass( className );
124             }
125             catch ( ClassNotFoundException e )
126             {
127                 // ignore
128             }
129         }
130         
131         _log.error( "Could not load class " + className + " to implement " + spiClass.getName() );
132         return null;
133     }
134     */

135     
136     private static ServletContainerAdapter createServletContainerAdapter( ServletContext JavaDoc servletContext )
137     {
138         String JavaDoc adapterClassName = System.getProperty( SERVLET_CONTAINER_ADAPTER_PROP );
139
140         if ( adapterClassName != null )
141         {
142             Class JavaDoc 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         /*
154         ClassLoaders loaders = ClassLoaders.getAppLoaders( ServletContainerAdapter.class, AdapterManager.class, true );
155         DiscoverServiceNames dsn = new DiscoverServiceNames( loaders );
156         ResourceNameIterator i = dsn.findResourceNames( ServletContainerAdapter.class.getName() );
157         
158         while ( i.hasNext() )
159         {
160             Class adapterClass = loadClass( loaders, i.nextResourceName(), ServletContainerAdapter.class );
161             
162             if ( adapterClass != null )
163             {
164                 ServletContainerAdapter sa =
165                         ( ServletContainerAdapter ) tryAdapter( adapterClass, servletContext );
166                 if ( sa != null ) return sa;
167             }
168         }
169         
170         */

171         
172         Class JavaDoc[] 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