001    package railo.runtime.config;
002    
003    import java.io.IOException;
004    import java.util.HashMap;
005    import java.util.Map;
006    
007    import org.w3c.dom.Document;
008    import org.w3c.dom.Element;
009    import org.xml.sax.SAXException;
010    
011    import railo.commons.io.SystemUtil;
012    import railo.commons.io.res.Resource;
013    import railo.commons.lang.ClassException;
014    import railo.commons.lang.SystemOut;
015    import railo.runtime.engine.CFMLEngineImpl;
016    import railo.runtime.exp.PageException;
017    import railo.transformer.library.function.FunctionLibException;
018    import railo.transformer.library.tag.TagLibException;
019    
020    
021    /**
022     * 
023     */
024    public final class ConfigServerFactory {
025        
026        /**
027         * creates a new ServletConfig Impl Object
028         * @param engine 
029         * @param initContextes
030         * @param contextes
031         * @param configDir
032         * @return new Instance
033         * @throws SAXException
034         * @throws ClassNotFoundException
035         * @throws PageException
036         * @throws IOException
037         * @throws TagLibException
038         * @throws FunctionLibException
039         */
040        public static ConfigServerImpl newInstance(CFMLEngineImpl engine,Map initContextes, Map contextes, Resource configDir) 
041            throws SAXException, ClassException, PageException, IOException, TagLibException, FunctionLibException {
042            SystemOut.print(SystemUtil.PRINTWRITER_OUT,
043                            "===================================================================\n"+
044                            "SERVER CONTEXT\n" +
045                            "-------------------------------------------------------------------\n"+
046                            "- config:"+configDir+"\n"+
047                            "===================================================================\n"
048                            
049                            );
050            
051            boolean doNew=ConfigWebFactory.doNew(configDir);
052            
053            Resource configFile=configDir.getRealResource("railo-server.xml");
054            if(!configFile.exists()) {
055                        configFile.createFile(true);
056                            //InputStream in = new TextFile("").getClass().getResourceAsStream("/resource/config/server.xml");
057                            ConfigWebFactory.createFileFromResource(
058                                 "/resource/config/server.xml",
059                                 configFile.getAbsoluteResource(),
060                                 "tpiasfap"
061                            );
062                    }
063                    //print.out(configFile);
064            Document doc=ConfigWebFactory.loadDocument(configFile);
065           
066            ConfigServerImpl config=new ConfigServerImpl(engine,initContextes,contextes,configDir,configFile);
067                    load(config,doc,false,doNew);
068                
069                    createContextFiles(configDir,config,doNew);
070                return config;
071        }
072        /**
073         * reloads the Config Object
074         * @param configServer
075         * @throws SAXException
076         * @throws ClassNotFoundException
077         * @throws PageException
078         * @throws IOException
079         * @throws TagLibException
080         * @throws FunctionLibException
081         */
082        public static void reloadInstance(ConfigServerImpl configServer) 
083            throws SAXException, ClassException, PageException, IOException, TagLibException, FunctionLibException {
084            Resource configFile=configServer.getConfigFile();
085            
086            if(configFile==null) return ;
087            if(second(configServer.getLoadTime())>second(configFile.lastModified())) return ;
088            boolean doNew=ConfigWebFactory.doNew(configServer.getConfigDir());
089            load(configServer,ConfigWebFactory.loadDocument(configFile),true,doNew);
090        }
091        
092        private static long second(long ms) {
093                    return ms/1000;
094            }
095        
096        /**
097         * @param configServer 
098         * @param doc
099         * @throws ClassNotFoundException
100         * @throws IOException
101         * @throws FunctionLibException
102         * @throws TagLibException
103         * @throws PageException
104         */
105        static void load(ConfigServerImpl configServer, Document doc, boolean isReload, boolean doNew) throws ClassException, PageException, IOException, TagLibException, FunctionLibException {
106            ConfigWebFactory.load(null,configServer,doc, isReload,doNew);
107            loadLabel(configServer,doc);
108        }
109        
110    
111            private static void loadLabel(ConfigServerImpl configServer, Document doc) {
112                    Element el= ConfigWebFactory.getChildByName(doc.getDocumentElement(),"labels");
113            Element[] children=ConfigWebFactory.getChildren(el,"label");
114            
115            Map<String, String> labels=new HashMap<String, String>();
116            if(children!=null)for(int i=0;i<children.length;i++) {
117               el=children[i];
118               
119               String id=el.getAttribute("id");
120               String name=el.getAttribute("name");
121               if(id!=null && name!=null) { 
122                   labels.put(id, name);
123               }
124            }
125            configServer.setLabels(labels);
126            }
127            
128            public static void createContextFiles(Resource configDir, ConfigServer config, boolean doNew) throws IOException {
129                    //Resource tagDir = configDir.getRealResource("library/tag/");
130                    //if()
131                    //f=cDir.getRealResource("Cache.cfc");
132            //if(!f.exists() || doNew)createFileFromResourceEL("/resource/context/admin/cdriver/Cache.cfc",f);
133                    
134                    // Security certificate
135            Resource secDir = configDir.getRealResource("security");
136            if(!secDir.exists())secDir.mkdirs();
137            Resource f = secDir.getRealResource("cacerts");
138            if(!f.exists())ConfigWebFactory.createFileFromResourceEL("/resource/security/cacerts",f);
139            System.setProperty("javax.net.ssl.trustStore",f.toString());
140            }
141    }