/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.config.support;
import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.enterprise.config.serverbeans.*;
import static com.sun.enterprise.config.util.PortConstants.*;
import com.sun.enterprise.config.util.PortManager;
import org.glassfish.api.admin.config.ConfigurationUpgrade;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.component.PostConstruct;
import org.jvnet.hk2.config.ConfigSupport;
import org.jvnet.hk2.config.TransactionFailure;
import org.jvnet.hk2.config.SingleConfigCode;
/**
* Startup service to add the new 3.1 system properties to the config elements
* (except DAS config, server-config) in existing domain.xml:
*
*
*
*
* Use the same prefix as the config's system property HTTP_LISTENER_PORT if it exists.
* Otherwise use "2" as the prefix.
*
* @author Jennifer Chou
*/
@Service
public class ConfigUpgradeService implements ConfigurationUpgrade, PostConstruct {
@Inject
Configs configs;
@Inject
Domain domain;
@Inject
Servers servers;
private static String PREFIX = "2";
private static final String DEFAULT_ADMIN_PORT = "4848";
private static final String DAS_CONFIG = "server-config";
private static final String DAS = "server";
public void postConstruct() {
upgradeConfigElements();
upgradeServerElements();
/*for (Server s : servers.getServer()) {
PortManager pm;
try {
pm = new PortManager(s.getCluster(), s.getConfig(), domain, s, Logger.getAnonymousLogger());
String message = pm.process();
} catch (TransactionFailure ex) {
Logger.getAnonymousLogger().log(Level.SEVERE,
Strings.get("ConfigUpgradeService.Failure", s.getName()), ex);
throw new RuntimeException(ex);
}
}*/
}
private void upgradeConfigElements() {
for (Config c : configs.getConfig()) {
try {
if (!c.getName().equals(DAS_CONFIG)) {
SystemPropertyBag bag = c;
String httpVal = bag.getSystemProperty(HTTP).getValue();
if (httpVal != null) {
PREFIX = String.valueOf(httpVal.charAt(0));
}
ConfigSupport.apply(new SingleConfigCode() {
public Object run(SystemPropertyBag config) throws PropertyVetoException, TransactionFailure {
createSystemProperty(config, ADMIN, DEFAULT_ADMIN_PORT);
createSystemProperty(config, OSGI, String.valueOf(DEFAULT_OSGI_SHELL_TELNET_PORT));
createSystemProperty(config, DEBUG, String.valueOf(DEFAULT_JAVA_DEBUGGER_PORT));
return null;
}
}, c);
}
} catch (Exception e) {
Logger.getAnonymousLogger().log(Level.SEVERE,
Strings.get("ConfigUpgradeService.Failure", c), e);
throw new RuntimeException(e);
}
}
}
private void upgradeServerElements() {
for (Server s : servers.getServer()) {
try {
if (!s.getName().equals(DAS)) {
SystemPropertyBag bag = s;
if (bag.getSystemProperty(HTTP) != null) {
final String httpVal = bag.getSystemProperty(HTTP).getValue();
if (httpVal != null) {
PREFIX = String.valueOf(httpVal.charAt(0));
ConfigSupport.apply(new SingleConfigCode() {
public Object run(SystemPropertyBag config) throws PropertyVetoException, TransactionFailure {
createSystemProperty(config, ADMIN, getNewPortVal(httpVal, Integer.parseInt(DEFAULT_ADMIN_PORT)));
createSystemProperty(config, OSGI, getNewPortVal(httpVal, DEFAULT_OSGI_SHELL_TELNET_PORT));
createSystemProperty(config, DEBUG, getNewPortVal(httpVal, DEFAULT_JAVA_DEBUGGER_PORT));
return null;
}
}, s);
}
}
}
} catch (Exception e) {
Logger.getAnonymousLogger().log(Level.SEVERE,
Strings.get("ConfigUpgradeService.Failure", s), e);
throw new RuntimeException(e);
}
}
}
private void createSystemProperty(SystemPropertyBag spb, String portName, String portVal) throws TransactionFailure, PropertyVetoException {
if (spb.getSystemProperty(portName) == null) {
SystemProperty newSysProp = spb.createChild(SystemProperty.class);
newSysProp.setName(portName);
newSysProp.setValue(PREFIX + portVal);
spb.getSystemProperty().add(newSysProp);
}
}
private String getNewPortVal(String httpPort, int defaultPort) {
String baseHttpPort = httpPort.substring(httpPort.length() - 4);
//String prefix = httpPort.substring(0, httpPort.length() - 4);
int currBaseHttpPort = Integer.parseInt(baseHttpPort);
String newPort = String.valueOf(defaultPort);
int incr = currBaseHttpPort - DEFAULT_INSTANCE_PORT;
if (incr >= 0) {
newPort = String.valueOf(defaultPort + incr);
}
return newPort;
}
}