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 java.io.Closeable;
034: import java.lang.annotation.*;
035: import javax.webbeans.*;
036:
037: import com.caucho.webbeans.cfg.WbWebBeans;
038: import com.caucho.webbeans.context.*;
039: import com.caucho.webbeans.manager.*;
040:
041: /**
042: * Component for a singleton beans
043: */
044: public class SingletonComponent extends ClassComponent implements
045: Closeable {
046: private static final Object[] NULL_ARGS = new Object[0];
047:
048: private Object _value;
049:
050: public SingletonComponent(WbWebBeans webBeans, Object value) {
051: super (webBeans);
052:
053: _value = value;
054:
055: setInstanceClass(value.getClass());
056: setTargetType(value.getClass());
057:
058: super .setScope(new SingletonScope());
059: }
060:
061: public SingletonComponent(WebBeansContainer webBeans, Object value) {
062: super (webBeans.getWbWebBeans());
063:
064: _value = value;
065:
066: setInstanceClass(value.getClass());
067: setTargetType(value.getClass());
068:
069: super .setScope(new SingletonScope());
070: }
071:
072: @Override
073: public void introspectConstructor() {
074: }
075:
076: /**
077: * Complete initialization
078: */
079: @Override
080: public void init() {
081: super .init();
082:
083: if (_value instanceof HandleAware)
084: ((HandleAware) _value).setSerializationHandle(getHandle());
085: }
086:
087: @Override
088: public void setScope(ScopeContext scope) {
089: }
090:
091: @Override
092: public Object get() {
093: return _value;
094: }
095:
096: @Override
097: public Object get(ConfigContext env) {
098: return _value;
099: }
100:
101: @Override
102: public Object create() {
103: return _value;
104: }
105:
106: @Override
107: protected Object createNew(ConfigContext env) {
108: throw new IllegalStateException();
109: }
110:
111: /**
112: * Frees the singleton on environment shutdown
113: */
114: public void close() {
115: if (_value != null)
116: destroy(_value);
117: }
118: }
|