Remote Debugging Play Framework

At times it is required to debug the code, debugging can be easy with any IDE. Running a play framework app and remote debug the application is possible by attaching the process and placing appropriate break points which would hit the breakpoint when the process is attached.

Start sbt-shell

Run Command clean compile test

This would result in starting a process with PID which can be traced from the sbt-shell window

opt/jdk1.8.0_181/bin/java -agentlib:jdwp=transport=dt_socket,address=localhost:35226,suspend=n,server=y -Xdebug -server -Xmx1536M -Didea.managed=true -Dfile.encoding=UTF-8 -Didea.runid=2018.2 -jar /home/sufia/.IdeaIC2018.2/config/plugins/Scala/launcher/sbt-launch.jar idea-shell
Listening for transport dt_socket at address: 35226

[sufia@sufia informis-server]$ lsof -i:35226
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 5981 sufia 4u IPv4 668592 0t0 TCP localhost:35226 (LISTEN)

In InteliJ->Run->Attach to Process, select the PID: 5981

This would hit the breakpoint and you would be able to debug through

Samples on Play

http://ics-software-engineering.github.io/play-example-mysql/

http://pedrorijo.com/blog/play-slick/

 

https://github.com/daxyonis

https://alexgaribay.com/2014/06/16/authentication-in-play-framework-using-java/

Chat Application

http://matthiasnehlsen.com/blog/2013/06/23/angularjs-and-play-framework/

Reactive, real-time log search with Play, Akka, AngularJS and Elasticsearch

Forums built with Play

https://github.com/hakandilek/publicplay

https://devhub.io/repos/eljaydub-play2-crud

http://www.programcreek.com/java-api-examples/index.php?source_dir=play2-crud-master/project-code/app/play/utils/crud/views/Utils.java#showField-FieldMetadata-fieldMetadata-Object-o

Improving Play! 2.1 with Knockout.js

 

http://vyazici.blogspot.in/2012/03/chat-application-with-play-framework.html

http://www.vdzon.com/2015/08/24/creating-a-web-application-with-play-akka-and-java/

http://geeks.aretotally.in/tag/play-framework-2/

https://dzone.com/articles/persistence-and-dao-testing-0

https://github.com/ianrae/mettle-framework

https://www.toptal.com/java/building-modern-web-applications-with-angularjs-and-play-framework

https://github.com/unterstein/play-knockout-sample

http://www.ibm.com/developerworks/library/wa-playful-web-dev-1-trs-bluemix/

 

Optimizing Static Asset Loading with Play Framework

FORMS

https://github.com/JoseVte/template-api-rest-java-playframework.git

https://github.com/opensas/play-demo/wiki/Step-6—styling-form

JPA

Neat Explanation of using JPA with Play:- http://www.waitingforcode.com/play-framework/database-and-jpa-in-play-framework/read

application.conf example :- https://raw.githubusercontent.com/akquinet/Play–Framework-Example–Library/master/conf/application.conf

http://stackoverflow.com/questions/20836923/difference-between-jpa-and-ebean-play-framework

https://github.com/nikitsenka/play-jpa-java-example

https://github.com/beingsagir/play-java-spring-data-jpa

https://github.com/jamesward/play2bars/tree/java-jpa

https://github.com/zarinfam/play24-guice-spring

http://gbmetzner.github.io/Multiple-DB/

Authorization

https://github.com/schaloner/deadbolt-2

http://stackoverflow.com/questions/28459333/how-to-build-an-uber-jar-fat-jar-using-sbt-within-intellij-idea

https://www.tunnelbear.com/blog/authenticating-users-in-the-play-framework/

https://github.com/mslinn/play-authenticated

https://github.com/knoldus/jwt-play-authentication

https://github.com/TunnelBear/PlayAuthenticationSample

ACTION COMPOSITION

http://iankent.uk/blog/action-composition-in-play-framework/

https://resources.oreilly.com/examples/9781783982400/tree/master/2400OS%20code%20bundle/Chapter%207/pfe-java/app/controllers

http://bryangilbert.com/post/code/scala/action-composition-auth/

Action composition in the Play Framework

APIs, REST

https://github.com/JoseVte/template-api-rest-java-playframework

Building a REST API in Java and Scala Using Play Framework – Part 1

https://semaphoreci.com/community/tutorials/how-to-add-integration-tests-to-a-play-framework-application-using-scala

DI

Dependency injection in Play Framework using Scala

Caching

Introducing the Google Guava Cache

http://www.concretepage.com/google-api/google-guava-cache-example-using-loadingcache-cachebuilder-and-cacheloader

http://fanli7.net/a/JAVAbiancheng/JAVAzonghe/20131003/guicing-play-framework.html

EXPLANATION:http://codingjunkie.net/google-guava-cache/

CACHING EXAMPLE :

