How to add an extra feature to a maven dependency plugin - java

I use maven-dependency-plugin to copy jar files of artifactItem, instead of giving artifactItem details at pom.xml as shown below
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>junit.jar</destFileName>
</artifactItem>
i am trying to extend the pluging to give artifactItems detail progamatically,by using below class
package com.nuwaza.aqua.sample.dependencymanager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.factory.DefaultArtifactFactory;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo;
import org.apache.maven.plugin.dependency.fromConfiguration.ArtifactItem;
import org.apache.maven.plugin.dependency.fromConfiguration.CopyMojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.artifact.versioning.VersionRange;
/**
*
* #id copy
* #goal copy
* #phase package
* #author anoopab
*/
public class BundleDataMojo extends CopyMojo {
#Parameter(required = true)
private List<ArtifactItem> artifactItems;
#Component
protected static ArtifactFactory factory;
public void execute() throws MojoExecutionException {
super.setArtifactItems(setArtifactDetail());
super.execute();
}
private List<ArtifactItem> setArtifactDetail() {
artifactItems = new ArrayList<ArtifactItem>();
ArtifactItem bundle = new ArtifactItem();
bundle.setGroupId("junit");
bundle.setArtifactId("junit");
bundle.setVersion("3.8.2");
bundle.setDestFileName("samplejunit.jar");
bundle.setType("jar");
bundle.setOverWrite("true");
artifactItems.add(bundle);
return artifactItems;
}
}
but i getting the following erorr
[info] Configured Artifact: junit:junit:3.8.2:jar
Exception in thread "main" java.lang.NullPointerException
at org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo.getArtifact(AbstractFromConfigurationMojo.java:221)
at org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo.getProcessedArtifactItems(AbstractFromConfigurationMojo.java:166)
at org.apache.maven.plugin.dependency.fromConfiguration.CopyMojo.execute(CopyMojo.java:67)
at com.nuwaza.aqua.sample.dependencymanager.BundleDataMojo.execute(BundleDataMojo.java:52)
at com.nuwaza.aqua.sample.dependencymanager.BundleDataMojo.main(BundleDataMojo.java:118)
that is
/**
* Used to look up Artifacts in the remote repository.
*/
#Component
protected ArtifactFactory factory;
this factory getting null value.....
ArtifactFactory interface at org.apache.maven.artifact.factory
I'm really confused by these. Could anyone give any help? Thanks a lot!

Related

Why do Apache-Storm org.apache.http.xxx imports work for local cluster but not for an actual storm cluster?

I can run an apache-storm program in local mode (using Intellij and Maven), but when I run on the storm cluster it bombs immediately with the error "Exception in thread “main” java.lang.NoClassDefFoundException: org.apache.http.client.HttpClient". I have verified the storm cluster works on a toy program with no http calls. Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jmm</groupId>
<artifactId>twitter2</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
And the start of my spout code:
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import org.apache.storm.utils.Utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import twitter4j.Status;
public class twitterSpout extends BaseRichSpout {
private SpoutOutputCollector collector;
String bearerToken = ""; //deleted here, but filled in for my program
HttpGet httpGet;
HttpResponse response;
HttpEntity entity;
URIBuilder uriBuilder;
And the topology:
import org.apache.storm.Config;
import org.apache.storm.StormSubmitter;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.tuple.Fields;
import org.apache.storm.LocalCluster;
public class twitterTopology {
public static void main(String[] args) throws Exception {
twitterSpout spout = new twitterSpout();
System.out.println("\nafter initializing twitterSpout\n");
LossyBolt countBolt = new LossyBolt();
ReportBolt reportBolt = new ReportBolt();
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("twitterSpout", spout);
builder.setBolt("lossy", countBolt).fieldsGrouping("twitterSpout", new Fields("bucket"));
builder.setBolt("report", reportBolt).fieldsGrouping("lossy", new Fields("bcurrent"));
Config config = new Config();
config.setDebug(true);
config.setNumWorkers(3);
/* this code works when I uncomment it and comment out StormSubmitter
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("twitterTopology", config, builder.createTopology());
Thread.sleep(70000);
cluster.killTopology("twitterTopology");;
cluster.shutdown();
*/
StormSubmitter.submitTopology("twitterTopology", config, builder.createTopology());
Thread.sleep(70000);
}
}
Any insight as to why it works in local mode but not in storm cluster mode would be appreciated!!

Validating JWT claims using nimbus jose-jwt (java)

I am using nimbus jost+jwt version 8.19 in a normal java project (not using spring). I have some claims such as iss, aud and sub and want to validate them. (I want iss, aud and sub to be a specific value). I want the parser to throw an exception when the claims dont match.
The example provided here worked fine in earlier versions , But it seems like it was changed in later versions.
Earlier (version 8.3) I used to validate using the following code
JWKSet jwkSet = new JWKSet(utils.rsakey);
JWKSource<SecurityContext> jwkSource = new ImmutableJWKSet<>(jwkSet);
ConfigurableJWTProcessor<SecurityContext> jwtProcessor = new DefaultJWTProcessor<>();
jwtProcessor.setJWSTypeVerifier(new DefaultJOSEObjectTypeVerifier<>(new JOSEObjectType("jwt")));
JWSAlgorithm expectedJWSAlg = JWSAlgorithm.RS256;
JWTClaimsSet validClaims= new JWTClaimsSet.Builder()
.issuer(InetAddress.getLocalHost().getHostName()
.subject("matchvalue")
.audience("matchvalue")
.build();
JWSKeySelector<SecurityContext> keySelector =
new JWSVerificationKeySelector<>(expectedJWSAlg, jwkSource);
jwtProcessor.setJWTClaimsSetVerifier(new DefaultJWTClaimsVerifier(
//exact match claims
validClaims,
//Required claims
new HashSet<>(Arrays.asList("exp", "sub","iss"))));
jwtProcessor.setJWSKeySelector(keySelector);
// Process the token
SecurityContext ctx = null; // optional context parameter, not required here
JWTClaimsSet tokenClaims= jwtProcessor.process(token, ctx);
but now (version 8.19) the DefaultJWTClaimsVerifier does not seem to be accepting exact match claims and Required claims parameters. Is there any way to implement the exact match and required claims?
All of my imports for refrence
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.Response;
import com.nimbusds.jose.EncryptionMethod;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JOSEObjectType;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.JWEHeader;
import com.nimbusds.jose.JWEObject;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.KeyLengthException;
import com.nimbusds.jose.Payload;
import com.nimbusds.jose.crypto.DirectDecrypter;
import com.nimbusds.jose.crypto.RSAEncrypter;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.KeyUse;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator;
import com.nimbusds.jose.jwk.source.ImmutableJWKSet;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.proc.BadJOSEException;
import com.nimbusds.jose.proc.DefaultJOSEObjectTypeVerifier;
import com.nimbusds.jose.proc.JWSKeySelector;
import com.nimbusds.jose.proc.JWSVerificationKeySelector;
import com.nimbusds.jose.proc.SecurityContext;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import com.nimbusds.jwt.proc.ConfigurableJWTProcessor;
import com.nimbusds.jwt.proc.DefaultJWTClaimsVerifier;
import com.nimbusds.jwt.proc.DefaultJWTProcessor;

Place ehCache 3 ehcache.xml outside of the Springboot 2 (Spring 5 ) project's jar file

Had spent quite some time figuring out how to externalize ehCache 3 ehcache.xml outside of the jar file of an Spring 5 (Springboot 2.x) project. This is important so that ehcache settings could be tweaked without having to redeploy the project.
Just sharing a solution that worked using Java 8 in case anybody else faces this challenge:
package com.myproject.config;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import javax.cache.Caching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Configures ehCache.
*
* #author
*
*/
#Configuration
#EnableCaching
public class CacheConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(CacheConfiguration.class);
#Value("${myproject.cache.ehcache.xml.fullpath:/dir/outside/of/project/config/ehcache.xml}")
private String ehcacheXmlFullPath;
#Bean
public CacheManager cacheManager() throws URISyntaxException {
// To get from the classpath: getClass().getResource("/ehcache.xml").toURI()
return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager(Paths.get(ehcacheXmlFullPath).toUri(),
getClass().getClassLoader()));
}
}

