Jenkins Extension Point for Job API - java

Is there a Extension Point for the Job REST API?
I want to add some information when http://server/jenkins/job/job_name/job_number/api/json is called.
Any hints?

OK, after a lot of research and tries, I've found the answer.
To expose additional data in the Job/Build REST API, the TransientActionFactory (http://javadoc.jenkins-ci.org/jenkins/model/TransientActionFactory.html) needs to be extended using the AbstractBuild (http://javadoc.jenkins-ci.org/hudson/model/AbstractBuild.html) as it type.
You'll have something like this:
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import java.util.Collection;
import java.util.Collections;
import jenkins.model.TransientActionFactory;
#Extension
public class MyTransientActionFactory extends TransientActionFactory<AbstractBuild> {
#Override
public Class<AbstractBuild> type() {
return AbstractBuild.class;
}
#Override
public Collection<? extends Action> createFor(AbstractBuild target) {
return Collections.singleton(new MyAction(target));
}
}
That will add MyAction to the AbstractBuild actions list which is shown in the REST API.

Related

How to create JobConsumer in Sling?

According to Apache Sling official site(https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html#job-consumers), this is the way to write JobConsumer.
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
#Component
#Service(value={JobConsumer.class})
#Property(name=JobConsumer.PROPERTY_TOPICS, value="my/special/jobtopic",)
public class MyJobConsumer implements JobConsumer {
public JobResult process(final Job job) {
// process the job and return the result
return JobResult.OK;
}
}
However #Service and #Property are both deprecated annotations.
I want to know the proper way to create JobConsumer.
Does anyone know how to write a code equivalent to the above?
The scr annotations are deprecated in AEM and it is recommended to use the official OSGi Declarative Services annotations going forward. There is a seminar by Adobe on using the OSGi R7 annotations.
The new way of writing the same would be
import org.osgi.service.component.annotations.Component;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
#Component(
immediate = true,
service = JobConsumer.class,
property = {
JobConsumer.PROPERTY_TOPICS +"=my/special/jobtopic"
}
)
public class MyJobConsumer implements JobConsumer {
public JobResult process(final Job job) {
// process the job and return the result
return JobResult.OK;
}
}
You can refer to the below blog. I have tried to explain it here.
https://www.linkedin.com/pulse/aem-how-write-sling-jobs-aem-veena-vikraman

Is it possible to have only 1 implementation of IAnnotationTransformer in a project

Can we have more than 1 implementation of IAnnotationTransformer in a project that is using TestNG?
I'm using TestNg version 7.0.0.
TestNG currently lets you wire in ONLY ONE implementation of IAnnotationTransformer. If you try to plug in multiple ones of them, the last one that got added is what will get invoked.
There's an open issue that is tracking this ask. See GITHUB-1894.
As an alternative you can build your own composite IAnnotationTransformer which can be used to iterate through all the other annotation transformer instances. Here's a sample (Its available in the above mentioned github link)
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import org.testng.collections.Lists;
import org.testng.internal.ClassHelper;
public class CompositeTransformer implements IAnnotationTransformer {
private static final String JVM_ARGS =
"com.rationaleemotions.github.issue1894.Listener1, com.rationaleemotions.github.issue1894.Listener2";
private List<IAnnotationTransformer> transformers = Lists.newArrayList();
public CompositeTransformer() {
// Ideally this would get a value from the command line. But just for demo purposes
// I am hard-coding the values.
String listeners = System.getProperty("transformers", JVM_ARGS);
Arrays.stream(listeners.split(","))
.forEach(
each -> {
Class<?> clazz = ClassHelper.forName(each.trim());
IAnnotationTransformer transformer =
(IAnnotationTransformer) ClassHelper.newInstance(clazz);
transformers.add(transformer);
});
}
#Override
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
for (IAnnotationTransformer each : transformers) {
each.transform(annotation, testClass, testConstructor, testMethod);
}
}
}

How to create model listener for virtual instance's create action in Liferay 7?

