KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > configuration > ConfigurationService


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ConfigurationDiscovery.java 2:54:36 PM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.configuration;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.objectweb.petals.PetalsException;
28 import org.objectweb.petals.kernel.admin.ContainerInformation;
29 import org.objectweb.petals.util.LoggingUtil;
30 import org.objectweb.petals.util.PropertyUtil;
31 import org.objectweb.petals.util.SystemUtil;
32
33 /**
34  * This class is used to discover other Petals container and set the
35  * configuration of this container
36  *
37  * @author ddesjardins - eBMWebsourcing
38  */

39 public class ConfigurationService {
40
41     /**
42      * Logger
43      */

44     protected LoggingUtil log;
45
46     protected List JavaDoc<ContainerInformation> containers = new ArrayList JavaDoc<ContainerInformation>();
47
48     /**
49      * Container UID
50      */

51     protected long containerUID = SystemUtil.getContainerUID();
52
53     /**
54      * Joram domain port
55      */

56     protected String JavaDoc domainPort = SystemUtil.getJoramDomainPort();
57
58     /**
59      * Host of the container
60      */

61     protected String JavaDoc host = SystemUtil.getHost();
62
63     /**
64      * Html adaptor port
65      */

66     protected String JavaDoc htmlPort = SystemUtil.getHtmlPort();
67
68     /**
69      * Joram id
70      */

71     protected String JavaDoc id = SystemUtil.getJoramId();
72
73     /**
74      * JMX server port
75      */

76     protected String JavaDoc jmxPort = SystemUtil.getJmxPort();
77
78     /**
79      * JNDI server port
80      */

81     protected String JavaDoc jndiPort = SystemUtil.getJndiPort();
82
83     /**
84      * Joram tcp port
85      */

86     protected String JavaDoc tcpPort = SystemUtil.getJoramTCPPort();
87
88     protected boolean isInitialized;
89
90     public ConfigurationService(LoggingUtil log) {
91         this.log = log;
92     }
93
94     /**
95      * Set up the container configuration
96      *
97      * In standalone mode:
98      * <ol>
99      * <li> set up only the container UID</li>
100      * </ol>
101      *
102      * In distributed mode:
103      * <ol>
104      * <li>scan the network for other JNDI servers</li>
105      * <li>connect to a JNDI server to retrieve the list of
106      * ContainerInformation</li>
107      * <li>set up the properties of this container (change the ports if they
108      * are already taken)</li>
109      * <li>write the new configuration on the harddrive</li>
110      * </ol>
111      */

112     public void setUpConfiguration() {
113         log.start();
114         List JavaDoc<String JavaDoc> ids = new ArrayList JavaDoc<String JavaDoc>();
115         List JavaDoc<String JavaDoc> takenPorts = new ArrayList JavaDoc<String JavaDoc>();
116
117         // Retreive the containers informations
118
for (ContainerInformation information : containers) {
119             if (information.getUid() != containerUID) {
120                 ids.add(information.getJoramId());
121                 if (host.equals(information.getHost())) {
122                     takenPorts.add(information.getJndiPort());
123                     takenPorts.add(information.getJoramTCPPort());
124                     takenPorts.add(information.getHtmlPort());
125                     takenPorts.add(information.getJmxPort());
126                     takenPorts.add(information.getJoramDomainPort());
127                 }
128             }
129         }
130
131         // Find not taken values
132
findNotTakenValues(ids, takenPorts);
133
134         if (containerUID == -1) {
135             containerUID = System.currentTimeMillis();
136             SystemUtil.setContainerUID(containerUID);
137         }
138
139         // Update the container properties
140
try {
141             PropertyUtil.updateContainerProperties(id, domainPort, tcpPort,
142                     jmxPort, htmlPort, jndiPort, containerUID);
143         } catch (PetalsException e) {
144             log.error("Error durring Petals setup.", e);
145         }
146         isInitialized = true;
147         log.end();
148     }
149
150     /**
151      * Find a not taken value in a range
152      *
153      * @param takens
154      * List of already taken values
155      * @param startValue
156      * start value
157      * @param range
158      * range of values
159      * @return a not taken value in the range from the start value
160      */

161     protected String JavaDoc findNotTaken(List JavaDoc<String JavaDoc> takens, int startValue, int range) {
162         int i = startValue;
163         while (i < (startValue + range) && takens.contains("" + i)) {
164             i++;
165         }
166         return "" + i;
167     }
168
169     /**
170      * Find not taken values in the id list and in the port list
171      *
172      * @param takenIds
173      * taken ids
174      * @param takenPorts
175      * taken ports
176      */

177     protected void findNotTakenValues(List JavaDoc<String JavaDoc> takenIds,
178             List JavaDoc<String JavaDoc> takenPorts) {
179         // Find new id
180
String JavaDoc newId = findNotTaken(takenIds, Integer.parseInt(id), 255);
181         if (!newId.equals(takenIds)) {
182             log.debug("A new id has been set for the container : " + newId);
183             id = newId;
184             SystemUtil.setContainerName(newId);
185             SystemUtil.setJoramId(newId);
186         }
187         // Find new joram tcp port
188
String JavaDoc newTCPPort = findNotTaken(takenPorts, Integer.parseInt(tcpPort),
189                 255);
190         if (!newTCPPort.equals(tcpPort)) {
191             takenPorts.add(newTCPPort);
192             log.debug("A new Joram tcp port has been set for the container : "
193                     + newTCPPort);
194             tcpPort = newTCPPort;
195             SystemUtil.setJoramTCPPort(newTCPPort);
196         }
197         // Find new html port
198
String JavaDoc newHTMLPort = findNotTaken(takenPorts, Integer
199                 .parseInt(htmlPort), 255);
200         if (!newHTMLPort.equals(htmlPort)) {
201             takenPorts.add(newHTMLPort);
202             log.debug("A new HTML port has been set for the container : "
203                     + newHTMLPort);
204             htmlPort = newHTMLPort;
205             SystemUtil.setHtmlPort(newHTMLPort);
206         }
207         // Find new jndi port
208
String JavaDoc newJNDIPort = findNotTaken(takenPorts, Integer
209                 .parseInt(jndiPort), 255);
210         if (!newJNDIPort.equals(jndiPort)) {
211             takenPorts.add(newJNDIPort);
212             log.debug("A new JNDI port has been set for the container : "
213                     + newJNDIPort);
214             jndiPort = newJNDIPort;
215             SystemUtil.setJndiPort(newJNDIPort);
216         }
217         // Find new jmx port
218
String JavaDoc newJMXPort = findNotTaken(takenPorts, Integer.parseInt(jmxPort),
219                 255);
220         if (!newJMXPort.equals(jmxPort)) {
221             takenPorts.add(newJMXPort);
222             log.debug("A new JMX port has been set for the container : "
223                     + newJMXPort);
224             jmxPort = newJMXPort;
225             SystemUtil.setJmxPort(newJMXPort);
226         }
227         // Find new domain port
228
String JavaDoc newDomainPort = findNotTaken(takenPorts, Integer
229                 .parseInt(domainPort), 255);
230         if (!newDomainPort.equals(domainPort)) {
231             takenPorts.add(newDomainPort);
232             domainPort = newDomainPort;
233             log.debug("A new Joram domain port has been set for the "
234                     + " container : " + newDomainPort);
235             SystemUtil.setJoramDomainPort(newDomainPort);
236         }
237     }
238
239     public void setContainers(List JavaDoc<ContainerInformation> containers) {
240         this.containers = containers;
241     }
242
243     public boolean isInitialized() {
244         return isInitialized;
245     }
246
247 }
248
Popular Tags