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 as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.config.types;
031:
032: import com.caucho.config.types.BeanConfig;
033: import com.caucho.config.*;
034: import com.caucho.webbeans.cfg.*;
035: import com.caucho.webbeans.manager.*;
036: import com.caucho.util.L10N;
037:
038: /**
039: * Configures an interface type. Allows class and uri syntax
040: */
041: public class InterfaceConfig extends BeanConfig {
042: private static final L10N L = new L10N(InterfaceConfig.class);
043:
044: private boolean _isDeploy;
045: private boolean _isFactory = true;
046:
047: private String _tagName = "bean";
048:
049: private String _valueName;
050: private Object _value;
051:
052: public InterfaceConfig() {
053: }
054:
055: public InterfaceConfig(Class type) {
056: setBeanConfigClass(type);
057: }
058:
059: public InterfaceConfig(Class type, String tagName) {
060: setBeanConfigClass(type);
061: setTagName(tagName);
062: }
063:
064: protected String getDefaultScope() {
065: return null;
066: }
067:
068: /**
069: * Override the old meaning of type for backward compat.
070: */
071: @Override
072: public void setType(Class cl) {
073: setClass(cl);
074: }
075:
076: /**
077: * Check for correct type.
078: */
079: @Override
080: public void setClass(Class cl) {
081: super .setClass(cl);
082:
083: if (!getBeanConfigClass().isAssignableFrom(cl))
084: throw new ConfigException(L.l(
085: "instance class '{0}' must implement '{1}'", cl
086: .getName(), getBeanConfigClass().getName()));
087: }
088:
089: /**
090: * Sets the default deploy value
091: */
092: public void setDeploy(boolean isDeploy) {
093: _isDeploy = isDeploy;
094: if (_isDeploy)
095: setScope("singleton");
096: }
097:
098: /**
099: * Sets the default factory value
100: */
101: public void setFactory(boolean isFactory) {
102: _isFactory = isFactory;
103: }
104:
105: /**
106: * Sets the tag name.
107: */
108: public void setTagName(String tagName) {
109: _tagName = tagName;
110: }
111:
112: /**
113: * Sets the tag name.
114: */
115: @Override
116: public String getTagName() {
117: return _tagName;
118: }
119:
120: /**
121: * If the name is set, the bean will get deployed
122: */
123: @Override
124: public void setName(String name) {
125: super .setName(name);
126:
127: setDeploy(true);
128: }
129:
130: /**
131: * If the name is set, the bean will get deployed
132: */
133: @Override
134: public void setJndiName(String name) {
135: super .setJndiName(name);
136:
137: setDeploy(true);
138: }
139:
140: /**
141: * Sets the value for old-style jndi lookup
142: */
143: public void setValue(String value) {
144: _valueName = value;
145: }
146:
147: /**
148: * Override init to handle value
149: */
150: @Override
151: public void init() {
152: if (_valueName != null) {
153: WebBeansContainer webBeans = WebBeansContainer.create();
154:
155: _comp = webBeans.findByName(_valueName);
156:
157: if (_comp == null)
158: throw new ConfigException(L.l(
159: "'{0}' is an unknown bean", _valueName));
160: } else
161: super .init();
162: }
163:
164: @Override
165: public void deploy() {
166: if (_isDeploy)
167: super .deploy();
168: }
169:
170: /**
171: * Returns the configured object for configuration
172: */
173: public Object replaceObject() {
174: if (_isFactory)
175: return getObject();
176: else
177: return this ;
178: }
179:
180: public String toString() {
181: return getClass().getSimpleName() + "["
182: + getBeanConfigClass().getName() + "]";
183: }
184: }
|