package com.test.model.listener;
import org.osgi.service.component.annotations.Component;
import com.google.gson.InstanceCreator;
import com.liferay.portal.kernel.exception.ModelListenerException;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.ModelListener;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.*;
#Component(immediate = true, service = ModelListener.class)
public class InsertInstanceModelListener extends BaseModelListener<Instance??> {
#Override
public void onAfterCreate(Instance?? model) throws ModelListenerException {
System.out.println("InsertInstanceModelListener.onAfterCreate()");
super.onAfterCreate(model);
}
}
I'm newbie of liferay.I think it's may be something like this,but don't know how to make it right.
You are on your way. This OSGi component needs to be specific,
you are probably looking for com.liferay.portal.kernel.model.VirtualHost
If I understood correctly.
You do not need to call supper though.

Play Framework 2.X : Dealing w/ onReqest and redirect in Global.java

I'm new to Play Framework. I'm working on a basic project and i'm working now on the authentication function. I want to redirect the unauthorized user to the /login route.
I discover the Global.java class that allows me to control actions accross my project, in particular with onRequest function. I'm planning of using it to do the redirection.
I search several solutions on the web but I couldn't find a working one.
My class:
import play.*;
import play.mvc.Action;
import play.mvc.*;
import play.mvc.Http.*;
import play.mvc.Result.*;
import play.libs.F.*;
import static play.mvc.Results.*;
import play.mvc.Http.Request;
import java.lang.reflect.Method;
public class Global extends GlobalSettings {
#Override
public Action onRequest(Request request, Method actionMethod) {
//Check if the user is connected
if (request.cookie("PLAY_SESSION") == null && !request.path().startsWith("/login")) {
System.out.println("UNAUTHORIZED");
return new Action.Simple() {
#Override
public Result call(Context ctx) throws Throwable {
return redirect(controllers.routes.Application.index());
}
};
}
return super.onRequest(request, actionMethod);
}
}
I found this and i don't understand why Play! doesn't want to compile :
error: <anonymous Global$1> is not abstract and does not override abstract method call(Context) in Action
error: method does not override or implement a method from a supertype
I'm not casual with Play and i don't really understand the problem. Can someone help me please ? Thanks !
I haven't used Play Framework for a while now but I think that the problem is that in 2.2 they made Action to return Promise and not just Result. Hence there is your problem.
Check your version of Action.Simple.call() that it matches
Result call(Context ctx) throws Throwable
See the difference between
https://www.playframework.com/documentation/2.2.x/api/java/index.html
https://www.playframework.com/documentation/2.1.x/api/java/index.html
(look at the return type of the call method)
EDIT
I am not sure whether this is the best approach but it should work.
#Override
public F.Promise<Result> call(Context ctx) throws Throwable {
return F.Promise.pure(redirect(controllers.routes.Application.index()));
}
F.Promise.pure() can be used to convert a Result (or anything that implements it, like Results.Status for example) to a Promise.
Example, where ok() returns play.mvc.Results.Status:
F.Promise.pure(ok("[No Preview Available]"));

Play Framework Does Not Create Models

I just downloaded the play framework from their site and am working through this tutorial.
I've noticed the framework creates the folders app/controllers and app/views, but not a models folder. I created it manually and added Task.java to it. When I get to the section entitled "Rendering the first page" and open localhost:9000/tasks I get a compilation error that says package play.models does not exist. Here is what my Task.java looks like:
package models;
import java.util.*;
public class Task {
public Long id;
#Required
public String label;
public static List<Task> all() {
return new ArrayList<Task>();
}
public static void create(Task task) {
}
public static void delete(Long id) {
}
}
Here is application.java, the file generating the compilation error:
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import play.data.*;
import play.models.*; // COMPILATION ERROR: "package play.models does not exist"!
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class);
public static Result index() {
//return ok(index.render("Your new application is ready."));
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(views.html.index.render(Task.all(), taskForm));
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
I believe it's supposed to be import models.Task; as opposed to import play.models.*;
That's quite confusing (IMHO) step in this tutorial, instead scroll down to Persist the tasks in a database section which describes preparing a model to cooperate with DB :) (it extends Model class, uses proper annotations, etc)
As you recognized it yet, you need to create a models package yourself.
Also as cYn wrote: you should import models like models.SomeModel into your controller
You are correct HukeLau_DABA , the Play will not create the models package for you. you have to create it.
I got these imports in my Application controller class. I got this sample play application running.
import play.api._
import play.api.mvc._
import play.api.data.Form
import play.api.data.Forms._
import models.Task
and another thing in Eclipse is it will not import the necessary imports automatically.
it is bit pain now, once the IDE support get better I hope this will change.

Categories

Resources