java EE and cordova

I want to integrate quercus into cordova application, I've create simple test file like this:
import com.caucho.quercus.Quercus;
public class Test {
public static void main(String[] args) {
Quercus interpreter = new Quercus();
interpreter.execute("echo 'foo';");
}
}
but if fail to compile because it require java EE and I have only open jdk 8 installed when I use umake to install all that require to create android app for cordova.
Will I be able to run java EE on android? If yes what I need to install to compile java EE test application and cordova app?
Here is a list of classes that throw error when I run javac:
import javax.annotation.sql.DataSourceDefinition;
import javax.annotation.sql.DataSourceDefinitions;
import javax.cache.Cache;
import javax.ejb.EJB;
import javax.ejb.EJBs;
import javax.ejb.Stateful;
import javax.el.ELResolver;
import javax.el.ExpressionFactory;
import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.NormalScope;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.AmbiguousResolutionException;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.IllegalProductException;
import javax.enterprise.inject.InjectionException;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.New;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Specializes;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedParameter;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.InterceptionType;
import javax.enterprise.inject.spi.Interceptor;
import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessInjectionTarget;
import javax.enterprise.inject.spi.ProcessProducer;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.inject.Stereotype;
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Qualifier;
import javax.inject.Scope;
import javax.interceptor.InterceptorBinding;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

WildFly Swarm Nullpointer #Inject bean

I have a schedule service which gets its job via REST as JSON.
The Resource class:
import java.io.IOException;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import de.fszm.scheduler.controller.SchedulerController;
import de.fszm.scheduler.entities.MySchedule;
#Path("/schedule")
public class ScheduleRESTResource {
#Inject
SchedulerController scheduleController;
#POST
#Path("/job")
#Consumes(MediaType.APPLICATION_JSON)
public void schedule(MySchedule schedule) throws IOException {
scheduleController.buildSchedule(schedule);
}
}
The ScheduleController:
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import de.fszm.scheduler.entities.MySchedule;
#Named
#RequestScoped
public class SchedulerController {
public void buildSchedule(MySchedule schedule) {
System.out.println("Test");
}
}
The SchedulerMain:
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.jaxrs.JAXRSArchive;
import de.fszm.scheduler.controller.SchedulerController;
import de.fszm.scheduler.entities.MySchedule;
import de.fszm.scheduler.rest.JaxRSActivator;
import de.fszm.scheduler.rest.ScheduleRESTResource;
public class SchedulerMain {
public static void main(String[] args) {
try {
Container container = new Container();
container.start();
JAXRSArchive appDeployment = ShrinkWrap.create(JAXRSArchive.class);
appDeployment.addResource(ScheduleRESTResource.class);
appDeployment.addResource(JaxRSActivator.class);
appDeployment.addClass(MySchedule.class);
appDeployment.addClass(SchedulerController.class);
appDeployment.addAllDependencies();
container.deploy(appDeployment);
} catch (Exception e) {
e.printStackTrace();
}
}
}
And the MySchedule ist just a POJO
My problem is that when I POST JSON to the I get a NullPointerException for the injected SchedulerController.
I also have a beans.xml in src/main/webapp/WEB-INF and in my pom.xml I have the following dependency for CDI (weld)
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>weld</artifactId>
</dependency>
and
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
I am using WildFly swarm 1.0.0 beta 6
What did I miss?
I think you are missing the Swarm CDI fraction.
Try adding this to your dependencies instead.
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>

Categories

Resources