<commit_before>package app.hongs.cmdlet.serv;

import app.hongs.Core;
import app.hongs.CoreConfig;
import app.hongs.HongsError;
import app.hongs.HongsException;
import app.hongs.cmdlet.CmdletHelper;
import app.hongs.cmdlet.anno.Cmdlet;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.Map;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.servlet.ServletContextHandler;

// JSP,Session 初始器依赖的类
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.server.session.HashSessionManager;

/**
 * 服务启动命令
 * @author Hongs
 */
@Cmdlet("server")
public class ServerCmdlet {

    @Cmdlet("start")
    public static void start(String[] args) throws HongsException {
        int    port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
        String conf = Core.CORE_PATH + File.separator + "web.xml";
        if ( ! (new File(conf)).exists( ) ) {
               conf = Core.CONF_PATH + File.separator + "web.xml";
        }
        String serd = Core.DATA_PATH + File.separator + "server" ;
        File   ppid = new  File(serd + File.separator +  port + ".pid" );
        File   ppcd = ppid.getParentFile( );

        // 检查进程
        if (ppid.exists() == true ) {
            CmdletHelper.println("Process already exists!");
            return;
        }
        if (ppcd.exists() == false) {
            ppcd.mkdirs();
        }
        try {
            String     pid = ManagementFactory.getRuntimeMXBean()
                            .getName().split("@" , 2)[0];
            FileWriter dip = new  FileWriter(ppid, true);
            dip.write( pid );
            dip.close(     );
        }
        catch (IOException e) {
            throw new HongsException.Common(e);
        }

        // 构建应用
        Server        server;
        WebAppContext webapp;
        server = new  Server ( port );
        webapp = new  WebAppContext();
        webapp.setDescriptor ( conf );
        webapp.setContextPath (Core.BASE_HREF);
        webapp.setResourceBase(Core.BASE_PATH);
        webapp.setParentLoaderPriority( true );
        server.setHandler    (webapp);

        // 外部配置
        CoreConfig c = CoreConfig.getInstance("_init_");
        for(Map.Entry  t : c.entrySet( )) {
            String k = (String) t.getKey  ();
            String v = (String) t.getValue();
            if (k.startsWith("jetty.attr.")) {
                webapp.setAttribute(k.substring(11), v);
            } else
            if (k.startsWith("jetty.init.")) {
                webapp.setInitParameter(k.substring(11), v);
            }
        }

        /**
         * 初始设置
         * 光能外部配置参数还不够方便
         * 可能需要替换 JSP 解析器或 Session 容器
         * 可以设置 jetty.init 来注入 Initer 对象
         */
        String xs = c.getProperty("jetty.init");
        if (null !=  xs) {
            String[] xa = xs.split(";");
            for ( String  xn: xa ) {
                     xn = xn.trim (   );
                if ("".equals(xn)) {
                    continue;
                }

                try {
                    ((Initer) Class.forName(xn).newInstance()).init(webapp);
                } catch (ClassNotFoundException ex) {
                    throw new HongsError.Common(ex);
                } catch (InstantiationException ex) {
                    throw new HongsError.Common(ex);
                } catch (IllegalAccessException ex) {
                    throw new HongsError.Common(ex);
                }
            }
        }

        // 停止机制
        Runtime.getRuntime( ).addShutdownHook(new Stoper(server, ppid));

        try {
            server.start();
            server.join( );
        }
        catch ( Exception e ) {
            throw new HongsException.Common(e);
        }
    }

    private static class Stoper extends Thread {

        private final Server server;
        private final File   ppid  ;

        public Stoper(Server server, File ppid) {
            this.server  = server;
            this.ppid    = ppid  ;
        }

        @Override
        public void run() {
            System.out.println("");
            if (server.isStopped( )) {
                System.err.println("Server is stopped !!!");
                return;
            }
            if (server.isStopping()) {
                System.err.println("Server is stopping...");
                return;<commit_msg>common 更名为 normal<commit_after>package app.hongs.cmdlet.serv;

import app.hongs.Core;
import app.hongs.CoreConfig;
import app.hongs.HongsError;
import app.hongs.HongsException;
import app.hongs.cmdlet.CmdletHelper;
import app.hongs.cmdlet.anno.Cmdlet;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.Map;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.servlet.ServletContextHandler;

// JSP,Session 初始器依赖的类
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.server.session.HashSessionManager;

/**
 * 服务启动命令
 * @author Hongs
 */
@Cmdlet("server")
public class ServerCmdlet {

    @Cmdlet("start")
    public static void start(String[] args) throws HongsException {
        int    port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
        String conf = Core.CORE_PATH + File.separator + "web.xml";
        if ( ! (new File(conf)).exists( ) ) {
               conf = Core.CONF_PATH + File.separator + "web.xml";
        }
        String serd = Core.DATA_PATH + File.separator + "server" ;
        File   ppid = new  File(serd + File.separator +  port + ".pid" );
        File   ppcd = ppid.getParentFile( );

        // 检查进程
        if (ppid.exists() == true ) {
            CmdletHelper.println("Process already exists!");
            return;
        }
        if (ppcd.exists() == false) {
            ppcd.mkdirs();
        }
        try {
            String     pid = ManagementFactory.getRuntimeMXBean()
                            .getName().split("@" , 2)[0];
            FileWriter dip = new  FileWriter(ppid, true);
            dip.write( pid );
            dip.close(     );
        }
        catch (IOException e) {
            throw new HongsException.Common(e);
        }

        // 构建应用
        Server        server;
        WebAppContext webapp;
        server = new  Server ( port );
        webapp = new  WebAppContext();
        webapp.setDescriptor ( conf );
        webapp.setContextPath (Core.BASE_HREF);
        webapp.setResourceBase(Core.BASE_PATH);
        webapp.setParentLoaderPriority( true );
        server.setHandler    (webapp);

        // 外部配置
        CoreConfig c = CoreConfig.getInstance("_init_");
        for(Map.Entry  t : c.entrySet( )) {
            String k = (String) t.getKey  ();
            String v = (String) t.getValue();
            if (k.startsWith("jetty.attr.")) {
                webapp.setAttribute(k.substring(11), v);
            } else
            if (k.startsWith("jetty.init.")) {
                webapp.setInitParameter(k.substring(11), v);
            }
        }

        /**
         * 初始设置
         * 光能外部配置参数还不够方便
         * 可能需要替换 JSP 解析器或 Session 容器
         * 可以设置 jetty.init 来注入 Initer 对象
         */
        String xs = c.getProperty("jetty.init");
        if (null !=  xs) {
            String[] xa = xs.split(";");
            for ( String  xn: xa ) {
                     xn = xn.trim (   );
                if ("".equals(xn)) {
                    continue;
                }

                try {
                    ((Initer) Class.forName(xn).newInstance()).init(webapp);
                } catch (ClassNotFoundException ex) {
                    throw new HongsError.Common(ex);
                } catch (InstantiationException ex) {
                    throw new HongsError.Common(ex);
                } catch (IllegalAccessException ex) {
                    throw new HongsError.Common(ex);
                }
            }
        }

        // 停止机制
        Runtime.getRuntime( ).addShutdownHook(new Stoper(server, ppid));

        try {
            server.start();
            server.join( );
        }
        catch ( Exception e ) {
            throw new HongsException.Common(e);
        }
        catch ( Error     e ) {
            throw new HongsError    .Common(e);
        }
    }

    private static class Stoper extends Thread {

        private final Server server;
        private final File   ppid  ;

        public Stoper(Server server, File ppid) {
            this.server  = server;
            this.ppid    = ppid  ;
        }

        @Override
        public void run() {
            System.out.println("");
            if (server.isStopped( )) {
                System.err.println("Server is stopped !!!");
                return;
            }
            if (server.isStopping()) {
                System.err.println("Server is stopping...");
                return;
{'fraction_non_alphanumeric': 0.11580496006725514, 'fraction_numerical': 0.0025220680958385876, 'mean_word_length': 1.7083096186681843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1465026', 'n_tokens_mistral': 2885, 'n_tokens_neox': 2825, 'n_words': 560}