001: package org.caramba.config;
002:
003: import org.apache.commons.lang.StringUtils;
004: import org.apache.commons.logging.Log;
005: import org.apache.commons.logging.LogFactory;
006: import org.caramba.CarambaException;
007: import org.caramba.DestroyMode;
008: import org.caramba.components.Container;
009: import org.caramba.components.Page;
010: import org.caramba.components.Panel;
011:
012: import javax.servlet.ServletConfig;
013: import javax.servlet.ServletException;
014: import java.lang.reflect.Modifier;
015: import java.net.MalformedURLException;
016: import java.net.URL;
017: import java.util.Iterator;
018: import java.util.Set;
019:
020: /**
021: * @author Pieter Degraeuwe
022: * Date: Aug 28, 2006
023: * Time: 8:26:19 AM
024: */
025: public class AutoDiscoverCarambaConfigFactory extends
026: DefaultCarambaConfigFactory {
027: private static final transient Log log = LogFactory
028: .getLog(AutoDiscoverCarambaConfigFactory.class);
029:
030: private static final String INIT_PARAM_CHECK_LIBRARIES = "checkLibaries";
031:
032: public CarambaConfig createCarambaConfig(
033: ServletConfig pServletConfig) throws ServletException {
034: CarambaConfig config;
035: try {
036: config = super .createCarambaConfig(pServletConfig);
037: } catch (ServletException e) {
038: //ignore error. It's due the fact that no caramba-config.xml was found.
039: config = new CarambaConfig();
040: }
041: config
042: .addPlugIn(new EnableImplicitComponentRegistrationPlugIn());
043: String checkLibsParam = pServletConfig
044: .getInitParameter(INIT_PARAM_CHECK_LIBRARIES);
045: boolean checkLibs = false;
046: if (StringUtils.isNotEmpty(checkLibsParam)) {
047: try {
048: checkLibs = new Boolean(checkLibsParam);
049: } catch (Exception e) {
050: throw new ServletException(
051: INIT_PARAM_CHECK_LIBRARIES
052: + " init param contains invalid value. Must be 'true' or 'false' (default: false) ");
053: }
054: }
055:
056: AutoDiscoverCarambaConfigUtil<Page> pageClassDiscoverer = new AutoDiscoverCarambaConfigUtil<Page>();
057: pageClassDiscoverer.loadImplementationsFromServletContext(
058: Page.class, pServletConfig.getServletContext(),
059: checkLibs);
060: discoverPageClasses(pServletConfig, config, pageClassDiscoverer
061: .getClasses());
062:
063: AutoDiscoverCarambaConfigUtil<Panel> panelClassDiscoverer = new AutoDiscoverCarambaConfigUtil<Panel>();
064: panelClassDiscoverer.loadImplementationsFromServletContext(
065: Panel.class, pServletConfig.getServletContext(),
066: checkLibs);
067: discoverPanelClasses(pServletConfig, config,
068: panelClassDiscoverer.getClasses());
069: return config;
070: }
071:
072: private void discoverPageClasses(ServletConfig pServletConfig,
073: CarambaConfig pConfig, Set<Class<? extends Page>> pClasses) {
074: for (Iterator<Class<? extends Page>> iterator = pClasses
075: .iterator(); iterator.hasNext();) {
076: Class<? extends Page> aClass = iterator.next();
077: if (Modifier.isAbstract(aClass.getModifiers()))
078: continue;
079: org.caramba.annotations.PageConfig annotation = (org.caramba.annotations.PageConfig) aClass
080: .getAnnotation(org.caramba.annotations.PageConfig.class);
081: PageConfig pageConfig;
082: if (annotation != null) {
083: pageConfig = constructPageConfig(pConfig, annotation
084: .name(), annotation.resource(), aClass
085: .getName());
086: if (StringUtils.isNotEmpty(annotation.destroyMode())) {
087: DestroyMode dm = DestroyMode.getByName(annotation
088: .destroyMode());
089: if (dm == null)
090: throw new CarambaException(
091: "Unsupported DestroyMode on page "
092: + pageConfig.getName() + " : "
093: + annotation.destroyMode());
094: pageConfig.setDestroyMode(dm);
095: }
096: } else {
097: pageConfig = constructPageConfig(pConfig, null, null,
098: aClass.getName());
099: }
100:
101: //only autodiscover pages which are not configured manually
102: PageConfig existingPageConfig = pConfig
103: .getPageConfig(pageConfig.getName());
104: if (existingPageConfig == null) {
105: URL resource = null;
106: try {
107: resource = pServletConfig.getServletContext()
108: .getResource(pageConfig.getResource());
109: } catch (MalformedURLException e) {
110: throw new CarambaException(
111: "Marformed resource for page "
112: + pageConfig.getName());
113: }
114: if (resource != null) {
115: pConfig.addPageConfig(pageConfig);
116: } else {
117: log
118: .warn("PageConfig skipped since resource was not found: "
119: + pageConfig.getName());
120: }
121: } else {
122: log
123: .warn("AutoDiscovered PageConfig skipped since it was already defined in the caramba-config.xml: "
124: + pageConfig.getName());
125: }
126: }
127: }
128:
129: private void discoverPanelClasses(ServletConfig pServletConfig,
130: CarambaConfig pConfig, Set<Class<? extends Panel>> pClasses) {
131: for (Iterator<Class<? extends Panel>> iterator = pClasses
132: .iterator(); iterator.hasNext();) {
133: Class<? extends Container> aClass = iterator.next();
134: if (Modifier.isAbstract(aClass.getModifiers()))
135: continue;
136: org.caramba.annotations.PanelConfig annotation = (org.caramba.annotations.PanelConfig) aClass
137: .getAnnotation(org.caramba.annotations.PanelConfig.class);
138: PanelConfig panelConfig;
139: if (annotation != null) {
140: panelConfig = constructPanelConfig(pConfig, annotation
141: .name(), annotation.resource(), aClass
142: .getName());
143: } else {
144: panelConfig = constructPanelConfig(pConfig, null, null,
145: aClass.getName());
146: }
147:
148: //only autodiscover panels which are not configured manually
149: PanelConfig existingPanelConfig = pConfig
150: .getPanelConfig(panelConfig.getName());
151: if (existingPanelConfig == null) {
152: URL resource = null;
153: try {
154: resource = pServletConfig.getServletContext()
155: .getResource(panelConfig.getResource());
156: } catch (MalformedURLException e) {
157: throw new CarambaException(
158: "Marformed resource for panel "
159: + panelConfig.getName());
160: }
161: if (resource != null) {
162: pConfig.addPanelConfig(panelConfig);
163: } else {
164: log
165: .warn("PanelConfig skipped since resource was not found: "
166: + panelConfig.getName());
167: }
168: } else {
169: log
170: .warn("AutoDiscovered PanelConfig skipped since it was already defined in the caramba-config.xml: "
171: + panelConfig.getName());
172: }
173:
174: }
175:
176: }
177: }
|