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 */ 019package org.apache.wiki.util; 020 021import org.apache.commons.io.FileUtils; 022import org.apache.commons.lang3.StringUtils; 023import org.apache.logging.log4j.LogManager; 024import org.apache.logging.log4j.Logger; 025import org.jdom2.Element; 026 027import java.io.File; 028import java.io.IOException; 029import java.lang.reflect.Constructor; 030import java.net.JarURLConnection; 031import java.net.MalformedURLException; 032import java.net.URL; 033import java.net.URLClassLoader; 034import java.util.ArrayList; 035import java.util.Enumeration; 036import java.util.Iterator; 037import java.util.List; 038import java.util.Map; 039import java.util.concurrent.ConcurrentHashMap; 040import java.util.jar.JarEntry; 041import java.util.jar.JarFile; 042 043/** 044 * Contains useful utilities for class file manipulation. This is a static class, 045 * so there is no need to instantiate it. 046 * 047 * @since 2.1.29. 048 */ 049public final class ClassUtil { 050 051 private static final Logger log = LogManager.getLogger(ClassUtil.class); 052 053 /** The location of the classmappings.xml document. It will be searched for in the classpath. It's value is "{@value}". */ 054 public static final String MAPPINGS = "ini/classmappings.xml"; 055 056 /** The location of the classmappings-extra.xml document. It will be searched for in the classpath. It's value is "{@value}". */ 057 public static final String MAPPINGS_EXTRA = "ini/classmappings-extra.xml"; 058 059 /** Initialize the class mappings document. */ 060 private static final Map< String, String > c_classMappings = populateClassMappingsFrom( MAPPINGS ); 061 062 /** Initialize the class mappings extra document. */ 063 private static final Map< String, String > c_classMappingsExtra = populateClassMappingsFrom( MAPPINGS_EXTRA ) ; 064 065 private static boolean classLoaderSetup; 066 private static ClassLoader loader; 067 068 private static Map< String, String > populateClassMappingsFrom( final String fileLoc ) { 069 final Map< String, String > map = new ConcurrentHashMap<>(); 070 final List< Element > nodes = XmlUtil.parse( fileLoc, "/classmappings/mapping" ); 071 072 if( !nodes.isEmpty() ) { 073 for( final Element f : nodes ) { 074 final String key = f.getChildText( "requestedClass" ); 075 final String className = f.getChildText( "mappedClass" ); 076 077 map.put( key, className ); 078 log.debug( "Mapped class '" + key + "' to class '" + className + "'" ); 079 } 080 } else { 081 log.info( "Didn't find class mapping document in " + MAPPINGS ); 082 } 083 return map; 084 } 085 086 /** 087 * Private constructor to prevent direct instantiation. 088 */ 089 private ClassUtil() {} 090 091 /** 092 * Attempts to find a class from a collection of packages. This will first 093 * attempt to find the class based on just the className parameter, but 094 * should that fail, will iterate through the "packages" -list, prefixes 095 * the package name to the className, and then tries to find the class 096 * again. 097 * 098 * @param packages A List of Strings, containing different package names. 099 * @param className The name of the class to find. 100 * @return The class, if it was found. 101 * @throws ClassNotFoundException if this particular class cannot be found from the list. 102 */ 103 public static Class<?> findClass( final List< String > packages, final List< String > externaljars, final String className ) throws ClassNotFoundException { 104 if (!classLoaderSetup) { 105 loader = setupClassLoader(externaljars); 106 } 107 108 try { 109 return loader.loadClass( className ); 110 } catch( final ClassNotFoundException e ) { 111 for( final String packageName : packages ) { 112 try { 113 return loader.loadClass( packageName + "." + className ); 114 } catch( final ClassNotFoundException ex ) { 115 // This is okay, we go to the next package. 116 } 117 } 118 119 } 120 121 throw new ClassNotFoundException( "Class '" + className + "' not found in search path!" ); 122 } 123 124 /** 125 * Setup the plugin classloader, checking if there are external JARS to add. 126 * 127 * @param externaljars external jars to load into the classloader. 128 * @return the classloader that can load classes from the configured external jars or, if not specified, the classloader that loaded this class. 129 */ 130 private static ClassLoader setupClassLoader( final List< String > externaljars) { 131 classLoaderSetup = true; 132 log.info( "setting up classloaders for external (plugin) jars" ); 133 if( externaljars.size() == 0 ) { 134 log.info( "no external jars configured, using standard classloading" ); 135 return ClassUtil.class.getClassLoader(); 136 } 137 final URL[] urls = new URL[externaljars.size()]; 138 int i = 0; 139 for( final String externaljar : externaljars ) { 140 try { 141 final File jarFile = new File( externaljar ); 142 final URL ucl = jarFile.toURI().toURL(); 143 urls[ i++ ] = ucl; 144 log.info( "added " + ucl + " to list of external jars" ); 145 } catch( final MalformedURLException e ) { 146 log.error( "exception (" + e.getMessage() + ") while setting up classloaders for external jar:" + externaljar + ", continuing without external jars." ); 147 } 148 } 149 150 if( i == 0 ) { 151 log.error( "all external jars threw an exception while setting up classloaders for them, continuing with standard classloading. " + 152 "See https://jspwiki-wiki.apache.org/Wiki.jsp?page=InstallingPlugins for help on how to install custom plugins." ); 153 return ClassUtil.class.getClassLoader(); 154 } 155 156 return new URLClassLoader(urls, ClassUtil.class.getClassLoader()); 157 } 158 159 /** 160 * 161 * It will first attempt to instantiate the class directly from the className, and will then try to prefix it with the packageName. 162 * 163 * @param packageName A package name (such as "org.apache.wiki.plugins"). 164 * @param className The class name to find. 165 * @return The class, if it was found. 166 * @throws ClassNotFoundException if this particular class cannot be found. 167 */ 168 public static Class< ? > findClass( final String packageName, final String className ) throws ClassNotFoundException { 169 try { 170 return ClassUtil.class.getClassLoader().loadClass( className ); 171 } catch( final ClassNotFoundException e ) { 172 return ClassUtil.class.getClassLoader().loadClass( packageName + "." + className ); 173 } 174 } 175 176 /** 177 * Lists all the files in classpath under a given package. 178 * 179 * @param rootPackage the base package. Can be {code null}. 180 * @return all files entries in classpath under the given package 181 */ 182 public static List< String > classpathEntriesUnder( final String rootPackage ) { 183 final List< String > results = new ArrayList<>(); 184 Enumeration< URL > en = null; 185 if( StringUtils.isNotEmpty( rootPackage ) ) { 186 try { 187 en = ClassUtil.class.getClassLoader().getResources( rootPackage ); 188 } catch( final IOException e ) { 189 log.error( e.getMessage(), e ); 190 } 191 } 192 193 while( en != null && en.hasMoreElements() ) { 194 final URL url = en.nextElement(); 195 try { 196 if( "jar".equals( url.getProtocol() ) ) { 197 jarEntriesUnder( results, ( JarURLConnection )url.openConnection(), rootPackage ); 198 } else if( "file".equals( url.getProtocol() ) ) { 199 fileEntriesUnder( results, new File( url.getFile() ), rootPackage ); 200 } 201 202 } catch( final IOException ioe ) { 203 log.error( ioe.getMessage(), ioe ); 204 } 205 } 206 return results; 207 } 208 209 /** 210 * Searchs for all the files in classpath under a given package, for a given {@link File}. If the 211 * {@link File} is a directory all files inside it are stored, otherwise the {@link File} itself is 212 * stored 213 * 214 * @param results collection in which the found entries are stored 215 * @param file given {@link File} to search in. 216 * @param rootPackage base package. 217 */ 218 static void fileEntriesUnder( final List< String > results, final File file, final String rootPackage ) { 219 log.debug( "scanning [" + file.getName() + "]" ); 220 if( file.isDirectory() ) { 221 final Iterator< File > files = FileUtils.iterateFiles( file, null, true ); 222 while( files.hasNext() ) { 223 final File subfile = files.next(); 224 // store an entry similar to the jarSearch(..) below ones 225 final String entry = StringUtils.replace( subfile.getAbsolutePath(), file.getAbsolutePath() + File.separatorChar, StringUtils.EMPTY ); 226 results.add( rootPackage + "/" + entry ); 227 } 228 } else { 229 results.add( file.getName() ); 230 } 231 } 232 233 /** 234 * Searchs for all the files in classpath under a given package, for a given {@link JarURLConnection}. 235 * 236 * @param results collection in which the found entries are stored 237 * @param jurlcon given {@link JarURLConnection} to search in. 238 * @param rootPackage base package. 239 */ 240 static void jarEntriesUnder( final List< String > results, final JarURLConnection jurlcon, final String rootPackage ) { 241 try( final JarFile jar = jurlcon.getJarFile() ) { 242 log.debug( "scanning [" + jar.getName() +"]" ); 243 final Enumeration< JarEntry > entries = jar.entries(); 244 while( entries.hasMoreElements() ) { 245 final JarEntry entry = entries.nextElement(); 246 if( entry.getName().startsWith( rootPackage ) && !entry.isDirectory() ) { 247 results.add( entry.getName() ); 248 } 249 } 250 } catch( final IOException ioe ) { 251 log.error( ioe.getMessage(), ioe ); 252 } 253 } 254 255 /** 256 * This method is used to locate and instantiate a mapped class. 257 * You may redefine anything in the resource file which is located in your classpath 258 * under the name <code>ClassUtil.MAPPINGS ({@value #MAPPINGS})</code>. 259 * <p> 260 * This is an extremely powerful system, which allows you to remap many of 261 * the JSPWiki core classes to your own class. Please read the documentation 262 * included in the default <code>{@value #MAPPINGS}</code> file to see 263 * how this method works. 264 * 265 * @param requestedClass The name of the class you wish to instantiate. 266 * @return An instantiated Object. 267 * @throws IllegalArgumentException If the class cannot be found or instantiated. 268 * @throws ReflectiveOperationException If the class cannot be found or instantiated. 269 * @since 2.5.40 270 */ 271 @SuppressWarnings("unchecked") 272 public static < T > T getMappedObject( final String requestedClass ) throws ReflectiveOperationException, IllegalArgumentException { 273 final Object[] initargs = {}; 274 return ( T )getMappedObject(requestedClass, initargs ); 275 } 276 277 /** 278 * This method is used to locate and instantiate a mapped class. 279 * You may redefine anything in the resource file which is located in your classpath 280 * under the name <code>{@value #MAPPINGS}</code>. 281 * <p> 282 * This is an extremely powerful system, which allows you to remap many of 283 * the JSPWiki core classes to your own class. Please read the documentation 284 * included in the default <code>{@value #MAPPINGS}</code> file to see 285 * how this method works. 286 * <p> 287 * This method takes in an object array for the constructor arguments for classes 288 * which have more than two constructors. 289 * 290 * @param requestedClass The name of the class you wish to instantiate. 291 * @param initargs The parameters to be passed to the constructor. May be <code>null</code>. 292 * @return An instantiated Object. 293 * @throws IllegalArgumentException If the class cannot be found or instantiated. 294 * @throws ReflectiveOperationException If the class cannot be found or instantiated. 295 * @since 2.5.40 296 */ 297 @SuppressWarnings( "unchecked" ) 298 public static < T > T getMappedObject( final String requestedClass, final Object... initargs ) throws ReflectiveOperationException, IllegalArgumentException { 299 final Class< ? > cl = getMappedClass( requestedClass ); 300 final Constructor< ? >[] ctors = cl.getConstructors(); 301 302 // Try to find the proper constructor by comparing the initargs array classes and the constructor types. 303 for( final Constructor< ? > ctor : ctors ) { 304 final Class< ? >[] params = ctor.getParameterTypes(); 305 if( params.length == initargs.length ) { 306 for( int arg = 0; arg < initargs.length; arg++ ) { 307 if( params[ arg ].isAssignableFrom( initargs[ arg ].getClass() ) ) { 308 // Ha, found it! Instantiating and returning... 309 return ( T )ctor.newInstance( initargs ); 310 } 311 } 312 } 313 } 314 315 // No arguments, so we can just call a default constructor and ignore the arguments. 316 return ( T )cl.getDeclaredConstructor().newInstance(); 317 } 318 319 /** 320 * Finds a mapped class from the c_classMappings list. If there is no mappped class, will use the requestedClass. 321 * 322 * @param requestedClass requested class. 323 * @return A Class object which you can then instantiate. 324 * @throws ClassNotFoundException if the class is not found. 325 */ 326 public static Class< ? > getMappedClass( final String requestedClass ) throws ClassNotFoundException { 327 String mappedClass = c_classMappings.get( requestedClass ); 328 if( mappedClass == null ) { 329 mappedClass = requestedClass; 330 } 331 332 return Class.forName( mappedClass ); 333 } 334 335 /** 336 * checks if {@code srcClassName} is a subclass of {@code parentClassname}. 337 * 338 * @param srcClassName expected subclass. 339 * @param parentClassName expected parent class. 340 * @return {@code true} if {@code srcClassName} is a subclass of {@code parentClassname}, {@code false} otherwise. 341 */ 342 public static boolean assignable( final String srcClassName, final String parentClassName ) { 343 try { 344 final Class< ? > src = Class.forName( srcClassName ); 345 final Class< ? > parent = Class.forName( parentClassName ); 346 return parent.isAssignableFrom( src ); 347 } catch( final Exception e ) { 348 log.error( e.getMessage(), e ); 349 } 350 return false; 351 } 352 353 /** 354 * Checks if a given class exists in classpath. 355 * 356 * @param className the class to check for existence. 357 * @return {@code true} if it exists, {@code false} otherwise. 358 */ 359 public static boolean exists( final String className ) { 360 try { 361 Class.forName( className, false, ClassUtil.class.getClassLoader() ); 362 return true; 363 } catch( final ClassNotFoundException e ) { 364 return false; 365 } 366 } 367 368 public static Map< String, String > getExtraClassMappings() { 369 return c_classMappingsExtra; 370 } 371 372}