001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.scan;
022
023 import java.io.IOException;
024 import java.io.InputStream;
025 import java.util.Properties;
026
027 /**
028 * @author Franck WOLFF
029 */
030 public abstract class AbstractScannedItem implements ScannedItem {
031
032 private final Scanner scanner;
033 private final ScannedItem marker;
034
035 private Class<?> clazz = null;
036 private Properties properties = null;
037
038 public AbstractScannedItem(Scanner scanner, ScannedItem marker) {
039 this.scanner = scanner;
040 this.marker = marker;
041 }
042
043 public ScannedItem getMarker() {
044 return marker;
045 }
046
047 public Scanner getScanner() {
048 return scanner;
049 }
050
051 public String getExtension() {
052 String name = getName();
053 int lastDot = name.indexOf('.');
054 return lastDot >= 0 ? name.substring(lastDot + 1) : null;
055 }
056
057 public String getClassName() {
058 if (!"class".equals(getExtension()))
059 throw new RuntimeException("Not a valid class name: " + getAbsolutePath());
060 return getRelativePath().substring(0, getRelativePath().length() - 6).replace('/', '.');
061 }
062
063 public byte[] getContent() throws IOException {
064 long size = getSize();
065 if (size > Integer.MAX_VALUE)
066 throw new IOException("Size over Integer.MAX_VALUE: " + size);
067
068 InputStream is = null;
069 try {
070 is = getInputStream();
071 byte[] data = new byte[(int)size];
072 is.read(data);
073 return data;
074 } finally {
075 if (is != null)
076 is.close();
077 }
078 }
079
080 public Class<?> loadAsClass() throws ClassNotFoundException, IOException, ClassFormatError {
081 if (clazz == null) {
082 ClassLoader loader = scanner.getLoader();
083 if (loader instanceof ScannedItemClassLoader)
084 clazz = ((ScannedItemClassLoader)loader).loadClass(this);
085 else
086 clazz = loader.loadClass(getClassName());
087 }
088 return clazz;
089 }
090
091 public Properties loadAsProperties() throws IOException, IllegalArgumentException {
092 if (properties == null) {
093 InputStream is = null;
094 try {
095 is = getInputStream();
096 properties = new Properties();
097 properties.load(getInputStream());
098 } finally {
099 if (is != null)
100 is.close();
101 }
102 }
103 return properties;
104 }
105
106 @Override
107 public String toString() {
108 if (marker != null)
109 return getAbsolutePath() + " [marker=" + marker + "]";
110 return getAbsolutePath();
111 }
112 }