This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve "get port" mechanism (#2331)
* improve "get port" mechanism It is not mandatory that the system property is used for setting the service port. So we should also respect the service property. The configuration admin is e.g. used by Pax Web. See also: http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html The service can both be configured using OSGi environment properties and using Configuration Admin. ... If you use both methods, Configuration Admin takes precedence. Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
- Loading branch information
1 parent
7033c63
commit f052a1a
Showing
7 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
....eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/net/HttpServiceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright (c) 2014-2016 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.net; | ||
|
||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.Constants; | ||
import org.osgi.framework.InvalidSyntaxException; | ||
import org.osgi.framework.ServiceReference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Some utility functions related to the http service | ||
* | ||
* @author Markus Rathgeb - Initial contribution and API | ||
*/ | ||
public class HttpServiceUtil { | ||
|
||
private HttpServiceUtil() { | ||
} | ||
|
||
/** | ||
* Get the port that is used by the HTTP service. | ||
* | ||
* @param bc the bundle context used for lookup | ||
* @return the port if used, -1 if no port could be found | ||
*/ | ||
public static int getHttpServicePort(final BundleContext bc) { | ||
return getHttpServicePortProperty(bc, "org.osgi.service.http.port"); | ||
} | ||
|
||
public static int getHttpServicePortSecure(final BundleContext bc) { | ||
return getHttpServicePortProperty(bc, "org.osgi.service.http.port.secure"); | ||
} | ||
|
||
// Utility method that could be used for non-secure and secure port. | ||
private static int getHttpServicePortProperty(final BundleContext bc, final String propertyName) { | ||
Object value; | ||
int port = -1; | ||
|
||
// Try to find the port by using the service property (respect service ranking). | ||
try { | ||
int candidate = Integer.MIN_VALUE; | ||
final ServiceReference[] refs = bc.getAllServiceReferences("org.osgi.service.http.HttpService", null); | ||
for (final ServiceReference ref : refs) { | ||
value = ref.getProperty(propertyName); | ||
if (value == null) { | ||
continue; | ||
} | ||
final int servicePort; | ||
try { | ||
servicePort = Integer.parseInt(value.toString()); | ||
} catch (final NumberFormatException ex) { | ||
continue; | ||
} | ||
value = ref.getProperty(Constants.SERVICE_RANKING); | ||
final int serviceRanking; | ||
if (value == null || !(value instanceof Integer)) { | ||
serviceRanking = 0; | ||
} else { | ||
serviceRanking = (Integer) value; | ||
} | ||
if (serviceRanking >= candidate) { | ||
candidate = serviceRanking; | ||
port = servicePort; | ||
} | ||
} | ||
} catch (final InvalidSyntaxException ex) { | ||
// This point of code should never be reached. | ||
final Logger logger = LoggerFactory.getLogger(HttpServiceUtil.class); | ||
logger.warn("This error should only be thrown if a filter could not be parsed. We don't use a filter..."); | ||
} | ||
if (port > 0) { | ||
return port; | ||
} | ||
|
||
// If the service does not provide the port, try to use the system property. | ||
value = bc.getProperty(propertyName); | ||
if (value != null) { | ||
if (value instanceof String) { | ||
try { | ||
return Integer.parseInt(value.toString()); | ||
} catch (final NumberFormatException ex) { | ||
// If the property could not be parsed, the HTTP servlet itself has to care and warn about. | ||
} | ||
} else if (value instanceof Integer) { | ||
return (Integer) value; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...core/org.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/net/NetUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright (c) 2014-2016 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.net; | ||
|
||
import java.net.Inet6Address; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.util.Enumeration; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Some utility functions related to network interfaces etc. | ||
* | ||
* @author Markus Rathgeb - Initial contribution and API | ||
*/ | ||
public class NetUtil { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(NetUtil.class); | ||
|
||
private NetUtil() { | ||
} | ||
|
||
/** | ||
* Get the first candidate for a local IPv4 host address (non loopback, non localhost). | ||
*/ | ||
public static String getLocalIpv4HostAddress() { | ||
try { | ||
String hostAddress = null; | ||
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); | ||
while (interfaces.hasMoreElements()) { | ||
final NetworkInterface current = interfaces.nextElement(); | ||
if (!current.isUp() || current.isLoopback() || current.isVirtual()) { | ||
continue; | ||
} | ||
final Enumeration<InetAddress> addresses = current.getInetAddresses(); | ||
while (addresses.hasMoreElements()) { | ||
final InetAddress current_addr = addresses.nextElement(); | ||
if (current_addr.isLoopbackAddress() || (current_addr instanceof Inet6Address)) { | ||
continue; | ||
} | ||
if (hostAddress != null) { | ||
LOGGER.warn("Found multiple local interfaces - ignoring {}", current_addr.getHostAddress()); | ||
} else { | ||
hostAddress = current_addr.getHostAddress(); | ||
} | ||
} | ||
} | ||
return hostAddress; | ||
} catch (SocketException ex) { | ||
LOGGER.error("Could not retrieve network interface: " + ex.getMessage(), ex); | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters