Updated Branches: refs/heads/master fc03723bb -> d0d28577f
DELTASPIKE-399 Added code to read resources. Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/d0d28577 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/d0d28577 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/d0d28577 Branch: refs/heads/master Commit: d0d28577f722ac159ccd09b69ff296954adcd284 Parents: fc03723 Author: John D. Ament <[email protected]> Authored: Sun Dec 15 20:31:56 2013 -0500 Committer: John D. Ament <[email protected]> Committed: Sun Dec 15 20:32:52 2013 -0500 ---------------------------------------------------------------------- .../api/resoureLoader/ExternalResource.java | 41 ++++++ .../resoureLoader/ExternalResourceLiteral.java | 38 ++++++ .../core/api/resoureLoader/XMLProperties.java | 35 +++++ .../ExternalResourceProvider.java | 38 ++++++ .../ClasspathResourceProvider.java | 83 ++++++++++++ .../ExternalResourceProducer.java | 128 +++++++++++++++++++ .../ExternalResourceProviderComparator.java | 35 +++++ .../resourceLoader/FileResourceProvider.java | 68 ++++++++++ .../resourceLoader/ResourceLoaderExtension.java | 40 ++++++ .../impl/resourceLoader/InjectResourceTest.java | 78 +++++++++++ 10 files changed, 584 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d0d28577/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..1102c14 --- /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/d0d28577/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..d1ed1d1 --- /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/d0d28577/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..9e8caac --- /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/d0d28577/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..f65f57f --- /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/d0d28577/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..dfc25e4 --- /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/d0d28577/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..f880d66 --- /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/d0d28577/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..2ad601b --- /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/d0d28577/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..ddabf41 --- /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/d0d28577/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..344c5c4 --- /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/d0d28577/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..473167e --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceLoader/InjectResourceTest.java @@ -0,0 +1,78 @@ +/* + * 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")); + } +}
