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.InputStream;
025 import java.util.Properties;
026
027 import org.granite.logging.Logger;
028
029 /**
030 * @author Franck WOLFF
031 */
032 public class ScannerFactory {
033
034 private static final Logger log = Logger.getLogger(ScannerFactory.class);
035
036
037 public static Scanner createScanner(ScannedItemHandler handler, String marker) {
038 if (isVFSAvailable() && !isEmbedded() && isJBoss(5)) {
039 log.debug("Using VFS aware scanner");
040 try {
041 Class<?> vfsScannerClass = ScannerFactory.class.getClassLoader().loadClass("org.granite.scan.VFSScanner");
042 return (Scanner)vfsScannerClass.getConstructor(ScannedItemHandler.class, String.class).newInstance(handler, marker);
043 }
044 catch (Exception e) {
045 throw new RuntimeException("Could not create VFSScanner", e);
046 }
047 }
048 if (isVFS3Available() && !isEmbedded() && isJBoss(6)) {
049 log.debug("Using VFS3 aware scanner");
050 try {
051 Class<?> vfsScannerClass = ScannerFactory.class.getClassLoader().loadClass("org.granite.scan.VFS3Scanner");
052 return (Scanner)vfsScannerClass.getConstructor(ScannedItemHandler.class, String.class).newInstance(handler, marker);
053 }
054 catch (Exception e) {
055 throw new RuntimeException("Could not create VFS3Scanner", e);
056 }
057 }
058
059 log.debug("Using default Scanner");
060 return new URLScanner(handler, marker);
061 }
062
063 private static boolean isVFSAvailable() {
064 try {
065 Class.forName("org.jboss.virtual.VFS");
066 log.trace("VFS detected");
067 return true;
068 }
069 catch (Throwable t) {
070 return false;
071 }
072 }
073
074 private static boolean isVFS3Available() {
075 try {
076 Class.forName("org.jboss.vfs.VFS");
077 log.trace("VFS3 detected");
078 return true;
079 }
080 catch (Throwable t) {
081 return false;
082 }
083 }
084
085 private static boolean isJBoss(int version) {
086 try {
087 Properties props = new Properties();
088 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/org/jboss/version.properties");
089 if (is == null)
090 is = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jboss/version.properties");
091 if (is != null) {
092 // JBoss AS 4, 5, 6
093 props.load(is);
094 is.close();
095 int major = Integer.parseInt(props.getProperty("version.major"));
096 int minor = Integer.parseInt(props.getProperty("version.minor"));
097
098 boolean isJBossVersion = major >= version && minor >= 0;
099 if (isJBossVersion)
100 log.trace("JBoss " + major + "." + minor + " detected");
101
102 return isJBossVersion;
103 }
104
105 // JBoss AS 7 ?
106 log.trace("JBoss AS 7+ detected");
107
108 return true;
109 }
110 catch (Throwable t) {
111 return false;
112 }
113 }
114
115 private static boolean isEmbedded() {
116 try {
117 Class.forName("org.jboss.embedded.Bootstrap");
118 log.trace("JBoss Embedded detected");
119 return true;
120 }
121 catch (Throwable t) {
122 return false;
123 }
124 }
125 }