DELTASPIKE-399 incorporated more design discussion changes.
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/a3f626fc Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/a3f626fc Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/a3f626fc Branch: refs/heads/master Commit: a3f626fc84a0b6bf957db3d55f47fe65d638fdea Parents: d351842 Author: John D. Ament <[email protected]> Authored: Thu Dec 26 20:51:20 2013 -0500 Committer: John D. Ament <[email protected]> Committed: Thu Dec 26 20:51:20 2013 -0500 ---------------------------------------------------------------------- .../api/literal/ExternalResourceLiteral.java | 18 +++- .../api/resoureloader/ExternalResource.java | 5 +- .../core/spi/literal/StorageTypeLiteral.java | 37 +++++++ .../ExternalResourceProvider.java | 6 +- .../core/spi/resourceloader/StorageType.java | 46 ++++++++ .../resourceloader/BaseResourceProvider.java | 96 +++++++++++++++++ .../ClasspathResourceProvider.java | 16 +-- .../ExternalResourceProducer.java | 68 ++++-------- .../ExternalResourceProviderComparator.java | 35 ------- .../resourceloader/FileResourceProvider.java | 28 +++-- .../resourceloader/ClasspathResourceTest.java | 73 +++++++++++++ .../impl/resourceloader/FileResourceTest.java | 105 +++++++++++++++++++ .../impl/resourceloader/InjectResourceTest.java | 73 ------------- 13 files changed, 418 insertions(+), 188 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ExternalResourceLiteral.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ExternalResourceLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ExternalResourceLiteral.java index 013966a..d8f20c7 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ExternalResourceLiteral.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ExternalResourceLiteral.java @@ -27,14 +27,22 @@ import javax.enterprise.util.AnnotationLiteral; */ public class ExternalResourceLiteral extends AnnotationLiteral<ExternalResource> implements ExternalResource { - private String value; - public ExternalResourceLiteral(final String value) + private String storage; + private String location; + public ExternalResourceLiteral(final String storage, final String location) { - this.value = value; + this.storage = storage; + this.location = location; } @Override - public String value() + public String location() { - return this.value; + return this.location; + } + + @Override + public String storage() + { + return this.storage; } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/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 index 4c88284..38e9408 100644 --- 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 @@ -37,5 +37,8 @@ import static java.lang.annotation.ElementType.METHOD; public @interface ExternalResource { @Nonbinding - String value(); + String storage(); + + @Nonbinding + String location() default ""; } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/literal/StorageTypeLiteral.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/literal/StorageTypeLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/literal/StorageTypeLiteral.java new file mode 100644 index 0000000..de71d41 --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/literal/StorageTypeLiteral.java @@ -0,0 +1,37 @@ +/* + * 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.literal; + +import org.apache.deltaspike.core.spi.resourceloader.StorageType; + +import javax.enterprise.util.AnnotationLiteral; + +public class StorageTypeLiteral extends AnnotationLiteral<StorageType> implements StorageType +{ + private String value; + public StorageTypeLiteral(String value) + { + this.value = value; + } + @Override + public String value() + { + return this.value; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/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 index 15d6cda..dcad2be 100644 --- 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 @@ -20,8 +20,8 @@ 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; +import java.util.Properties; /** * Provides lookup capability to find a resource. @@ -30,8 +30,8 @@ import java.io.InputStream; public interface ExternalResourceProvider { - InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint); + InputStream readStream(final ExternalResource externalResource); - int getPriority(); + Properties readProperties(final ExternalResource externalResource); } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/StorageType.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/StorageType.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/StorageType.java new file mode 100644 index 0000000..6cb664d --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/resourceloader/StorageType.java @@ -0,0 +1,46 @@ +/* + * 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 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.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.FIELD; + +/** + * Represents the type of storage that the given ExternalResourceProvider handles. + * + */ +@Target( { TYPE, METHOD, PARAMETER, FIELD }) +@Retention(value = RetentionPolicy.RUNTIME) +@Documented +@Qualifier +public @interface StorageType +{ + String FILE = "file"; + String CLASSPATH = "classpath"; + + String value(); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java new file mode 100644 index 0000000..fd16e77 --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/BaseResourceProvider.java @@ -0,0 +1,96 @@ +/* + * 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.inject.Any; +import javax.enterprise.inject.Instance; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.inject.Inject; +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.util.Properties; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * An abstract ExternalResourceProvider implementation with some basic utility functionality. + */ +public abstract class BaseResourceProvider implements ExternalResourceProvider +{ + private static final Logger logger = Logger.getLogger(BaseResourceProvider.class.getName()); + + @Inject + @Any + private Instance<InjectionPoint> injectionPoint; + + protected boolean isXml() + { + final boolean isXml = BeanUtils.extractAnnotation(getInjectionPoint().getAnnotated(), XMLProperties.class) + != null; + return isXml; + } + + protected InjectionPoint getInjectionPoint() + { + return this.injectionPoint.get(); + } + + protected Set<Annotation> getAnnotations() + { + return this.getInjectionPoint().getAnnotated().getAnnotations(); + } + + protected void loadInputStreamToProperties(InputStream inputStream, Properties properties, String name) + { + boolean isXml = this.isXml(); + try + { + if (isXml) + { + properties.loadFromXML(inputStream); + } + else + { + properties.load(inputStream); + } + } + catch (IOException e) + { + logger.log(Level.WARNING,"Unable to read resource " + name,e); + + } + } + + @Override + public Properties readProperties(ExternalResource externalResource) + { + final Properties properties = new Properties(); + final String name = externalResource.location(); + final InputStream inputStream = this.readStream(externalResource); + this.loadInputStreamToProperties(inputStream, properties, name); + return properties; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/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 index 551817c..b552c00 100644 --- 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 @@ -19,11 +19,10 @@ 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.spi.resourceloader.StorageType; 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; @@ -35,16 +34,17 @@ import java.util.logging.Logger; * A classpath based resource provider */ @ApplicationScoped -public class ClasspathResourceProvider implements ExternalResourceProvider +@StorageType(StorageType.CLASSPATH) +public class ClasspathResourceProvider extends BaseResourceProvider { private static final Logger logger = Logger.getLogger(ClasspathResourceProvider.class.getName()); @Override - public InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint) + public InputStream readStream(final ExternalResource externalResource) { try { - return readClassPath(externalResource.value()); + return readClassPath(externalResource.location()); } catch (IOException e) { @@ -56,12 +56,6 @@ public class ClasspathResourceProvider implements ExternalResourceProvider } } - @Override - public int getPriority() - { - return 10; - } - private InputStream readClassPath(final String name) throws IOException { Enumeration<URL> urls = ClassUtils.getClassLoader(null).getResources(name); http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/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 index 06a016c..7ef976a 100644 --- 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 @@ -19,9 +19,9 @@ 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.literal.StorageTypeLiteral; import org.apache.deltaspike.core.spi.resourceloader.ExternalResourceProvider; -import org.apache.deltaspike.core.util.BeanUtils; +import org.apache.deltaspike.core.spi.resourceloader.StorageType; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Any; @@ -32,9 +32,7 @@ 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.lang.annotation.Annotation; import java.util.Properties; import java.util.logging.Logger; @@ -51,41 +49,34 @@ public class ExternalResourceProducer @Any private Instance<ExternalResourceProvider> resourceProviders; + private ExternalResourceProvider getProvider(String storageTypeName) + { + StorageType storageType = new StorageTypeLiteral(storageTypeName); + ExternalResourceProvider provider = resourceProviders.select(storageType).get(); + return provider; + } + @Produces - @ExternalResource("") + @ExternalResource(storage = "",location = "") public InputStream getInputStream(final InjectionPoint injectionPoint) { - final InputStream is = findInputStream(injectionPoint); + ExternalResource externalResource = getAnnotation(injectionPoint); + ExternalResourceProvider provider = getProvider(externalResource.storage()); + final InputStream is = provider.readStream(externalResource); return is; } @Produces - @ExternalResource("") + @ExternalResource(storage = "",location = "") 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; - } + ExternalResource externalResource = getAnnotation(injectionPoint); + ExternalResourceProvider provider = getProvider(externalResource.storage()); + final Properties properties = provider.readProperties(externalResource); + return properties; } - public void closeInputStream(@Disposes @ExternalResource("") InputStream inputStream) + public void closeInputStream(@Disposes @ExternalResource(storage = "",location = "") InputStream inputStream) { if (inputStream != null) { @@ -102,24 +93,11 @@ public class ExternalResourceProducer 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) + for (Annotation annotation : injectionPoint.getQualifiers()) { - final InputStream is = provider.readStream(externalResource,injectionPoint); - if (is != null) + if (annotation instanceof ExternalResource) { - return is; + return (ExternalResource)annotation; } } return null; http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/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 f61e172..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/a3f626fc/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 index 320c92b..0fcbf2d 100644 --- 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 @@ -19,33 +19,24 @@ 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.spi.resourceloader.StorageType; 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; +import java.util.logging.Level; +import java.util.logging.Logger; /** * A file based resource provider, looking for a file based on the name. */ @ApplicationScoped -public class FileResourceProvider implements ExternalResourceProvider +@StorageType(StorageType.FILE) +public class FileResourceProvider extends BaseResourceProvider { - @Override - public InputStream readStream(final ExternalResource externalResource, final InjectionPoint injectionPoint) - { - return readFile(externalResource.value()); - } - - @Override - public int getPriority() - { - return 20; - } - + private static final Logger logger = Logger.getLogger(FileResourceProvider.class.getName()); InputStream readFile(final String name) { File f = new File(name); @@ -57,6 +48,7 @@ public class FileResourceProvider implements ExternalResourceProvider } catch (FileNotFoundException e) { + logger.log(Level.SEVERE, "Problem reading resource.", e); return null; } } @@ -65,4 +57,10 @@ public class FileResourceProvider implements ExternalResourceProvider return null; } } + + @Override + public InputStream readStream(ExternalResource externalResource) + { + return readFile(externalResource.location()); + } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java new file mode 100644 index 0000000..d57bee1 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java @@ -0,0 +1,73 @@ +/* + * 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.spi.resourceloader.StorageType; +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.asset.StringAsset; +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 ClasspathResourceTest { + @Deployment + public static Archive<?> createResourceLoaderArchive() + { + Archive<?> arch = ShrinkWrap.create(WebArchive.class, "resourceloader.war") + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") + .add(new StringAsset("some.propertykey = somevalue"),"WEB-INF/classes/myconfig.properties") + .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive()); + return arch; + } + + @Inject + @ExternalResource(storage = StorageType.CLASSPATH,location="myconfig.properties") + private InputStream inputStream; + + @Inject + @ExternalResource(storage = StorageType.CLASSPATH,location="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/a3f626fc/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java new file mode 100644 index 0000000..932b6c1 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java @@ -0,0 +1,105 @@ +/* + * 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.literal.ExternalResourceLiteral; +import org.apache.deltaspike.core.api.resoureloader.ExternalResource; +import org.apache.deltaspike.core.spi.resourceloader.StorageType; +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.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Instance; +import javax.inject.Inject; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +@RunWith(Arquillian.class) +public class FileResourceTest +{ + private static String tempFileName; + + @BeforeClass + public static void createTempFile() + { + String javaiotmpdir = System.getProperty("java.io.tmpdir","/tmp"); + File tmpDir = new File(javaiotmpdir); + try + { + File f = File.createTempFile("props",System.currentTimeMillis()+"",tmpDir); + FileWriter fw = new FileWriter(f); + fw.write("some.propertykey=somevalue"); + fw.close(); + tempFileName = f.getAbsolutePath(); + f.deleteOnExit(); + } + catch (IOException e) + { + + } + } + + @Deployment + public static Archive<?> createResourceLoaderArchive() + { + Archive<?> arch = ShrinkWrap.create(WebArchive.class, "resourceloader.war") + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") + .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive()); + return arch; + } + + @Inject + @Any + private Instance<InputStream> inputStreamInst; + + @Inject + @Any + private Instance<Properties> propsInst; + + @Test + public void testInputStream() throws IOException { + InputStream inputStream = inputStreamInst + .select(new ExternalResourceLiteral(StorageType.FILE, tempFileName)).get(); + Assert.assertNotNull(inputStream); + Properties p = new Properties(); + p.load(inputStream); + Assert.assertEquals("somevalue", p.getProperty("some.propertykey", "wrong answer")); + } + + @Test + public void testProperties() { + Properties props = this.propsInst.select(new ExternalResourceLiteral(StorageType.FILE,tempFileName)).get(); + Assert.assertEquals("somevalue", props.getProperty("some.propertykey", "wrong answer")); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/a3f626fc/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 ced10cd..0000000 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/InjectResourceTest.java +++ /dev/null @@ -1,73 +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.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.asset.StringAsset; -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") - .add(new StringAsset("some.propertykey = somevalue"),"WEB-INF/classes/myconfig.properties") - .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")); - } -}
