Archive

Posts Tagged ‘Java’

Glowroot and Axis

27 September 2018 Leave a comment

Unfortunately by default Glowroot does not support logging Axis’ service calls and their response time. You have to write a plugin for this, which is actually very easy once you grasp how the plugins work.

 

package org.glowroot.agent.plugin.axis.v.one;

import javax.xml.namespace.QName;

import org.glowroot.agent.plugin.api.Agent;
import org.glowroot.agent.plugin.api.MessageSupplier;
import org.glowroot.agent.plugin.api.ThreadContext;
import org.glowroot.agent.plugin.api.ThreadContext.Priority;
import org.glowroot.agent.plugin.api.Timer;
import org.glowroot.agent.plugin.api.TimerName;
import org.glowroot.agent.plugin.api.TraceEntry;
import org.glowroot.agent.plugin.api.checker.Nullable;
import org.glowroot.agent.plugin.api.weaving.BindReceiver;
import org.glowroot.agent.plugin.api.weaving.BindThrowable;
import org.glowroot.agent.plugin.api.weaving.BindTraveler;
import org.glowroot.agent.plugin.api.weaving.OnBefore;
import org.glowroot.agent.plugin.api.weaving.OnReturn;
import org.glowroot.agent.plugin.api.weaving.OnThrow;
import org.glowroot.agent.plugin.api.weaving.Pointcut;
import org.glowroot.agent.plugin.api.weaving.Shim;

public class AxisAspect {

  @Shim("org.apache.axis.client.Call")
  public interface Call {
    String getTargetEndpointAddress();
    QName getOperationName();
  }

  private static class TraceEntryOrTimer {

    private final @Nullable TraceEntry traceEntry;
    private final @Nullable Timer timer;

    private TraceEntryOrTimer(final TraceEntry traceEntry) {
      this.traceEntry = traceEntry;
      timer = null;
    }

    private TraceEntryOrTimer(final Timer timer) {
      this.timer = timer;
      traceEntry = null;
    }

    private void onReturn() {
      if (traceEntry != null) {
        traceEntry.end();
      } else if (timer != null) {
        timer.stop();
      }
    }

    private void onThrow(final Throwable t) {
      if (traceEntry != null) {
        traceEntry.endWithError(t);
      } else if (timer != null) {
        timer.stop();
      }
    }
  }

  @Pointcut(className = "org.apache.axis.client.Call", methodName = "invoke", methodParameterTypes = {}, timerName = "axis service")
  public static class ResourceAdvice {

    private static final TimerName timerName = Agent.getTimerName(ResourceAdvice.class);

    @OnBefore
    public static @Nullable TraceEntryOrTimer onBefore(final ThreadContext context, @BindReceiver final Call call) {
      String transactionName = call.getTargetEndpointAddress() + "#" + call.getOperationName().getLocalPart();
      context.setTransactionName(transactionName, Priority.CORE_PLUGIN);
      TraceEntry traceEntry = context.startServiceCallEntry("HTTP", transactionName, MessageSupplier.create("axis service: {}.{}()",
      call.getTargetEndpointAddress(), call.getOperationName().getLocalPart()), timerName);
      return new TraceEntryOrTimer(traceEntry);
    }

    @OnReturn
    public static void onReturn(@BindTraveler @Nullable final TraceEntryOrTimer entryOrTimer) {
      entryOrTimer.onReturn();
    }

    @OnThrow
    public static void onThrow(@BindThrowable final Throwable t, @BindTraveler @Nullable final TraceEntryOrTimer entryOrTimer) {
      entryOrTimer.onThrow(t);
    }
  }
}

 

 

and the corresponding glowroot.plugin.json

{
“name” : “Axis 1.4 Plugin”,
“id” : “axis”,
“aspects” : [“org.glowroot.agent.plugin.axis.v.one.AxisAspect”]
}

Advertisements
Categories: glowroot, Java Tags: ,

<BEA-000000> <java.lang.NegativeArraySizeException

17 December 2015 Leave a comment

If you get this error with WebLogic’s admin server

