diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-07 13:14:31 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2009-04-07 13:14:31 +0000 |
commit | 084d3e5e0936b31a0d81c65144cace58948bc9be (patch) | |
tree | 98746cec755bc0b917d687f77b77ec9f180b63b7 /java/sca/modules/sca-api/src/main | |
parent | 2d9bf0b1c21888610d6b1e7cfeb202ba7c03f8f8 (diff) |
Start adding the new non-SCA client API proposal code (see http://lists.oasis-open.org/archives/sca-j/200903/msg00121.html). This is code mostly from that initial proposal with minimal change sto get it to work in Tuscany, so it needs updating from the rest of that email thread
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@762759 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/sca-api/src/main')
6 files changed, 490 insertions, 0 deletions
diff --git a/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/NoSuchDomainException.java b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/NoSuchDomainException.java new file mode 100644 index 0000000000..51a4dbe3ad --- /dev/null +++ b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/NoSuchDomainException.java @@ -0,0 +1,40 @@ +/* + * 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.oasisopen.sca; + +public class NoSuchDomainException extends Exception { + private static final long serialVersionUID = -3990087696861167748L; + + public NoSuchDomainException() { + super((Throwable)null); + } + + public NoSuchDomainException(String message) { + super(message); + } + + public NoSuchDomainException(Throwable cause) { + super(cause); + } + + public NoSuchDomainException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/NoSuchServiceException.java b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/NoSuchServiceException.java new file mode 100644 index 0000000000..62d1b07f23 --- /dev/null +++ b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/NoSuchServiceException.java @@ -0,0 +1,41 @@ +/* + * 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.oasisopen.sca; + +public class NoSuchServiceException extends Exception { + private static final long serialVersionUID = -3542376318558454581L; + + public NoSuchServiceException() { + super((Throwable)null); + } + + public NoSuchServiceException(String message) { + super(message); + } + + public NoSuchServiceException(Throwable cause) { + super(cause); + } + + public NoSuchServiceException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/SCARuntimeException.java b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/SCARuntimeException.java new file mode 100644 index 0000000000..ed957cff6d --- /dev/null +++ b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/SCARuntimeException.java @@ -0,0 +1,41 @@ +/* + * 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.oasisopen.sca; + +public class SCARuntimeException extends RuntimeException { + private static final long serialVersionUID = -3867882539949395157L; + + public SCARuntimeException() { + super((Throwable)null); + } + + public SCARuntimeException(String message) { + super(message); + } + + public SCARuntimeException(Throwable cause) { + super(cause); + } + + public SCARuntimeException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/SCAClient.java b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/SCAClient.java new file mode 100644 index 0000000000..c054179d8a --- /dev/null +++ b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/SCAClient.java @@ -0,0 +1,41 @@ +/* + * Copyright(C) OASIS(R) 2005,2009. All Rights Reserved. 2299 + * OASIS trademark, IPR and other policies apply. 2300 + */ +package org.oasisopen.sca.client; + +import java.net.URI; +import org.oasisopen.sca.NoSuchDomainException; +import org.oasisopen.sca.NoSuchServiceException; + +/** + * Client side interface that can be used to lookup SCA Services within a SCA + * Domain. + * <p> + * The SCAClientFactory is used to obtain an implementation instance of the + * SCAClient. + * + * @see SCAClientFactory + * @author OASIS Open + */ +public interface SCAClient { + + /** + * Returns a reference proxy that implements the business interface <T> of a + * service in a domain + * + * @param serviceURI the relative URI of the target service. Takes the form + * componentName/serviceName. Can also take the extended form + * componentName/serviceName/bindingName to use a specific + * binding of the target service + * @param domainURI the URI of an SCA Domain. + * @param interfaze The business interface class of the service in the + * domain + * @param <T> The business interface class of the service in the domain + * @return a proxy to the target service, in the specified SCA Domain that + * implements the business interface <B>. + * @throws NoSuchServiceException Service requested was not found + * @throws NoSuchDomainException Domain requested was not found + */ + <T> T getService(Class<T> interfaze, String serviceURI, URI domainURI) throws NoSuchServiceException, NoSuchDomainException; +} diff --git a/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/SCAClientFactory.java b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/SCAClientFactory.java new file mode 100644 index 0000000000..e2abcf08e4 --- /dev/null +++ b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/SCAClientFactory.java @@ -0,0 +1,89 @@ +/* + * Copyright(C) OASIS(R) 2005,2009. All Rights Reserved. + * OASIS trademark, IPR and other policies apply. + */ +package org.oasisopen.sca.client; + +import java.util.Properties; + +import org.oasisopen.sca.client.impl.SCAClientFactoryFinder; + +/** + * The SCAClientFactory can be used by non-SCA managed code to lookup services + * that exist in a SCADomain. + * + * @see SCAClientFactoryFinder + * @see SCAClient + * @author OASIS Open + */ +public abstract class SCAClientFactory { + + /** + * The default implementation of the SCAClientFactory. A Vendor may use + * reflection to inject a default SCAClientFactory instance that will be + * used in the newInstance() methods rather than using the + * SCAClientFactoryFinder. + */ + protected static SCAClientFactory defaultFactory; + + /** + * Creates a new instance of the SCAClient that can be used to lookup SCA + * Services. + * + * @return A new SCAClient + */ + public static SCAClient newInstance() { + return newInstance(null, null); + } + + /** + * Creates a new instance of the SCAClient that can be used to lookup SCA + * Services. + * + * @param properties Properties that may be used when creating a new + * instance of the SCAClient + * @return A new SCAClient instance + */ + public static SCAClient newInstance(Properties properties) { + return newInstance(properties, null); + } + + /** + * Creates a new instance of the SCAClient that can be used to lookup SCA + * Services. + * + * @param classLoader ClassLoader that may be used when creating a new + * instance of the SCAClient + * @return A new SCAClient instance + */ + public static SCAClient newInstance(ClassLoader classLoader) { + return newInstance(null, classLoader); + } + + /** + * Creates a new instance of the SCAClient that can be used to lookup SCA + * Services. + * + * @param properties Properties that may be used when creating a new + * instance of the SCAClient + * @param classLoader ClassLoader that may be used when creating a new + * instance of the SCAClient + * @return A new SCAClient instance + */ + public static SCAClient newInstance(Properties properties, ClassLoader classLoader) { + final SCAClientFactory factory; + if (defaultFactory == null) { + factory = SCAClientFactoryFinder.find(properties, classLoader); + } else { + factory = defaultFactory; + } + return factory.createSCAClient(); + } + + /** + * This method is invoked to create a new SCAClient instance. + * + * @return A new SCAClient instance + */ + protected abstract SCAClient createSCAClient(); +} diff --git a/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/impl/SCAClientFactoryFinder.java b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/impl/SCAClientFactoryFinder.java new file mode 100644 index 0000000000..c13e4e9142 --- /dev/null +++ b/java/sca/modules/sca-api/src/main/java/org/oasisopen/sca/client/impl/SCAClientFactoryFinder.java @@ -0,0 +1,238 @@ +/* + * Copyright(C) OASIS(R) 2005,2009. All Rights Reserved. + * OASIS trademark, IPR and other policies apply. + */ +package org.oasisopen.sca.client.impl; + +import java.io.BufferedReader; +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.Properties; + +import org.oasisopen.sca.SCARuntimeException; +import org.oasisopen.sca.client.SCAClientFactory; + +/** + * This is a default class that returns an SCAClientFactory implementation - + * this class can be replaced by a vendor implementation. + * + * @see SCAClientFactory + * @author OASIS Open + */ +public class SCAClientFactoryFinder { + + /** + * The name of the System Property used to determine the SPI implementation + * to use for the SCAClientFactory. + */ + private static final String SCA_CLIENT_FACTORY_PROVIDER_KEY = SCAClientFactory.class.getName(); + + /** + * The name of the file loaded from the ClassPath to determine 2488 the SPI + * implementation to use for the SCAClientFactory. 2489 + */ + private static final String SCA_CLIENT_FACTORY_PROVIDER_META_INF_SERVICE = "META-INF/services/" + SCA_CLIENT_FACTORY_PROVIDER_KEY; + + /** + * Private Constructor. + */ + private SCAClientFactoryFinder() { + } + + /** + * Creates an instance of the SCAClientFactory implementation. This + * discovers the SCAClientFactory implementation and instantiates the + * provider's implementation. + * + * @param properties Properties that may be used when creating a new + * instance of the SCAClient + * @param classLoader ClassLoader that may be used when creating a new + * instance of the SCAClient + * @return new instance of the SCAClientFactory + * @throws SCARuntimeException Failed to create SCAClientFactory + * implementation. + */ + public static SCAClientFactory find(Properties properties, ClassLoader classLoader) { + if (classLoader == null) { + classLoader = getThreadContextClassLoader(); + if (classLoader == null) { + classLoader = SCAClientFactoryFinder.class.getClassLoader(); + } + } + final String factoryImplClassName = discoverProviderFactoryImplClass(properties, classLoader); + final Class<? extends SCAClientFactory> factoryImplClass = loadProviderFactoryClass(factoryImplClassName, classLoader); + final SCAClientFactory factory = instantiateSCAClientFactoryClass(factoryImplClass); + return factory; + } + + /** + * Gets the Context ClassLoader for the current Thread. + * + * @return The Context ClassLoader for the current Thread. + */ + private static ClassLoader getThreadContextClassLoader() { + final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader(); + return threadClassLoader; + } + + /** + * Attempts to discover the class name for the SCAClientFactory + * implementation from the specified Properties, the System Properties or + * the specified ClassLoader. + * + * @return The class name of the SCAClientFactory implementation + * @throw SCARuntimeException Failed to find implementation for + * SCAClientFactory. + */ + private static String discoverProviderFactoryImplClass(Properties properties, ClassLoader classLoader) throws SCARuntimeException { + String providerClassName = checkPropertiesForSPIClassName(properties); + if (providerClassName != null) { + return providerClassName; + } + + providerClassName = checkPropertiesForSPIClassName(System.getProperties()); + if (providerClassName != null) { + return providerClassName; + } + + return checkMETAINFServicesForClassName(classLoader); + } + + /** + * Attempts to find the class name for the SCAClientFactory implementation + * from the specified Properties. + * + * @return The class name for the SCAClientFactory implementation or + * <code>null</code> if not found. + */ + private static String checkPropertiesForSPIClassName(Properties properties) { + if (properties == null) { + return null; + } + + final String providerClassName = properties.getProperty(SCA_CLIENT_FACTORY_PROVIDER_KEY); + if (providerClassName != null && providerClassName.length() > 0) { + return providerClassName; + } + + return null; + } + + /** + * Attempts to find the class name for the SCAClientFactory implementation + * from the META-INF/services directory + * + * @return The class name for the SCAClientFactory implementation or + * <code>null</code> if not found. + */ + private static String checkMETAINFServicesForClassName(ClassLoader cl) { + final URL url = cl.getResource(SCA_CLIENT_FACTORY_PROVIDER_META_INF_SERVICE); + if (url == null) { + return null; + } + + InputStream in = null; + try { + in = url.openStream(); + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); + + String line; + while ((line = readNextLine(reader)) != null) { + if (!line.startsWith("#") && line.length() > 0) { + return line; + } + } + return null; + } finally { + closeStream(reader); + } + } catch (IOException ex) { + throw new SCARuntimeException("Failed to discover SCAClientFactory provider", ex); + } finally { + closeStream(in); + } + } + + /** + * Reads the next line from the reader and returns the trimmed version of + * that line + * + * @param reader The reader from which to read the next line + * @return The trimmed next line or <code>null</code> if the end of the + * stream has been reached + * @throws IOException I/O error occurred while reading from Reader + */ + private static String readNextLine(BufferedReader reader) throws IOException { + + String line = reader.readLine(); + if (line != null) { + line = line.trim(); + } + return line; + } + + /** + * Loads the specified SCAClientFactory implementation class. + * + * @param factoryImplClassName The name of the SCAClientFactory + * Implementation class to load + * @return The specified SCAClientFactory Implementation class + * @throws SCARuntimeException Failed to load the SCAClientFactory + * implementation class + */ + private static Class<? extends SCAClientFactory> loadProviderFactoryClass(String factoryImplClassName, ClassLoader classLoader) + throws SCARuntimeException { + + try { + final Class<?> providerClass = classLoader.loadClass(factoryImplClassName); + final Class<? extends SCAClientFactory> providerFactoryClass = providerClass.asSubclass(SCAClientFactory.class); + return providerFactoryClass; + } catch (ClassNotFoundException ex) { + throw new SCARuntimeException("Failed to load SCAClientFactory implementation class " + factoryImplClassName, ex); + } catch (ClassCastException ex) { + throw new SCARuntimeException("Loaded SCAClientFactory implementation class " + factoryImplClassName + + " is not a subclass of " + + SCAClientFactory.class.getName(), ex); + } + } + + /** + * Instantiate an instance of the specified SCAClientFactory implementation + * class. + * + * @param factoryImplClass The SCAClientFactory implementation class to + * instantiate. + * @return An instance of the SCAClientFactory implementation class + * @throws SCARuntimeException Failed to instantiate the specified specified + * SCAClientFactory implementation class + */ + private static SCAClientFactory instantiateSCAClientFactoryClass(Class<? extends SCAClientFactory> factoryImplClass) throws SCARuntimeException { + + try { + final SCAClientFactory provider = factoryImplClass.newInstance(); + return provider; + } catch (Throwable ex) { + throw new SCARuntimeException("Failed to instantiate SCAClientFactory implementation class " + factoryImplClass, ex); + } + } + + /** + * Utility method for closing Closeable Object. + * + * @param closeable The Object to close. + */ + private static void closeStream(Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (IOException ex) { + throw new SCARuntimeException("Failed to close stream", ex); + } + } + } +} |