001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal;
020:
021: import org.apache.beehive.netui.pageflow.DefaultServletContainerAdapter;
022: import org.apache.beehive.netui.pageflow.ServletContainerAdapter;
023: import org.apache.beehive.netui.pageflow.adapter.Adapter;
024: import org.apache.beehive.netui.pageflow.adapter.AdapterContext;
025: import org.apache.beehive.netui.util.internal.DiscoveryUtils;
026: import org.apache.beehive.netui.util.logging.Logger;
027:
028: import javax.servlet.ServletContext;
029:
030: /**
031: * @exclude
032: */
033: public class AdapterManager {
034: private static final Logger _log = Logger
035: .getInstance(AdapterManager.class);
036:
037: private static final String SERVLET_CONTAINER_ADAPTER_ATTR = InternalConstants.ATTR_PREFIX
038: + "servletAdapter";
039: private static final String SERVLET_CONTAINER_ADAPTER_PROP = "beehive.servletcontaineradapter";
040:
041: public static ServletContainerAdapter getServletContainerAdapter(
042: ServletContext servletContext) {
043: ServletContainerAdapter adapter = (ServletContainerAdapter) servletContext
044: .getAttribute(SERVLET_CONTAINER_ADAPTER_ATTR);
045:
046: if (adapter == null) {
047: if (_log.isErrorEnabled()) {
048: _log
049: .error("ServletContainerAdapter manager not initialized correctly.");
050: }
051:
052: //
053: // We can initialize it now, but it's not good because many requests could conceivably be in this
054: // code at the same time.
055: //
056: return initServletContext(servletContext);
057: }
058:
059: return adapter;
060: }
061:
062: public static ServletContainerAdapter initServletContext(
063: ServletContext servletContext) {
064: ServletContainerAdapter servletContainerAdapter = createServletContainerAdapter(servletContext);
065: servletContext.setAttribute(SERVLET_CONTAINER_ADAPTER_ATTR,
066: servletContainerAdapter);
067: return servletContainerAdapter;
068: }
069:
070: // TODO: this method could move to a more general place.
071: private static Adapter tryAdapter(Class adapterClass,
072: Object externalContext) {
073: try {
074: Adapter sa = (Adapter) adapterClass.newInstance();
075:
076: try {
077: AdapterContext context = new AdapterContext(
078: externalContext);
079:
080: if (sa.accept(context)) {
081: _log.info("Adapter " + adapterClass.getName()
082: + " accepted.");
083: sa.setContext(context);
084: return sa;
085: } else {
086: _log.info("Adapter " + adapterClass.getName()
087: + " is present but did not accept.");
088: }
089: } catch (Exception e) {
090: _log.error(adapterClass.getName()
091: + ".accept() threw an exception.", e);
092: } catch (LinkageError e) {
093: _log
094: .error(
095: adapterClass.getName()
096: + ".accept() caused a linkage error and may be out of date.",
097: e);
098: }
099: } catch (InstantiationException e) {
100: _log.error("Could not create instance of Adapter class "
101: + adapterClass.getName(), e);
102: } catch (IllegalAccessException e) {
103: _log.error("Could not create instance of Adapter class "
104: + adapterClass.getName(), e);
105: } catch (Exception e) {
106: _log.error("Error creating instance of Adapter class "
107: + adapterClass.getName(), e);
108: }
109:
110: return null;
111: }
112:
113: private static ServletContainerAdapter createServletContainerAdapter(
114: ServletContext servletContext) {
115: String adapterClassName = System
116: .getProperty(SERVLET_CONTAINER_ADAPTER_PROP);
117:
118: if (adapterClassName != null) {
119: Class adapterClass = DiscoveryUtils.loadImplementorClass(
120: adapterClassName, ServletContainerAdapter.class);
121:
122: if (adapterClass != null) {
123: ServletContainerAdapter sa = (ServletContainerAdapter) tryAdapter(
124: adapterClass, servletContext);
125: if (sa != null)
126: return sa;
127: }
128: }
129:
130: Class[] classes = DiscoveryUtils
131: .getImplementorClasses(ServletContainerAdapter.class);
132:
133: for (int i = 0; i < classes.length; i++) {
134: ServletContainerAdapter sa = (ServletContainerAdapter) tryAdapter(
135: classes[i], servletContext);
136: if (sa != null)
137: return sa;
138: }
139:
140: _log
141: .info("No ServletContainerAdapter specified or discovered; using "
142: + DefaultServletContainerAdapter.class);
143: ServletContainerAdapter sa = new DefaultServletContainerAdapter() {
144: public boolean accept(AdapterContext context) {
145: return true;
146: }
147: };
148: sa.setContext(new AdapterContext(servletContext));
149: return sa;
150: }
151: }
|