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.IOException; 023import java.io.InputStream; 024import java.io.OutputStream; 025 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029public abstract class ResourceStreamSourceAbstract implements ResourceStreamSource { 030 031 private static Logger LOG = LoggerFactory.getLogger(ResourceStreamSourceAbstract.class); 032 033 @Override 034 public final InputStream readResource(final String resourcePath) { 035 036 try { 037 final InputStream resourceStream = doReadResource(resourcePath); 038 if (resourceStream != null) { 039 return resourceStream; 040 } 041 if (LOG.isDebugEnabled()) { 042 LOG.debug("could not load resource path '" + resourcePath + "' from " + getName()); 043 } 044 } catch (final IOException e) { 045 if (LOG.isDebugEnabled()) { 046 LOG.debug("could not load resource path '" + resourcePath + "' from " + getName() + ", exception: " + e.getMessage()); 047 } 048 } 049 return null; 050 } 051 052 /** 053 * Mandatory hook method; subclasses can return either <tt>null</tt> or 054 * throw an exception if the resource could not be found. 055 */ 056 protected abstract InputStream doReadResource(String resourcePath) throws IOException; 057 058 /** 059 * Default implementation returns <tt>null</tt> (that is, not supported). 060 */ 061 @Override 062 public OutputStream writeResource(final String resourcePath) { 063 return null; 064 } 065 066 @Override 067 public String toString() { 068 return getName(); 069 } 070}