|
| 1 | +package org.fugerit.java.doc.base.config; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.InputStreamReader; |
| 5 | + |
| 6 | +import org.fugerit.java.core.cfg.ConfigException; |
| 7 | +import org.fugerit.java.core.lang.helpers.ClassHelper; |
| 8 | +import org.fugerit.java.core.util.checkpoint.CheckpointUtils; |
| 9 | + |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | + |
| 12 | +/** |
| 13 | + * DocTypeHandler Initializer. |
| 14 | + * If AOT initialization is needed it is possible to call these Facade. |
| 15 | + * |
| 16 | + */ |
| 17 | +@Slf4j |
| 18 | +public class InitHandler { |
| 19 | + |
| 20 | + public static final String PATH_INIT_DOC = "config/init_doc/doc-init.xml"; |
| 21 | + |
| 22 | + public static boolean initDoc( DocTypeHandler handler ) throws ConfigException { |
| 23 | + boolean init = true; |
| 24 | + long startTime = System.currentTimeMillis(); |
| 25 | + try ( InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( PATH_INIT_DOC ) ); |
| 26 | + ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { |
| 27 | + handler.handle( DocInput.newInput( handler.getType() , reader ) , DocOutput.newOutput( baos ) ); |
| 28 | + log.info( "Init handler time {} -> {}", handler, CheckpointUtils.formatTimeDiffMillis( startTime , System.currentTimeMillis() ) ); |
| 29 | + } catch (Exception e) { |
| 30 | + throw new ConfigException( "Init exception : "+e, e ); |
| 31 | + } |
| 32 | + return init; |
| 33 | + } |
| 34 | + |
| 35 | + public static void initDocAsync( DocTypeHandler handler ) { |
| 36 | + Runnable runInitDoc = new Runnable() { |
| 37 | + @Override |
| 38 | + public void run() { |
| 39 | + log.info( "Init handler start : {}", handler ); |
| 40 | + try { |
| 41 | + boolean initOk = initDoc(handler); |
| 42 | + log.info( "Init handler end : {} -> {}", handler, initOk ); |
| 43 | + } catch (ConfigException e) { |
| 44 | + log.info( "Init handler error "+e, e ); |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + Thread t = new Thread( runInitDoc ); |
| 49 | + t.start(); |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments