001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 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.imr;
023:
024: import java.util.*;
025:
026: import org.jacorb.imr.AdminPackage.*;
027:
028: /**
029: * This class contains the information about a logical server.
030: * It has methods for managing the associated POAs, holding and
031: * releasing the server, and, for the "client side", a method
032: * that blocks until the server is released.
033: *
034: * @author Nicolas Noffke
035: * @version $Id: ImRServerInfo.java,v 1.14 2006/07/14 11:41:38 alphonse.bendt Exp $
036: */
037: public class ImRServerInfo implements java.io.Serializable {
038: public static final long serialVersionUID = 1l;
039:
040: protected String command;
041: protected boolean holding = false;
042: protected String host;
043: protected String name;
044: protected boolean active;
045: protected boolean restarting = false;
046:
047: private final List poas = new ArrayList();
048: private ResourceLock poas_lock = null;
049:
050: /**
051: * The Constructor. It sets up the internal attributes.
052: *
053: * @param name the logical server name
054: * @param host the name of the host on which the server should be restarted
055: * (ignored when no startup command is specified).
056: * @param command the startup command for this server, passed to the
057: * server startup daemon on <code>host</code> (in case there is one active).
058: * @exception IllegalServerName thrown when <code>name</code> is
059: * <code>null</code> or of length zero.
060: */
061:
062: public ImRServerInfo(String name, String host, String command)
063: throws IllegalServerName {
064: if (name == null || name.length() == 0) {
065: throw new IllegalServerName(name);
066: }
067:
068: this .name = name;
069: this .host = host;
070: this .command = command;
071: active = false;
072: poas_lock = new ResourceLock();
073: }
074:
075: /**
076: * "Converts" this Object to a <code>ServerInfo</code> instance containing
077: * the same info as this object.
078: *
079: * @return a <code>ServerInfo</code> object.
080: */
081:
082: public ServerInfo toServerInfo() {
083: poas_lock.gainExclusiveLock();
084:
085: // The ServerInfo class stores its POAs in an array, therefore
086: // the vector has to copied. Because of backward compatibility
087: // issues we decided not to use toArray() from the jdk1.2
088:
089: // build array
090: final POAInfo[] _info;
091: synchronized (poas) {
092: ImRPOAInfo[] _poas = (ImRPOAInfo[]) poas
093: .toArray(new ImRPOAInfo[poas.size()]);
094:
095: _info = new POAInfo[_poas.length];
096: for (int i = 0; i < _info.length; i++) {
097: _info[i] = _poas[i].toPOAInfo();
098: }
099: }
100:
101: poas_lock.releaseExclusiveLock();
102:
103: return new ServerInfo(name, command, _info, host, active,
104: holding);
105: }
106:
107: /**
108: * Adds a POA to this server.
109: *
110: * @param poa the POA to add.
111: */
112:
113: public void addPOA(ImRPOAInfo poa) {
114: if (!active) {
115: active = true;
116: }
117:
118: poas_lock.gainSharedLock();
119: synchronized (poas) {
120: poas.add(poa);
121: }
122: poas_lock.releaseSharedLock();
123: }
124:
125: /**
126: * Builds an array of of the names of the POAs associated with this server.
127: * <br> This method is needed for deleting a server since its POAs have to be
128: * as well removed from the central storage.
129: * @return an array of POA names
130: */
131:
132: protected String[] getPOANames() {
133: // build array
134: final String[] names;
135: synchronized (poas) {
136: ImRPOAInfo[] _poas = (ImRPOAInfo[]) poas
137: .toArray(new POAInfo[poas.size()]);
138:
139: names = new String[_poas.length];
140: for (int i = 0; i < names.length; i++) {
141: names[i] = _poas[i].name;
142: }
143: }
144:
145: return names;
146: }
147:
148: /**
149: * Sets the server down, i.e. not active. If a request for a POA
150: * of this server is received, the repository tries to restart the server.
151: * <br>The server is automatically set back to active when the first of
152: * its POAs gets reregistered.
153: */
154:
155: public void setDown() {
156: synchronized (poas) {
157: // sets all associated to not active.
158: for (int _i = 0; _i < poas.size(); _i++) {
159: ((ImRPOAInfo) poas.get(_i)).active = false;
160: }
161: }
162:
163: active = false;
164: restarting = false;
165: }
166:
167: /**
168: * This method blocks until the server is released, i.e. set
169: * to not holding. <br> This will not time out since holding a
170: * server is only done by administrators.
171: */
172:
173: public synchronized void awaitRelease() {
174: while (holding) {
175: try {
176: wait();
177: } catch (InterruptedException e) {
178: // ignored
179: }
180: }
181: }
182:
183: /**
184: * Release the server and unblock all waiting threads.
185: */
186:
187: public synchronized void release() {
188: holding = false;
189: notifyAll();
190: }
191:
192: /**
193: * Tests if this server should be restarted. That is the
194: * case if the server is not active and nobody else is currently
195: * trying to restart it. <br>
196: * If true is returned the server is set to restarting. That means
197: * the thread calling this method has to restart the server,
198: * otherwise it will stay down indefinetly.
199: *
200: * @return true, if the server should be restarted by the calling thread.
201: */
202:
203: public synchronized boolean shouldBeRestarted() {
204: boolean _restart = !(active || restarting);
205: if (_restart) {
206: restarting = true;
207: }
208:
209: return _restart;
210: }
211:
212: public void setNotRestarting() {
213: restarting = false;
214: }
215: }
|