####<2015-12-17 14:30:14 EET> <Critical> <EmbeddedLDAP> <www.myserver.com> <AdminSOA> <VDE Replication Thread> <<anonymous>> <> <9f23d04b03212987:b0dda39:151a2d3b538:-8000-0000000000004207> <1450355414644> <BEA-000000> <java.lang.NegativeArraySizeException
	at com.octetstring.vde.EntryChanges.readBytes(EntryChanges.java:274)
	at com.octetstring.vde.EntryChanges.<init>(EntryChanges.java:72)
	at com.octetstring.vde.replication.BackendChangeLog.getChange(BackendChangeLog.java:548)
	at com.octetstring.vde.replication.Replicator.run(Replicator.java:180)
	at com.octetstring.vde.replication.Replication.run(Replication.java:339)
> )

chances are that there are corrupted LDAP files in the <domain home>/servers/<server name>/data/ldap/ldapfiles folder. One possible solution is to delete the changelog.data and changelog.index files (take a backup first of these two files) and restart the admin server.

Categories: Java, ldap, Oracle, WebLogic Tags: , , ,

How to issue a JSON POST request to Spring Data Rest with a JPA relation

25 November 2014 Leave a comment

Lets say we have a class Company which has a relation to a class User

@Entity
@Table(name = "company")
public class Company implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	
	private Long id;
	private String name;
	private User user;

	public Company() {
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "name", nullable = false, length = 200)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne(fetch = FetchType.LAZY, optional = false, targetEntity=my.example.User.class)
	@JoinColumn(name = "user_id")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

and their corresponding repositories:

@RepositoryRestResource(collectionResourceRel = "company", path = "company")
public interface CompanyRepository extends JpaRepository<Company, Long>  {
}

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends JpaRepository<User, Long> {

}

In order to add a user to the company (let’s say we want to link this company to the user with id of 2) you will need to issue the following JSON request to the company repository:

{
"name" : "a company name",
"user" : "http://localhost:8086/user/2"
}

The “user” variable should be the URL that is pointing to the user repository.

Categories: Java, Spring, Spring Data Rest Tags: ,

There Will Be Code

30 September 2013 Leave a comment

“One might argue that a book about code is somehow behind the times—that code is no
longer the issue; that we should be concerned about models and requirements instead.
Indeed some have suggested that we are close to the end of code. That soon all code will
be generated instead of written. That programmers simply won’t be needed because business people will generate programs from specifications.

Nonsense! We will never be rid of code, because code represents the details of the
requirements. At some level those details cannot be ignored or abstracted; they have to be
specified. And specifying requirements in such detail that a machine can execute them is
programming. Such a specification is code.

I expect that the level of abstraction of our languages will continue to increase. I
also expect that the number of domain-specific languages will continue to grow. This
will be a good thing. But it will not eliminate code. Indeed, all the specifications written
in these higher level and domain-specific language will be code! It will still need to
be rigorous, accurate, and so formal and detailed that a machine can understand and
execute it.

The folks who think that code will one day disappear are like mathematicians who
hope one day to discover a mathematics that does not have to be formal. They are hoping
that one day we will discover a way to create machines that can do what we want rather
than what we say. These machines will have to be able to understand us so well that they
can translate vaguely specified needs into perfectly executing programs that precisely meet
those needs.

This will never happen. Not even humans, with all their intuition and creativity,
have been able to create successful systems from the vague feelings of their customers.
Indeed, if the discipline of requirements specification has taught us anything, it is that
well-specified requirements are as formal as code and can act as executable tests of that
code!

Remember that code is really the language in which we ultimately express the requirements.
We may create languages that are closer to the requirements. We may create tools
that help us parse and assemble those requirements into formal structures. But we will
never eliminate necessary precision—so there will always be code.”

Robert C. Martin – Clean Code. A Handbook of Agile Software Craftmanship.

Well said Robert. This summarises, in a few paragraphs, how important code is in our every day activities.

Categories: Books, Java Tags: ,

How to programmatically add a a4j:commandButton and a4j:actionparam

2 April 2013 Leave a comment

After spending a few hours I finally managed to programmatically add an ajax command button and action parameter to JSF (1.2) components. So the following

