1 22 package org.objectweb.petals.kernel.configuration; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 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 39 public class ConfigurationService { 40 41 44 protected LoggingUtil log; 45 46 protected List <ContainerInformation> containers = new ArrayList <ContainerInformation>(); 47 48 51 protected long containerUID = SystemUtil.getContainerUID(); 52 53 56 protected String domainPort = SystemUtil.getJoramDomainPort(); 57 58 61 protected String host = SystemUtil.getHost(); 62 63 66 protected String htmlPort = SystemUtil.getHtmlPort(); 67 68 71 protected String id = SystemUtil.getJoramId(); 72 73 76 protected String jmxPort = SystemUtil.getJmxPort(); 77 78 81 protected String jndiPort = SystemUtil.getJndiPort(); 82 83 86 protected String tcpPort = SystemUtil.getJoramTCPPort(); 87 88 protected boolean isInitialized; 89 90 public ConfigurationService(LoggingUtil log) { 91 this.log = log; 92 } 93 94 112 public void setUpConfiguration() { 113 log.start(); 114 List <String > ids = new ArrayList <String >(); 115 List <String > takenPorts = new ArrayList <String >(); 116 117 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 findNotTakenValues(ids, takenPorts); 133 134 if (containerUID == -1) { 135 containerUID = System.currentTimeMillis(); 136 SystemUtil.setContainerUID(containerUID); 137 } 138 139 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 161 protected String findNotTaken(List <String > 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 177 protected void findNotTakenValues(List <String > takenIds, 178 List <String > takenPorts) { 179 String 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 String 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 String 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 String 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 String 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 String 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 <ContainerInformation> containers) { 240 this.containers = containers; 241 } 242 243 public boolean isInitialized() { 244 return isInitialized; 245 } 246 247 } 248 | Popular Tags |