001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.commons.resource; 021 022import java.io.InputStream; 023import java.io.OutputStream; 024import java.io.SequenceInputStream; 025import java.util.ArrayList; 026import java.util.List; 027import java.util.Vector; 028 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032public class ResourceStreamSourceComposite extends ResourceStreamSourceAbstract { 033 034 private static Logger LOG = LoggerFactory.getLogger(ResourceStreamSourceComposite.class); 035 036 private final List<ResourceStreamSource> resourceStreamSources = new ArrayList<ResourceStreamSource>(); 037 038 public ResourceStreamSourceComposite(final ResourceStreamSource... resourceStreamSources) { 039 for (final ResourceStreamSource rss : resourceStreamSources) { 040 addResourceStreamSource(rss); 041 } 042 } 043 044 public void addResourceStreamSource(final ResourceStreamSource rss) { 045 this.resourceStreamSources.add(rss); 046 } 047 048 @Override 049 protected InputStream doReadResource(final String resourcePath) { 050 Vector<InputStream> compositionStreams = new Vector<InputStream>(); 051 for (final ResourceStreamSource rss : resourceStreamSources) { 052 final InputStream resourceStream = rss.readResource(resourcePath); 053 if (resourceStream != null) { 054 compositionStreams.add(resourceStream); 055 } 056 } 057 if (!compositionStreams.isEmpty()) { 058 return new SequenceInputStream(compositionStreams.elements()); 059 } 060 if (LOG.isDebugEnabled()) { 061 LOG.debug("could not load resource path '" + resourcePath + "' from " + getName()); 062 } 063 return null; 064 } 065 066 @Override 067 public OutputStream writeResource(final String resourcePath) { 068 return null; // No support for writing resources 069 } 070 071 @Override 072 public String getName() { 073 return "composite [" + resourceStreamNames() + "]"; 074 } 075 076 private String resourceStreamNames() { 077 final StringBuilder buf = new StringBuilder(); 078 boolean first = true; 079 for (final ResourceStreamSource rss : resourceStreamSources) { 080 if (first) { 081 first = false; 082 } else { 083 buf.append(", "); 084 } 085 buf.append(rss.getName()); 086 } 087 return buf.toString(); 088 } 089 090}