001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2006 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021:
022: package org.jacorb.notification.lifecycle;
023:
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.jacorb.notification.conf.Attributes;
026: import org.jacorb.notification.conf.Default;
027: import org.omg.PortableServer.POA;
028: import org.omg.PortableServer.Servant;
029: import org.omg.PortableServer.POAPackage.ObjectNotActive;
030: import org.omg.PortableServer.POAPackage.ServantNotActive;
031: import org.omg.PortableServer.POAPackage.WrongPolicy;
032:
033: /**
034: * @author Alphonse Bendt
035: * @version $Id: ServantLifecyleControl.java,v 1.3 2006/02/20 19:25:43 alphonse.bendt Exp $
036: */
037:
038: public class ServantLifecyleControl implements ManageableServant {
039: private final IServantLifecyle delegate_;
040:
041: private final boolean runGCDuringDeactivation_;
042:
043: private org.omg.CORBA.Object this Ref_;
044:
045: private Servant this Servant_;
046:
047: public ServantLifecyleControl(IServantLifecyle delegate,
048: Configuration config) {
049: this (delegate, config.getAttribute(Attributes.RUN_SYSTEM_GC,
050: Default.DEFAULT_RUN_SYSTEM_GC).equalsIgnoreCase("on"));
051: }
052:
053: public ServantLifecyleControl(IServantLifecyle delegate,
054: boolean runGCDuringDeactivation) {
055: delegate_ = delegate;
056: runGCDuringDeactivation_ = runGCDuringDeactivation;
057: }
058:
059: public synchronized org.omg.CORBA.Object activate() {
060: if (this Ref_ == null) {
061: try {
062: this Ref_ = delegate_.getPOA().servant_to_reference(
063: getServant());
064: } catch (ServantNotActive e) {
065: throw new RuntimeException();
066: } catch (WrongPolicy e) {
067: throw new RuntimeException();
068: }
069: }
070:
071: return this Ref_;
072: }
073:
074: private Servant getServant() {
075: if (this Servant_ == null) {
076: this Servant_ = delegate_.newServant();
077: }
078:
079: return this Servant_;
080: }
081:
082: public synchronized void deactivate() {
083: if (this Servant_ != null) {
084: final POA _poa = delegate_.getPOA();
085: try {
086: final byte[] _oid = _poa.servant_to_id(this Servant_);
087: delegate_.getPOA().deactivate_object(_oid);
088: } catch (WrongPolicy e) {
089: throw new RuntimeException();
090: } catch (ObjectNotActive e) {
091: throw new RuntimeException();
092: } catch (ServantNotActive e) {
093: throw new RuntimeException();
094: } finally {
095: thisRef_ = null;
096: thisServant_ = null;
097:
098: if (runGCDuringDeactivation_) {
099: System.runFinalization();
100: System.gc();
101: }
102: }
103: }
104: }
105: }
|