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