001/* 002 * Copyright (C) 2014 Jörg Prante 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.xbib.elasticsearch.plugin.jdbc.classloader; 017 018import java.io.ByteArrayOutputStream; 019import java.io.IOException; 020import java.io.InputStream; 021import java.security.cert.Certificate; 022import java.util.jar.Attributes; 023import java.util.jar.Manifest; 024 025public abstract class AbstractResourceHandle implements ResourceHandle { 026 027 public byte[] getBytes() throws IOException { 028 InputStream in = getInputStream(); 029 try { 030 return load(in); 031 } finally { 032 if (in != null) { 033 try { 034 in.close(); 035 } catch (Exception ignored) { 036 // ignore 037 } 038 } 039 } 040 } 041 042 public Manifest getManifest() throws IOException { 043 return null; 044 } 045 046 public Certificate[] getCertificates() { 047 return null; 048 } 049 050 public Attributes getAttributes() throws IOException { 051 Manifest m = getManifest(); 052 if (m == null) { 053 return null; 054 } 055 056 String entry = getUrl().getFile(); 057 return m.getAttributes(entry); 058 } 059 060 public void close() { 061 } 062 063 public String toString() { 064 return "[" + getName() + ": " + getUrl() + "; code source: " + getCodeSourceUrl() + "]"; 065 } 066 067 static byte[] load(InputStream inputStream) throws IOException { 068 try { 069 byte[] buffer = new byte[4096]; 070 ByteArrayOutputStream out = new ByteArrayOutputStream(); 071 for (int count = inputStream.read(buffer); count >= 0; count = inputStream.read(buffer)) { 072 out.write(buffer, 0, count); 073 } 074 return out.toByteArray(); 075 } finally { 076 if (inputStream != null) { 077 try { 078 inputStream.close(); 079 } catch (Exception ignored) { 080 } 081 } 082 } 083 } 084}