https://www.javacodegeeks.com/2015/01/using-google-guava-cache-for-local-caching.html

https://github.com/Haneke/HanekeSwift/issues/101

http://mydailyjava.blogspot.in/2013/07/extending-guava-caches-to-overflow-to.html

 

https://blog.codecentric.de/en/2016/03/caching-de-luxe-spring-guava/

https://vladmihalcea.com/2014/03/03/caching-best-practices/

MODULE DEFINITION and CONFIGURATION

Hibernate with JPA Annotations and Guice

 

Best of Breed Opensource on Play

https://www.theguardian.com/info/developer-blog/2015/dec/30/how-to-add-a-form-to-a-play-application

https://blog.redelastic.com/diving-into-akka-streams-2770b3aeabb0

http://matthiasnehlsen.com/blog/2014/08/04/building-a-geo-aware-link-shortener-with-play-framework/

File Cutters, File Spiltting

https://blog.knoldus.com/2012/03/20/handling-large-data-file-using-scala-and-akka/

https://www.programcreek.com/java-api-examples/?code=reljicd/play-framework-blog/play-framework-blog-master/app/actions/PostExistsAndUserIsOwnerAction.java# playframework samples

https://www.programcreek.com/java-api-examples/?code=CSCfi/exam/exam-master/app/security/CombinedRoleAndPermissionHandler.java#

https://github.com/commercetools/commercetools-sync-javahttps://github.com/commercetools/sphere-play-sdk/tree/0c8abf48b45f349d97f28d6b6b8fc56b9e8a8374

http://stackoverflow.com/questions/41402495/akka-stream-splitting-a-stream-of-bytestring-into-multiple-files

http://stackoverflow.com/questions/29517478/splitting-a-large-log-file-in-to-multiple-files-in-scala

https://github.com/johandahlberg/Splitter

http://james-iry.blogspot.in/2007/09/monads-are-elephants-part-1.html

http://stackoverflow.com/questions/13454347/monads-with-java-8

https://stedolan.github.io/jq/tutorial/

grpc play framework:

https://github.com/raboof/play-scala-grpc-server/tree/master/app

https://github.com/jfloff/grpc-java-sbt

http://jlogg.weebly.com/blog/writing-rest-apis-using-the-grpc-framework

Setting up PHP Environment for Development

https://gist.github.com/DaRaFF/3995789

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

https://www.linode.com/docs/websites/apache/apache-web-server-on-ubuntu-14-04

http://serverfault.com/questions/357108/what-permissions-should-my-website-files-folders-have-on-a-linux-webserver

OSGI CONTAINERS

https://github.com/doolse/sbt-osgi-felix

https://github.com/mbknor/ddsl-play2-module

https://github.com/mathieuancelin/play-osgi

https://github.com/tux2323/sbt-osgi-demo

https://github.com/sbt/sbt-osgi

NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index

URL url = Thread.currentThread().getContextClassLoader()
    .getResource("javax/persistence/Table.class");
System.out.println(url);

I’ve ran into the same problem. The question here is that play-java-jpa artifact (javaJpa key in the build.sbt file) depends on a different version of the spec (version 2.0 -> “org.hibernate.javax.persistence” % “hibernate-jpa-2.0-api” % “1.0.1.Final”).

When you added hibernate-entitymanager 4.3 this brought the newer spec (2.1) and a different factory provider for the entitymanager. Basically you ended up having both jars in the classpath as transitive dependencies.

Edit your build.sbt file like this and it will temporarily fix you problem until play releases a new version of the jpa plugin for the newer api dependency.

libraryDependencies ++= Seq(
javaJdbc,
javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api"),
"org.hibernate" % "hibernate-entitymanager" % "4.3.0.Final"
)

Ubuntu not showing eclipse menu

  1. Open your eclipse.desktop file:
    sudo -H gedit /usr/share/applications/eclipse.desktop
    

    (If you can’t find it in this path, try in ~/.local/share/applications/eclipse.desktop. Otherwise, you could have to find yours using locate command).

  2. Replace the Exec= line with this:
    Exec=env UBUNTU_MENUPROXY= eclipse
    

    Where “eclipse” is the path to your eclipse executable. In this case it’s just “eclipse” since there’s a symlink in /usr/bin folder.

    NOTE: If you can’t find your eclipse.desktop file, you can simply create one from scratch in the above path, and fill it with these lines:

    [Desktop Entry] 
    Type=Application 
    Name=Eclipse 
    Icon=eclipse 
    Exec=env UBUNTU_MENUPROXY= eclipse 
    Terminal=false 
    Categories=Development;IDE;Java;
    
  3. Save the file.

Setting up Play Development Environment on Ubuntu

# Configure apt for java
# http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
apt-get update
apt-get install -y python-software-properties
add-apt-repository -y ppa:webupd8team/java
echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections

# Update apt sources
apt-get update

