All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beigesoft.android.ajetty.JettyService Maven / Gradle / Ivy

Go to download

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)!

The newest version!
/*
BSD 2-Clause License

Copyright (c) 2019, Beigesoft™
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.beigesoft.android.ajetty;

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; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy