Tag: java

Migrating Legacy AdMob to Google Play Services / Google Mobile Ads SDK

I developed my first Android app a few years back and at the time it was using Admob for mobile banner ads. Google bought AdMob a while back, and the time has come where Legacy AdMob usage is being retired this month (August 2014) and so you need to upgrade to the Google Play Services based Google Mobile Ads SDK.

First. Wow. I have to say, the steps and docs for how to do this seem to be spread over many different places. I’m not sure if all of these places actually walk you through the same steps just written in a different way, but it’s taken me a while to work out what I actually need to do. Some useful refs:

If you logon on the AdMob site, it will prompt you to complete a data migration step and update some account info – take care of that first here.

Info about legacy AdMod shutting down is here. Additional info in the FAQ.

Steps for moving to the Google Play Services based advertising seems to be covered here. I’m currently working through these steps for one of my apps, if I come across anything useful to share then I’ll post another update later.

(Page views: 50)

Java 7 & 8 install location on Mac OS X

Somewhere between Java 6 and 7 it seems I lost track of where your JDK gets installed on Mac OS X. Prior to Java 7, it seems it was installed to:

/System/Library/Frameworks/JavaVM.framework/Versions

with symlinks pointing to the exact locations.

I was just setting up a new Eclipse install and was looking for where my 8 was installed – it was clearly installed as ‘java -version’ was telling me I was running 8, but it was no longer in the above location.

/usr/libexec/java_home (which I’ve mentioned before here) was telling me the following:

/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home

Hmm, so there you go. Looking in /Library/Java/JavaVirtualMachines/ I had multiple versions of 7 and 8. If you need to point Eclipse to a JRE location for your installed JREs, then from 7 onwards I think this is what you need.

(Page views: 53)

SSLProtocolException in SE7 & SE8: “handshake alert: unrecognized_name”

Caused by: com.sun.jersey.api.client.ClientHandlerException: 
javax.net.ssl.SSLProtocolException: handshake alert:  
unrecognized_name at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle (URLConnectionClientHandler.java:151) 
at com.sun.jersey.api.client.Client.handle(Client.java:648) 
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680) 
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) 
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:5 68) 

This exception is from an SSL check in SE7 and above that checks that your SSL certificate matches your domain name. For development, if you’re using a self-signed SSL certificate for testing which does not match your domain name, you can turn off the check and ignore the error with:

-Djsse.enableSNIExtension=false

(Page views: 167)

EE6 and EE7 Maven deps for JBoss AS7 and WildFly8

For EE7 on WildFly8.x, use:

Full profile:

        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-7.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

Web profile:

        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-web-7.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>            
        </dependency

For EE6 on JBoss AS7.x, use:

        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

Info from JBoss’s EE spec Git repo here and referenced from this question here.

(Page views: 69)

JSF 2.x Conditional error messages

To conditionally show a Twitter Bootstrap styled DIV with a list of error messages, use the rendered property on panelGroup to check whether messageList has messages to be displayed, and if so, render the enclosed content.

layout=”block” displays the output as a DIV, and row is one of the Bootstrap classes for row display.

<div class="span12 alert alert-danger"></div>

Set messages to display from your Backing Bean with:

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("your message");

(Page views: 127)

JSTL notes

A few notes for a few common JSTL things so I don’t forget:

taglibs:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

Conditionals:

<c:choose>
  <c:when test=””>
  </c:when>
  <!--other when conditions -->
  <c:otherwise>
  </c:otherwise>
</c:choose>

String replace using replace function:

${fn:replace(foo, '"', '\"')}

This next tip for using a loop varStatus to build HTML ids is from: http://stackoverflow.com/questions/6600738/use-jstl-foreach-loops-varstatus-as-an-id

<c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
  <div id="divIDNo${theCount}">
  </div>
</c:forEach>

This unexpectedly gives:

<div id="divIDNojavax.servlet.jsp.jstl.core.LoopTagSupport$1Status@5570e2">

To get the actual varStatus id you need to use:

${theCount.index} gives value from 0
${theCount.count} gives value from 1

For example:

<div id="divIDNo${theCount.index}">

 

(Page views: 57)

Useful Java ME 8 Embedded code snippets

Collection of ME 8 Embedded code snippets:

  • Using java.util.TimerTask and Timer – for scheduling periodic tasks (reading a sensor, checking a status etc eg
MyTimerTask myTimerTask = new MyTimerTask();
Timer timer = new Timer();
timer.schedule(myTimerTask, millisBeforeStarting, millisBetweenExecution);
  • Opening a GPIO pin:
this.pin = PeripheralManager.open(this.pinId); 
this.pin.setValue(true); // true = on, false = off
  • Adding an EventListener to a switch:
GPIOPinConfig config = new GPIOPinConfig(this.portId, this.pinId,
    GPIOPinConfig.DIR_INPUT_ONLY, PeripheralConfig.DEFAULT,
    GPIOPinConfig.TRIGGER_BOTH_EDGES,
this.pin = PeripheralManager.open(config);
this.pin.setInputListener(this);

… where this implements PinListener and valueChanged() method:

public void valueChanged(PinEvent event) {
    GPIOPin eventPin = event.getPeripheral();
    ...
}
  • Opening an I2C device for input/output:
I2CDeviceConfig config = new I2CDeviceConfig(i2cBus, address, addressSizeBits, serialClock);
myDevice = PeripheralManager.open(config);
  • Read from a UART device:
this.uart = PeripheralManager.open(UART_DEVICE_ID);
this.uart.setBaudRate(9600);

InputStream is = Channels.newInputStream(uart);
this.serialBufferedReader = new BufferedReader(new InputStreamReader(is));
  • Read using Generic Connection Framework:
CommConnection conn = (CommConnection)Connector.open("comm:/dev/ttyAMA0;baudrate=9600");
InputStream inputStream = conn.openInputStream();
this.serialBufferedReader = new BufferedReader(new
    InputStreamReader(inputStream));
  • Open and write to a record store:
this.store = RecordStore.openRecordStore(storeName, true);
byte[] dataBytes = data.getBytes();
recordNum = store.addRecord(dataBytes, 0, dataBytes.length);

(Page views: 63)

Debugging JAXB unmarshalling issues

JAXB appears to fail silently in some cases if the XML it’s attempting to unmarshall to mapped classes doesn’t have the necessary mapped properties.

You can get additional output by adding the following:

-Djaxb.debug=true

- displays information during JAXB initialization

Before you call unmarshall() on your Unmarshaller, call setEventHandler() and add a DefaultValidationEventHandler as follows:

Unmarshaller um = jaxbContext.createUnmarshaller();
um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());

- this will output additional failure information about missing mappings for xml elements, useful if your mapped class ends up with missing values.

(Page views: 108)

Maven settings.xml config for default Sonar db server

If you want to set a default Sonar configuration for all your Maven projects to run against, you can add your settings as a profile in the profiles section in your default settings.xml in either your ~/.m2 dir or your maven_install_dir/conf dir (depending which you’re using)

<profiles>
    <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- MySQL -->
                <sonar.jdbc.url>
                    jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
                </sonar.jdbc.url>
                <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
                <sonar.jdbc.username>your_sonar_db_userid</sonar.jdbc.username>
                <sonar.jdbc.password>your_sonar_pwd</sonar.jdbc.password>
            </properties>
        </profile>
  </profiles>

 

(Page views: 380)