1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Modifier;
29 import java.net.URL;
30 import java.net.URLClassLoader;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Properties;
35
36
37
38
39
40
41
42 public abstract class AbstractLoadMojo
43 extends AbstractMojo
44 {
45
46
47
48
49
50
51
52
53
54
55 protected String classNames;
56
57
58
59
60
61
62
63
64
65 protected String resourcePaths;
66
67
68
69
70
71
72
73
74 protected void execute( File outputFile, ClassLoader classLoader )
75 throws MojoExecutionException
76 {
77 getLog().info( "[MAVEN-CORE-IT-LOG] Using class loader " + classLoader );
78
79
80
81
82
83
84
85
86
87
88
89 ClassLoader childClassLoader = new URLClassLoader( new URL[0], classLoader );
90
91 Properties loaderProperties = new Properties();
92
93 if ( classNames != null && classNames.length() > 0 )
94 {
95 String[] names = classNames.split( "," );
96 for ( String name : names )
97 {
98 getLog().info( "[MAVEN-CORE-IT-LOG] Loading class " + name );
99
100
101 try
102 {
103 Class type = classLoader.loadClass( name );
104 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded class from " + type.getClassLoader() );
105 try
106 {
107 if ( !type.equals( childClassLoader.loadClass( name ) ) )
108 {
109 throw new ClassNotFoundException( name );
110 }
111 }
112 catch ( ClassNotFoundException cnfe )
113 {
114 getLog().error( "[MAVEN-CORE-IT-LOG] Detected class loader defect while loading " + name );
115 throw cnfe;
116 }
117 loaderProperties.setProperty( name, "" + type.hashCode() );
118
119 Method[] methods = type.getDeclaredMethods();
120 List methodNames = new ArrayList();
121 for ( Method method : methods )
122 {
123 if ( Modifier.isPublic( method.getModifiers() ) )
124 {
125 methodNames.add( method.getName() );
126 }
127 }
128 Collections.sort( methodNames );
129 StringBuilder buffer = new StringBuilder( 1024 );
130 for ( Object methodName : methodNames )
131 {
132 if ( buffer.length() > 0 )
133 {
134 buffer.append( ',' );
135 }
136 buffer.append( methodName );
137 }
138
139 loaderProperties.setProperty( name + ".methods", buffer.toString() );
140 }
141 catch ( ClassNotFoundException e )
142 {
143
144 getLog().info( "[MAVEN-CORE-IT-LOG] Class not available" );
145 }
146 catch ( LinkageError e )
147 {
148
149 getLog().info( "[MAVEN-CORE-IT-LOG] Class not linkable", e );
150 }
151 }
152 }
153
154 if ( resourcePaths != null && resourcePaths.length() > 0 )
155 {
156 String[] paths = resourcePaths.split( "," );
157 for ( String path : paths )
158 {
159 getLog().info( "[MAVEN-CORE-IT-LOG] Loading resource " + path );
160
161
162 URL url = classLoader.getResource( path );
163 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded resource from " + url );
164 if ( url != null && !url.equals( childClassLoader.getResource( path ) ) )
165 {
166 getLog().error( "[MAVEN-CORE-IT-LOG] Detected class loader defect while getting " + path );
167 url = null;
168 }
169 if ( url != null )
170 {
171 loaderProperties.setProperty( path, url.toString() );
172 }
173
174
175 try
176 {
177 List urls = Collections.list( classLoader.getResources( path ) );
178 if ( !urls.equals( Collections.list( childClassLoader.getResources( path ) ) ) )
179 {
180 getLog().error( "[MAVEN-CORE-IT-LOG] Detected class loader defect while getting " + path );
181 urls = Collections.EMPTY_LIST;
182 }
183 loaderProperties.setProperty( path + ".count", "" + urls.size() );
184 for ( int j = 0; j < urls.size(); j++ )
185 {
186 loaderProperties.setProperty( path + "." + j, urls.get( j ).toString() );
187 }
188 }
189 catch ( IOException e )
190 {
191 throw new MojoExecutionException( "Resources could not be enumerated: " + path, e );
192 }
193 }
194 }
195
196 getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + outputFile );
197
198 PropertiesUtil.write( outputFile, loaderProperties );
199
200 getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + outputFile );
201 }
202
203 }