Repository: deltaspike Updated Branches: refs/heads/master a28c394ca -> 5ebbdaf00
DELTASPIKE-531 @InjectableResource Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/5ebbdaf0 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/5ebbdaf0 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/5ebbdaf0 Branch: refs/heads/master Commit: 5ebbdaf00f8a0145c473a0f433b94a7c84769ffc Parents: a28c394 Author: gpetracek <[email protected]> Authored: Thu Mar 6 09:02:24 2014 +0100 Committer: gpetracek <[email protected]> Committed: Thu Mar 6 09:02:24 2014 +0100 ---------------------------------------------------------------------- .../api/literal/ExternalResourceLiteral.java | 50 -------- .../api/literal/InjectableResourceLiteral.java | 50 ++++++++ .../AbstractResourceProvider.java | 97 +++++++++++++++ .../resourceloader/BaseResourceProvider.java | 97 --------------- .../ClasspathResourceProvider.java | 10 +- .../api/resourceloader/ExternalResource.java | 44 ------- .../ExternalResourceProvider.java | 38 ------ .../resourceloader/FileResourceProvider.java | 6 +- .../api/resourceloader/InjectableResource.java | 44 +++++++ .../InjectableResourceProvider.java | 38 ++++++ .../ExternalResourceProducer.java | 114 ------------------ .../InjectableResourceProducer.java | 117 +++++++++++++++++++ .../resourceloader/ResourceLoaderExtension.java | 2 +- .../resourceloader/ClasspathResourceTest.java | 10 +- .../resourceloader/ClasspathWebProfileTest.java | 10 +- .../impl/resourceloader/FileResourceTest.java | 6 +- .../api/resourceloader/WebResourceProvider.java | 10 +- .../resourceloader/WebResourceProviderTest.java | 10 +- 18 files changed, 378 insertions(+), 375 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/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 deleted file mode 100644 index 806824c..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/ExternalResourceLiteral.java +++ /dev/null @@ -1,50 +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.literal; - -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; -import org.apache.deltaspike.core.api.resourceloader.ExternalResourceProvider; - -import javax.enterprise.util.AnnotationLiteral; - -public class ExternalResourceLiteral extends AnnotationLiteral<ExternalResource> implements ExternalResource -{ - private static final long serialVersionUID = 1705986508118055892L; - - private final Class<? extends ExternalResourceProvider> resourceProvider; - private final String location; - - public ExternalResourceLiteral(Class<? extends ExternalResourceProvider> resourceProvider, String location) - { - this.resourceProvider = resourceProvider; - this.location = location; - } - - @Override - public String location() - { - return this.location; - } - - @Override - public Class<? extends ExternalResourceProvider> resourceProvider() - { - return this.resourceProvider; - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/InjectableResourceLiteral.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/InjectableResourceLiteral.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/InjectableResourceLiteral.java new file mode 100644 index 0000000..c5a83eb --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/literal/InjectableResourceLiteral.java @@ -0,0 +1,50 @@ +/* + * 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.literal; + +import org.apache.deltaspike.core.api.resourceloader.InjectableResource; +import org.apache.deltaspike.core.api.resourceloader.InjectableResourceProvider; + +import javax.enterprise.util.AnnotationLiteral; + +public class InjectableResourceLiteral extends AnnotationLiteral<InjectableResource> implements InjectableResource +{ + private static final long serialVersionUID = 1705986508118055892L; + + private final Class<? extends InjectableResourceProvider> resourceProvider; + private final String location; + + public InjectableResourceLiteral(Class<? extends InjectableResourceProvider> resourceProvider, String location) + { + this.resourceProvider = resourceProvider; + this.location = location; + } + + @Override + public String location() + { + return this.location; + } + + @Override + public Class<? extends InjectableResourceProvider> resourceProvider() + { + return this.resourceProvider; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/AbstractResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/AbstractResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/AbstractResourceProvider.java new file mode 100644 index 0000000..c5625b3 --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/AbstractResourceProvider.java @@ -0,0 +1,97 @@ +/* + * 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.resourceloader; + +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.Collections; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * An abstract InjectableResourceProvider implementation with some basic utility functionality. + */ +public abstract class AbstractResourceProvider implements InjectableResourceProvider +{ + private static final Logger logger = Logger.getLogger(AbstractResourceProvider.class.getName()); + + @Inject + @Any + private Instance<InjectionPoint> injectionPoint; + + protected boolean isXml(String fileName) + { + return fileName.endsWith(".xml"); + } + + 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(name); + 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(InjectableResource injectableResource) + { + final Properties properties = new Properties(); + final String name = injectableResource.location(); + final InputStream inputStream = this.readStream(injectableResource); + this.loadInputStreamToProperties(inputStream, properties, name); + return properties; + } + + @Override + public List<InputStream> readStreams(InjectableResource injectableResource) + { + return Collections.singletonList(this.readStream(injectableResource)); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/BaseResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/BaseResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/BaseResourceProvider.java deleted file mode 100644 index fc8be90..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/BaseResourceProvider.java +++ /dev/null @@ -1,97 +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.resourceloader; - -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.Collections; -import java.util.List; -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(String fileName) - { - return fileName.endsWith(".xml"); - } - - 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(name); - 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; - } - - @Override - public List<InputStream> readStreams(ExternalResource externalResource) - { - return Collections.singletonList(this.readStream(externalResource)); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ClasspathResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ClasspathResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ClasspathResourceProvider.java index 9c31127..f1ff1db 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ClasspathResourceProvider.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ClasspathResourceProvider.java @@ -34,16 +34,16 @@ import java.util.logging.Logger; * A classpath based resource provider */ @ApplicationScoped -public class ClasspathResourceProvider extends BaseResourceProvider +public class ClasspathResourceProvider extends AbstractResourceProvider { private static final Logger logger = Logger.getLogger(ClasspathResourceProvider.class.getName()); @Override - public InputStream readStream(final ExternalResource externalResource) + public InputStream readStream(final InjectableResource injectableResource) { try { - List<InputStream> matchedStreams = this.readClassPath(externalResource.location(),true); + List<InputStream> matchedStreams = this.readClassPath(injectableResource.location(),true); return matchedStreams.get(0); } catch (IOException e) @@ -57,11 +57,11 @@ public class ClasspathResourceProvider extends BaseResourceProvider } @Override - public List<InputStream> readStreams(ExternalResource externalResource) + public List<InputStream> readStreams(InjectableResource injectableResource) { try { - return readClassPath(externalResource.location(),false); + return readClassPath(injectableResource.location(),false); } catch (IOException e) { http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ExternalResource.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ExternalResource.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ExternalResource.java deleted file mode 100644 index 9b76bec..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ExternalResource.java +++ /dev/null @@ -1,44 +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.resourceloader; - -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 - Class<? extends ExternalResourceProvider> resourceProvider() default ClasspathResourceProvider.class; - - @Nonbinding - String location() default ""; -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ExternalResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ExternalResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/ExternalResourceProvider.java deleted file mode 100644 index ac451c0..0000000 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/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.api.resourceloader; - -import java.io.InputStream; -import java.util.List; -import java.util.Properties; - -/** - * Provides lookup capability to find a resource. - * - */ -public interface ExternalResourceProvider -{ - - InputStream readStream(final ExternalResource externalResource); - - List<InputStream> readStreams(final ExternalResource externalResource); - - Properties readProperties(final ExternalResource externalResource); - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/FileResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/FileResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/FileResourceProvider.java index 729250b..8d5f5a0 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/FileResourceProvider.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/FileResourceProvider.java @@ -30,7 +30,7 @@ import java.util.logging.Logger; * A file based resource provider, looking for a file based on the name. */ @ApplicationScoped -public class FileResourceProvider extends BaseResourceProvider +public class FileResourceProvider extends AbstractResourceProvider { private static final Logger logger = Logger.getLogger(FileResourceProvider.class.getName()); InputStream readFile(final String name) @@ -55,8 +55,8 @@ public class FileResourceProvider extends BaseResourceProvider } @Override - public InputStream readStream(ExternalResource externalResource) + public InputStream readStream(InjectableResource injectableResource) { - return readFile(externalResource.location()); + return readFile(injectableResource.location()); } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResource.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResource.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResource.java new file mode 100644 index 0000000..281f59b --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResource.java @@ -0,0 +1,44 @@ +/* + * 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.resourceloader; + +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 InjectableResource +{ + @Nonbinding + Class<? extends InjectableResourceProvider> resourceProvider() default ClasspathResourceProvider.class; + + @Nonbinding + String location() default ""; +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResourceProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResourceProvider.java new file mode 100644 index 0000000..1f0c40c --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/resourceloader/InjectableResourceProvider.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.resourceloader; + +import java.io.InputStream; +import java.util.List; +import java.util.Properties; + +/** + * Provides lookup capability to find a resource. + * + */ +public interface InjectableResourceProvider +{ + + InputStream readStream(final InjectableResource injectableResource); + + List<InputStream> readStreams(final InjectableResource injectableResource); + + Properties readProperties(final InjectableResource injectableResource); + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/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 1da6526..0000000 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/ExternalResourceProducer.java +++ /dev/null @@ -1,114 +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.provider.BeanProvider; -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; -import org.apache.deltaspike.core.api.resourceloader.ExternalResourceProvider; - -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.lang.annotation.Annotation; -import java.util.List; -import java.util.Properties; -import java.util.logging.Level; -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.getName()); - - @Inject - @Any - private Instance<ExternalResourceProvider> resourceProviders; - - @Produces - @ExternalResource(resourceProvider = ExternalResourceProvider.class,location = "") - public InputStream getInputStream(final InjectionPoint injectionPoint) - { - ExternalResource externalResource = getAnnotation(injectionPoint); - ExternalResourceProvider provider = BeanProvider.getContextualReference(externalResource.resourceProvider()); - final InputStream is = provider.readStream(externalResource); - return is; - } - - @Produces - @ExternalResource(resourceProvider = ExternalResourceProvider.class,location = "") - public List<InputStream> getInputStreams(final InjectionPoint injectionPoint) - { - ExternalResource externalResource = getAnnotation(injectionPoint); - ExternalResourceProvider provider = BeanProvider.getContextualReference(externalResource.resourceProvider()); - return provider.readStreams(externalResource); - } - - @Produces - @ExternalResource(resourceProvider = ExternalResourceProvider.class,location = "") - public Properties getProperties(final InjectionPoint injectionPoint) throws IOException - { - ExternalResource externalResource = getAnnotation(injectionPoint); - ExternalResourceProvider provider = BeanProvider.getContextualReference(externalResource.resourceProvider()); - final Properties properties = provider.readProperties(externalResource); - return properties; - } - - public void closeInputStream(@Disposes - @ExternalResource(resourceProvider = ExternalResourceProvider.class, location = "") - InputStream inputStream) - { - if (inputStream != null) - { - try - { - inputStream.close(); - } - catch (IOException e) - { - if (logger.isLoggable(Level.FINE)) - { - logger.log(Level.FINE,"Unable to close input stream ",e); - } - } - } - } - - private ExternalResource getAnnotation(final InjectionPoint injectionPoint) - { - for (Annotation annotation : injectionPoint.getQualifiers()) - { - if (annotation instanceof ExternalResource) - { - return (ExternalResource)annotation; - } - } - return null; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/InjectableResourceProducer.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/InjectableResourceProducer.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/InjectableResourceProducer.java new file mode 100644 index 0000000..1e61376 --- /dev/null +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/resourceloader/InjectableResourceProducer.java @@ -0,0 +1,117 @@ +/* + * 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.provider.BeanProvider; +import org.apache.deltaspike.core.api.resourceloader.InjectableResource; +import org.apache.deltaspike.core.api.resourceloader.InjectableResourceProvider; + +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.lang.annotation.Annotation; +import java.util.List; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Handles the creation/loading of external resources. + * + */ +@ApplicationScoped +public class InjectableResourceProducer +{ + private static final Logger logger = Logger.getLogger(InjectableResourceProducer.class.getName()); + + @Inject + @Any + private Instance<InjectableResourceProvider> resourceProviders; + + @Produces + @InjectableResource(resourceProvider = InjectableResourceProvider.class,location = "") + public InputStream getInputStream(final InjectionPoint injectionPoint) + { + InjectableResource injectableResource = getAnnotation(injectionPoint); + InjectableResourceProvider provider = + BeanProvider.getContextualReference(injectableResource.resourceProvider()); + final InputStream is = provider.readStream(injectableResource); + return is; + } + + @Produces + @InjectableResource(resourceProvider = InjectableResourceProvider.class,location = "") + public List<InputStream> getInputStreams(final InjectionPoint injectionPoint) + { + InjectableResource injectableResource = getAnnotation(injectionPoint); + InjectableResourceProvider provider = + BeanProvider.getContextualReference(injectableResource.resourceProvider()); + return provider.readStreams(injectableResource); + } + + @Produces + @InjectableResource(resourceProvider = InjectableResourceProvider.class,location = "") + public Properties getProperties(final InjectionPoint injectionPoint) throws IOException + { + InjectableResource injectableResource = getAnnotation(injectionPoint); + InjectableResourceProvider provider = + BeanProvider.getContextualReference(injectableResource.resourceProvider()); + final Properties properties = provider.readProperties(injectableResource); + return properties; + } + + public void closeInputStream(@Disposes + @InjectableResource(resourceProvider = InjectableResourceProvider.class, location = "") + InputStream inputStream) + { + if (inputStream != null) + { + try + { + inputStream.close(); + } + catch (IOException e) + { + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE,"Unable to close input stream ",e); + } + } + } + } + + private InjectableResource getAnnotation(final InjectionPoint injectionPoint) + { + for (Annotation annotation : injectionPoint.getQualifiers()) + { + if (annotation instanceof InjectableResource) + { + return (InjectableResource)annotation; + } + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/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 index ca0c2d6..66ab8ce 100644 --- 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 @@ -36,7 +36,7 @@ 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(InjectableResourceProducer.class,beanManager)); beforeBeanDiscovery.addAnnotatedType(this.createAnnotatedType(FileResourceProvider.class,beanManager)); } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/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 index 7aba4c1..384e607 100644 --- 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 @@ -18,7 +18,7 @@ */ package org.apache.deltaspike.test.core.impl.resourceloader; -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; +import org.apache.deltaspike.core.api.resourceloader.InjectableResource; import org.apache.deltaspike.test.category.SeCategory; import org.apache.deltaspike.test.util.ArchiveUtils; import org.jboss.arquillian.container.test.api.Deployment; @@ -52,11 +52,11 @@ public class ClasspathResourceTest } @Inject - @ExternalResource(location="myconfig.properties") + @InjectableResource(location="myconfig.properties") private InputStream inputStream; @Inject - @ExternalResource(location="myconfig.properties") + @InjectableResource(location="myconfig.properties") private Properties properties; @@ -77,14 +77,14 @@ public class ClasspathResourceTest } @Test - public void testAmbiguousFileLookup(@ExternalResource(location="META-INF/beans.xml") InputStream inputStream) + public void testAmbiguousFileLookup(@InjectableResource(location="META-INF/beans.xml") InputStream inputStream) { // for some reason, this works Assert.assertNull(inputStream); } @Test - public void testSuccessfulAmbiguousLookup(@ExternalResource(location="META-INF/beans.xml") List<InputStream> inputStreams) + public void testSuccessfulAmbiguousLookup(@InjectableResource(location="META-INF/beans.xml") List<InputStream> inputStreams) { Assert.assertTrue(inputStreams.size() > 1); //the count is different on as7 compared to the standalone setup } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathWebProfileTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathWebProfileTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathWebProfileTest.java index 9f89f2c..bb7968f 100644 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathWebProfileTest.java +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathWebProfileTest.java @@ -18,7 +18,7 @@ */ package org.apache.deltaspike.test.core.impl.resourceloader; -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; +import org.apache.deltaspike.core.api.resourceloader.InjectableResource; import org.apache.deltaspike.test.category.WebProfileCategory; import org.apache.deltaspike.test.util.ArchiveUtils; import org.jboss.arquillian.container.test.api.Deployment; @@ -57,11 +57,11 @@ public class ClasspathWebProfileTest } @Inject - @ExternalResource(location="myconfig.properties") + @InjectableResource(location="myconfig.properties") private InputStream inputStream; @Inject - @ExternalResource(location="myconfig.properties") + @InjectableResource(location="myconfig.properties") private Properties properties; @Test @@ -81,13 +81,13 @@ public class ClasspathWebProfileTest } @Test - public void testAmbiguousFileLookup(@ExternalResource(location="META-INF/beans.xml") InputStream inputStream) + public void testAmbiguousFileLookup(@InjectableResource(location="META-INF/beans.xml") InputStream inputStream) { Assert.assertNull(inputStream); // for some reason, this works, exception no longer thrown. } @Test - public void testSuccessfulAmbiguousLookup(@ExternalResource(location="META-INF/beans.xml") List<InputStream> inputStreams) + public void testSuccessfulAmbiguousLookup(@InjectableResource(location="META-INF/beans.xml") List<InputStream> inputStreams) { Assert.assertTrue(inputStreams.size() > 1); //the count is different on as7 compared to the standalone setup } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/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 index e24bea4..42399a3 100644 --- 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 @@ -18,7 +18,7 @@ */ package org.apache.deltaspike.test.core.impl.resourceloader; -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; +import org.apache.deltaspike.core.api.resourceloader.InjectableResource; import org.apache.deltaspike.core.api.resourceloader.FileResourceProvider; import org.apache.deltaspike.core.util.ExceptionUtils; import org.apache.deltaspike.test.category.SeCategory; @@ -81,7 +81,7 @@ public class FileResourceTest } @Test - public void testInputStream(@ExternalResource(resourceProvider = FileResourceProvider.class, + public void testInputStream(@InjectableResource(resourceProvider = FileResourceProvider.class, location="target/propsdsfileresource.properties") InputStream inputStream) throws IOException { @@ -92,7 +92,7 @@ public class FileResourceTest } @Test - public void testProperties(@ExternalResource(resourceProvider = FileResourceProvider.class, + public void testProperties(@InjectableResource(resourceProvider = FileResourceProvider.class, location="target/propsdsfileresource.properties") Properties properties) { http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebResourceProvider.java b/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebResourceProvider.java index 68da89b..8d16676 100644 --- a/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebResourceProvider.java +++ b/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebResourceProvider.java @@ -24,28 +24,28 @@ import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import javax.servlet.ServletContext; -import org.apache.deltaspike.core.api.resourceloader.BaseResourceProvider; -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; +import org.apache.deltaspike.core.api.resourceloader.AbstractResourceProvider; +import org.apache.deltaspike.core.api.resourceloader.InjectableResource; import org.apache.deltaspike.servlet.api.Web; /** * Loads resources using {@link ServletContext#getResource(String)}. */ @ApplicationScoped -public class WebResourceProvider extends BaseResourceProvider +public class WebResourceProvider extends AbstractResourceProvider { @Inject @Web private ServletContext servletContext; @Override - public InputStream readStream(ExternalResource externalResource) + public InputStream readStream(InjectableResource injectableResource) { /* * ServletContext.getResourceAsStream() requires the path to start with "/". We add it here if it is missing * because it is a common mistake to miss it. */ - String path = externalResource.location(); + String path = injectableResource.location(); if (!path.startsWith("/")) { path = "/" + path; http://git-wip-us.apache.org/repos/asf/deltaspike/blob/5ebbdaf0/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java index 01a0e6e..fca3853 100644 --- a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java +++ b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java @@ -26,7 +26,7 @@ import java.util.Properties; import javax.inject.Inject; -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; +import org.apache.deltaspike.core.api.resourceloader.InjectableResource; import org.apache.deltaspike.servlet.api.resourceloader.WebResourceProvider; import org.apache.deltaspike.test.category.WebProfileCategory; import org.apache.deltaspike.test.servlet.impl.Deployments; @@ -59,19 +59,19 @@ public class WebResourceProviderTest } @Inject - @ExternalResource(location = "/foobar.txt", resourceProvider = WebResourceProvider.class) + @InjectableResource(location = "/foobar.txt", resourceProvider = WebResourceProvider.class) private InputStream streamAbsolutePath; @Inject - @ExternalResource(location = "foobar.txt", resourceProvider = WebResourceProvider.class) + @InjectableResource(location = "foobar.txt", resourceProvider = WebResourceProvider.class) private InputStream streamRelativePath; @Inject - @ExternalResource(location = "/foo/bar.txt", resourceProvider = WebResourceProvider.class) + @InjectableResource(location = "/foo/bar.txt", resourceProvider = WebResourceProvider.class) private InputStream streamDirectory; @Inject - @ExternalResource(location = "/foobar.properties", resourceProvider = WebResourceProvider.class) + @InjectableResource(location = "/foobar.properties", resourceProvider = WebResourceProvider.class) private Properties propertiesAbsolutePath; @Test
