KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > imr > ImRServerInfo


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.imr;
22
23 import java.util.*;
24
25 import org.jacorb.imr.RegistrationPackage.*;
26 import org.jacorb.imr.AdminPackage.*;
27
28 /**
29  * This class contains the information about a logical server.
30  * It has methods for managing the associated POAs, holding and
31  * releasing the server, and, for the "client side", a method
32  * that blocks until the server is released.
33  *
34  * @author Nicolas Noffke
35  *
36  * @version $Id: ImRServerInfo.java,v 1.11 2004/05/06 12:39:59 nicolas Exp $
37  *
38  */

39
40 public class ImRServerInfo
41     implements java.io.Serializable JavaDoc
42 {
43     protected String JavaDoc command;
44     protected boolean holding = false;
45     protected String JavaDoc host;
46     protected String JavaDoc name;
47     protected boolean active;
48     protected boolean restarting = false;
49
50     private Vector poas = null;
51     private ResourceLock poas_lock = null;
52
53     /**
54      * The Constructor. It sets up the internal attributes.
55      *
56      * @param name the logical server name
57      * @param host the name of the host on which the server should be restarted
58      * (ignored when no startup command is specified).
59      * @param command the startup command for this server, passed to the
60      * server startup daemon on <code>host</code> (in case there is one active).
61      * @exception IllegalServerName thrown when <code>name</code> is
62      * <code>null</code> or of length zero.
63      */

64
65     public ImRServerInfo(String JavaDoc name, String JavaDoc host, String JavaDoc command)
66         throws IllegalServerName
67     {
68         if (name == null || name.length() == 0)
69             throw new IllegalServerName(name);
70     
71         this.name = name;
72         this.host = host;
73         this.command = command;
74         active = false;
75         poas = new Vector();
76         poas_lock = new ResourceLock();
77     }
78     
79     /**
80      * "Converts" this Object to a <code>ServerInfo</code> instance containing
81      * the same info as this object.
82      *
83      * @return a <code>ServerInfo</code> object.
84      */

85
86     public ServerInfo toServerInfo()
87     {
88         poas_lock.gainExclusiveLock();
89
90         // The ServerInfo class stores its POAs in an array, therefore
91
// the vector has to copied. Because of backward compatibility
92
// issues we decided not to use toArray() from the jdk1.2
93

94         // build array
95
POAInfo[] _poas = new POAInfo[poas.size()];
96         Enumeration _poa_enum = poas.elements();
97
98         // copy vector into array
99
int _i = 0;
100         while(_poa_enum.hasMoreElements())
101             _poas[_i++] = ((ImRPOAInfo) _poa_enum.nextElement()).toPOAInfo();
102
103         poas_lock.releaseExclusiveLock();
104
105         return new ServerInfo(name, command, _poas, host, active, holding);
106     }
107
108     /**
109      * Adds a POA to this server.
110      *
111      * @param poa the POA to add.
112      */

113
114     public void addPOA(ImRPOAInfo poa)
115     {
116         if (! active)
117             active = true;
118
119         poas_lock.gainSharedLock();
120         poas.addElement(poa);
121         poas_lock.releaseSharedLock();
122     }
123
124     /**
125      * Builds an array of of the names of the POAs associated with this server.
126      * <br> This method is needed for deleting a server since its POAs have to be
127      * as well removed from the central storage.
128      * @return an array of POA names
129      */

130
131     protected String JavaDoc[] getPOANames()
132     {
133         // not synchronizing here since this method is only called
134
// prior to destructing this object.
135
String JavaDoc[] _poa_names = new String JavaDoc[poas.size()];
136         Enumeration _poa_enum = poas.elements();
137
138         int _i = 0;
139         while(_poa_enum.hasMoreElements())
140             _poa_names[_i++] = ((ImRPOAInfo) _poa_enum.nextElement()).name;
141     
142         return _poa_names;
143     }
144
145     /**
146      * Sets the server down, i.e. not active. If a request for a POA
147      * of this server is received, the repository tries to restart the server.
148      * <br>The server is automatically set back to active when the first of
149      * its POAs gets reregistered.
150      */

151
152     public void setDown()
153     {
154         // sets all associated to not active.
155
for (int _i = 0; _i < poas.size(); _i++)
156             ((ImRPOAInfo) poas.elementAt(_i)).active = false;
157
158         active = false;
159         restarting = false;
160     }
161
162     /**
163      * This method blocks until the server is released, i.e. set
164      * to not holding. <br> This will not time out since holding a
165      * server is only done by administrators.
166      */

167
168     public synchronized void awaitRelease()
169     {
170         while(holding)
171         {
172             try
173             {
174                 wait();
175             }
176             catch (java.lang.Exception JavaDoc _e)
177             {
178             }
179         }
180     }
181
182     /**
183      * Release the server and unblock all waiting threads.
184      */

185
186     public synchronized void release()
187     {
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     {
205         boolean _restart = !(active || restarting);
206         if (_restart)
207             restarting = true;
208         
209         return _restart;
210     }
211
212     public void setNotRestarting()
213     {
214         restarting = false;
215     }
216
217 } // ImRServerInfo
218

219
Popular Tags