<a4j:commandButton value="Click Me"
	styleClass="buttons" rendered="true" immediate="true"
	id="buttonId" actionListener="#{newRequestBean.doSomething}"
	ignoreDupResponses="true">
	<a4j:actionparam name="theVat" id="vatId" value="IComeFromTheBean" assignTo="#{newRequestBean.vat}" noEscape="false"/>
</a4j:commandButton>

translates to

MethodExpression methodExpression = application.getExpressionFactory().createMethodExpression(
                                FacesContext.getCurrentInstance().getELContext(),
                                "#{newRequestBean.doSomething}",
                                null,
                                new Class[0]);
HtmlAjaxCommandButton button = (HtmlAjaxCommandButton) application.createComponent(HtmlAjaxCommandButton.COMPONENT_TYPE);
button.setValue("Click Me");
button.setStyleClass("buttons");
button.setRendered(true);
button.setImmediate(true);
button.setId("buttonId");
button.setIgnoreDupResponses(true);
button.setActionExpression(methodExpression);

ValueBinding vb = context.getApplication().createValueBinding("#{newRequestBean.vat}");
HtmlActionParameter param = (HtmlActionParameter) application.createComponent(HtmlActionParameter.COMPONENT_TYPE);
param.setId("vatId");
param.setName("theVat");
param.setValue("IComeFromTheBean");
param.setAssignToBinding(vb);
param.setNoEscape(false);

button.addActionListener(param);
button.getChildren().add(param);

Things to notice:

1) The last parameter of the method expression should be an empty array (new Class[0]), otherwise you will get the exception “java.lang.IllegalArgumentException: wrong number of arguments“. Your method should have no parameters.
2) You should add the HtmlActionParameter as the action listener of the command button. This is an absolutely essential step in order to make it work (well, this is only if you want to pass parameters to the call). It took me literally hours to figure this out.
3) The context variable is a FacesContext.

Categories: Java, JSF, RichFaces Tags: , ,

SOAPException: faultCode=SOAP-ENV:Client; msg=For input string: "3773 "; targetException=java.lang.NumberFormatException

7 December 2012 1 comment

I get this warning with JMeter

WARN - jmeter.protocol.http.sampler.WebServiceSampler: [SOAPException: faultCode=SOAP-ENV:Client; msg=For input string: "3773 "; targetException=java.lang.NumberFormatException: For input string: "3773 "]

every time I try to test a web service by running a WebService(SOAP) Request. Strange it might seem, but I found out that the error is actually a network error; to be more specific an HTTP/1.1 504 Proxy Timeout ( The connection timed out. ). The original error is totally misleading.

The connection times out, so I get no SOAP response back. What I get though is the headers, the content and the content length back. But for some reason the content length is padded with some spaces at the end and when the HTTPUtils (the class that handles the SOAP request/response) tries to parse it with this statement

Integer.parseInt(value);

it fails. I had to rebuild jmeter and step through it with breakpoints in order to see what’s going on. The solution is as simple as adding a trim to the statement (a couple of lines above the offending statement):

String value = valuebuf.toString().trim();

This fixes the issue and you get the actual response back which is

Error Code 10060: The gateway could not receive a timely response from the website you are trying to access. This might indicate that the network is congested, or that the website is experiencing technical difficulties..

Android INSTALL_FAILED_CONTAINER_ERROR on installing an application

28 April 2012 4 comments

I got this error while I rebooted my android device in the middle of installing an application (it was taking ages so I assumed that it was stuck). It seems that android did not have time to clean the file system, so every time i was trying to install an application I was getting the error:

ASEC file ‘/mnt/secure/asec/smdl2tmp1.asec’ currently exists – destroy it first! (Address already in use)

The solution to this is actually very simple. Make sure that you have connected your device to your computer and that it’s running on debug mode (it’s not mounted). Open a command window, go to your Android\android-sdk\platform-tools and type adb shell. This should open the adb shell and connect as root. Then simply type rm /mnt/secure/asec/smdl2tmp1.asec and the problem should go away.

 

Categories: Android Tags: , ,