001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.cfg;
030:
031: import com.caucho.config.program.ConfigProgram;
032: import java.util.*;
033: import java.lang.reflect.*;
034:
035: import javax.annotation.*;
036: import javax.el.*;
037:
038: import javax.faces.*;
039: import javax.faces.application.*;
040: import javax.faces.component.*;
041: import javax.faces.component.html.*;
042: import javax.faces.context.*;
043: import javax.faces.convert.*;
044: import javax.faces.el.*;
045: import javax.faces.event.*;
046: import javax.faces.lifecycle.*;
047: import javax.faces.render.*;
048: import javax.faces.validator.*;
049:
050: import javax.xml.bind.annotation.*;
051:
052: import com.caucho.config.*;
053: import com.caucho.util.*;
054:
055: public class FactoryConfig {
056: private static final L10N L = new L10N(FactoryConfig.class);
057:
058: private String _id;
059:
060: private Class _applicationFactory;
061:
062: private Class _facesContextFactory;
063:
064: private Class _lifecycleFactory;
065:
066: private Class _renderKitFactory;
067:
068: public void setId(String id) {
069: _id = id;
070: }
071:
072: public void setApplicationFactory(Class factory)
073: throws ConfigException {
074: if (!ApplicationFactory.class.isAssignableFrom(factory))
075: throw new ConfigException(
076: L
077: .l(
078: "application-factory '{0}' class must extend ApplicationFactory.",
079: factory.getName()));
080:
081: if (!hasConstructor(factory, ApplicationFactory.class))
082: throw new ConfigException(
083: L
084: .l(
085: "application-factory class '{0}' must either have an (ApplicationFactory) constructor or a null-arg constructor.",
086: factory.getName()));
087:
088: _applicationFactory = factory;
089: }
090:
091: private Class getApplicationFactory() throws ConfigException {
092: return _applicationFactory;
093: }
094:
095: public void setFacesContextFactory(Class factory)
096: throws ConfigException {
097: if (!FacesContextFactory.class.isAssignableFrom(factory))
098: throw new ConfigException(
099: L
100: .l(
101: "faces-context-factory '{0}' class must extend FacesContextFactory.",
102: factory.getName()));
103:
104: if (!hasConstructor(factory, FacesContextFactory.class))
105: throw new ConfigException(
106: L
107: .l(
108: "faces-context-factory class '{0}' must either have an (FacesContextFactory) constructor or a null-arg constructor.",
109: factory.getName()));
110:
111: _facesContextFactory = factory;
112: }
113:
114: private Class getFacesContextFactory() throws ConfigException {
115: return _facesContextFactory;
116: }
117:
118: public void setLifecycleFactory(Class factory)
119: throws ConfigException {
120: if (!LifecycleFactory.class.isAssignableFrom(factory))
121: throw new ConfigException(
122: L
123: .l(
124: "lifecycle-factory '{0}' class must extend LifecycleFactory.",
125: factory.getName()));
126:
127: if (!hasConstructor(factory, LifecycleFactory.class))
128: throw new ConfigException(
129: L
130: .l(
131: "lifecycle-factory class '{0}' must either have an (LifecycleFactory) constructor or a null-arg constructor.",
132: factory.getName()));
133:
134: _lifecycleFactory = factory;
135: }
136:
137: private Class getLifecycleFactory() throws ConfigException {
138: return _lifecycleFactory;
139: }
140:
141: public void setRenderKitFactory(Class factory)
142: throws ConfigException {
143: if (!RenderKitFactory.class.isAssignableFrom(factory))
144: throw new ConfigException(
145: L
146: .l(
147: "render-kit-factory '{0}' class must extend RenderKitFactory.",
148: factory.getName()));
149:
150: if (!hasConstructor(factory, RenderKitFactory.class))
151: throw new ConfigException(
152: L
153: .l(
154: "render-kit-factory class '{0}' must either have an (RenderKitFactory) constructor or a null-arg constructor.",
155: factory.getName()));
156:
157: _renderKitFactory = factory;
158: }
159:
160: private Class getRenderKitFactory() throws ConfigException {
161: return _renderKitFactory;
162: }
163:
164: public void setFactoryExtension(ConfigProgram program)
165: throws ConfigException {
166: }
167:
168: void init() {
169: try {
170: if (_applicationFactory != null) {
171: FactoryFinder.setFactory(
172: FactoryFinder.APPLICATION_FACTORY,
173: _applicationFactory.getName());
174:
175: }
176:
177: if (_facesContextFactory != null) {
178: FactoryFinder.setFactory(
179: FactoryFinder.FACES_CONTEXT_FACTORY,
180: _facesContextFactory.getName());
181:
182: }
183:
184: if (_lifecycleFactory != null) {
185: FactoryFinder.setFactory(
186: FactoryFinder.LIFECYCLE_FACTORY,
187: _lifecycleFactory.getName());
188:
189: }
190:
191: if (_renderKitFactory != null) {
192: FactoryFinder.setFactory(
193: FactoryFinder.RENDER_KIT_FACTORY,
194: _renderKitFactory.getName());
195:
196: }
197: } catch (RuntimeException e) {
198: throw e;
199: } catch (Exception e) {
200: throw ConfigException.create(e);
201: }
202: }
203:
204: private boolean hasConstructor(Class factoryClass, Class api) {
205: try {
206: Constructor ctor = factoryClass.getConstructor(api);
207:
208: if (ctor != null)
209: return true;
210: } catch (NoSuchMethodException e) {
211: }
212:
213: try {
214: Constructor ctor = factoryClass.getConstructor();
215:
216: if (ctor != null)
217: return true;
218: } catch (NoSuchMethodException e) {
219: }
220:
221: return false;
222: }
223: }
|