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 Emil Ong
028: */
029:
030: package com.caucho.mule;
031:
032: import java.io.Reader;
033: import java.util.WeakHashMap;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036: import javax.webbeans.ComponentFactory;
037:
038: import com.caucho.util.L10N;
039: import com.caucho.webbeans.manager.*;
040:
041: import org.mule.impl.container.ContainerKeyPair;
042: import org.mule.umo.lifecycle.Initialisable;
043: import org.mule.umo.lifecycle.InitialisationException;
044: import org.mule.umo.manager.ContainerException;
045: import org.mule.umo.manager.ObjectNotFoundException;
046: import org.mule.umo.manager.UMOContainerContext;
047:
048: public class ResinContainerContext implements UMOContainerContext {
049: private static final L10N L = new L10N(ResinContainerContext.class);
050: private static final Logger log = Logger
051: .getLogger(ResinContainerContext.class.getName());
052:
053: private final WeakHashMap<Class, ComponentFactory> _componentMap = new WeakHashMap<Class, ComponentFactory>();
054:
055: private final WebBeansContainer _webBeans = WebBeansContainer
056: .create();
057:
058: private String _name = "resin";
059:
060: public ResinContainerContext() {
061: }
062:
063: public void initialise() throws InitialisationException {
064: }
065:
066: public void dispose() {
067: }
068:
069: public void setName(String name) {
070: _name = name;
071: }
072:
073: public String getName() {
074: return _name;
075: }
076:
077: public Object getComponent(Object key)
078: throws ObjectNotFoundException {
079: if (key == null)
080: throw new ObjectNotFoundException("Component key is null");
081:
082: if (log.isLoggable(Level.FINE)) {
083: log
084: .fine(L
085: .l(
086: "ResinContainerContext.getComponent({0} with type {1})",
087: key, key.getClass().getName()));
088: }
089:
090: if (key instanceof ContainerKeyPair) {
091: ContainerKeyPair pair = (ContainerKeyPair) key;
092:
093: // XXX pair.getContainerName() appears to be null most of the time
094: // ignore it?
095:
096: key = pair.getKey();
097: }
098:
099: if (key instanceof Class) {
100: Class clazz = (Class) key;
101: ComponentFactory component = null;
102:
103: if (log.isLoggable(Level.FINE))
104: log.fine("Creating new instance from " + clazz);
105:
106: synchronized (_componentMap) {
107: component = _componentMap.get(clazz);
108:
109: if (component == null) {
110: component = _webBeans.resolveByType(clazz);
111:
112: if (component == null)
113: component = _webBeans.createTransient(clazz);
114:
115: _componentMap.put(clazz, component);
116: }
117: }
118:
119: return component.get();
120: } else if (key instanceof String) {
121: ComponentFactory component = _webBeans
122: .findByName((String) key);
123:
124: if (component == null) {
125: throw new ObjectNotFoundException(L.l(
126: "Cannot find component with name '{0}'", key));
127: }
128:
129: return component.get();
130: } else {
131: throw new ObjectNotFoundException(L.l(
132: "Component keys of type {0} are not understood",
133: key.getClass().getName()));
134: }
135: }
136:
137: public void configure(Reader configuration, String doctype,
138: String encoding) throws ContainerException {
139: // Resin beans are configured from
140: // resin.conf, web.xml, resin-web.xml, and/or web-beans.xml
141: }
142: }
|