org.beigesoft.android.ajetty.JettyService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-jetty Show documentation
Show all versions of a-jetty Show documentation
A-Jetty for Android. It is Jetty 9.2 adapted for Android
configured as minimum server with WebAppDeployer that can deploy ordinal WAR (JSP/JSTL must be pre-compiled into servlets by A-Tomcat).
It's only for tests purposes. It doesn't comply to the latest Android policy (loading executable binaries from outside)!
package org.beigesoft.android.ajetty;
/*
* Copyright (c) 2016 Beigesoft ™
*
* Licensed under the GNU General Public License (GPL), Version 2.0
* (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/
import java.util.Map;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import org.beigesoft.ajetty.BootStrap;
/**
* A-Jetty Android service.
*
* @author Yury Demidenko
*/
public class JettyService extends Service {
/**
* Action start.
**/
public static final String ACTION_START =
"org.beigesoft.android.ajetty.START";
/**
* Action stop.
**/
public static final String ACTION_STOP
= "org.beigesoft.android.ajetty.STOP";
/**
* Application beans map reference to lock.
**/
private Map beansMap;
/**
* Flag to avoid double invoke.
**/
private boolean isActionPerforming = false;
/**
* on create.
**/
@Override
public final void onCreate() {
ApplicationPlus appPlus = (ApplicationPlus) getApplicationContext();
this.beansMap = appPlus.getBeansMap();
}
/**
* onBind handler. No bind provide.
* @param pIntent Intent
* @return IBinder IBinder
**/
@Override
public final IBinder onBind(final Intent pIntent) {
return null;
}
/**
* Called when we receive an Intent. When we receive an intent sent
* to us via startService(), this is the method that gets called.
* So here we react appropriately depending on the
* Intent's action, which specifies what is being requested of us.
* @param pIntent Intent
* @param pFlags flags
* @param pStartId startId
* @return int status
*/
@Override
public final int onStartCommand(final Intent pIntent,
final int pFlags, final int pStartId) {
String action = pIntent.getAction();
if (action.equals(ACTION_START)) {
synchronized (this) {
if (!this.isActionPerforming) {
this.isActionPerforming = true;
StartThread stThread = new StartThread();
stThread.start();
}
}
} else if (action.equals(ACTION_STOP)) {
synchronized (this) {
if (!this.isActionPerforming) {
this.isActionPerforming = true;
StopThread stThread = new StopThread();
stThread.start();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopSelf();
}
return START_NOT_STICKY; // Means we started the service, but don't want
//it to restart in case it's killed.
}
/**
* Android Service destroy.
* @see android.app.Service#onDestroy().
*/
@Override
public final void onDestroy() {
//nothing;
}
/**
* Get BootStrapEmbedded from app-context.
* It invoked by start/stop threads.
* @return BootStrap BootStrap
*/
private BootStrap getBootStrap() {
BootStrap bootStrap = null;
// this.beansMap already synchronized
Object bootStrapO = this.beansMap
.get(BootStrap.class.getCanonicalName());
if (bootStrapO != null) {
bootStrap = (BootStrap) bootStrapO;
} else {
//already stopped
stopSelf();
}
return bootStrap;
}
/**
* Thread to start A-Jetty.
*/
private class StartThread extends Thread {
@Override
public void run() {
synchronized (JettyService.this.beansMap) {
BootStrap bootStrap = getBootStrap();
if (bootStrap != null && !bootStrap.getIsStarted()) {
try {
if (bootStrap.getServer() == null) {
bootStrap.createServer();
bootStrap.getDeploymentManager().getContextAttributes()
.setAttribute("android.content.Context", JettyService.this);
}
bootStrap.startServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}
synchronized (JettyService.this) {
JettyService.this.isActionPerforming = false;
}
}
};
/**
* Thread to stop A-Jetty.
*/
private class StopThread extends Thread {
@Override
public void run() {
synchronized (JettyService.this.beansMap) {
BootStrap bootStrap = JettyService.this
.getBootStrap();
if (bootStrap != null && bootStrap.getIsStarted()) {
try {
bootStrap.stopServer();
JettyService.this.beansMap
.remove(BootStrap.class.getCanonicalName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
synchronized (JettyService.this) {
JettyService.this.isActionPerforming = false;
}
}
};
//Simple getters and setters:
/**
* Getter for beansMap.
* @return Map
**/
public final Map getBeansMap() {
return this.beansMap;
}
/**
* Setter for beansMap.
* @param pBeansMap reference
**/
public final void setBeansMap(final Map pBeansMap) {
this.beansMap = pBeansMap;
}
/**
* Getter for isActionPerforming.
* @return boolean
**/
public final boolean getIsActionPerforming() {
return this.isActionPerforming;
}
}