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 Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.cfg;
031:
032: import java.util.*;
033: import java.util.logging.*;
034:
035: import javax.annotation.*;
036: import javax.jms.*;
037: import javax.resource.spi.*;
038:
039: import com.caucho.config.*;
040: import com.caucho.config.types.*;
041: import com.caucho.ejb.manager.*;
042: import com.caucho.webbeans.cfg.AbstractBeanConfig;
043: import com.caucho.webbeans.component.ComponentImpl;
044: import com.caucho.webbeans.manager.WebBeansContainer;
045:
046: import com.caucho.util.*;
047:
048: /**
049: * ejb-message-bean configuration
050: */
051: public class MessageBeanConfig extends AbstractBeanConfig {
052: private static final L10N L = new L10N(MessageBeanConfig.class);
053: private static final Logger log = Logger
054: .getLogger(MessageBeanConfig.class.getName());
055:
056: private ActivationSpec _activationSpec;
057:
058: private Class _destinationType;
059: private String _destinationName;
060: private Object _destination;
061: private int _messageConsumerMax;
062:
063: /**
064: * Sets the activation spec
065: */
066: public void setActivationSpec(ActivationSpec spec) {
067: _activationSpec = spec;
068: }
069:
070: public void setDestinationType(Class type) {
071: _destinationType = type;
072: }
073:
074: public void setDestinationName(String name) {
075: _destinationName = name;
076: }
077:
078: public void setDestination(Object destination) {
079: _destination = destination;
080:
081: if (destination == null)
082: throw new NullPointerException();
083: }
084:
085: public void setMessageConsumerMax(int messageConsumerMax) {
086: _messageConsumerMax = messageConsumerMax;
087: }
088:
089: @PostConstruct
090: public void init() {
091: if (getInstanceClass() == null)
092: throw new ConfigException(L
093: .l("ejb-message-bean requires a 'class' attribute"));
094:
095: EjbContainer ejbContainer = EjbContainer.create();
096: EjbConfigManager configManager = ejbContainer
097: .getConfigManager();
098:
099: EjbMessageBean bean = new EjbMessageBean(configManager,
100: "config");
101: bean.setConfigLocation(getFilename(), getLine());
102:
103: bean.setEJBClass(getInstanceClass());
104:
105: String name = getName();
106:
107: if (name == null)
108: name = getJndiName();
109:
110: if (name == null)
111: name = getInstanceClass().getSimpleName();
112:
113: bean.setEJBName(name);
114:
115: if (getInit() != null)
116: bean.setInit(getInit());
117:
118: String loc = getInstanceClass().getName() + ": ";
119: WebBeansContainer webBeans = WebBeansContainer.create();
120:
121: if (_destination != null) {
122: bean.setDestinationValue((Destination) _destination);
123: } else if (_activationSpec != null) {
124: bean.setActivationSpec(_activationSpec);
125: } else {
126: ComponentImpl destComp;
127:
128: Class destinationType = _destinationType;
129:
130: if (_destinationType == null)
131: destinationType = Destination.class;
132:
133: if (_destinationName != null)
134: destComp = webBeans.bind(loc, destinationType,
135: _destinationName);
136: else
137: destComp = webBeans.bind(loc, destinationType);
138:
139: if (destComp == null)
140: throw new ConfigException(L.l(
141: "'{0}' is an unknown destination type '{1}'",
142: _destinationName, _destinationType.getName()));
143:
144: bean.setDestinationValue((Destination) destComp.get());
145:
146: bean.setMessageConsumerMax(_messageConsumerMax);
147:
148: ComponentImpl comp = webBeans.bind(loc,
149: ConnectionFactory.class);
150:
151: if (comp == null)
152: throw new ConfigException(
153: L
154: .l("ejb-message-bean requires a configured JMS ConnectionFactory"));
155: bean.setConnectionFactoryValue((ConnectionFactory) comp
156: .get());
157: }
158:
159: bean.init();
160:
161: configManager.setBeanConfig(name, bean);
162:
163: // XXX: timing?
164: // configManager.start();
165: }
166: }
|