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.webbeans.component;
031:
032: import com.caucho.config.ConfigContext;
033: import com.caucho.jmx.*;
034: import com.caucho.webbeans.cfg.WbWebBeans;
035: import com.caucho.webbeans.context.DependentScope;
036: import com.caucho.webbeans.context.SingletonScope;
037:
038: import java.lang.reflect.*;
039: import java.lang.annotation.*;
040: import java.util.logging.*;
041: import java.util.ArrayList;
042: import java.io.Closeable;
043:
044: import javax.annotation.*;
045: import javax.webbeans.*;
046:
047: /**
048: * Configuration for a singleton component.
049: */
050: public class SingletonClassComponent extends ClassComponent implements
051: Closeable {
052: private static final Logger log = Logger
053: .getLogger(SingletonClassComponent.class.getName());
054:
055: private Object _value;
056:
057: public SingletonClassComponent(WbWebBeans webbeans) {
058: super (webbeans);
059:
060: super .setScope(new SingletonScope());
061: }
062:
063: /**
064: * Returns the singleton object
065: */
066: @Override
067: public Object getIfExists() {
068: return get();
069: }
070:
071: /**
072: * Returns the singleton object
073: */
074: @Override
075: public Object get() {
076: if (_value == null) {
077: return get(null);
078: }
079:
080: return _value;
081: }
082:
083: /**
084: * Returns the singleton object
085: */
086: @Override
087: public Object get(ConfigContext env) {
088: if (_value == null) {
089: _value = createNew(null);
090:
091: init(_value, new ConfigContext(this , _value,
092: new SingletonScope()));
093:
094: if (_value instanceof HandleAware)
095: ((HandleAware) _value)
096: .setSerializationHandle(getHandle());
097: }
098:
099: return _value;
100: }
101:
102: /**
103: * The singleton instance is created in its original class loader context
104: */
105: @Override
106: protected Object createNew(ConfigContext env) {
107: ClassLoader loader = getWebBeans().getClassLoader();
108:
109: Thread thread = Thread.currentThread();
110: ClassLoader oldLoader = thread.getContextClassLoader();
111:
112: try {
113: thread.setContextClassLoader(loader);
114:
115: return super .createNew(env);
116: } finally {
117: thread.setContextClassLoader(oldLoader);
118: }
119: }
120:
121: /**
122: * The singleton instance is initialized in its original class loader context
123: */
124: @Override
125: protected Object init(Object value, ConfigContext env) {
126: ClassLoader loader = getWebBeans().getClassLoader();
127:
128: Thread thread = Thread.currentThread();
129: ClassLoader oldLoader = thread.getContextClassLoader();
130:
131: try {
132: thread.setContextClassLoader(loader);
133:
134: value = super .init(value, env);
135:
136: try {
137: if (getMBeanInterface() != null)
138: Jmx.register(value, getMBeanName());
139: } catch (Exception e) {
140: log.log(Level.WARNING, e.toString(), e);
141: }
142:
143: return value;
144: } finally {
145: thread.setContextClassLoader(oldLoader);
146: }
147: }
148:
149: /**
150: * Frees the singleton on environment shutdown
151: */
152: public void close() {
153: if (_value != null)
154: destroy(_value);
155: }
156: }
|