apt-get install -y unzip wget git vim oracle-java8-installer oracle-java8-set-default

# Install scala
cd /opt
wget http://www.scala-lang.org/files/archive/scala-2.12.0.tgz
tar -zxvf scala-2.12.0.tgz
# end scala

#Installing playframework on ubuntu there are two options

OFFLINE INSTALLER
Go to https://downloads.typesafe.com/typesafe-activator/1.3.12/typesafe-activator-1.3.12.zip, download the offline installer close to 617MB

OR MINIMAL ACTIVATOR

# Install play with activator
#Download and Install Play (the folder should have permission to write)
#wget http://downloads.typesafe.com/typesafe-activator/1.3.12/typesafe-activator-1.3.12-minimal.zip
#unzip typesafe-activator-1.3.12-minimal.zip
#mv activator-1.3.12-minimal activator

The off-line installer downloads all the dependencies required to run play applications. In a scenario where in we download only the small foot print of activator the dependencies are downloaded at the time of loading the project, which can be annoying and time consuming.

Follow the instructions at https://www.playframework.com/documentation/2.5.x/Installing to install the play and chose “installing play with activator” and follow instructions listed
Extracting Activator

Extract the archive on a location where you have write access. Running activator writes some files to directories within the distribution, so don’t install to /opt, /usr/local or anywhere else you’d need special permission to write to.
Adding Activator to your Path

For convenience, you should add the Activator installation directory to your system PATH:

export PATH=$PATH:/opt/activator/bin

source ~/.bashrc

Making the activator as an executable execute the following command

sudo chmod a+x /activator/bin/activator

Just in case after executing the above commands and when we issue activator in the terminal, if it says command not found, we need to manually edit the bashrc file as follows

gedit source ~/.bashrc

add the command to the bottom of the file

export PATH=$PATH:/opt/activator/bin

 

unzip typesafe-activator-1.3.12.zip
mv typesafe-activator-1.3.12 activator

cd /opt
export PATH=$PATH:/opt/activator/bin
source ~/.bashrc
sudo chmod a+x /activator/bin/activator
./activator
# end play with activator

#echo ‘PATH=${PATH}:/opt/scala-2.12.0/bin:/opt/play-2.2.5’ >> /etc/profile
echo ‘PATH=${PATH}:/opt/scala-2.12.0/bin’ >> /etc/profile

echo ‘PATH=${PATH}:/opt/activator/bin’ >> /etc/profile

sudo apt-get install git

echo “Installing IntelliJ IDEA…”

# We need root to install
[ $(id -u) != “0” ] && exec sudo “$0” “$@”

# Attempt to install a JDK
# apt-get install openjdk-8-jdk
# add-apt-repository ppa:webupd8team/java && apt-get update && apt-get install oracle-java8-installer

# Prompt for edition
#while true; do
#    read -p “Enter ‘U’ for Ultimate or ‘C’ for Community: ” ed
#    case $ed in
#        [Uu]* ) ed=U; break;;
#        [Cc]* ) ed=C; break;;
#    esac
#done
ed=C

