Page MenuHomePhorge

Init.java
No OneTemporary

Init.java

/*
* Copyright (C) 2014-2015 Lannocc Technologies
* @%@~LICENSE~@%@
*/
package com.iovar.web.sbin;
// local imports:
import com.iovar.web.*;
import com.iovar.web.bin.shell.*;
import com.iovar.web.bin.shell.task.TaskData;
import com.iovar.web.dev.*;
import com.iovar.web.proc.*;
// java imports:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
// 3rd-party imports:
/**
* System initialization.
* Servlet init() looks for 'inittab' init-param that points to location of
* init configuration file describing the actual steps.
*
* @author shawn@lannocc.com
*/
public class Init extends HttpServlet
{
public static final String INIT_PARAM = "inittab";
public static final String TAG_INIT = "init";
public static final String TAG_BOOT_WAIT = "bootwait";
protected void doHead (final HttpServletRequest req, final HttpServletResponse resp) throws IOException
{
Utils.doHead (this, req, resp);
}
protected void doGet (final HttpServletRequest req, final HttpServletResponse resp) throws IOException
{
Utils.doGet (this, req, resp);
}
protected void doPut (final HttpServletRequest req, final HttpServletResponse resp) throws IOException
{
Utils.doPut (this, req, resp);
}
public void init ()
{
final String init = this.getInitParameter (INIT_PARAM);
if (init==null) return;
Log.debug ("system init thread split");
Log.debug ("init param: "+init);
/*
* Unfortunately, the Servlet API provides no good mechanism to insert
* code that should be executed after all filter/servlet inits but
* before servicing any user request.
*
* Tomcat will not let any servlets service a request while still within
* the init() process so we use this thread split and small sleep
* timeout as an ugly hack.
*
* FIXME: find a better place for "system initialization" ?
*/
new Thread (new Runnable ()
{
public void run ()
{
Log.debug ("system init thread started");
final ServletContext context = getServletContext ();
try
{
Thread.sleep (1500);
Log.info ("system init begin");
Log.debug ("calling shell init for master environment");
final ShellSession.Instance shell = Shell.init (context);
final Transport config = Transport.handler (init, context);
Log.debug ("init config resource: "+config);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();
Document xml = factory.newDocumentBuilder ().parse (config.get ());
if (!TAG_INIT.equals (xml.getDocumentElement ().getNodeName ()))
{
throw new IllegalArgumentException ("expecting <"+TAG_INIT+"> document, got: "+xml.getDocumentElement ());
}
NodeList tags = xml.getElementsByTagName (TAG_BOOT_WAIT);
for (int t=0; t<tags.getLength (); t++) try
{
//final String bootwait = tags.item (t).getNodeValue ();
final Node bootwait = tags.item (t).getFirstChild ();
Log.info ("bootwait: "+bootwait);
if (bootwait!=null)
{
//final Transport resource = Transport.handler (bootwait.getNodeValue (), context);
//Log.debug ("bootwait resource: "+resource);
// FIXME (user)
Shell.exec (bootwait.getNodeValue (), null, new TaskData (shell, context, null));
//shell.saveTo ("local:/proc/shell/"+shell.getId (), context);
}
}
catch (final ServletException e)
{
Log.error ("failure executing bootwait", e);
}
catch (final IOException e)
{
Log.error ("failure executing bootwait", e);
}
}
catch (final InterruptedException e)
{
Log.warn ("interrupted", e);
}
catch (final IOException e)
{
Log.fatal ("failure loading init file: "+init, e);
}
catch (final ParserConfigurationException e)
{
Log.fatal ("failure loading init file: "+init, e);
}
catch (final SAXException e)
{
Log.fatal ("failure loading init file: "+init, e);
}
catch (final IllegalArgumentException e)
{
Log.fatal ("failure loading init file: "+init, e);
}
finally
{
Log.info ("system init end");
}
}
}).start();
}
}

File Metadata

Mime Type
text/x-java
Expires
Mon, Sep 14, 3:50 AM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3587
Default Alt Text
Init.java (5 KB)

Event Timeline