diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-04-20 00:19:13 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-04-20 00:19:13 +0000 |
commit | 24bb593b60f3a5367f4ca32d991db6514ed976e5 (patch) | |
tree | d29a91683d896495cd4efd43285ea08e821d9ea9 /sca-java-2.x/trunk/modules/host-http | |
parent | cba00a3e3d60c464fab8f2b4df6e4da7de14ab59 (diff) |
Simplify the HttpPortAllocator and allows the default implementation to
allocate the port based on the system property/environment var (HTTP_PORT/
HTTPS_PORT) or a free port if the value is 0.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1095242 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/host-http')
6 files changed, 65 insertions, 226 deletions
diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/DefaultHttpPortAllocatorExtensionPoint.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/DefaultHttpPortAllocatorExtensionPoint.java deleted file mode 100644 index 5c5847f77b..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/DefaultHttpPortAllocatorExtensionPoint.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.host.http.extensibility; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.extensibility.ServiceDeclaration; -import org.apache.tuscany.sca.extensibility.ServiceHelper; - -public class DefaultHttpPortAllocatorExtensionPoint implements HttpPortAllocatorExtensionPoint { - private final static Logger logger = Logger.getLogger(DefaultHttpPortAllocatorExtensionPoint.class.getName()); - - private ExtensionPointRegistry registry; - private List<HttpPortAllocator> portAllocators = new ArrayList<HttpPortAllocator>(); - private boolean loaded; - - public DefaultHttpPortAllocatorExtensionPoint(ExtensionPointRegistry registry) { - this.registry = registry; - } - public void addPortAllocators(HttpPortAllocator httpPortAllocator) { - this.portAllocators.add(httpPortAllocator); - } - - public void removePortAllocators(HttpPortAllocator httpPortAllocator) { - this.portAllocators.remove(httpPortAllocator); - } - - public List<HttpPortAllocator> getPortAllocators() { - loadServletHosts(); - return this.portAllocators; - } - - private synchronized void loadServletHosts() { - if (loaded) - return; - - // Get the activator service declarations - Collection<ServiceDeclaration> activatorDeclarations; - try { - // Load the port allocators by ranking - activatorDeclarations = registry.getServiceDiscovery().getServiceDeclarations(HttpPortAllocator.class.getName(), true); - } catch (IOException e) { - throw new IllegalStateException(e); - } - - // Load and instantiate http port allocators - for (ServiceDeclaration allocatorDeclaration : activatorDeclarations) { - if (logger.isLoggable(Level.FINE)) { - logger.fine("Loading " + allocatorDeclaration.getClassName()); - } - HttpPortAllocator allocator = null; - try { - Class<HttpPortAllocator> allocatorClass = (Class<HttpPortAllocator>)allocatorDeclaration.loadClass(); - try { - allocator = ServiceHelper.newInstance(allocatorClass, ExtensionPointRegistry.class, registry); - } catch (NoSuchMethodException e) { - try { - allocator = - ServiceHelper.newInstance(allocatorClass, - new Class<?>[] {ExtensionPointRegistry.class, Map.class}, - registry, - allocatorDeclaration.getAttributes()); - - } catch (NoSuchMethodException e1) { - allocator = ServiceHelper.newInstance(allocatorClass); - - } - } - } catch (Throwable e) { - String optional = allocatorDeclaration.getAttributes().get("optional"); - if ("true".equalsIgnoreCase(optional)) { - // If the optional flag is true, just log the error - logger.log(Level.SEVERE, e.getMessage(), e); - continue; - } else { - throw new IllegalArgumentException(e); - } - } - addPortAllocators(allocator); - } - - loaded = true; - } -} diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ExtensibleHttpPortAllocator.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ExtensibleHttpPortAllocator.java deleted file mode 100644 index c1f3083cf8..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ExtensibleHttpPortAllocator.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.host.http.extensibility; - -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.host.http.HttpScheme; - -public class ExtensibleHttpPortAllocator implements HttpPortAllocator { - HttpPortAllocatorExtensionPoint httpPortAllocators; - - public ExtensibleHttpPortAllocator(ExtensionPointRegistry registry) { - this.httpPortAllocators = registry.getExtensionPoint(HttpPortAllocatorExtensionPoint.class); - } - - public static ExtensibleHttpPortAllocator getInstance(ExtensionPointRegistry registry) { - UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); - return utilityExtensionPoint.getUtility(ExtensibleHttpPortAllocator.class); - } - - public int getDefaultPort(HttpScheme scheme) { - if(this.httpPortAllocators.getPortAllocators().isEmpty()) { - throw new RuntimeException("No port allocators registered"); - } - - return this.httpPortAllocators.getPortAllocators().get(0).getDefaultPort(scheme); - } -} diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/HttpPortAllocatorExtensionPoint.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/HttpPortAllocatorExtensionPoint.java deleted file mode 100644 index b7e748f217..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/HttpPortAllocatorExtensionPoint.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.host.http.extensibility; - -import java.util.List; - -/** - * Extension Point to allow registration of different port allocators - * @version $Rev$ $Date$ - */ -public interface HttpPortAllocatorExtensionPoint { - - /** - * Register a new http port allocator - * @param httpPortAllocator the http port allocator - */ - void addPortAllocators(HttpPortAllocator httpPortAllocator); - - /** - * Unregister a http port allocator - * @param httpPortAllocator the http port allocator - */ - void removePortAllocators(HttpPortAllocator httpPortAllocator); - - /** - * Get a list of all registered http port allocators - * @return the list of http port allocators - */ - List<HttpPortAllocator> getPortAllocators(); -} diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java index b345402d8b..af41e85213 100644 --- a/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java +++ b/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java @@ -19,6 +19,11 @@ package org.apache.tuscany.sca.host.http.extensibility.impl; +import java.io.IOException; +import java.net.ServerSocket; +import java.security.AccessController; +import java.security.PrivilegedAction; + import org.apache.tuscany.sca.host.http.HttpScheme; import org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator; @@ -27,15 +32,68 @@ public class DefaultHttpPortAllocatorImpl implements HttpPortAllocator { public int getDefaultPort(HttpScheme scheme) { int port = 0; - if(scheme == HttpScheme.HTTP) { - port = 8080; - } else if(scheme == HttpScheme.HTTPS) { - port = 8443; - } else { - throw new IllegalArgumentException("Scheme not support : " + scheme.toString()); + if (scheme == null || scheme == HttpScheme.HTTP) { + try { + port = Integer.parseInt(getVariable("HTTP_PORT", "8080")); + if (port == 0) { + port = findFreePort(8080, 9080); + } + } catch (NumberFormatException e) { + port = 8080; + } + } else if (scheme == HttpScheme.HTTPS) { + try { + port = Integer.parseInt(getVariable("HTTPS_PORT", "8443")); + if (port == 0) { + port = findFreePort(8443, 9443); + } + } catch (NumberFormatException e) { + port = 8443; + } } return port; + + } + + private static String getVariable(final String variableName, final String defaultValue) { + return AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { + String value = System.getProperty(variableName); + if (value == null || value.length() == 0) { + value = System.getenv(variableName); + if (value == null || value.length() == 0) { + value = defaultValue; + } + } + return value; + } + }); + } + + private int findFreePort(final int start, final int end) { + return AccessController.doPrivileged(new PrivilegedAction<Integer>() { + public Integer run() { + for (int p = start; p <= end; p++) { + ServerSocket socket = null; + try { + socket = new ServerSocket(p); + return p; + } catch (IOException e) { + // Ignore + } finally { + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + return -1; + } + }); } } diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator b/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator index 87ef89118d..70bc948ffc 100644 --- a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator +++ b/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator @@ -15,5 +15,5 @@ # specific language governing permissions and limitations # under the License. -org.apache.tuscany.sca.host.http.extensibility.impl.DefaultHttpPortAllocatorImpl,ranking=900 +org.apache.tuscany.sca.host.http.extensibility.impl.DefaultHttpPortAllocatorImpl diff --git a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocatorExtensionPoint b/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocatorExtensionPoint deleted file mode 100644 index 06d49c5ea3..0000000000 --- a/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocatorExtensionPoint +++ /dev/null @@ -1,19 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -org.apache.tuscany.sca.host.http.extensibility.DefaultHttpPortAllocatorExtensionPoint - |