Java tutorial
package com.ericsson.research.trap; /* * ##_BEGIN_LICENSE_## * Transport Abstraction Package (trap) * ---------- * Copyright (C) 2014 Ericsson AB * ---------- * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. Neither the name of the Ericsson AB nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ##_END_LICENSE_## */ import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Logger; import org.apache.maven.artifact.Artifact; import org.apache.maven.project.MavenProject; import org.codehaus.classworlds.ClassRealm; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.configurator.AbstractComponentConfigurator; import org.codehaus.plexus.component.configurator.ComponentConfigurationException; import org.codehaus.plexus.component.configurator.ComponentConfigurator; import org.codehaus.plexus.component.configurator.ConfigurationListener; import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter; import org.codehaus.plexus.component.configurator.converters.special.ClassRealmConverter; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.codehaus.plexus.configuration.PlexusConfiguration; /** * * @plexus.component * role="org.codehaus.plexus.component.configurator.ComponentConfigurator" * role-hint="include-project-dependencies" * @plexus.requirement role= * "org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup" * role-hint="default" * */ @Component(role = ComponentConfigurator.class, hint = "include-project-dependencies") public class IndexConfigurator extends AbstractComponentConfigurator { private final static Logger logger = Logger.getLogger(IndexConfigurator.class.getName()); public void configureComponent(Object component, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm, ConfigurationListener listener) throws ComponentConfigurationException { this.addProjectDependenciesToClassRealm(expressionEvaluator, containerRealm); this.converterLookup.registerConverter(new ClassRealmConverter(containerRealm)); ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter(); converter.processConfiguration(this.converterLookup, component, containerRealm.getClassLoader(), configuration, expressionEvaluator, listener); } @SuppressWarnings("unchecked") private void addProjectDependenciesToClassRealm(ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm) throws ComponentConfigurationException { Set<String> runtimeClasspathElements = new HashSet<String>(); try { runtimeClasspathElements .addAll((List<String>) expressionEvaluator.evaluate("${project.runtimeClasspathElements}")); } catch (ExpressionEvaluationException e) { throw new ComponentConfigurationException( "There was a problem evaluating: ${project.runtimeClasspathElements}", e); } Collection<URL> urls = this.buildURLs(runtimeClasspathElements); urls.addAll(this.buildAritfactDependencies(expressionEvaluator)); for (URL url : urls) { containerRealm.addConstituent(url); } } private Collection<URL> buildAritfactDependencies(ExpressionEvaluator expressionEvaluator) throws ComponentConfigurationException { MavenProject project; try { project = (MavenProject) expressionEvaluator.evaluate("${project}"); } catch (ExpressionEvaluationException e1) { throw new ComponentConfigurationException("There was a problem evaluating: ${project}", e1); } Collection<URL> urls = new ArrayList<URL>(); for (Object a : project.getArtifacts()) { try { urls.add(((Artifact) a).getFile().toURI().toURL()); } catch (MalformedURLException e) { throw new ComponentConfigurationException("Unable to resolve artifact dependency: " + a, e); } } return urls; } private Collection<URL> buildURLs(Set<String> runtimeClasspathElements) throws ComponentConfigurationException { List<URL> urls = new ArrayList<URL>(runtimeClasspathElements.size()); for (String element : runtimeClasspathElements) { try { final URL url = new File(element).toURI().toURL(); urls.add(url); } catch (MalformedURLException e) { throw new ComponentConfigurationException("Unable to access project dependency: " + element, e); } } return urls; } }