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.jca;
031:
032: import com.caucho.lifecycle.*;
033: import com.caucho.loader.*;
034: import com.caucho.util.L10N;
035: import com.caucho.webbeans.component.*;
036:
037: import java.util.logging.Logger;
038: import javax.resource.spi.*;
039: import javax.webbeans.*;
040:
041: /**
042: * Controller for a resource-adapter
043: */
044: public class ResourceAdapterController implements EnvironmentListener {
045: private static final L10N L = new L10N(
046: ResourceAdapterController.class);
047: private static final Logger log = Logger
048: .getLogger(ResourceAdapterController.class.getName());
049:
050: private final ComponentFactory<ResourceAdapter> _comp;
051: private final ResourceArchive _raConfig;
052:
053: private Lifecycle _lifecycle = new Lifecycle();
054: private ResourceAdapter _ra;
055:
056: public ResourceAdapterController(
057: ComponentFactory<ResourceAdapter> comp,
058: ResourceArchive raConfig) {
059: _comp = comp;
060: _raConfig = raConfig;
061:
062: Environment.addEnvironmentListener(this );
063: }
064:
065: public ResourceAdapter getResourceAdapter() {
066: start();
067:
068: return _ra;
069: }
070:
071: /**
072: * Starts the resource adapter
073: */
074: private void start() {
075: if (!_lifecycle.toActive())
076: return;
077:
078: _ra = _comp.get();
079:
080: try {
081: _ra.start(ResourceManagerImpl.create());
082: } catch (RuntimeException e) {
083: throw e;
084: } catch (Exception e) {
085: throw new StartLifecycleException(e);
086: }
087: }
088:
089: /**
090: * Stops the resource adapter
091: */
092: private void stop() {
093: if (!_lifecycle.toStop())
094: return;
095:
096: try {
097: ResourceAdapter ra = _ra;
098:
099: if (ra != null)
100: ra.stop();
101: } catch (RuntimeException e) {
102: throw e;
103: } catch (Exception e) {
104: throw new StartLifecycleException(e);
105: }
106: }
107:
108: /**
109: * Handles the environment config phase.
110: */
111: public void environmentConfig(EnvironmentClassLoader loader)
112: throws StartLifecycleException {
113: }
114:
115: /**
116: * Handles the case where the environment is starting (after init).
117: */
118: public void environmentStart(EnvironmentClassLoader loader)
119: throws StartLifecycleException {
120: start();
121: }
122:
123: /**
124: * Handles the case where the environment is stopping
125: */
126: public void environmentStop(EnvironmentClassLoader loader) {
127: stop();
128: }
129:
130: @Override
131: public String toString() {
132: return getClass().getSimpleName() + "[" + _comp + "]";
133: }
134: }
|