001: package org.caramba.config;
002:
003: import org.apache.commons.lang.StringUtils;
004: import org.caramba.CarambaContextFactory;
005:
006: import javax.servlet.ServletConfig;
007: import javax.servlet.ServletException;
008: import java.io.IOException;
009: import java.io.InputStream;
010:
011: /**
012: * This configFactory sets the PageFactory and the PanelFactory to
013: *
014: * @depricetad use the {@link EnableImplicitComponentRegistrationPlugIn} instead
015: */
016: public class AnnotationCarambaConfigFactory extends
017: DefaultCarambaConfigFactory {
018:
019: public CarambaConfig createCarambaConfig(
020: ServletConfig pServletConfig) throws ServletException {
021: CarambaConfig carambaConfig;
022: carambaConfig = new CarambaConfig();
023: String contextFactory = pServletConfig
024: .getInitParameter("contextFactory");
025: if (StringUtils.isNotEmpty(contextFactory)) {
026: try {
027: Class contextFactoryclass = Class
028: .forName(contextFactory);
029: Object contextFactoryInstance = contextFactoryclass
030: .newInstance();
031: if (contextFactoryInstance instanceof CarambaContextFactory) {
032: carambaConfig
033: .setContextFactory((CarambaContextFactory) contextFactoryInstance);
034: } else {
035: throw new ServletException(
036: "Specified CarambaConfigFactory is not an instance of "
037: + CarambaContextFactory.class
038: .getName());
039: }
040: } catch (ClassNotFoundException e) {
041: throw new ServletException(
042: "Cannot find specified contextFactory class "
043: + contextFactory, e);
044: } catch (IllegalAccessException e) {
045: throw new ServletException(
046: "Cannot instantiate specified contextFactory class "
047: + contextFactory, e);
048: } catch (InstantiationException e) {
049: throw new ServletException(
050: "Cannot instantiate specified contextFactory class "
051: + contextFactory, e);
052: }
053: }
054:
055: String configFileNames = pServletConfig
056: .getInitParameter("config");
057: String[] configFiles;
058: if (StringUtils.isEmpty(configFileNames)) {
059: configFiles = new String[] { "WEB-INF/caramba-config.xml" };
060: } else {
061: configFiles = configFileNames.split("[,]");
062: }
063:
064: for (int i = 0; i < configFiles.length; i++) {
065: String configFile = configFiles[i].trim();
066: InputStream configStream = pServletConfig
067: .getServletContext()
068: .getResourceAsStream(configFile);
069: if (configStream == null) {
070: throw new ServletException(
071: "Could not find config file: " + configFile);
072: }
073: try {
074: carambaConfig.addConfig(load(configStream));
075: } catch (ConfigLoaderException e) {
076: throw new ServletException(
077: "Could not parse config file: " + configFile, e);
078: } finally {
079: try {
080: configStream.close();
081: } catch (IOException e) {
082: throw new ServletException(
083: "Something went wrong while closing the stream of configfile "
084: + configFile, e);
085: }
086: }
087: }
088:
089: // PageConfig[] pageConfigs = carambaConfig.getPageConfigs();
090: // for (int i = 0; i < pageConfigs.length; i++) {
091: // PageConfig pageConfig = pageConfigs[i];
092: // pageConfig.addPageLifeCycleListener(/*listener that does the implicitComponentRegistration*/);
093: // }
094:
095: carambaConfig.setPageFactory(new AnnotationPageFactory());
096: carambaConfig.setPanelFactory(new AnnotationPanelFactory());
097:
098: return carambaConfig;
099: }
100: }
|