| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CLEnum |
|
| 2.0;2 | ||||
| CLEnum$1 |
|
| 2.0;2 | ||||
| CLEnum$2 |
|
| 2.0;2 | ||||
| CLEnum$3 |
|
| 2.0;2 | ||||
| CLEnum$4 |
|
| 2.0;2 | ||||
| CLEnum$5 |
|
| 2.0;2 | ||||
| CLEnum$6 |
|
| 2.0;2 |
| 1 | /** | |
| 2 | * Copyright 2009 Martin Vysny. | |
| 3 | * | |
| 4 | * This file is part of WebVM. | |
| 5 | * | |
| 6 | * WebVM is free software: you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation, either version 3 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * WebVM is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with WebVM. If not, see <http://www.gnu.org/licenses/>. | |
| 18 | */ | |
| 19 | package sk.baka.webvm.analyzer.classloader; | |
| 20 | ||
| 21 | import java.io.InputStream; | |
| 22 | import java.net.URL; | |
| 23 | import java.util.Collections; | |
| 24 | import java.util.EnumSet; | |
| 25 | import java.util.Enumeration; | |
| 26 | import sk.baka.tools.IOUtils; | |
| 27 | ||
| 28 | /** | |
| 29 | * Auto-detection of the class loader type. | |
| 30 | * @author Martin Vysny | |
| 31 | */ | |
| 32 | 0 | public enum CLEnum { |
| 33 | ||
| 34 | /** | |
| 35 | * The WebVM WAR classloader. | |
| 36 | */ | |
| 37 | 0 | WAR { |
| 38 | ||
| 39 | @Override | |
| 40 | protected boolean matches(final ClassLoader cl) { | |
| 41 | try { | |
| 42 | 0 | final Enumeration<URL> e = cl.getResources("WEB-INF/web.xml"); |
| 43 | 0 | for (final URL url : Collections.list(e)) { |
| 44 | 0 | final InputStream in = url.openStream(); |
| 45 | try { | |
| 46 | 0 | final String webXml = IOUtils.toString(in); |
| 47 | 0 | if (webXml.contains("WebVM")) { |
| 48 | 0 | return true; |
| 49 | } | |
| 50 | } finally { | |
| 51 | 0 | IOUtils.closeQuietly(in); |
| 52 | 0 | } |
| 53 | 0 | } |
| 54 | 0 | } catch (Exception ex) { |
| 55 | 0 | return false; |
| 56 | 0 | } |
| 57 | 0 | return false; |
| 58 | } | |
| 59 | }, | |
| 60 | /** | |
| 61 | * A classloader which loads ejb.jar | |
| 62 | */ | |
| 63 | 0 | EJBJAR { |
| 64 | ||
| 65 | @Override | |
| 66 | protected boolean matches(final ClassLoader cl) { | |
| 67 | 0 | return cl.getResource("META-INF/ejb-jar.xml") != null; |
| 68 | } | |
| 69 | }, | |
| 70 | /** | |
| 71 | * Classloader for the EAR application. | |
| 72 | */ | |
| 73 | 0 | EAR { |
| 74 | ||
| 75 | protected boolean matches(final ClassLoader cl) { | |
| 76 | 0 | return cl.getResource("META-INF/application.xml") != null; |
| 77 | } | |
| 78 | }, | |
| 79 | /** | |
| 80 | * A {@link ClassLoader#getSystemClassLoader() system classloader}. | |
| 81 | */ | |
| 82 | 0 | SYSTEM { |
| 83 | ||
| 84 | protected boolean matches(final ClassLoader cl) { | |
| 85 | 0 | return cl == ClassLoader.getSystemClassLoader(); |
| 86 | } | |
| 87 | }, | |
| 88 | /** | |
| 89 | * Root classloader (its parent is null). | |
| 90 | */ | |
| 91 | 0 | ROOT { |
| 92 | ||
| 93 | protected boolean matches(final ClassLoader cl) { | |
| 94 | 0 | return cl.getParent() == null; |
| 95 | } | |
| 96 | }, | |
| 97 | /** | |
| 98 | * An unknown classloader type. | |
| 99 | */ | |
| 100 | 0 | SERVER { |
| 101 | ||
| 102 | protected boolean matches(final ClassLoader cl) { | |
| 103 | 0 | return false; |
| 104 | } | |
| 105 | }; | |
| 106 | ||
| 107 | /** | |
| 108 | * Checks if this enum constant matches given classloader. | |
| 109 | * @param cl the classloader to match, not null | |
| 110 | * @return true if given classloader is of this type. | |
| 111 | */ | |
| 112 | protected abstract boolean matches(final ClassLoader cl); | |
| 113 | ||
| 114 | /** | |
| 115 | * Detects the type of classes this class loader manages. | |
| 116 | * @param cl the classloader, not null. | |
| 117 | * @return class loader type | |
| 118 | */ | |
| 119 | public static EnumSet<CLEnum> getTypes(final ClassLoader cl) { | |
| 120 | 0 | final EnumSet<CLEnum> result = EnumSet.noneOf(CLEnum.class); |
| 121 | 0 | for (final CLEnum e : values()) { |
| 122 | 0 | if (e.matches(cl)) { |
| 123 | 0 | result.add(e); |
| 124 | } | |
| 125 | } | |
| 126 | 0 | if (result.isEmpty()) { |
| 127 | 0 | result.add(SERVER); |
| 128 | } | |
| 129 | 0 | return result; |
| 130 | } | |
| 131 | } |