Added Kevin's config injector
Project: http://git-wip-us.apache.org/repos/asf/knox/repo Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/826f3d52 Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/826f3d52 Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/826f3d52 Branch: refs/heads/KNOX-481 Commit: 826f3d526ef89cff70fd1a6e35c2b7c5498eb4af Parents: 2543b3f Author: Sumit Gupta <su...@apache.org> Authored: Thu Jan 29 15:39:22 2015 -0500 Committer: Sumit Gupta <su...@apache.org> Committed: Thu Feb 12 16:01:14 2015 -0500 ---------------------------------------------------------------------- .../impl/DispatchDeploymentContributor.java | 2 +- .../gateway/hive/HiveHttpClientDispatch.java | 20 +- gateway-spi/pom.xml | 4 + .../config/FilterConfigurationAdapter.java | 35 ++ .../FilterConfigurationAdapterDescriptor.java | 29 ++ .../gateway/dispatch/GatewayDispatchFilter.java | 25 +- .../gateway/dispatch/HttpClientDispatch.java | 46 +-- ...ay.config.spi.ConfigurationAdapterDescriptor | 18 + gateway-util-configinjector/pom.xml | 63 +++ .../org/apache/hadoop/gateway/config/Alias.java | 31 ++ .../gateway/config/ConfigurationAdapter.java | 24 ++ .../gateway/config/ConfigurationBinding.java | 24 ++ .../gateway/config/ConfigurationException.java | 30 ++ .../config/ConfigurationInjectorBuilder.java | 107 ++++++ .../apache/hadoop/gateway/config/Configure.java | 30 ++ .../apache/hadoop/gateway/config/Default.java | 31 ++ .../apache/hadoop/gateway/config/Optional.java | 30 ++ .../config/impl/BeanConfigurationAdapter.java | 43 +++ .../BeanConfigurationAdapterDescriptor.java | 30 ++ .../impl/ConfigurationAdapterFactory.java | 132 +++++++ .../impl/DefaultConfigurationBinding.java | 29 ++ .../impl/DefaultConfigurationInjector.java | 224 +++++++++++ .../config/impl/MapConfigurationAdapter.java | 38 ++ .../impl/MapConfigurationAdapterDescriptor.java | 30 ++ .../config/impl/MappedConfigurationBinding.java | 38 ++ .../impl/PropertiesConfigurationAdapter.java | 38 ++ ...ropertiesConfigurationAdapterDescriptor.java | 30 ++ .../AbstractConfigurationAdapterDescriptor.java | 42 ++ .../spi/ConfigurationAdapterDescriptor.java | 28 ++ .../config/spi/ConfigurationInjector.java | 27 ++ ...ay.config.spi.ConfigurationAdapterDescriptor | 20 + ...oop.gateway.config.spi.ConfigurationInjector | 18 + .../gateway/config/AdapterSampleTest.java | 57 +++ .../apache/hadoop/gateway/config/FuncTest.java | 379 +++++++++++++++++++ .../gateway/config/MapFieldSampleTest.java | 45 +++ .../gateway/config/MapMethodSampleTest.java | 49 +++ .../config/PropertiesFactorySampleTest.java | 40 ++ .../config/PropertiesFieldSampleTest.java | 39 ++ .../config/PropertiesMethodSampleTest.java | 57 +++ .../apache/hadoop/gateway/config/UsageTest.java | 43 +++ .../BeanConfigurationAdapterDescriptorTest.java | 57 +++ .../impl/BeanConfigurationAdapterTest.java | 40 ++ pom.xml | 6 + 43 files changed, 2081 insertions(+), 47 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/DispatchDeploymentContributor.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/DispatchDeploymentContributor.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/DispatchDeploymentContributor.java index 3bc260c..345784f 100644 --- a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/DispatchDeploymentContributor.java +++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/DispatchDeploymentContributor.java @@ -61,7 +61,7 @@ public class DispatchDeploymentContributor extends ProviderDeploymentContributor } FilterDescriptor filter = resource.addFilter().name( getName() ).role( getRole() ).impl( GatewayDispatchFilter.class ); filter.param().name(DISPATCH_IMPL_PARAM).value(HttpClientDispatch.class.getName()); - filter.param().name("replayBufferSize").value(replayBufferSize); + filter.param().name(REPLAY_BUFFER_SIZE_PARAM).value(replayBufferSize); if( context.getGatewayConfig().isHadoopKerberosSecured() ) { filter.param().name("kerberos").value("true"); } http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-service-hive/src/main/java/org/apache/hadoop/gateway/hive/HiveHttpClientDispatch.java ---------------------------------------------------------------------- diff --git a/gateway-service-hive/src/main/java/org/apache/hadoop/gateway/hive/HiveHttpClientDispatch.java b/gateway-service-hive/src/main/java/org/apache/hadoop/gateway/hive/HiveHttpClientDispatch.java index 51f6d6b..b19b03d 100644 --- a/gateway-service-hive/src/main/java/org/apache/hadoop/gateway/hive/HiveHttpClientDispatch.java +++ b/gateway-service-hive/src/main/java/org/apache/hadoop/gateway/hive/HiveHttpClientDispatch.java @@ -17,6 +17,7 @@ */ package org.apache.hadoop.gateway.hive; +import org.apache.hadoop.gateway.config.Configure; import org.apache.hadoop.gateway.dispatch.HttpClientDispatch; import org.apache.hadoop.gateway.security.PrimaryPrincipal; import org.apache.http.Header; @@ -46,19 +47,14 @@ import java.security.Principal; * default HttpClientDispatch. */ public class HiveHttpClientDispatch extends HttpClientDispatch { - private static final String BASIC_AUTH_PREEMPTIVE_PARAM = "basicAuthPreemptive"; private static final String PASSWORD_PLACEHOLDER = "*"; private boolean basicAuthPreemptive = false; + private boolean kerberos = false; private static final EmptyJaasCredentials EMPTY_JAAS_CREDENTIALS = new EmptyJaasCredentials(); @Override public void init() { super.init(); - //TODO: [sumit] get config passed in -// String basicAuthPreemptiveString = filterConfig.getInitParameter( BASIC_AUTH_PREEMPTIVE_PARAM ); -// if( basicAuthPreemptiveString != null ) { -// setBasicAuthPreemptive( Boolean.parseBoolean( basicAuthPreemptiveString ) ); -// } } protected Principal getPrimaryPrincipal() { @@ -83,6 +79,7 @@ public class HiveHttpClientDispatch extends HttpClientDispatch { } } + @Configure public void setBasicAuthPreemptive( boolean basicAuthPreemptive ) { this.basicAuthPreemptive = basicAuthPreemptive; } @@ -90,7 +87,16 @@ public class HiveHttpClientDispatch extends HttpClientDispatch { public boolean isBasicAuthPreemptive() { return basicAuthPreemptive; } - + + public boolean isKerberos() { + return kerberos; + } + + @Configure + public void setKerberos(boolean kerberos) { + this.kerberos = kerberos; + } + protected HttpResponse executeKerberosDispatch(HttpUriRequest outboundRequest, DefaultHttpClient client) throws IOException, ClientProtocolException { //DefaultHttpClient client = new DefaultHttpClient(); http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-spi/pom.xml ---------------------------------------------------------------------- diff --git a/gateway-spi/pom.xml b/gateway-spi/pom.xml index 6e7eeea..11ddda7 100644 --- a/gateway-spi/pom.xml +++ b/gateway-spi/pom.xml @@ -48,6 +48,10 @@ </dependency> <dependency> <groupId>${gateway-group}</groupId> + <artifactId>gateway-util-configinjector</artifactId> + </dependency> + <dependency> + <groupId>${gateway-group}</groupId> <artifactId>gateway-util-urltemplate</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapter.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapter.java new file mode 100644 index 0000000..b1d3618 --- /dev/null +++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapter.java @@ -0,0 +1,35 @@ +/** + * 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.hadoop.gateway.config; + + +import javax.servlet.FilterConfig; + +public class FilterConfigurationAdapter implements ConfigurationAdapter { + + private FilterConfig config; + + public FilterConfigurationAdapter(FilterConfig config) { + this.config = config; + } + + @Override + public String getConfigurationValue(String name) throws ConfigurationException { + return config.getInitParameter(name); + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapterDescriptor.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapterDescriptor.java new file mode 100644 index 0000000..4d106e1 --- /dev/null +++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/FilterConfigurationAdapterDescriptor.java @@ -0,0 +1,29 @@ +/** + * 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.hadoop.gateway.config; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import javax.servlet.FilterConfig; + +public class FilterConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public FilterConfigurationAdapterDescriptor() { + add(FilterConfig.class, FilterConfigurationAdapter.class); + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/GatewayDispatchFilter.java ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/GatewayDispatchFilter.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/GatewayDispatchFilter.java index 13fa51b..f359568 100644 --- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/GatewayDispatchFilter.java +++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/GatewayDispatchFilter.java @@ -33,6 +33,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import static org.apache.hadoop.gateway.config.ConfigurationInjectorBuilder.configuration; + public class GatewayDispatchFilter extends AbstractGatewayFilter { private static Map<String, Adapter> METHOD_ADAPTERS = createMethodAdapters(); @@ -54,14 +56,11 @@ public class GatewayDispatchFilter extends AbstractGatewayFilter { super.init(filterConfig); String dispatchImpl = filterConfig.getInitParameter("dispatch-impl"); dispatch = newDispatch(dispatchImpl); + configuration().target(dispatch).source(filterConfig).inject(); CloseableHttpClient client = HttpClients.createSystem(); //[sumit] this can perhaps be stashed in the servlet context to increase sharing of the client dispatch.setHttpClient(client); dispatch.init(); - //TODO [sumit] set buffer size in config object passed to init or use setters before init? -// if (replayBufferSizeString != null) { -// dispatch.setReplayBufferSize(Integer.valueOf(replayBufferSizeString)); -// } } public Dispatch getDispatch() { @@ -139,15 +138,15 @@ public class GatewayDispatchFilter extends AbstractGatewayFilter { } private Dispatch newDispatch(String dispatchImpl) throws ServletException { - try { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - if( loader == null ) { - loader = this.getClass().getClassLoader(); - } - Class<Dispatch> clazz = (Class)loader.loadClass(dispatchImpl); - return clazz.newInstance(); - } catch( Exception e ) { - throw new ServletException( e ); + try { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + if ( loader == null ) { + loader = this.getClass().getClassLoader(); } + Class<Dispatch> clazz = (Class) loader.loadClass(dispatchImpl); + return clazz.newInstance(); + } catch ( Exception e ) { + throw new ServletException(e); + } } } http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/HttpClientDispatch.java ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/HttpClientDispatch.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/HttpClientDispatch.java index 56da081..78fbdb1 100644 --- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/HttpClientDispatch.java +++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/HttpClientDispatch.java @@ -35,6 +35,8 @@ import org.apache.hadoop.gateway.audit.api.AuditServiceFactory; import org.apache.hadoop.gateway.audit.api.Auditor; import org.apache.hadoop.gateway.audit.api.ResourceType; import org.apache.hadoop.gateway.audit.log4j.audit.AuditConstants; +import org.apache.hadoop.gateway.config.Configure; +import org.apache.hadoop.gateway.config.Default; import org.apache.hadoop.gateway.config.GatewayConfig; import org.apache.hadoop.gateway.i18n.messages.MessagesFactory; import org.apache.hadoop.gateway.i18n.resources.ResourcesFactory; @@ -60,26 +62,26 @@ import org.apache.http.message.BasicHeader; */ public class HttpClientDispatch extends AbstractGatewayDispatch { + // private static final String CT_APP_WWW_FORM_URL_ENCODED = "application/x-www-form-urlencoded"; + // private static final String CT_APP_XML = "application/xml"; + protected static final String Q_DELEGATION_EQ = "?delegation="; + protected static final String AMP_DELEGATION_EQ = "&delegation="; + protected static final String COOKIE = "Cookie"; + protected static final String SET_COOKIE = "Set-Cookie"; + protected static final String WWW_AUTHENTICATE = "WWW-Authenticate"; + protected static final String NEGOTIATE = "Negotiate"; - // private static final String CT_APP_WWW_FORM_URL_ENCODED = "application/x-www-form-urlencoded"; - // private static final String CT_APP_XML = "application/xml"; - protected static final String Q_DELEGATION_EQ = "?delegation="; - protected static final String AMP_DELEGATION_EQ = "&delegation="; - protected static final String COOKIE = "Cookie"; - protected static final String SET_COOKIE = "Set-Cookie"; - protected static final String WWW_AUTHENTICATE = "WWW-Authenticate"; - protected static final String NEGOTIATE = "Negotiate"; + protected static SpiGatewayMessages LOG = MessagesFactory.get(SpiGatewayMessages.class); + protected static SpiGatewayResources RES = ResourcesFactory.get(SpiGatewayResources.class); + protected static Auditor auditor = AuditServiceFactory.getAuditService().getAuditor(AuditConstants.DEFAULT_AUDITOR_NAME, + AuditConstants.KNOX_SERVICE_NAME, AuditConstants.KNOX_COMPONENT_NAME); + private static final int DEFAULT_REPLAY_BUFFER_SIZE = 4 * 1024; // 4K - protected static SpiGatewayMessages LOG = MessagesFactory.get(SpiGatewayMessages.class); - protected static SpiGatewayResources RES = ResourcesFactory.get(SpiGatewayResources.class); - protected static Auditor auditor = AuditServiceFactory.getAuditService().getAuditor(AuditConstants.DEFAULT_AUDITOR_NAME, - AuditConstants.KNOX_SERVICE_NAME, AuditConstants.KNOX_COMPONENT_NAME); + protected AppCookieManager appCookieManager; - protected AppCookieManager appCookieManager; + protected static final String REPLAY_BUFFER_SIZE_PARAM = "replayBufferSize"; - protected static final String REPLAY_BUFFER_SIZE_PARAM = "replayBufferSize"; - - private int replayBufferSize = 0; + private int replayBufferSize = 0; @Override public void init() { @@ -95,15 +97,6 @@ public class HttpClientDispatch extends AbstractGatewayDispatch { this.appCookieManager = appCookieManager; } -// protected void init(FilterConfig filterConfig, AppCookieManager cookieManager) throws ServletException { -// super.init(filterConfig); -// appCookieManager = cookieManager; -// String replayBufferSizeString = filterConfig.getInitParameter(REPLAY_BUFFER_SIZE_PARAM); -// if (replayBufferSizeString != null) { -// setReplayBufferSize(Integer.valueOf(replayBufferSizeString)); -// } -// } - protected void executeRequest( HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, @@ -310,7 +303,8 @@ public class HttpClientDispatch extends AbstractGatewayDispatch { return replayBufferSize; } - protected void setReplayBufferSize(int size) { + @Configure + protected void setReplayBufferSize(@Default("4") int size) { replayBufferSize = size; } http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-spi/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor b/gateway-spi/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor new file mode 100755 index 0000000..30196a4 --- /dev/null +++ b/gateway-spi/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor @@ -0,0 +1,18 @@ +########################################################################## +# 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.hadoop.gateway.config.FilterConfigurationAdapterDescriptor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/pom.xml ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/pom.xml b/gateway-util-configinjector/pom.xml new file mode 100755 index 0000000..b247e99 --- /dev/null +++ b/gateway-util-configinjector/pom.xml @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.knox</groupId> + <artifactId>gateway</artifactId> + <version>0.6.0-SNAPSHOT</version> + </parent> + <artifactId>gateway-util-configinjector</artifactId> + + <name>gateway-util-configinjector</name> + <description>A lightweight config injection utility</description> + + <dependencies> + <dependency> + <groupId>commons-beanutils</groupId> + <artifactId>commons-beanutils</artifactId> + <scope>compile</scope> + <version>1.9.2</version> + </dependency> + + <dependency> + <groupId>${gateway-group}</groupId> + <artifactId>gateway-test-utils</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-library</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Alias.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Alias.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Alias.java new file mode 100755 index 0000000..b002e7a --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Alias.java @@ -0,0 +1,31 @@ +/** + * 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.hadoop.gateway.config; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER} ) +@Retention( RetentionPolicy.RUNTIME ) +@Documented +public @interface Alias { + public String value(); +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationAdapter.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationAdapter.java new file mode 100755 index 0000000..22bddc1 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationAdapter.java @@ -0,0 +1,24 @@ +/** + * 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.hadoop.gateway.config; + +public interface ConfigurationAdapter { + + String getConfigurationValue(String name) throws ConfigurationException; + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationBinding.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationBinding.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationBinding.java new file mode 100755 index 0000000..a2b2b67 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationBinding.java @@ -0,0 +1,24 @@ +/** + * 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.hadoop.gateway.config; + +public interface ConfigurationBinding { + + String getConfigurationName(String name); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationException.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationException.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationException.java new file mode 100755 index 0000000..c979cc6 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationException.java @@ -0,0 +1,30 @@ +/** + * 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.hadoop.gateway.config; + +public class ConfigurationException extends RuntimeException { + + public ConfigurationException( String message, Throwable cause ) { + super( message, cause ); + } + + public ConfigurationException( String message ) { + super( message ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationInjectorBuilder.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationInjectorBuilder.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationInjectorBuilder.java new file mode 100755 index 0000000..e0fadbd --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/ConfigurationInjectorBuilder.java @@ -0,0 +1,107 @@ +/** + * 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.hadoop.gateway.config; + +import org.apache.hadoop.gateway.config.impl.ConfigurationAdapterFactory; +import org.apache.hadoop.gateway.config.impl.DefaultConfigurationBinding; +import org.apache.hadoop.gateway.config.impl.MappedConfigurationBinding; +import org.apache.hadoop.gateway.config.spi.ConfigurationInjector; + +import java.util.Iterator; +import java.util.ServiceLoader; + +public class ConfigurationInjectorBuilder { + + private static ConfigurationBinding DEFAULT_BINDING = new DefaultConfigurationBinding(); + private static ConfigurationInjector INSTANCE = null; + + private static synchronized ConfigurationInjector getInjector() { + if( INSTANCE == null ) { + INSTANCE = createInjector(); + } + return INSTANCE; + } + + private static synchronized ConfigurationInjector createInjector() { + ConfigurationInjector injector = null; + ServiceLoader<ConfigurationInjector> loader = ServiceLoader.load( ConfigurationInjector.class ); + if( loader != null ) { + Iterator<ConfigurationInjector> iterator = loader.iterator(); + if( iterator != null ) { + while( iterator.hasNext() ) { + injector = iterator.next(); + break; + } + } + } + if( injector == null ) { + throw new ConfigurationException( String.format( + "Failed to load an implementation of %s", ConfigurationInjector.class.getName() ) ); + } + return injector; + } + + private Object target = null; + private ConfigurationAdapter source = null; + private ConfigurationBinding binding = null; + + public static ConfigurationInjectorBuilder configuration() { + return new ConfigurationInjectorBuilder(); + } + + public ConfigurationInjectorBuilder target( Object target ) { + this.target = target; + return this; + } + + public ConfigurationInjectorBuilder source( Object source ) { + this.source = ConfigurationAdapterFactory.get(source); + return this; + } + + public ConfigurationInjectorBuilder source( ConfigurationAdapter adapter ) { + this.source = adapter; + return this; + } + + public ConfigurationInjectorBuilder binding( ConfigurationBinding binding ) { + this.binding = binding; + return this; + } + + public ConfigurationInjectorBuilder bind( String targetName, String sourceName ) { + ((MappedConfigurationBinding)binding()).bind( targetName, sourceName ); + return this; + } + + public ConfigurationBinding binding() { + if( binding == null ) { + binding = new MappedConfigurationBinding(); + } + return binding; + } + + public void inject() throws ConfigurationException { + ConfigurationInjector injector = getInjector(); + if( binding == null ) { + binding = DEFAULT_BINDING; + } + injector.configure( target, source, binding ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Configure.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Configure.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Configure.java new file mode 100755 index 0000000..895648f --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Configure.java @@ -0,0 +1,30 @@ +/** + * 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.hadoop.gateway.config; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER} ) +@Retention( RetentionPolicy.RUNTIME ) +@Documented +public @interface Configure { +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Default.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Default.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Default.java new file mode 100755 index 0000000..1ef9042 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Default.java @@ -0,0 +1,31 @@ +/** + * 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.hadoop.gateway.config; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target( { ElementType.PARAMETER } ) +@Retention( RetentionPolicy.RUNTIME ) +@Documented +public @interface Default { + public String value(); +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Optional.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Optional.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Optional.java new file mode 100755 index 0000000..1299e75 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/Optional.java @@ -0,0 +1,30 @@ +/** + * 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.hadoop.gateway.config; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target( { ElementType.FIELD } ) +@Retention( RetentionPolicy.RUNTIME ) +@Documented +public @interface Optional { +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapter.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapter.java new file mode 100755 index 0000000..b5eadb5 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapter.java @@ -0,0 +1,43 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationException; + +public class BeanConfigurationAdapter implements ConfigurationAdapter { + + private Object bean; + + public BeanConfigurationAdapter( Object bean ) { + this.bean = bean; + } + + @Override + public String getConfigurationValue( String name ) throws ConfigurationException { + try { + Object obj = PropertyUtils.getSimpleProperty( bean, name ); + String str = obj != null ? obj.toString() : null; + return str; + } catch( Exception e ) { + throw new ConfigurationException( String.format( "" ), e ); + } + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..92a6cca --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java @@ -0,0 +1,30 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import java.util.Map; + +public class BeanConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public BeanConfigurationAdapterDescriptor() { + add( Object.class, BeanConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java new file mode 100755 index 0000000..73cd97b --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java @@ -0,0 +1,132 @@ +/** + * 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.hadoop.gateway.config.impl; + + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationException; +import org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor; + +import java.lang.reflect.Constructor; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; + +public class ConfigurationAdapterFactory { + + private static Map<Class<?>, Class<? extends ConfigurationAdapter>> ADAPTERS = null; + + private static synchronized Map<Class<?>, Class<? extends ConfigurationAdapter>> getAdapters() { + if( ADAPTERS == null ) { + loadAdapters(); + } + return ADAPTERS; + } + + private static void loadAdapters() { + Map<Class<?>, Class<? extends ConfigurationAdapter>> all = + new HashMap<Class<?>, Class<? extends ConfigurationAdapter>>(); + ServiceLoader<ConfigurationAdapterDescriptor> loader = ServiceLoader.load( ConfigurationAdapterDescriptor.class ); + if( loader != null ) { + Iterator<ConfigurationAdapterDescriptor> i = loader.iterator(); + if( i != null ) { + while( i.hasNext() ) { + ConfigurationAdapterDescriptor descriptor = i.next(); + Map<Class<?>, Class<? extends ConfigurationAdapter>> add = descriptor.providedConfigurationAdapters(); + if( add != null ) { + all.putAll( add ); + } + } + } + } + ADAPTERS = Collections.unmodifiableMap( all ); + } + + public static ConfigurationAdapter get( Object config ) throws ConfigurationException { + if( config == null ) { + throw new NullPointerException( "Configuration adapter instantiation impossible for null config object." ); + } + try { + Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters = getAdapters(); + Class configType = config.getClass(); + Class adapterType = findAdapterTypeForConfigTypeOrParent( adapters, configType ); + if( adapterType == null ) { + throw new ConfigurationException( "No configuration adapter found for config type " + configType.getName() ); + } + Constructor c = findConstructorForConfigType( adapterType, configType ); + if( !c.isAccessible() ) { + c.setAccessible( true ); + } + Object adapter = c.newInstance( config ); + return ConfigurationAdapter.class.cast( adapter ); + } catch( ConfigurationException e ) { + throw e; + } catch( Exception e ) { + throw new ConfigurationException( "Configuration adapter instantiation failed.", e ); + } + } + + public static Constructor findConstructorForConfigType( Class<?> adapterType, Class<?> configType ) throws NoSuchMethodException { + Constructor constructor = null; + Constructor[] constructors = adapterType.getConstructors(); + for( Constructor candidate : constructors ) { + Class<?>[] paramTypes = candidate.getParameterTypes(); + if( paramTypes.length == 1 ) { + Class<?> paramType = paramTypes[0]; + if( paramType.isAssignableFrom( configType ) ) { + constructor = candidate; + break; + } + } + } + if( constructor == null ) { + throw new NoSuchMethodException( "No constructor for " + adapterType.getName() + " that will accept " + configType.getName() ); + } + return constructor; + } + + public static Class<? extends ConfigurationAdapter> findAdapterTypeForConfigTypeOrParent( + Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters, Class<?> configType ) { + Class<? extends ConfigurationAdapter> adapterType = null; + while( configType != null ) { + adapterType = findAdapterTypeForConfigType( adapters, configType ); + if( adapterType != null ) { + break; + } + configType = configType.getSuperclass(); + } + return adapterType; + } + + public static Class<? extends ConfigurationAdapter> findAdapterTypeForConfigType( + Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters, Class<?> configType ) { + Class<? extends ConfigurationAdapter> adapterType = adapters.get( configType ); + if( adapterType == null ) { + for( Class interfaceType : configType.getInterfaces() ) { + adapterType = findAdapterTypeForConfigTypeOrParent( adapters, interfaceType ); + if( adapterType != null ) { + break; + } + } + } + return adapterType; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java new file mode 100755 index 0000000..5629bc1 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java @@ -0,0 +1,29 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationBinding; + +public class DefaultConfigurationBinding implements ConfigurationBinding { + + @Override + public String getConfigurationName( String name ) { + return name; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java new file mode 100755 index 0000000..98b4bae --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java @@ -0,0 +1,224 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.commons.beanutils.ConvertUtilsBean2; +import org.apache.hadoop.gateway.config.*; +import org.apache.hadoop.gateway.config.spi.ConfigurationInjector; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +public class DefaultConfigurationInjector implements ConfigurationInjector { + + private static ConvertUtilsBean2 DEFAULT_CONVERTER = new ConvertUtilsBean2(); + + @Override + public void configure( Object target, ConfigurationAdapter adapter, ConfigurationBinding binding ) + throws ConfigurationException { + Class type = target.getClass(); + while( type != null ) { + injectClass( type, target, adapter, binding ); + type = type.getSuperclass(); + } + } + + private void injectClass( Class type, Object target, ConfigurationAdapter config, ConfigurationBinding binding ) + throws ConfigurationException { + Field[] fields = type.getDeclaredFields(); + for( Field field : fields ) { + injectFieldValue( field, target, config, binding ); + } + Method[] methods = type.getDeclaredMethods(); + for( Method method : methods ) { + injectMethodValue( method, target, config, binding ); + } + } + + private void injectFieldValue( Field field, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding ) + throws ConfigurationException { + Configure annotation = field.getAnnotation( Configure.class ); + if( annotation != null ) { + Alias alias = field.getAnnotation( Alias.class ); + String name = getConfigName( field, alias ); + String bind = getBindName( target, name, binding ); + Object value = retrieveValue( target, bind, name, field.getType(), adapter, binding ); + if( value == null ) { + Optional optional = field.getAnnotation( Optional.class ); + if( optional == null ) { + throw new ConfigurationException( String.format( + "Failed to find configuration for %s bound to %s of %s via %s", + bind, name, target.getClass().getName(), adapter.getClass().getName() ) ); + } + } else { + try { + if( !field.isAccessible() ) { + field.setAccessible( true ); + } + field.set( target, value ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to inject field configuration property %s of %s", + name, target.getClass().getName() ), e ); + } + } + } + } + + private void injectMethodValue( Method method, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding ) + throws ConfigurationException { + Configure methodTag = method.getAnnotation( Configure.class ); + if( methodTag != null ) { + Alias aliasTag = method.getAnnotation( Alias.class ); + String methodName = getConfigName( method, aliasTag ); + Class[] argTypes = method.getParameterTypes(); + Object[] args = new Object[ argTypes.length ]; + Annotation[][] argTags = method.getParameterAnnotations(); + for( int i=0; i<argTypes.length; i++ ) { + String argName = getConfigName( methodName, argTags[i] ); + String bndName = getBindName( target, argName, binding ); + Object argValue = retrieveValue( target, bndName, argName, argTypes[i], adapter, binding ); + if( argValue == null ) { + Default defTag = findAnnotation( argTags[i], Default.class ); + if( defTag != null ) { + String strValue = defTag.value(); + argValue = convertValue( target, argName, strValue, argTypes[i] ); + } else { + throw new ConfigurationException( String.format( + "Failed to find configuration for %s of %s via %s", + bndName, argName, target.getClass().getName(), adapter.getClass().getName() ) ); + } + } + args[ i ] = argValue; + } + if( !method.isAccessible() ) { + method.setAccessible( true ); + } + try { + method.invoke( target, args ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to inject method configuration via %s of %s", + methodName, target.getClass().getName() ), e ); + } + } + } + + private Object convertValue( Object target, String name, String strValue, Class<?> type ) { + Object objValue = null; + try { + objValue = DEFAULT_CONVERTER.convert( strValue, type ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to convert configuration for %s of %s to %s", + name, target.getClass().getName(), type.getName() ), e ); + } + return objValue; + } + + private Object retrieveValue( Object target, String bind, String name, Class<?> type, ConfigurationAdapter adapter, ConfigurationBinding binding ) { + String strValue = null; + try { + strValue = adapter.getConfigurationValue( bind ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to retrieve configuration for %s bound to %s of %s via %s", + bind, name, target.getClass().getName(), adapter.getClass().getName() ), e ); + } + Object objValue = convertValue( target, name, strValue, type ); + return objValue; + } + + private <T extends Annotation> T findAnnotation( Annotation[] annotations, Class<T> type ) { + T found = null; + for( Annotation current : annotations ) { + if( type.isAssignableFrom( current.getClass() ) ) { + found = (T)current; + break; + } + } + return found; + } + + private static String pickName( String implied, Alias explicit ) { + String name = implied; + if( explicit != null ) { + String tagValue = explicit.value().trim(); + if( tagValue.length() > 0 ) { + name = tagValue; + } + } + return name; + } + + private static String getBindName( Object target, String name, ConfigurationBinding binding ) { + String bind = null; + try { + bind = binding.getConfigurationName( name ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to bind configuration for %s of %s via %s", + name, target.getClass().getName(), binding.getClass().getName() ), e ); + } + if( bind == null ) { + bind = name; + } + return bind; + } + + private static String getConfigName( Field field, Alias tag ) { + return pickName( field.getName(), tag ); + } + + private static String getConfigName( String name, Annotation[] tags ) { + if( tags != null ) { + for( Annotation tag : tags ) { + if( tag != null && tag instanceof Alias ) { + Alias aliasTag = Alias.class.cast( tag ); + String aliasValue = aliasTag.value().trim(); + if( aliasValue.length() > 0 ) { + name = aliasValue; + break; + } + } + } + } + return name; + } + + private static String getConfigName( Method method, Alias tag ) { + return pickName( getConfigName( method ), tag ); + } + + private static String getConfigName( Method method ) { + String methodName = method.getName(); + StringBuilder name = new StringBuilder( methodName.length() ); + if( methodName != null && + methodName.length() > 3 && + methodName.startsWith( "set" ) && + Character.isUpperCase( methodName.charAt( 3 ) ) ) { + name.append( methodName.substring( 3 ) ); + name.setCharAt( 0, Character.toLowerCase( name.charAt( 0 ) ) ); + } else { + name.append( name ); + } + return name.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java new file mode 100755 index 0000000..c70a473 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java @@ -0,0 +1,38 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.Map; + +public class MapConfigurationAdapter implements ConfigurationAdapter { + + private Map config; + + public MapConfigurationAdapter( Map map ) { + this.config = map; + } + + @Override + public String getConfigurationValue( String name ) { + Object value = config.get( name ); + return value == null ? null : value.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..4099483 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java @@ -0,0 +1,30 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import java.util.Map; + +public class MapConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public MapConfigurationAdapterDescriptor() { + add( Map.class, MapConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java new file mode 100755 index 0000000..161869f --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java @@ -0,0 +1,38 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationBinding; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class MappedConfigurationBinding implements ConfigurationBinding { + + private Map<String,String> map = new ConcurrentHashMap<String, String>(); + + public void bind( String targetName, String sourceName ) { + map.put( targetName, sourceName ); + } + + @Override + public String getConfigurationName( String name ) { + return map.get( name ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java new file mode 100755 index 0000000..9378501 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java @@ -0,0 +1,38 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationException; + +import java.util.Properties; + +public class PropertiesConfigurationAdapter implements ConfigurationAdapter { + + private Properties properties; + + public PropertiesConfigurationAdapter( Properties properties ) { + this.properties = properties; + } + + @Override + public String getConfigurationValue( String name ) throws ConfigurationException { + return properties.getProperty( name ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..b93e49a --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java @@ -0,0 +1,30 @@ +/** + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import java.util.Properties; + +public class PropertiesConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public PropertiesConfigurationAdapterDescriptor() { + add( Properties.class, PropertiesConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..5372911 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java @@ -0,0 +1,42 @@ +/** + * 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.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.HashMap; +import java.util.Map; + +public abstract class AbstractConfigurationAdapterDescriptor implements ConfigurationAdapterDescriptor { + + private static Map<Class<?>, Class<? extends ConfigurationAdapter>> ADAPTERS = + new HashMap<Class<?>, Class<? extends ConfigurationAdapter>>(); + + protected AbstractConfigurationAdapterDescriptor() { + } + + protected void add( Class<?> configType, Class<? extends ConfigurationAdapter> adapterType ) { + ADAPTERS.put( configType, adapterType ); + } + + @Override + public Map<Class<?>, Class<? extends ConfigurationAdapter>> providedConfigurationAdapters() { + return ADAPTERS; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..3e304d4 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java @@ -0,0 +1,28 @@ +/** + * 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.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.Map; + +public interface ConfigurationAdapterDescriptor { + + Map<Class<?>,Class<? extends ConfigurationAdapter>> providedConfigurationAdapters(); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java new file mode 100755 index 0000000..0bbd1d9 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java @@ -0,0 +1,27 @@ +/** + * 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.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationBinding; + +public interface ConfigurationInjector { + + void configure(Object target, ConfigurationAdapter adapter, ConfigurationBinding binding); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor new file mode 100755 index 0000000..5a240e0 --- /dev/null +++ b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor @@ -0,0 +1,20 @@ +########################################################################## +# 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.hadoop.gateway.config.impl.MapConfigurationAdapterDescriptor +org.apache.hadoop.gateway.config.impl.PropertiesConfigurationAdapterDescriptor +org.apache.hadoop.gateway.config.impl.BeanConfigurationAdapterDescriptor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector new file mode 100755 index 0000000..e3a0d65 --- /dev/null +++ b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector @@ -0,0 +1,18 @@ +########################################################################## +# 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.hadoop.gateway.config.impl.DefaultConfigurationInjector \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/826f3d52/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java new file mode 100755 index 0000000..a3b807a --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java @@ -0,0 +1,57 @@ +/** + * 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.hadoop.gateway.config; + +import org.junit.Test; + +import java.util.Hashtable; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class AdapterSampleTest { + + public static class Target { + @Configure + private String username = null; + } + + public static class Adapter implements ConfigurationAdapter { + private Hashtable config; + public Adapter( Hashtable config ) { + this.config = config; + } + @Override + public String getConfigurationValue( String name ) throws ConfigurationException { + Object value = config.get( name.toUpperCase() ); + return value == null ? null : value.toString(); + } + } + + static Hashtable config = new Hashtable(); + static{ config.put( "USERNAME", "somebody" ); } + + @Test + public void sample() { + Target target = new Target(); + Adapter adapter = new Adapter( config ); + ConfigurationInjectorBuilder.configuration().target( target ).source( adapter ).inject(); + assertThat( target.username, is( "somebody" ) ); + } + +}