Updated Branches: refs/heads/master 3b52b8f8e -> 3b6277460
DELTASPIKE-399 Fixed test that was failing in Weld and implemented package renaming peer review note. Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/fc149b09 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/fc149b09 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/fc149b09 Branch: refs/heads/master Commit: fc149b09fdc76c3a8c394a78d2f5a2cfa282f834 Parents: 3b52b8f Author: John D. Ament <[email protected]> Authored: Tue Dec 17 23:16:54 2013 -0500 Committer: John D. Ament <[email protected]> Committed: Tue Dec 17 23:16:54 2013 -0500 ---------------------------------------------------------------------- .../api/resoureLoader/ExternalResource.java | 41 ------ .../resoureLoader/ExternalResourceLiteral.java | 38 ------ .../core/api/resoureLoader/XMLProperties.java | 35 ----- .../api/resoureloader/ExternalResource.java | 41 ++++++ .../resoureloader/ExternalResourceLiteral.java | 38 ++++++ .../core/api/resoureloader/XMLProperties.java | 35 +++++ .../ExternalResourceProvider.java | 38 ------ .../ExternalResourceProvider.java | 38 ++++++ .../ClasspathResourceProvider.java | 83 ------------ .../ExternalResourceProducer.java | 128 ------------------- .../ExternalResourceProviderComparator.java | 35 ----- .../resourceLoader/FileResourceProvider.java | 68 ---------- .../resourceLoader/ResourceLoaderExtension.java | 40 ------ .../ClasspathResourceProvider.java | 83 ++++++++++++ .../ExternalResourceProducer.java | 128 +++++++++++++++++++ .../ExternalResourceProviderComparator.java | 35 +++++ .../resourceloader/FileResourceProvider.java | 68 ++++++++++ .../resourceloader/ResourceLoaderExtension.java | 40 ++++++ .../impl/resourceLoader/InjectResourceTest.java | 78 ----------- .../impl/resourceloader/InjectResourceTest.java | 71 ++++++++++ 20 files changed, 577 insertions(+), 584 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResource.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResource.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResource.java deleted file mode 100644 index 1102c14..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResource.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.api.resoureLoader; - -import javax.enterprise.util.Nonbinding; -import javax.inject.Qualifier; -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.PARAMETER; -import static java.lang.annotation.ElementType.METHOD; - -@Target( { TYPE, METHOD, PARAMETER, FIELD }) -@Retention(value = RetentionPolicy.RUNTIME) -@Documented -@Qualifier -public @interface ExternalResource -{ - @Nonbinding - String value(); -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResourceLiteral.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResourceLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResourceLiteral.java deleted file mode 100644 index d1ed1d1..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/ExternalResourceLiteral.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.api.resoureLoader; - -import javax.enterprise.util.AnnotationLiteral; - -/** - * - */ -public class ExternalResourceLiteral extends AnnotationLiteral<ExternalResource> implements ExternalResource -{ - private String value; - public ExternalResourceLiteral(final String value) - { - this.value = value; - } - @Override - public String value() - { - return this.value; - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/XMLProperties.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/XMLProperties.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/XMLProperties.java deleted file mode 100644 index 9e8caac..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureLoader/XMLProperties.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.api.resoureLoader; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.PARAMETER; -import static java.lang.annotation.ElementType.METHOD; - -@Target( { METHOD, PARAMETER, FIELD }) -@Retention(value = RetentionPolicy.RUNTIME) -@Documented -public @interface XMLProperties -{ -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResource.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResource.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResource.java new file mode 100644 index 0000000..4c88284 --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResource.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.apache.deltaspike.core.api.resoureloader; + +import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.METHOD; + +@Target( { TYPE, METHOD, PARAMETER, FIELD }) +@Retention(value = RetentionPolicy.RUNTIME) +@Documented +@Qualifier +public @interface ExternalResource +{ + @Nonbinding + String value(); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResourceLiteral.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResourceLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResourceLiteral.java new file mode 100644 index 0000000..c93797b --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/ExternalResourceLiteral.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.deltaspike.core.api.resoureloader; + +import javax.enterprise.util.AnnotationLiteral; + +/** + * + */ +public class ExternalResourceLiteral extends AnnotationLiteral<ExternalResource> implements ExternalResource +{ + private String value; + public ExternalResourceLiteral(final String value) + { + this.value = value; + } + @Override + public String value() + { + return this.value; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/XMLProperties.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/XMLProperties.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/XMLProperties.java new file mode 100644 index 0000000..9a1a4a4 --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resoureloader/XMLProperties.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.deltaspike.core.api.resoureloader; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.METHOD; + +@Target( { METHOD, PARAMETER, FIELD }) +@Retention(value = RetentionPolicy.RUNTIME) +@Documented +public @interface XMLProperties +{ +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceLoader/ExternalResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceLoader/ExternalResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceLoader/ExternalResourceProvider.java deleted file mode 100644 index f65f57f..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceLoader/ExternalResourceProvider.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.spi.resourceLoader; - -import org.apache.deltaspike.core.api.resoureLoader.ExternalResource; - -import javax.enterprise.inject.spi.InjectionPoint; -import java.io.InputStream; - -/** - * Provides lookup capability to find a resource. - * - * @author johndament - */ -public interface ExternalResourceProvider -{ - - InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint); - - int getPriority(); - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.java new file mode 100644 index 0000000..05f2788 --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/ExternalResourceProvider.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.deltaspike.core.spi.resourceloader; + +import org.apache.deltaspike.core.api.resoureloader.ExternalResource; + +import javax.enterprise.inject.spi.InjectionPoint; +import java.io.InputStream; + +/** + * Provides lookup capability to find a resource. + * + * @author johndament + */ +public interface ExternalResourceProvider +{ + + InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint); + + int getPriority(); + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ClasspathResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ClasspathResourceProvider.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ClasspathResourceProvider.java deleted file mode 100644 index dfc25e4..0000000 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ClasspathResourceProvider.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.impl.resourceLoader; - -import org.apache.deltaspike.core.api.resoureLoader.ExternalResource; -import org.apache.deltaspike.core.spi.resourceLoader.ExternalResourceProvider; -import org.apache.deltaspike.core.util.ClassUtils; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.spi.InjectionPoint; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Enumeration; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * A classpath based resource provider - */ -@ApplicationScoped -public class ClasspathResourceProvider implements ExternalResourceProvider -{ - private static final Logger logger = Logger.getLogger(ClasspathResourceProvider.class.getCanonicalName()); - - @Override - public InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint) - { - try - { - return readClassPath(externalResource.value()); - } - catch (IOException e) - { - if (logger.isLoggable(Level.FINE)) - { - logger.log(Level.FINE, "Problem reading resource.", e); - } - return null; - } - } - - @Override - public int getPriority() - { - return 10; - } - - private InputStream readClassPath(final String name) throws IOException - { - Enumeration<URL> urls = ClassUtils.getClassLoader(null).getResources(name); - if (logger.isLoggable(Level.FINEST)) - { - logger.finest("Found URLS " + urls); - } - while (urls.hasMoreElements()) - { - URL url = urls.nextElement(); - InputStream is = url.openStream(); - if (is != null) - { - return is; - } - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProducer.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProducer.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProducer.java deleted file mode 100644 index f880d66..0000000 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProducer.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.impl.resourceLoader; - -import org.apache.deltaspike.core.api.resoureLoader.ExternalResource; -import org.apache.deltaspike.core.api.resoureLoader.XMLProperties; -import org.apache.deltaspike.core.spi.resourceLoader.ExternalResourceProvider; -import org.apache.deltaspike.core.util.BeanUtils; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Any; -import javax.enterprise.inject.Disposes; -import javax.enterprise.inject.Instance; -import javax.enterprise.inject.Produces; -import javax.enterprise.inject.spi.InjectionPoint; -import javax.inject.Inject; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Properties; -import java.util.logging.Logger; - -/** - * Handles the creation/loading of external resources. - * - */ -@ApplicationScoped -public class ExternalResourceProducer -{ - private static final Logger logger = Logger.getLogger(ExternalResourceProducer.class.getCanonicalName()); - - @Inject - @Any - private Instance<ExternalResourceProvider> resourceProviders; - - @Produces - @ExternalResource("") - public InputStream getInputStream(final InjectionPoint injectionPoint) - { - final InputStream is = findInputStream(injectionPoint); - return is; - } - - @Produces - @ExternalResource("") - public Properties getProperties(final InjectionPoint injectionPoint) throws IOException - { - final InputStream is = findInputStream(injectionPoint); - final boolean isXml = BeanUtils.extractAnnotation(injectionPoint.getAnnotated(), XMLProperties.class) != null; - if (is != null) - { - Properties properties = new Properties(); - if (isXml) - { - properties.loadFromXML(is); - } - else - { - properties.load(is); - } - is.close(); - return properties; - } - else - { - return null; - } - } - - public void closeInputStream(@Disposes @ExternalResource("") InputStream inputStream) - { - if (inputStream != null) - { - try - { - inputStream.close(); - } - catch (IOException e) - { - - } - } - } - - private ExternalResource getAnnotation(final InjectionPoint injectionPoint) - { - return BeanUtils.extractAnnotation(injectionPoint.getAnnotated(),ExternalResource.class); - } - - private InputStream findInputStream(final InjectionPoint injectionPoint) - { - final ExternalResource externalResource = getAnnotation(injectionPoint); - final List<ExternalResourceProvider> providerList = new ArrayList<ExternalResourceProvider>(); - for (ExternalResourceProvider erp : resourceProviders) - { - providerList.add(erp); - } - Collections.sort(providerList,new ExternalResourceProviderComparator()); - for (final ExternalResourceProvider provider : providerList) - { - final InputStream is = provider.readStream(externalResource,injectionPoint); - if (is != null) - { - return is; - } - } - return null; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProviderComparator.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProviderComparator.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProviderComparator.java deleted file mode 100644 index 2ad601b..0000000 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ExternalResourceProviderComparator.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.impl.resourceLoader; - -import org.apache.deltaspike.core.spi.resourceLoader.ExternalResourceProvider; - -import java.util.Comparator; - -/** - * Compares two external resources. - */ -public class ExternalResourceProviderComparator implements Comparator<ExternalResourceProvider> -{ - @Override - public int compare(ExternalResourceProvider o1, ExternalResourceProvider o2) - { - return o1.getPriority() - o2.getPriority(); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/FileResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/FileResourceProvider.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/FileResourceProvider.java deleted file mode 100644 index ddabf41..0000000 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/FileResourceProvider.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.impl.resourceLoader; - -import org.apache.deltaspike.core.api.resoureLoader.ExternalResource; -import org.apache.deltaspike.core.spi.resourceLoader.ExternalResourceProvider; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.spi.InjectionPoint; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; - -/** - * A file based resource provider, looking for a file based on the name. - */ -@ApplicationScoped -public class FileResourceProvider implements ExternalResourceProvider -{ - @Override - public InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint) - { - return readFile(externalResource.value()); - } - - @Override - public int getPriority() - { - return 20; - } - - InputStream readFile(final String name) - { - File f = new File(name); - if (f.exists() && f.canRead() && f.isFile()) - { - try - { - return new FileInputStream(f); - } - catch (FileNotFoundException e) - { - return null; - } - } - else - { - return null; - } - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ResourceLoaderExtension.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ResourceLoaderExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ResourceLoaderExtension.java deleted file mode 100644 index 344c5c4..0000000 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceLoader/ResourceLoaderExtension.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.deltaspike.core.impl.resourceLoader; - -import javax.enterprise.inject.spi.*; - -/** - * This is needed for certain class loading cases (EARs, external modules). - * Simply registers additional resource loader classes to the context. - */ -public class ResourceLoaderExtension implements Extension -{ - public void addResourceLoaders(final BeforeBeanDiscovery beforeBeanDiscovery, final BeanManager beanManager) - { - beforeBeanDiscovery.addAnnotatedType(this.createAnnotatedType(ClasspathResourceProvider.class,beanManager)); - beforeBeanDiscovery.addAnnotatedType(this.createAnnotatedType(ExternalResourceProducer.class,beanManager)); - beforeBeanDiscovery.addAnnotatedType(this.createAnnotatedType(FileResourceProvider.class,beanManager)); - } - - private AnnotatedType<?> createAnnotatedType(final Class<?> clazz, final BeanManager beanManager) - { - return beanManager.createAnnotatedType(clazz); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java new file mode 100644 index 0000000..0004346 --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ClasspathResourceProvider.java @@ -0,0 +1,83 @@ +/* + * 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.deltaspike.core.impl.resourceloader; + +import org.apache.deltaspike.core.api.resoureloader.ExternalResource; +import org.apache.deltaspike.core.spi.resourceloader.ExternalResourceProvider; +import org.apache.deltaspike.core.util.ClassUtils; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.spi.InjectionPoint; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Enumeration; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * A classpath based resource provider + */ +@ApplicationScoped +public class ClasspathResourceProvider implements ExternalResourceProvider +{ + private static final Logger logger = Logger.getLogger(ClasspathResourceProvider.class.getCanonicalName()); + + @Override + public InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint) + { + try + { + return readClassPath(externalResource.value()); + } + catch (IOException e) + { + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, "Problem reading resource.", e); + } + return null; + } + } + + @Override + public int getPriority() + { + return 10; + } + + private InputStream readClassPath(final String name) throws IOException + { + Enumeration<URL> urls = ClassUtils.getClassLoader(null).getResources(name); + if (logger.isLoggable(Level.FINEST)) + { + logger.finest("Found URLS " + urls); + } + while (urls.hasMoreElements()) + { + URL url = urls.nextElement(); + InputStream is = url.openStream(); + if (is != null) + { + return is; + } + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java new file mode 100644 index 0000000..6807ce8 --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java @@ -0,0 +1,128 @@ +/* + * 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.deltaspike.core.impl.resourceloader; + +import org.apache.deltaspike.core.api.resoureloader.ExternalResource; +import org.apache.deltaspike.core.api.resoureloader.XMLProperties; +import org.apache.deltaspike.core.spi.resourceloader.ExternalResourceProvider; +import org.apache.deltaspike.core.util.BeanUtils; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Instance; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.inject.Inject; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Properties; +import java.util.logging.Logger; + +/** + * Handles the creation/loading of external resources. + * + */ +@ApplicationScoped +public class ExternalResourceProducer +{ + private static final Logger logger = Logger.getLogger(ExternalResourceProducer.class.getCanonicalName()); + + @Inject + @Any + private Instance<ExternalResourceProvider> resourceProviders; + + @Produces + @ExternalResource("") + public InputStream getInputStream(final InjectionPoint injectionPoint) + { + final InputStream is = findInputStream(injectionPoint); + return is; + } + + @Produces + @ExternalResource("") + public Properties getProperties(final InjectionPoint injectionPoint) throws IOException + { + final InputStream is = findInputStream(injectionPoint); + final boolean isXml = BeanUtils.extractAnnotation(injectionPoint.getAnnotated(), XMLProperties.class) != null; + if (is != null) + { + Properties properties = new Properties(); + if (isXml) + { + properties.loadFromXML(is); + } + else + { + properties.load(is); + } + is.close(); + return properties; + } + else + { + return null; + } + } + + public void closeInputStream(@Disposes @ExternalResource("") InputStream inputStream) + { + if (inputStream != null) + { + try + { + inputStream.close(); + } + catch (IOException e) + { + + } + } + } + + private ExternalResource getAnnotation(final InjectionPoint injectionPoint) + { + return BeanUtils.extractAnnotation(injectionPoint.getAnnotated(),ExternalResource.class); + } + + private InputStream findInputStream(final InjectionPoint injectionPoint) + { + final ExternalResource externalResource = getAnnotation(injectionPoint); + final List<ExternalResourceProvider> providerList = new ArrayList<ExternalResourceProvider>(); + for (ExternalResourceProvider erp : resourceProviders) + { + providerList.add(erp); + } + Collections.sort(providerList,new ExternalResourceProviderComparator()); + for (final ExternalResourceProvider provider : providerList) + { + final InputStream is = provider.readStream(externalResource,injectionPoint); + if (is != null) + { + return is; + } + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProviderComparator.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProviderComparator.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProviderComparator.java new file mode 100644 index 0000000..f61e172 --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProviderComparator.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.deltaspike.core.impl.resourceloader; + +import org.apache.deltaspike.core.spi.resourceloader.ExternalResourceProvider; + +import java.util.Comparator; + +/** + * Compares two external resources. + */ +public class ExternalResourceProviderComparator implements Comparator<ExternalResourceProvider> +{ + @Override + public int compare(ExternalResourceProvider o1, ExternalResourceProvider o2) + { + return o1.getPriority() - o2.getPriority(); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/FileResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/FileResourceProvider.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/FileResourceProvider.java new file mode 100644 index 0000000..320c92b --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/FileResourceProvider.java @@ -0,0 +1,68 @@ +/* + * 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.deltaspike.core.impl.resourceloader; + +import org.apache.deltaspike.core.api.resoureloader.ExternalResource; +import org.apache.deltaspike.core.spi.resourceloader.ExternalResourceProvider; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.spi.InjectionPoint; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + +/** + * A file based resource provider, looking for a file based on the name. + */ +@ApplicationScoped +public class FileResourceProvider implements ExternalResourceProvider +{ + @Override + public InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint) + { + return readFile(externalResource.value()); + } + + @Override + public int getPriority() + { + return 20; + } + + InputStream readFile(final String name) + { + File f = new File(name); + if (f.exists() && f.canRead() && f.isFile()) + { + try + { + return new FileInputStream(f); + } + catch (FileNotFoundException e) + { + return null; + } + } + else + { + return null; + } + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ResourceLoaderExtension.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ResourceLoaderExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ResourceLoaderExtension.java new file mode 100644 index 0000000..98cdb5d --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ResourceLoaderExtension.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.apache.deltaspike.core.impl.resourceloader; + +import javax.enterprise.inject.spi.*; + +/** + * This is needed for certain class loading cases (EARs, external modules). + * Simply registers additional resource loader classes to the context. + */ +public class ResourceLoaderExtension implements Extension +{ + public void addResourceLoaders(final BeforeBeanDiscovery beforeBeanDiscovery, final BeanManager beanManager) + { + beforeBeanDiscovery.addAnnotatedType(this.createAnnotatedType(ClasspathResourceProvider.class,beanManager)); + beforeBeanDiscovery.addAnnotatedType(this.createAnnotatedType(ExternalResourceProducer.class,beanManager)); + beforeBeanDiscovery.addAnnotatedType(this.createAnnotatedType(FileResourceProvider.class,beanManager)); + } + + private AnnotatedType<?> createAnnotatedType(final Class<?> clazz, final BeanManager beanManager) + { + return beanManager.createAnnotatedType(clazz); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceLoader/InjectResourceTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceLoader/InjectResourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceLoader/InjectResourceTest.java deleted file mode 100644 index 473167e..0000000 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceLoader/InjectResourceTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.deltaspike.test.core.impl.resourceLoader; - - -import org.apache.deltaspike.core.api.resoureLoader.ExternalResource; -import org.apache.deltaspike.core.impl.resourceLoader.ClasspathResourceProvider; -import org.apache.deltaspike.core.impl.resourceLoader.ExternalResourceProducer; -import org.apache.deltaspike.core.impl.resourceLoader.FileResourceProvider; -import org.apache.deltaspike.test.util.ArchiveUtils; -import org.jboss.arquillian.container.test.api.Deployment; -import org.jboss.arquillian.junit.Arquillian; -import org.jboss.shrinkwrap.api.Archive; -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.asset.EmptyAsset; -import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; - -import javax.inject.Inject; -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; - -@RunWith(Arquillian.class) -public class InjectResourceTest { - @Deployment - public static Archive<?> createResourceLoaderArchive() - { - Archive<?> arch = ShrinkWrap.create(JavaArchive.class, "resourceLoader.jar") - .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") - .addClasses(ExternalResourceProducer.class, FileResourceProvider.class, ClasspathResourceProvider.class); - for(JavaArchive ja : ArchiveUtils.getDeltaSpikeCoreArchive()) - { - arch.merge(ja); - } - return arch; - } - - @Inject - @ExternalResource("myconfig.properties") - private InputStream inputStream; - - @Inject - @ExternalResource("myconfig.properties") - private Properties props; - - @Test - public void testInputStream() throws IOException { - Assert.assertNotNull(inputStream); - Properties p = new Properties(); - p.load(inputStream); - Assert.assertEquals("somevalue", p.getProperty("some.propertykey", "wrong answer")); - } - - @Test - public void testProperties() { - Assert.assertEquals("somevalue", props.getProperty("some.propertykey", "wrong answer")); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc149b09/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/InjectResourceTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/InjectResourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/InjectResourceTest.java new file mode 100644 index 0000000..ecbd906 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/InjectResourceTest.java @@ -0,0 +1,71 @@ +/* + * 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.deltaspike.test.core.impl.resourceloader; + + +import org.apache.deltaspike.core.api.resoureloader.ExternalResource; +import org.apache.deltaspike.test.util.ArchiveUtils; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +@RunWith(Arquillian.class) +public class InjectResourceTest { + @Deployment + public static Archive<?> createResourceLoaderArchive() + { + Archive<?> arch = ShrinkWrap.create(WebArchive.class, "resourceloader.war") + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") + .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive()); + return arch; + } + + @Inject + @ExternalResource("myconfig.properties") + private InputStream inputStream; + + @Inject + @ExternalResource("myconfig.properties") + private Properties props; + + @Test + public void testInputStream() throws IOException { + Assert.assertNotNull(inputStream); + Properties p = new Properties(); + p.load(inputStream); + Assert.assertEquals("somevalue", p.getProperty("some.propertykey", "wrong answer")); + } + + @Test + public void testProperties() { + Assert.assertEquals("somevalue", props.getProperty("some.propertykey", "wrong answer")); + } +}