# Fetch the most recent version
VERSION=$(wget “https://www.jetbrains.com/intellij-repository/releases” -qO- | grep -P -o -m 1 “(?<=https://www.jetbrains.com/intellij-repository/releases/com/jetbrains/intellij/idea/BUILD/)[^/]+(?=/)”)

# Prepend base URL for download
URL=”https://download.jetbrains.com/idea/ideaI$ed-$VERSION.tar.gz”

echo $URL

# Truncate filename
FILE=$(basename ${URL})

# Set download directory
DEST=~/Downloads/$FILE

echo “Downloading idea-I$ed-$VERSION to $DEST…”

# Download binary
wget -cO ${DEST} ${URL} –read-timeout=5 –tries=0

echo “Download complete!”

# Set directory name
DIR=”/opt/idea-I$ed-$VERSION”

echo “Installing to $DIR”

# Untar file
if mkdir ${DIR}; then
tar -xzf ${DEST} -C ${DIR} –strip-components=1
fi

# Grab executable folder
BIN=”$DIR/bin”

# Add permissions to install directory
chmod -R +rwx ${DIR}

# Set desktop shortcut path
DESK=/usr/share/applications/IDEA.desktop

# Add desktop shortcut
echo “[Desktop Entry]\nEncoding=UTF-8\nName=IntelliJ IDEA\nComment=IntelliJ IDEA\nExec=${BIN}/idea.sh\nIcon=${BIN}/idea.png\nTerminal=false\nStartupNotify=true\nType=Application” -e > ${DESK}

# Create symlink entry
ln -s ${BIN}/idea.sh /usr/local/bin/idea

echo “Done….Installing IntelliJ IDEA…”

echo “Start….Installing Scala..”

Installing SBT on Ubuntu

Sbt is the default build tool for Scala. This post talks about steps to install sbt on ubuntu.
 

Steps to Install SBT on Ubuntu

#Add typesafe repository     
wget http://apt.typesafe.com/repo-deb-build-0002.deb `

#Install the repository
sudo dpkg -i repo-deb-build-0002.deb

#Refresh
sudo apt-get update

#Install sbt
sudo apt-get install sbt

#check whether sbt is installed

sbt

Add Specific Version of SBT

Some project need specific version of sbt like 0.13.

If you get error saying

Detected sbt version 0.12.2 Cannot find sbt launcher 0.12.2 Please download: From typesafe.artifactoryonline.com/typesafe/ivy-releases/… To /home/{username}/.sbt/.lib/0.12.2/sbt-launch.jar

Run the following commands by replacing with your username.

mkdir -p /home/{username}/.sbt/.lib/0.12.2/ 
 wget -O /home/{username}/.sbt/.lib/0.12.2/sbt-launch.jar 
 http://typesafe.artifactoryonline.com/typesafe/ivy-releases/
 org.scala-sbt/sbt-launch/0.12.2/sbt-launch.jar

Step by Step load existing Play Project in IDEA

While setting up play with IDEA, it could be daunting to get things right in the first go itself. A step by step tutorial to help one out in setting up a development environment and trouble shooting issues while setting it up.

It starts off with having per-requisites on your OS. If you are looking out for a complete development environment bash script to wire up an environment you could look up earlier post, Bash Script for Play Development Environment

Once you have the Development Environment, we then proceed to load existing data sources as an IDEA project. We need to ensure before we move in that appropriate plug-ins are installed.

Pre-Requisites for IDEA

  • Add SBT Plugin: File->Settings->Plugins->Browse repositories-> SBT-> Install
  • Add Scala: File->Settings->Plugins->Browse repositories-> Scala->Install

sbtplugin


Checkout the project from the repository.

Navigate to the location where the sources would like to be cloned. On the bash issue the clone command to clone the repository.

git clone https://xxxxxx.xxxxxx.git

The command would clone the repository locally in the folder.

Load the Project

Open IDEA, Select option Import Project-> Open the Project Folder (datacaptive)-> open the folder by name Project (project) -> Select file build.sbt

Open the Project by pointing to build.sbt project file in the project folder.

In case the SDK is not selected following error is got, associate appropriate JDK by clicking “New” select JVM (located at /usr/lib/jvm/java-8-oracle)

jvmresolveerror

IDEA would start to resolve dependencies. It might take a little long to resolve for the first time.

resolvedependencies

After IDEA resolves the dependencies, it inquires to select the modules/data to include in the project

resolvingdependencyto

On resolving the project would be loaded, as shown below

screenshot-from-2016-11-25-12-05-48


Exceptional scenarios while loading the project

Controller Errors

Some time, the project would not load as anticipated. Following are some scenarios which were encountered and how they had been tackled.

If you encounter errors on Controllers, open up the sbt console in IDEA

SBT Console in IDEA

run clean and compile commands and issue a build for the project.

Create a Run Configuration as shown at the bottom of this post.

Run Successfuly


 

sbtrc client

Just in case On Resolving Dependencies, if there are errors “like unable to find sbtrc…”

 

...
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: com.typesafe.sbtrc#client-2-11;0.3.1: not found
[warn] :: com.typesafe.sbtrc#actor-client-2-11;0.3.1: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::

projects-> play-fork-run.sbt -> comment out

addSbtPlugin("com.typesafe.play" % "sbt-fork-run-plugin" % "2.5.9")

If I remove “sbt-fork-run-plugin” from that project, then it resolves the sbtrc dependencies.
https://github.com/gifman009/Licenta/blob/master/project/play-fork-run.sbt

I’m not sure if this is an Activator or SBT bug.


Unable to resolve symbol IDEA

When loading a project in IDEA, cannot resolve symbol with play framework occurs or play.data.validation does not exist

Solved it by "File" > "Open Project" and choose the base folder of the Play framework project (delete all idea settings file from the folder before so that it will reimport using the SBT settings).

http://stackoverflow.com/questions/21577573/intellij-idea-can-not-resolve-symbol-with-play-framework


Source Folder not loaded

The above steps at times would not reflect the Project Source files. In such  a case,

  1. closed IntelliJ,
  2. deleted the .idea folder or stash it
  3. restarted IntelliJ and opened the same project.

IntelliJ will generate the new .idea folder and the folders will appear.


Run Configuration

  1. Create a new Run Configuration – From the main menu, select Run -> Edit Configurations
  2. Click on the + to add a new configuration
  3. From the list of configurations, choose “SBT Task”
  4. In the “tasks” input box, simply put “run”
  5. Apply changes and select OK.
  6. Now you can choose “Run” from the main Run menu and run your application

data captive run configuration