001: package org.jacorb.poa;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import org.omg.CORBA.INTERNAL;
027: import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
028: import org.omg.PortableServer.POAManagerPackage.State;
029:
030: /**
031: * The poa manager class, an implementation of org.omg.PortableServer.POAManager
032: *
033: * @author Reimo Tiedemann, FU Berlin
034: * @version $Id: POAManager.java,v 1.16 2006/05/22 15:03:49 alphonse.bendt Exp $
035: */
036:
037: public class POAManager extends
038: org.omg.PortableServer._POAManagerLocalBase {
039: public State state = State.HOLDING;
040: private final org.jacorb.orb.ORB orb;
041: private final List poas = new ArrayList();
042: private POAManagerMonitor monitor;
043: protected boolean poaCreationFailed;
044:
045: protected POAManager(org.jacorb.orb.ORB _orb) {
046: orb = _orb;
047: monitor = new POAManagerMonitorLightImpl();
048: try {
049: monitor.configure(orb.getConfiguration());
050: } catch (org.apache.avalon.framework.configuration.ConfigurationException ce) {
051: // Never thrown
052: }
053: monitor.init(this );
054: monitor.openMonitor();
055: monitor.printMessage("ready");
056: }
057:
058: public void activate() throws AdapterInactive {
059: checkCreation();
060:
061: switch (state.value()) {
062: case State._INACTIVE:
063: throw new AdapterInactive();
064: case State._ACTIVE:
065: break;
066: default:
067: state = State.ACTIVE;
068: monitor.setToActive();
069:
070: final POA[] poaArray;
071:
072: synchronized (this ) {
073: poaArray = (POA[]) poas.toArray(new POA[poas.size()]);
074: }
075: // notify all registered poas
076: Thread thread = new Thread() {
077: public void run() {
078: for (int i = 0; i < poaArray.length; i++) {
079: try {
080: poaArray[i].changeToActive();
081: } catch (Throwable e) {
082: }
083: }
084: }
085: };
086: thread.setName("POAChangeToActive");
087: thread.start();
088: }
089: }
090:
091: public void deactivate(boolean etherealize_objects,
092: boolean wait_for_completion) throws AdapterInactive {
093: checkCreation();
094:
095: if (wait_for_completion && isInInvocationContext()) {
096: throw new org.omg.CORBA.BAD_INV_ORDER();
097: }
098:
099: switch (state.value()) {
100: case State._INACTIVE:
101: throw new AdapterInactive();
102: default:
103: state = State.INACTIVE;
104: monitor.setToInactive(wait_for_completion,
105: etherealize_objects);
106:
107: final boolean etherealize = etherealize_objects;
108: final POA[] poaArray;
109:
110: synchronized (this ) {
111: poaArray = (POA[]) poas.toArray(new POA[poas.size()]);
112: }
113: // notify all registered poas
114: Thread thread = new Thread() {
115: public void run() {
116: for (int i = poaArray.length - 1; i >= 0; i--) {
117: try {
118: poaArray[i].changeToInactive(etherealize);
119: } catch (Throwable e) {
120: }
121: }
122: }
123: };
124: thread.setName("POAChangeToInactive");
125: thread.start();
126: if (wait_for_completion) {
127: try {
128: thread.join();
129: } catch (InterruptedException e) {
130: }
131: }
132: }
133: }
134:
135: public void discard_requests(boolean wait_for_completion)
136: throws AdapterInactive {
137: checkCreation();
138:
139: if (wait_for_completion && isInInvocationContext()) {
140: throw new org.omg.CORBA.BAD_INV_ORDER();
141: }
142:
143: switch (state.value()) {
144: case State._INACTIVE:
145: throw new AdapterInactive();
146: case State._DISCARDING:
147: break;
148: default:
149: state = State.DISCARDING;
150: monitor.setToDiscarding(wait_for_completion);
151:
152: final POA[] poaArray;
153:
154: synchronized (this ) {
155: poaArray = (POA[]) poas.toArray(new POA[poas.size()]);
156: }
157: // notify all registered poas
158: Thread thread = new Thread() {
159: public void run() {
160: for (int i = poaArray.length - 1; i >= 0; i--) {
161: try {
162: poaArray[i].changeToDiscarding();
163: } catch (Throwable e) {
164: }
165: }
166: }
167: };
168: thread.setName("POAChangeToDiscarding");
169: thread.start();
170: if (wait_for_completion) {
171: try {
172: thread.join();
173: } catch (InterruptedException e) {
174: }
175: }
176: }
177: }
178:
179: public State get_state() {
180: return state;
181: }
182:
183: protected synchronized POA getRegisteredPOA(String name) {
184: POA result;
185: Iterator en = poas.iterator();
186: while (en.hasNext()) {
187: result = (POA) en.next();
188: if (name.equals(result._getQualifiedName())) {
189: return result;
190: }
191: }
192: throw new INTERNAL("POA not registered: "
193: + POAConstants.ROOT_POA_NAME
194: + POAConstants.OBJECT_KEY_SEPARATOR + name);
195: }
196:
197: public void hold_requests(boolean wait_for_completion)
198: throws AdapterInactive {
199: checkCreation();
200:
201: if (wait_for_completion && isInInvocationContext()) {
202: throw new org.omg.CORBA.BAD_INV_ORDER();
203: }
204: switch (state.value()) {
205: case State._INACTIVE:
206: throw new AdapterInactive();
207: case State._HOLDING:
208: break;
209: default:
210: state = State.HOLDING;
211: monitor.setToHolding(wait_for_completion);
212:
213: final POA[] poaArray;
214:
215: synchronized (this ) {
216: poaArray = (POA[]) poas.toArray(new POA[poas.size()]);
217: }
218: // notify all registered poas
219: Thread thread = new Thread() {
220: public void run() {
221: for (int i = poaArray.length - 1; i >= 0; i--) {
222: try {
223: poaArray[i].changeToHolding();
224: } catch (Throwable e) {
225: }
226: }
227: }
228: };
229: thread.setName("POAChangeToHolding");
230: thread.start();
231: if (wait_for_completion) {
232: try {
233: thread.join();
234: } catch (InterruptedException e) {
235: }
236: }
237: }
238: }
239:
240: /**
241: * it returns true if the current thread is not in an invocation
242: * context dispatched by some POA belonging to the same ORB as this POAManager.
243: */
244: private boolean isInInvocationContext() {
245: try {
246: if (orb.getPOACurrent().getORB() == orb) {
247: return true;
248: }
249: } catch (org.omg.PortableServer.CurrentPackage.NoContext e) {
250: }
251: return false;
252: }
253:
254: protected synchronized void registerPOA(POA poa) {
255: if (!poas.contains(poa)) {
256: poas.add(poa);
257: monitor.addPOA(poa._getQualifiedName());
258: }
259: }
260:
261: protected void setMonitor(POAManagerMonitor _monitor) {
262: monitor = _monitor;
263: }
264:
265: protected synchronized void unregisterPOA(POA poa) {
266: poas.remove(poa);
267: monitor.removePOA(poa._getQualifiedName());
268: }
269:
270: private void checkCreation() {
271: if (poaCreationFailed) {
272: throw new org.omg.CORBA.INTERNAL(
273: "POA Creation failed; unable to deactive");
274: }
275: }
276: }
|